2018-06-13 22:39:39 +03:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
import scrypt from 'scrypt-js';
|
|
|
|
|
2018-06-18 12:42:41 +03:00
|
|
|
import { defaultPath, entropyToMnemonic, fromMnemonic, HDNode } from './hdnode';
|
2018-06-15 11:18:17 +03:00
|
|
|
import * as secretStorage from './secret-storage';
|
|
|
|
import { ProgressCallback } from './secret-storage';
|
|
|
|
import { recoverAddress, SigningKey } from './signing-key';
|
|
|
|
|
2018-06-17 23:32:57 +03:00
|
|
|
import { BlockTag, Provider, TransactionRequest, TransactionResponse } from '../providers/provider';
|
2018-06-21 03:29:54 +03:00
|
|
|
import { Wordlist } from '../wordlists/wordlist';
|
2018-06-15 11:18:17 +03:00
|
|
|
|
2018-06-17 23:32:57 +03:00
|
|
|
import { BigNumber, BigNumberish } from '../utils/bignumber';
|
2018-06-18 12:42:41 +03:00
|
|
|
import { arrayify, Arrayish, concat, hexlify, joinSignature } from '../utils/bytes';
|
|
|
|
import { hashMessage } from '../utils/hash';
|
2018-06-13 22:39:39 +03:00
|
|
|
import { keccak256 } from '../utils/keccak256';
|
2018-06-17 23:32:57 +03:00
|
|
|
import { defineReadOnly, resolveProperties, shallowCopy } from '../utils/properties';
|
2018-06-13 22:39:39 +03:00
|
|
|
import { randomBytes } from '../utils/random-bytes';
|
2018-06-18 12:42:41 +03:00
|
|
|
import { sign as signTransaction } from '../utils/transaction';
|
2018-06-13 22:39:39 +03:00
|
|
|
import { toUtf8Bytes, UnicodeNormalizationForm } from '../utils/utf8';
|
|
|
|
|
|
|
|
import * as errors from '../utils/errors';
|
|
|
|
|
|
|
|
|
|
|
|
// This ensures we inject a setImmediate into the global space, which
|
|
|
|
// dramatically improves the performance of the scrypt PBKDF.
|
|
|
|
console.log("Fix this! Setimmediate");
|
|
|
|
//import _setimmediate = require('setimmediate');
|
|
|
|
|
2018-06-18 12:42:41 +03:00
|
|
|
export abstract class Signer {
|
|
|
|
provider?: Provider;
|
2018-06-13 22:39:39 +03:00
|
|
|
|
2018-06-18 12:42:41 +03:00
|
|
|
abstract getAddress(): Promise<string>
|
2018-06-13 22:39:39 +03:00
|
|
|
|
2018-06-18 12:42:41 +03:00
|
|
|
abstract signMessage(transaction: Arrayish | string): Promise<string>;
|
|
|
|
abstract sendTransaction(transaction: TransactionRequest): Promise<TransactionResponse>;
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-18 12:42:41 +03:00
|
|
|
export class Wallet extends Signer {
|
2018-06-17 23:32:57 +03:00
|
|
|
|
2018-06-18 12:42:41 +03:00
|
|
|
readonly provider: Provider;
|
|
|
|
|
2018-06-13 22:39:39 +03:00
|
|
|
private readonly signingKey: SigningKey;
|
|
|
|
|
|
|
|
constructor(privateKey: SigningKey | HDNode | Arrayish, provider?: Provider) {
|
2018-06-18 12:42:41 +03:00
|
|
|
super();
|
2018-06-13 22:39:39 +03:00
|
|
|
errors.checkNew(this, Wallet);
|
|
|
|
|
|
|
|
// Make sure we have a valid signing key
|
|
|
|
if (privateKey instanceof SigningKey) {
|
2018-06-18 12:42:41 +03:00
|
|
|
defineReadOnly(this, 'signingKey', privateKey);
|
2018-06-13 22:39:39 +03:00
|
|
|
} else {
|
2018-06-18 12:42:41 +03:00
|
|
|
defineReadOnly(this, 'signingKey', new SigningKey(privateKey));
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
|
2018-06-18 12:42:41 +03:00
|
|
|
defineReadOnly(this, 'provider', provider);
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
|
2018-06-19 09:12:57 +03:00
|
|
|
get address(): string { return this.signingKey.address; }
|
|
|
|
|
|
|
|
get mnemonic(): string { return this.signingKey.mnemonic; }
|
|
|
|
get path(): string { return this.signingKey.mnemonic; }
|
|
|
|
|
|
|
|
get privateKey(): string { return this.signingKey.privateKey; }
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new instance of this Wallet connected to provider.
|
|
|
|
*/
|
2018-06-18 12:42:41 +03:00
|
|
|
connect(provider: Provider): Wallet {
|
|
|
|
return new Wallet(this.signingKey, provider);
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
|
2018-06-18 12:42:41 +03:00
|
|
|
|
2018-06-15 11:18:17 +03:00
|
|
|
getAddress(): Promise<string> {
|
2018-06-13 22:39:39 +03:00
|
|
|
return Promise.resolve(this.address);
|
|
|
|
}
|
|
|
|
|
2018-06-18 12:42:41 +03:00
|
|
|
sign(transaction: TransactionRequest): Promise<string> {
|
|
|
|
return resolveProperties(transaction).then((tx) => {
|
|
|
|
return signTransaction(tx, this.signingKey.signDigest.bind(this.signingKey));
|
|
|
|
});
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
|
2018-06-18 12:42:41 +03:00
|
|
|
signMessage(message: Arrayish | string): Promise<string> {
|
|
|
|
return Promise.resolve(joinSignature(this.signingKey.signDigest(hashMessage(message))));
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
|
2018-06-17 23:32:57 +03:00
|
|
|
|
2018-06-18 12:42:41 +03:00
|
|
|
getBalance(blockTag?: BlockTag): Promise<BigNumber> {
|
|
|
|
if (!this.provider) { throw new Error('missing provider'); }
|
|
|
|
return this.provider.getBalance(this.address, blockTag);
|
2018-06-17 23:32:57 +03:00
|
|
|
}
|
|
|
|
|
2018-06-18 12:42:41 +03:00
|
|
|
getTransactionCount(blockTag?: BlockTag): Promise<number> {
|
2018-06-13 22:39:39 +03:00
|
|
|
if (!this.provider) { throw new Error('missing provider'); }
|
2018-06-18 12:42:41 +03:00
|
|
|
return this.provider.getTransactionCount(this.address, blockTag);
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
|
2018-06-17 23:32:57 +03:00
|
|
|
sendTransaction(transaction: TransactionRequest): Promise<TransactionResponse> {
|
2018-06-13 22:39:39 +03:00
|
|
|
if (!this.provider) { throw new Error('missing provider'); }
|
|
|
|
|
|
|
|
if (!transaction || typeof(transaction) !== 'object') {
|
|
|
|
throw new Error('invalid transaction object');
|
|
|
|
}
|
|
|
|
|
2018-06-17 23:32:57 +03:00
|
|
|
var tx = shallowCopy(transaction);
|
2018-06-13 22:39:39 +03:00
|
|
|
|
2018-06-17 23:32:57 +03:00
|
|
|
if (tx.to != null) {
|
|
|
|
tx.to = this.provider.resolveName(tx.to);
|
|
|
|
}
|
2018-06-13 22:39:39 +03:00
|
|
|
|
2018-06-17 23:32:57 +03:00
|
|
|
if (tx.gasLimit == null) {
|
2018-06-18 12:42:41 +03:00
|
|
|
tx.gasLimit = this.provider.estimateGas(tx);
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
|
2018-06-17 23:32:57 +03:00
|
|
|
if (tx.gasPrice == null) {
|
2018-06-18 12:42:41 +03:00
|
|
|
tx.gasPrice = this.provider.getGasPrice();
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
|
2018-06-17 23:32:57 +03:00
|
|
|
if (tx.nonce == null) {
|
|
|
|
tx.nonce = this.getTransactionCount();
|
|
|
|
}
|
2018-06-13 22:39:39 +03:00
|
|
|
|
2018-06-17 23:32:57 +03:00
|
|
|
if (tx.chainId == null) {
|
|
|
|
tx.chainId = this.provider.getNetwork().then((network) => network.chainId);
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
|
2018-06-17 23:32:57 +03:00
|
|
|
return resolveProperties(tx).then((tx) => {
|
|
|
|
console.log('To Sign', tx);
|
|
|
|
return this.provider.sendTransaction(this.sign(tx));
|
2018-06-13 22:39:39 +03:00
|
|
|
});
|
2018-06-17 23:32:57 +03:00
|
|
|
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
|
2018-06-15 11:18:17 +03:00
|
|
|
send(addressOrName: string, amountWei: BigNumberish, options: any): Promise<TransactionResponse> {
|
2018-06-13 22:39:39 +03:00
|
|
|
if (!options) { options = {}; }
|
|
|
|
|
|
|
|
return this.sendTransaction({
|
|
|
|
to: addressOrName,
|
|
|
|
gasLimit: options.gasLimit,
|
|
|
|
gasPrice: options.gasPrice,
|
|
|
|
nonce: options.nonce,
|
|
|
|
value: amountWei,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-15 11:18:17 +03:00
|
|
|
encrypt(password: Arrayish | string, options: any, progressCallback: ProgressCallback): Promise<string> {
|
2018-06-13 22:39:39 +03: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
|
|
|
|
var safeOptions = {};
|
|
|
|
for (var key in options) { safeOptions[key] = options[key]; }
|
|
|
|
options = safeOptions;
|
|
|
|
|
|
|
|
// Set the mnemonic and path
|
|
|
|
options.mnemonic = this.mnemonic;
|
|
|
|
options.path = this.path
|
|
|
|
}
|
|
|
|
|
|
|
|
return secretStorage.encrypt(this.privateKey, password, options, progressCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-19 09:12:57 +03:00
|
|
|
/**
|
|
|
|
* Static methods to create Wallet instances.
|
|
|
|
*/
|
2018-06-15 11:18:17 +03:00
|
|
|
static createRandom(options: any): Wallet {
|
2018-06-13 22:39:39 +03:00
|
|
|
var entropy: Uint8Array = randomBytes(16);
|
|
|
|
|
|
|
|
if (!options) { options = { }; }
|
|
|
|
|
|
|
|
if (options.extraEntropy) {
|
|
|
|
entropy = arrayify(keccak256(concat([entropy, options.extraEntropy])).substring(0, 34));
|
|
|
|
}
|
|
|
|
|
2018-06-21 03:29:54 +03:00
|
|
|
var mnemonic = entropyToMnemonic(entropy, options.locale);
|
|
|
|
return Wallet.fromMnemonic(mnemonic, options.path, options.locale);
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
|
2018-06-15 11:18:17 +03:00
|
|
|
static fromEncryptedWallet(json: string, password: Arrayish, progressCallback: ProgressCallback): Promise<Wallet> {
|
2018-06-13 22:39:39 +03:00
|
|
|
if (progressCallback && typeof(progressCallback) !== 'function') {
|
|
|
|
throw new Error('invalid callback');
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
|
|
|
|
if (secretStorage.isCrowdsaleWallet(json)) {
|
|
|
|
try {
|
|
|
|
var privateKey = secretStorage.decryptCrowdsale(json, password);
|
|
|
|
resolve(new Wallet(privateKey));
|
|
|
|
} catch (error) {
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (secretStorage.isValidWallet(json)) {
|
|
|
|
|
|
|
|
secretStorage.decrypt(json, password, progressCallback).then(function(signingKey) {
|
|
|
|
var wallet = new Wallet(signingKey);
|
|
|
|
/*
|
|
|
|
if (signingKey.mnemonic && signingKey.path) {
|
|
|
|
wallet.mnemonic = signingKey.mnemonic;
|
|
|
|
wallet.path = signingKey.path;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
resolve(wallet);
|
|
|
|
}, function(error) {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
|
|
|
|
} else {
|
|
|
|
reject('invalid wallet JSON');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-21 03:29:54 +03:00
|
|
|
static fromMnemonic(mnemonic: string, path?: string, wordlist?: Wordlist): Wallet {
|
2018-06-13 22:39:39 +03:00
|
|
|
if (!path) { path = defaultPath; }
|
2018-06-21 03:29:54 +03:00
|
|
|
return new Wallet(fromMnemonic(mnemonic, wordlist).derivePath(path));
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-15 11:18:17 +03:00
|
|
|
static fromBrainWallet(username: Arrayish | string, password: Arrayish | string, progressCallback: ProgressCallback): Promise<Wallet> {
|
2018-06-13 22:39:39 +03:00
|
|
|
if (progressCallback && typeof(progressCallback) !== 'function') {
|
|
|
|
throw new Error('invalid callback');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof(username) === 'string') {
|
|
|
|
username = toUtf8Bytes(username, UnicodeNormalizationForm.NFKC);
|
|
|
|
} else {
|
|
|
|
username = arrayify(username);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof(password) === 'string') {
|
|
|
|
password = toUtf8Bytes(password, UnicodeNormalizationForm.NFKC);
|
|
|
|
} else {
|
|
|
|
password = arrayify(password);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
scrypt(password, username, (1 << 18), 8, 1, 32, function(error, progress, key) {
|
|
|
|
if (error) {
|
|
|
|
reject(error);
|
|
|
|
|
|
|
|
} else if (key) {
|
|
|
|
resolve(new Wallet(hexlify(key)));
|
|
|
|
|
|
|
|
} else if (progressCallback) {
|
|
|
|
return progressCallback(progress);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2018-06-18 12:42:41 +03:00
|
|
|
|
|
|
|
|
2018-06-19 09:12:57 +03:00
|
|
|
/**
|
|
|
|
* Determine if this is an encryped JSON wallet.
|
|
|
|
*/
|
|
|
|
static isEncryptedWallet(json: string): boolean {
|
|
|
|
return (secretStorage.isValidWallet(json) || secretStorage.isCrowdsaleWallet(json));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verify a signed message, returning the address of the signer.
|
|
|
|
*/
|
2018-06-18 12:42:41 +03:00
|
|
|
static verifyMessage(message: Arrayish | string, signature: string): string {
|
|
|
|
signature = hexlify(signature);
|
|
|
|
if (signature.length != 132) { throw new Error('invalid signature'); }
|
|
|
|
var digest = hashMessage(message);
|
|
|
|
|
|
|
|
var recoveryParam = parseInt(signature.substring(130), 16);
|
|
|
|
if (recoveryParam >= 27) { recoveryParam -= 27; }
|
|
|
|
if (recoveryParam < 0) { throw new Error('invalid signature'); }
|
|
|
|
|
|
|
|
return recoverAddress(
|
|
|
|
digest,
|
|
|
|
{
|
|
|
|
r: signature.substring(0, 66),
|
|
|
|
s: '0x' + signature.substring(66, 130),
|
|
|
|
recoveryParam: recoveryParam
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|