2018-06-13 22:39:39 +03:00
|
|
|
'use strict';
|
|
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
|
|
};
|
|
|
|
var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
|
if (mod && mod.__esModule) return mod;
|
|
|
|
var result = {};
|
|
|
|
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
|
|
|
result["default"] = mod;
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
|
var scrypt_js_1 = __importDefault(require("scrypt-js"));
|
2018-06-15 11:18:17 +03:00
|
|
|
var hdnode_1 = require("./hdnode");
|
|
|
|
var secretStorage = __importStar(require("./secret-storage"));
|
|
|
|
var signing_key_1 = require("./signing-key");
|
2018-06-17 23:47:28 +03:00
|
|
|
var bytes_1 = require("../utils/bytes");
|
2018-06-13 22:39:39 +03:00
|
|
|
var keccak256_1 = require("../utils/keccak256");
|
2018-06-17 23:32:57 +03:00
|
|
|
var properties_1 = require("../utils/properties");
|
2018-06-13 22:39:39 +03:00
|
|
|
var random_bytes_1 = require("../utils/random-bytes");
|
2018-06-17 23:32:57 +03:00
|
|
|
var transaction_1 = require("../utils/transaction");
|
2018-06-13 22:39:39 +03:00
|
|
|
var utf8_1 = require("../utils/utf8");
|
|
|
|
var errors = __importStar(require("../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");
|
2018-06-17 23:32:57 +03:00
|
|
|
// @TODO: Move to HDNode
|
2018-06-13 22:39:39 +03:00
|
|
|
var defaultPath = "m/44'/60'/0'/0/0";
|
|
|
|
var Wallet = /** @class */ (function () {
|
|
|
|
function Wallet(privateKey, provider) {
|
|
|
|
//private _provider;
|
|
|
|
this.defaultGasLimit = 1500000;
|
|
|
|
errors.checkNew(this, Wallet);
|
|
|
|
// Make sure we have a valid signing key
|
|
|
|
if (privateKey instanceof signing_key_1.SigningKey) {
|
|
|
|
this.signingKey = privateKey;
|
|
|
|
if (this.signingKey.mnemonic) {
|
2018-06-17 23:32:57 +03:00
|
|
|
properties_1.defineReadOnly(this, 'mnemonic', privateKey.mnemonic);
|
|
|
|
properties_1.defineReadOnly(this, 'path', privateKey.path);
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.signingKey = new signing_key_1.SigningKey(privateKey);
|
|
|
|
}
|
2018-06-17 23:32:57 +03:00
|
|
|
properties_1.defineReadOnly(this, 'privateKey', this.signingKey.privateKey);
|
2018-06-13 22:39:39 +03:00
|
|
|
this.provider = provider;
|
2018-06-17 23:32:57 +03:00
|
|
|
properties_1.defineReadOnly(this, 'address', this.signingKey.address);
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
Wallet.prototype.sign = function (transaction) {
|
2018-06-17 23:32:57 +03:00
|
|
|
return transaction_1.sign(transaction, this.signingKey.signDigest.bind(this.signingKey));
|
2018-06-13 22:39:39 +03:00
|
|
|
};
|
|
|
|
Wallet.prototype.getAddress = function () {
|
|
|
|
return Promise.resolve(this.address);
|
|
|
|
};
|
|
|
|
Wallet.prototype.getBalance = function (blockTag) {
|
|
|
|
if (!this.provider) {
|
|
|
|
throw new Error('missing provider');
|
|
|
|
}
|
|
|
|
return this.provider.getBalance(this.address, blockTag);
|
|
|
|
};
|
|
|
|
Wallet.prototype.getTransactionCount = function (blockTag) {
|
|
|
|
if (!this.provider) {
|
|
|
|
throw new Error('missing provider');
|
|
|
|
}
|
|
|
|
return this.provider.getTransactionCount(this.address, blockTag);
|
|
|
|
};
|
2018-06-17 23:32:57 +03:00
|
|
|
Wallet.prototype.getGasPrice = function () {
|
|
|
|
if (!this.provider) {
|
|
|
|
throw new Error('missing provider');
|
|
|
|
}
|
|
|
|
return this.provider.getGasPrice();
|
|
|
|
};
|
2018-06-13 22:39:39 +03:00
|
|
|
Wallet.prototype.estimateGas = function (transaction) {
|
|
|
|
if (!this.provider) {
|
|
|
|
throw new Error('missing provider');
|
|
|
|
}
|
|
|
|
var calculate = {};
|
|
|
|
['from', 'to', 'data', 'value'].forEach(function (key) {
|
|
|
|
if (transaction[key] == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
calculate[key] = transaction[key];
|
|
|
|
});
|
|
|
|
if (transaction.from == null) {
|
|
|
|
calculate.from = this.address;
|
|
|
|
}
|
|
|
|
return this.provider.estimateGas(calculate);
|
|
|
|
};
|
|
|
|
Wallet.prototype.sendTransaction = function (transaction) {
|
2018-06-17 23:32:57 +03:00
|
|
|
var _this = this;
|
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 = properties_1.shallowCopy(transaction);
|
|
|
|
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) {
|
|
|
|
tx.gasLimit = this.estimateGas(tx);
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
2018-06-17 23:32:57 +03:00
|
|
|
if (tx.gasPrice == null) {
|
|
|
|
tx.gasPrice = this.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(function (network) { return network.chainId; });
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
2018-06-17 23:32:57 +03:00
|
|
|
return properties_1.resolveProperties(tx).then(function (tx) {
|
|
|
|
console.log('To Sign', tx);
|
|
|
|
return _this.provider.sendTransaction(_this.sign(tx));
|
2018-06-13 22:39:39 +03:00
|
|
|
});
|
|
|
|
};
|
|
|
|
Wallet.prototype.send = function (addressOrName, amountWei, options) {
|
|
|
|
if (!options) {
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
return this.sendTransaction({
|
|
|
|
to: addressOrName,
|
|
|
|
gasLimit: options.gasLimit,
|
|
|
|
gasPrice: options.gasPrice,
|
|
|
|
nonce: options.nonce,
|
|
|
|
value: amountWei,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
Wallet.hashMessage = function (message) {
|
2018-06-17 23:47:28 +03:00
|
|
|
var payload = bytes_1.concat([
|
2018-06-13 22:39:39 +03:00
|
|
|
utf8_1.toUtf8Bytes('\x19Ethereum Signed Message:\n'),
|
|
|
|
utf8_1.toUtf8Bytes(String(message.length)),
|
|
|
|
((typeof (message) === 'string') ? utf8_1.toUtf8Bytes(message) : message)
|
|
|
|
]);
|
|
|
|
return keccak256_1.keccak256(payload);
|
|
|
|
};
|
|
|
|
Wallet.prototype.signMessage = function (message) {
|
|
|
|
var signingKey = new signing_key_1.SigningKey(this.privateKey);
|
|
|
|
var sig = signingKey.signDigest(Wallet.hashMessage(message));
|
2018-06-17 23:47:28 +03:00
|
|
|
return (bytes_1.hexZeroPad(sig.r, 32) + bytes_1.hexZeroPad(sig.s, 32).substring(2) + (sig.recoveryParam ? '1c' : '1b'));
|
2018-06-13 22:39:39 +03:00
|
|
|
};
|
|
|
|
Wallet.verifyMessage = function (message, signature) {
|
2018-06-17 23:47:28 +03:00
|
|
|
signature = bytes_1.hexlify(signature);
|
2018-06-13 22:39:39 +03:00
|
|
|
if (signature.length != 132) {
|
|
|
|
throw new Error('invalid signature');
|
|
|
|
}
|
|
|
|
var digest = Wallet.hashMessage(message);
|
|
|
|
var recoveryParam = parseInt(signature.substring(130), 16);
|
|
|
|
if (recoveryParam >= 27) {
|
|
|
|
recoveryParam -= 27;
|
|
|
|
}
|
|
|
|
if (recoveryParam < 0) {
|
|
|
|
throw new Error('invalid signature');
|
|
|
|
}
|
2018-06-15 11:18:17 +03:00
|
|
|
return signing_key_1.recoverAddress(digest, {
|
|
|
|
r: signature.substring(0, 66),
|
|
|
|
s: '0x' + signature.substring(66, 130),
|
|
|
|
recoveryParam: recoveryParam
|
|
|
|
});
|
2018-06-13 22:39:39 +03:00
|
|
|
};
|
|
|
|
Wallet.prototype.encrypt = function (password, options, progressCallback) {
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
Wallet.createRandom = function (options) {
|
|
|
|
var entropy = random_bytes_1.randomBytes(16);
|
|
|
|
if (!options) {
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
if (options.extraEntropy) {
|
2018-06-17 23:47:28 +03:00
|
|
|
entropy = bytes_1.arrayify(keccak256_1.keccak256(bytes_1.concat([entropy, options.extraEntropy])).substring(0, 34));
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
var mnemonic = hdnode_1.entropyToMnemonic(entropy);
|
|
|
|
return Wallet.fromMnemonic(mnemonic, options.path);
|
|
|
|
};
|
|
|
|
Wallet.isEncryptedWallet = function (json) {
|
|
|
|
return (secretStorage.isValidWallet(json) || secretStorage.isCrowdsaleWallet(json));
|
|
|
|
};
|
|
|
|
Wallet.fromEncryptedWallet = function (json, password, progressCallback) {
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
Wallet.fromMnemonic = function (mnemonic, path) {
|
|
|
|
if (!path) {
|
|
|
|
path = defaultPath;
|
|
|
|
}
|
|
|
|
return new Wallet(hdnode_1.fromMnemonic(mnemonic).derivePath(path));
|
|
|
|
};
|
|
|
|
Wallet.fromBrainWallet = function (username, password, progressCallback) {
|
|
|
|
if (progressCallback && typeof (progressCallback) !== 'function') {
|
|
|
|
throw new Error('invalid callback');
|
|
|
|
}
|
|
|
|
if (typeof (username) === 'string') {
|
|
|
|
username = utf8_1.toUtf8Bytes(username, utf8_1.UnicodeNormalizationForm.NFKC);
|
|
|
|
}
|
|
|
|
else {
|
2018-06-17 23:47:28 +03:00
|
|
|
username = bytes_1.arrayify(username);
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
if (typeof (password) === 'string') {
|
|
|
|
password = utf8_1.toUtf8Bytes(password, utf8_1.UnicodeNormalizationForm.NFKC);
|
|
|
|
}
|
|
|
|
else {
|
2018-06-17 23:47:28 +03:00
|
|
|
password = bytes_1.arrayify(password);
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
scrypt_js_1.default(password, username, (1 << 18), 8, 1, 32, function (error, progress, key) {
|
|
|
|
if (error) {
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
else if (key) {
|
2018-06-17 23:47:28 +03:00
|
|
|
resolve(new Wallet(bytes_1.hexlify(key)));
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
else if (progressCallback) {
|
|
|
|
return progressCallback(progress);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
return Wallet;
|
|
|
|
}());
|
|
|
|
exports.Wallet = Wallet;
|