2018-06-13 15:39:39 -04:00
|
|
|
'use strict';
|
|
|
|
|
2018-09-24 15:55:17 -04:00
|
|
|
import { arrayify, concat, joinSignature } from './utils/bytes';
|
|
|
|
import { BigNumber } from './utils/bignumber';
|
|
|
|
import { hashMessage } from './utils/hash';
|
|
|
|
import { defaultPath, HDNode, entropyToMnemonic, fromMnemonic } from './utils/hdnode';
|
|
|
|
import { isCrowdsaleWallet, isSecretStorageWallet } from './utils/json-wallet';
|
|
|
|
import { keccak256 } from './utils/keccak256';
|
|
|
|
import { defineReadOnly, resolveProperties, shallowCopy } from './utils/properties';
|
|
|
|
import { randomBytes } from './utils/random-bytes';
|
|
|
|
import * as secretStorage from './utils/secret-storage';
|
|
|
|
import { SigningKey } from './utils/signing-key';
|
2018-10-14 19:00:15 -04:00
|
|
|
import { populateTransaction, serialize as serializeTransaction } from './utils/transaction';
|
2018-09-24 15:55:17 -04:00
|
|
|
import { Wordlist } from './utils/wordlist';
|
2018-06-13 15:39:39 -04:00
|
|
|
|
2018-07-30 18:59:52 -04:00
|
|
|
// Imported Abstracts
|
|
|
|
import { Signer as AbstractSigner } from './abstract-signer';
|
2018-09-24 15:55:17 -04:00
|
|
|
import { Provider } from './providers/abstract-provider';
|
2018-07-30 18:59:52 -04:00
|
|
|
|
|
|
|
// Imported Types
|
2018-09-24 15:55:17 -04:00
|
|
|
import { ProgressCallback } from './utils/secret-storage';
|
|
|
|
import { Arrayish } from './utils/bytes';
|
|
|
|
import { BlockTag, TransactionRequest, TransactionResponse } from './providers/abstract-provider';
|
2018-06-13 15:39:39 -04:00
|
|
|
|
2018-09-24 15:55:17 -04:00
|
|
|
import * as errors from './errors';
|
2018-06-13 15:39:39 -04:00
|
|
|
|
2018-07-30 18:59:52 -04:00
|
|
|
export class Wallet extends AbstractSigner {
|
2018-06-17 16:32:57 -04:00
|
|
|
|
2018-07-30 18:59:52 -04:00
|
|
|
readonly provider: Provider;
|
2018-06-18 05:42:41 -04:00
|
|
|
|
2018-06-13 15:39:39 -04:00
|
|
|
private readonly signingKey: SigningKey;
|
|
|
|
|
2018-07-30 18:59:52 -04:00
|
|
|
constructor(privateKey: SigningKey | HDNode | Arrayish, provider?: Provider) {
|
2018-06-18 05:42:41 -04:00
|
|
|
super();
|
2018-06-13 15:39:39 -04:00
|
|
|
errors.checkNew(this, Wallet);
|
|
|
|
|
|
|
|
// Make sure we have a valid signing key
|
2018-07-25 21:15:43 -04:00
|
|
|
if (SigningKey.isSigningKey(privateKey)) {
|
2018-06-18 05:42:41 -04:00
|
|
|
defineReadOnly(this, 'signingKey', privateKey);
|
2018-06-13 15:39:39 -04:00
|
|
|
} else {
|
2018-06-18 05:42:41 -04:00
|
|
|
defineReadOnly(this, 'signingKey', new SigningKey(privateKey));
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
|
|
|
|
2018-06-18 05:42:41 -04:00
|
|
|
defineReadOnly(this, 'provider', provider);
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
|
|
|
|
2018-06-19 02:12:57 -04:00
|
|
|
get address(): string { return this.signingKey.address; }
|
|
|
|
|
|
|
|
get mnemonic(): string { return this.signingKey.mnemonic; }
|
2019-01-17 16:32:51 -05:00
|
|
|
get path(): string { return this.signingKey.path; }
|
2018-06-19 02:12:57 -04:00
|
|
|
|
|
|
|
get privateKey(): string { return this.signingKey.privateKey; }
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new instance of this Wallet connected to provider.
|
|
|
|
*/
|
2018-07-30 18:59:52 -04:00
|
|
|
connect(provider: Provider): Wallet {
|
|
|
|
if (!(Provider.isProvider(provider))) {
|
2018-07-16 03:27:49 -04:00
|
|
|
errors.throwError('invalid provider', errors.INVALID_ARGUMENT, { argument: 'provider', value: provider });
|
|
|
|
}
|
2018-06-18 05:42:41 -04:00
|
|
|
return new Wallet(this.signingKey, provider);
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
|
|
|
|
2018-06-15 04:18:17 -04:00
|
|
|
getAddress(): Promise<string> {
|
2018-06-13 15:39:39 -04:00
|
|
|
return Promise.resolve(this.address);
|
|
|
|
}
|
|
|
|
|
2018-06-18 05:42:41 -04:00
|
|
|
sign(transaction: TransactionRequest): Promise<string> {
|
|
|
|
return resolveProperties(transaction).then((tx) => {
|
2018-07-12 02:42:46 -04:00
|
|
|
let rawTx = serializeTransaction(tx);
|
|
|
|
let signature = this.signingKey.signDigest(keccak256(rawTx));
|
2018-10-14 19:00:15 -04:00
|
|
|
return serializeTransaction(tx, signature);
|
2018-06-18 05:42:41 -04:00
|
|
|
});
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
|
|
|
|
2018-06-18 05:42:41 -04:00
|
|
|
signMessage(message: Arrayish | string): Promise<string> {
|
|
|
|
return Promise.resolve(joinSignature(this.signingKey.signDigest(hashMessage(message))));
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
|
|
|
|
2018-06-17 16:32:57 -04:00
|
|
|
|
2018-06-18 05:42:41 -04:00
|
|
|
getBalance(blockTag?: BlockTag): Promise<BigNumber> {
|
|
|
|
if (!this.provider) { throw new Error('missing provider'); }
|
|
|
|
return this.provider.getBalance(this.address, blockTag);
|
2018-06-17 16:32:57 -04:00
|
|
|
}
|
|
|
|
|
2018-06-18 05:42:41 -04:00
|
|
|
getTransactionCount(blockTag?: BlockTag): Promise<number> {
|
2018-06-13 15:39:39 -04:00
|
|
|
if (!this.provider) { throw new Error('missing provider'); }
|
2018-06-18 05:42:41 -04:00
|
|
|
return this.provider.getTransactionCount(this.address, blockTag);
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
|
|
|
|
2018-06-17 16:32:57 -04:00
|
|
|
sendTransaction(transaction: TransactionRequest): Promise<TransactionResponse> {
|
2018-12-14 18:32:48 -05:00
|
|
|
if (!this.provider) { throw new Error('missing provider'); }
|
|
|
|
|
|
|
|
if (transaction.nonce == null) {
|
|
|
|
transaction = shallowCopy(transaction);
|
|
|
|
transaction.nonce = this.getTransactionCount("pending");
|
|
|
|
}
|
|
|
|
|
2018-10-14 19:00:15 -04:00
|
|
|
return populateTransaction(transaction, this.provider, this.address).then((tx) => {
|
|
|
|
return this.sign(tx).then((signedTransaction) => {
|
|
|
|
return this.provider.sendTransaction(signedTransaction);
|
|
|
|
});
|
|
|
|
});
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
|
|
|
|
2018-08-04 19:33:51 -04:00
|
|
|
encrypt(password: Arrayish | string, options?: any, progressCallback?: ProgressCallback): Promise<string> {
|
2018-06-13 15:39:39 -04:00
|
|
|
if (typeof(options) === 'function' && !progressCallback) {
|
|
|
|
progressCallback = options;
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (progressCallback && typeof(progressCallback) !== 'function') {
|
|
|
|
throw new Error('invalid callback');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!options) { options = {}; }
|
|
|
|
|
|
|
|
if (this.mnemonic) {
|
|
|
|
// Make sure we don't accidentally bubble the mnemonic up the call-stack
|
2018-08-13 16:52:35 +01:00
|
|
|
options = shallowCopy(options);
|
2018-06-13 15:39:39 -04:00
|
|
|
|
|
|
|
// Set the mnemonic and path
|
|
|
|
options.mnemonic = this.mnemonic;
|
|
|
|
options.path = this.path
|
|
|
|
}
|
|
|
|
|
|
|
|
return secretStorage.encrypt(this.privateKey, password, options, progressCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-19 02:12:57 -04:00
|
|
|
/**
|
|
|
|
* Static methods to create Wallet instances.
|
|
|
|
*/
|
2018-07-18 16:30:42 -04:00
|
|
|
static createRandom(options?: any): Wallet {
|
2018-06-13 15:39:39 -04:00
|
|
|
var entropy: Uint8Array = randomBytes(16);
|
|
|
|
|
|
|
|
if (!options) { options = { }; }
|
|
|
|
|
|
|
|
if (options.extraEntropy) {
|
|
|
|
entropy = arrayify(keccak256(concat([entropy, options.extraEntropy])).substring(0, 34));
|
|
|
|
}
|
|
|
|
|
2018-06-20 20:29:54 -04:00
|
|
|
var mnemonic = entropyToMnemonic(entropy, options.locale);
|
|
|
|
return Wallet.fromMnemonic(mnemonic, options.path, options.locale);
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
|
|
|
|
2018-10-07 00:42:52 -04:00
|
|
|
static fromEncryptedJson(json: string, password: Arrayish, progressCallback?: ProgressCallback): Promise<Wallet> {
|
2018-07-17 01:44:04 -04:00
|
|
|
if (isCrowdsaleWallet(json)) {
|
2018-07-03 16:44:05 -04:00
|
|
|
try {
|
2018-07-14 17:19:08 -04:00
|
|
|
if (progressCallback) { progressCallback(0); }
|
2018-07-03 16:44:05 -04:00
|
|
|
let privateKey = secretStorage.decryptCrowdsale(json, password);
|
2018-07-14 17:19:08 -04:00
|
|
|
if (progressCallback) { progressCallback(1); }
|
2018-07-03 16:44:05 -04:00
|
|
|
return Promise.resolve(new Wallet(privateKey));
|
|
|
|
} catch (error) {
|
|
|
|
return Promise.reject(error);
|
|
|
|
}
|
|
|
|
|
2018-07-17 01:44:04 -04:00
|
|
|
} else if (isSecretStorageWallet(json)) {
|
2018-07-03 16:44:05 -04:00
|
|
|
|
|
|
|
return secretStorage.decrypt(json, password, progressCallback).then(function(signingKey) {
|
|
|
|
return new Wallet(signingKey);
|
|
|
|
});
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
|
|
|
|
2018-07-03 16:44:05 -04:00
|
|
|
return Promise.reject('invalid wallet JSON');
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
|
|
|
|
2018-06-20 20:29:54 -04:00
|
|
|
static fromMnemonic(mnemonic: string, path?: string, wordlist?: Wordlist): Wallet {
|
2018-06-13 15:39:39 -04:00
|
|
|
if (!path) { path = defaultPath; }
|
2018-06-20 20:29:54 -04:00
|
|
|
return new Wallet(fromMnemonic(mnemonic, wordlist).derivePath(path));
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
|
|
|
}
|