2016-07-25 10:55:16 +03:00
|
|
|
'use strict';
|
|
|
|
|
2017-03-08 09:47:29 +03:00
|
|
|
var scrypt = require('scrypt-js');
|
2017-02-24 22:41:24 +03:00
|
|
|
var utils = require('ethers-utils');
|
2016-07-22 00:03:32 +03:00
|
|
|
|
2017-03-08 09:47:29 +03:00
|
|
|
var HDNode = require('./hdnode');
|
2016-07-22 00:03:32 +03:00
|
|
|
|
2017-03-08 09:47:29 +03:00
|
|
|
var secretStorage = require('./secret-storage');
|
|
|
|
var SigningKey = require('./signing-key');
|
2016-07-22 00:03:32 +03:00
|
|
|
|
2016-08-05 03:28:55 +03:00
|
|
|
// This ensures we inject a setImmediate into the global space, which
|
|
|
|
// dramatically improves the performance of the scrypt PBKDF.
|
|
|
|
require('setimmediate');
|
2016-07-22 00:03:32 +03:00
|
|
|
|
2017-03-08 09:47:29 +03:00
|
|
|
var defaultPath = "m/44'/60'/0'/0/0";
|
|
|
|
|
2016-07-22 00:03:32 +03:00
|
|
|
var transactionFields = [
|
|
|
|
{name: 'nonce', maxLength: 32, },
|
|
|
|
{name: 'gasPrice', maxLength: 32, },
|
|
|
|
{name: 'gasLimit', maxLength: 32, },
|
|
|
|
{name: 'to', length: 20, },
|
|
|
|
{name: 'value', maxLength: 32, },
|
|
|
|
{name: 'data'},
|
|
|
|
];
|
|
|
|
|
2016-07-27 00:57:11 +03:00
|
|
|
function Wallet(privateKey, provider) {
|
2016-07-22 00:03:32 +03:00
|
|
|
if (!(this instanceof Wallet)) { throw new Error('missing new'); }
|
|
|
|
|
2016-07-23 10:27:56 +03:00
|
|
|
// Make sure we have a valid signing key
|
|
|
|
var signingKey = privateKey;
|
|
|
|
if (!(privateKey instanceof SigningKey)) {
|
|
|
|
signingKey = new SigningKey(privateKey);
|
|
|
|
}
|
|
|
|
utils.defineProperty(this, 'privateKey', signingKey.privateKey);
|
2016-07-22 00:03:32 +03:00
|
|
|
|
2017-02-24 22:41:24 +03:00
|
|
|
// Provider
|
2016-08-03 00:43:18 +03:00
|
|
|
Object.defineProperty(this, 'provider', {
|
|
|
|
enumerable: true,
|
|
|
|
get: function() { return provider; },
|
|
|
|
set: function(value) {
|
2016-08-04 02:36:44 +03:00
|
|
|
provider = value;
|
2016-08-03 00:43:18 +03:00
|
|
|
}
|
|
|
|
});
|
2017-02-24 22:41:24 +03:00
|
|
|
if (provider) { this.provider = provider; }
|
2016-08-03 00:43:18 +03:00
|
|
|
|
2017-03-08 09:47:29 +03:00
|
|
|
var defaultGasLimit = 1500000;
|
2017-02-24 22:41:24 +03:00
|
|
|
Object.defineProperty(this, 'defaultGasLimit', {
|
2016-08-03 00:43:18 +03:00
|
|
|
enumerable: true,
|
2017-03-01 10:36:14 +03:00
|
|
|
get: function() { return defaultGasLimit; },
|
2017-02-24 22:41:24 +03:00
|
|
|
set: function(value) {
|
|
|
|
if (typeof(value) !== 'number') { throw new Error('invalid defaultGasLimit'); }
|
|
|
|
defaultGasLimit = value;
|
2016-08-04 02:36:44 +03:00
|
|
|
}
|
2017-02-24 22:41:24 +03:00
|
|
|
});
|
2016-07-27 00:57:11 +03:00
|
|
|
|
2016-07-22 00:03:32 +03:00
|
|
|
utils.defineProperty(this, 'address', signingKey.address);
|
|
|
|
|
|
|
|
utils.defineProperty(this, 'sign', function(transaction) {
|
2017-03-08 09:47:29 +03:00
|
|
|
var chainId = transaction.chainId;
|
|
|
|
if (!chainId && this.provider) { chainId = this.provider.chainId; }
|
|
|
|
if (!chainId) { chainId = 0; }
|
|
|
|
|
2016-07-22 00:03:32 +03:00
|
|
|
var raw = [];
|
|
|
|
transactionFields.forEach(function(fieldInfo) {
|
2017-02-24 22:41:24 +03:00
|
|
|
var value = transaction[fieldInfo.name] || ([]);
|
|
|
|
value = utils.arrayify(utils.hexlify(value), fieldInfo.name);
|
2016-07-22 00:03:32 +03:00
|
|
|
|
|
|
|
// Fixed-width field
|
2016-08-07 22:18:42 +03:00
|
|
|
if (fieldInfo.length && value.length !== fieldInfo.length && value.length > 0) {
|
2016-07-22 00:03:32 +03:00
|
|
|
var error = new Error('invalid ' + fieldInfo.name);
|
|
|
|
error.reason = 'wrong length';
|
|
|
|
error.value = value;
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Variable-width (with a maximum)
|
|
|
|
if (fieldInfo.maxLength) {
|
|
|
|
value = utils.stripZeros(value);
|
|
|
|
if (value.length > fieldInfo.maxLength) {
|
|
|
|
var error = new Error('invalid ' + fieldInfo.name);
|
|
|
|
error.reason = 'too long';
|
|
|
|
error.value = value;
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-24 22:41:24 +03:00
|
|
|
raw.push(utils.hexlify(value));
|
2016-07-22 00:03:32 +03:00
|
|
|
});
|
|
|
|
|
2017-03-08 09:47:29 +03:00
|
|
|
if (chainId) {
|
|
|
|
raw.push(utils.hexlify(chainId));
|
|
|
|
raw.push('0x');
|
|
|
|
raw.push('0x');
|
|
|
|
}
|
|
|
|
|
2017-04-05 00:19:18 +03:00
|
|
|
var digest = utils.keccak256(utils.RLP.encode(raw));
|
2016-07-22 00:03:32 +03:00
|
|
|
|
|
|
|
var signature = signingKey.signDigest(digest);
|
|
|
|
|
2017-03-08 09:47:29 +03:00
|
|
|
var v = 27 + signature.recoveryParam
|
|
|
|
if (chainId) {
|
|
|
|
raw.pop();
|
|
|
|
raw.pop();
|
|
|
|
raw.pop();
|
|
|
|
v += chainId * 2 + 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
raw.push(utils.hexlify(v));
|
2017-02-24 22:41:24 +03:00
|
|
|
raw.push(signature.r);
|
|
|
|
raw.push(signature.s);
|
2016-07-22 00:03:32 +03:00
|
|
|
|
2017-04-05 00:19:18 +03:00
|
|
|
return utils.RLP.encode(raw);
|
2016-07-22 00:03:32 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-09-16 09:08:36 +03:00
|
|
|
utils.defineProperty(Wallet, 'parseTransaction', function(rawTransaction) {
|
2017-02-24 22:41:24 +03:00
|
|
|
rawTransaction = utils.hexlify(rawTransaction, 'rawTransaction');
|
2017-04-05 00:19:18 +03:00
|
|
|
var signedTransaction = utils.RLP.decode(rawTransaction);
|
2017-03-08 09:47:29 +03:00
|
|
|
if (signedTransaction.length !== 9) { throw new Error('invalid transaction'); }
|
|
|
|
|
2016-09-16 09:08:36 +03:00
|
|
|
var raw = [];
|
|
|
|
|
|
|
|
var transaction = {};
|
|
|
|
transactionFields.forEach(function(fieldInfo, index) {
|
|
|
|
transaction[fieldInfo.name] = signedTransaction[index];
|
|
|
|
raw.push(signedTransaction[index]);
|
|
|
|
});
|
|
|
|
|
2016-09-16 09:46:19 +03:00
|
|
|
if (transaction.to) {
|
2017-02-24 22:41:24 +03:00
|
|
|
if (transaction.to == '0x') {
|
2016-09-16 09:46:19 +03:00
|
|
|
delete transaction.to;
|
|
|
|
} else {
|
2017-02-24 22:41:24 +03:00
|
|
|
transaction.to = utils.getAddress(transaction.to);
|
2016-09-16 09:46:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
['gasPrice', 'gasLimit', 'nonce', 'value'].forEach(function(name) {
|
|
|
|
if (!transaction[name]) { return; }
|
|
|
|
if (transaction[name].length === 0) {
|
2017-02-24 22:41:24 +03:00
|
|
|
transaction[name] = utils.bigNumberify(0);
|
2016-09-16 09:46:19 +03:00
|
|
|
} else {
|
2017-02-24 22:41:24 +03:00
|
|
|
transaction[name] = utils.bigNumberify(transaction[name]);
|
2016-09-16 09:46:19 +03:00
|
|
|
}
|
|
|
|
});
|
2016-09-16 09:08:36 +03:00
|
|
|
|
2016-09-16 09:46:19 +03:00
|
|
|
if (transaction.nonce) {
|
2017-02-24 22:41:24 +03:00
|
|
|
transaction.nonce = transaction.nonce.toNumber();
|
|
|
|
} else {
|
|
|
|
transaction.nonce = 0;
|
2016-09-16 09:46:19 +03:00
|
|
|
}
|
|
|
|
|
2017-03-08 09:47:29 +03:00
|
|
|
var v = utils.arrayify(signedTransaction[6]);
|
|
|
|
var r = utils.arrayify(signedTransaction[7]);
|
|
|
|
var s = utils.arrayify(signedTransaction[8]);
|
2017-02-24 22:41:24 +03:00
|
|
|
|
2017-03-08 09:47:29 +03:00
|
|
|
if (v.length === 1 && r.length >= 1 && r.length <= 32 && s.length >= 1 && s.length <= 32) {
|
|
|
|
transaction.v = v[0];
|
|
|
|
transaction.r = signedTransaction[7];
|
|
|
|
transaction.s = signedTransaction[8];
|
2017-02-24 22:41:24 +03:00
|
|
|
|
2017-03-08 09:47:29 +03:00
|
|
|
var chainId = (transaction.v - 35) / 2;
|
|
|
|
if (chainId < 0) { chainId = 0; }
|
|
|
|
chainId = parseInt(chainId);
|
|
|
|
|
|
|
|
transaction.chainId = chainId;
|
|
|
|
|
|
|
|
var recoveryParam = transaction.v - 27;
|
|
|
|
|
|
|
|
if (chainId) {
|
|
|
|
raw.push(utils.hexlify(chainId));
|
|
|
|
raw.push('0x');
|
|
|
|
raw.push('0x');
|
|
|
|
recoveryParam -= chainId * 2 + 8;
|
|
|
|
}
|
|
|
|
|
2017-04-05 00:19:18 +03:00
|
|
|
var digest = utils.keccak256(utils.RLP.encode(raw));
|
2017-03-08 09:47:29 +03:00
|
|
|
try {
|
|
|
|
transaction.from = SigningKey.recover(digest, r, s, recoveryParam);
|
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
2017-02-24 22:41:24 +03:00
|
|
|
}
|
2016-09-16 09:46:19 +03:00
|
|
|
}
|
2016-09-16 09:08:36 +03:00
|
|
|
|
2017-03-08 09:47:29 +03:00
|
|
|
|
2016-09-16 09:08:36 +03:00
|
|
|
return transaction;
|
|
|
|
});
|
|
|
|
|
2017-03-08 09:47:29 +03:00
|
|
|
utils.defineProperty(Wallet.prototype, 'getAddress', function() {
|
|
|
|
return this.address;
|
|
|
|
});
|
|
|
|
|
2017-02-24 22:41:24 +03:00
|
|
|
utils.defineProperty(Wallet.prototype, 'getBalance', function(blockTag) {
|
|
|
|
if (!this.provider) { throw new Error('missing provider'); }
|
2017-03-01 10:36:14 +03:00
|
|
|
return this.provider.getBalance(this.address, blockTag);
|
2016-08-03 00:43:18 +03:00
|
|
|
});
|
|
|
|
|
2017-03-01 10:36:14 +03:00
|
|
|
utils.defineProperty(Wallet.prototype, 'getTransactionCount', function(blockTag) {
|
2017-02-24 22:41:24 +03:00
|
|
|
if (!this.provider) { throw new Error('missing provider'); }
|
2017-03-01 10:36:14 +03:00
|
|
|
return this.provider.getTransactionCount(this.address, blockTag);
|
2016-08-03 00:43:18 +03:00
|
|
|
});
|
|
|
|
|
2016-08-03 09:26:36 +03:00
|
|
|
utils.defineProperty(Wallet.prototype, 'estimateGas', function(transaction) {
|
2017-02-24 22:41:24 +03:00
|
|
|
if (!this.provider) { throw new Error('missing provider'); }
|
2016-08-03 00:43:18 +03:00
|
|
|
|
2017-03-01 10:36:14 +03:00
|
|
|
var calculate = {};
|
|
|
|
['from', 'to', 'data', 'value'].forEach(function(key) {
|
|
|
|
if (transaction[key] == null) { return; }
|
|
|
|
calculate[key] = transaction[key];
|
2016-08-03 09:26:36 +03:00
|
|
|
});
|
2017-03-01 10:36:14 +03:00
|
|
|
|
|
|
|
if (transaction.from == null) { calculate.from = this.address; }
|
|
|
|
|
|
|
|
return this.provider.estimateGas(calculate);
|
2016-08-03 09:26:36 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
utils.defineProperty(Wallet.prototype, 'sendTransaction', function(transaction) {
|
2017-02-24 22:41:24 +03:00
|
|
|
if (!this.provider) { throw new Error('missing provider'); }
|
|
|
|
|
2016-08-04 10:20:01 +03:00
|
|
|
var gasLimit = transaction.gasLimit;
|
2017-02-24 22:41:24 +03:00
|
|
|
if (gasLimit == null) { gasLimit = this.defaultGasLimit; }
|
2016-08-03 00:43:18 +03:00
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
2017-05-20 22:42:16 +03:00
|
|
|
var gasPricePromise = null;
|
|
|
|
if (transaction.gasPrice) {
|
|
|
|
gasPricePromise = Promise.resolve(transaction.gasPrice);
|
|
|
|
} else {
|
|
|
|
gasPricePromise = this.provider.getGasPrice();
|
|
|
|
}
|
2017-02-24 22:41:24 +03:00
|
|
|
|
2017-05-20 22:42:16 +03:00
|
|
|
var noncePromise = null;
|
|
|
|
if (transaction.nonce) {
|
|
|
|
noncePromise = Promise.resolve(transaction.nonce);
|
|
|
|
} else {
|
|
|
|
noncePromise = this.provider.getTransactionCount(self.address, 'pending');
|
|
|
|
}
|
2016-08-03 09:26:36 +03:00
|
|
|
|
2017-03-08 09:47:29 +03:00
|
|
|
var chainId = this.provider.chainId;
|
|
|
|
|
2017-05-20 22:42:16 +03:00
|
|
|
var toPromise = null;
|
|
|
|
if (transaction.to) {
|
|
|
|
toPromise = this.provider.resolveName(transaction.to);
|
|
|
|
} else {
|
|
|
|
toPromise = Promise.resolve(undefined);
|
|
|
|
}
|
2017-02-24 22:41:24 +03:00
|
|
|
|
|
|
|
var data = utils.hexlify(transaction.data || '0x');
|
|
|
|
var value = utils.hexlify(transaction.value || 0);
|
|
|
|
|
2017-05-20 22:42:16 +03:00
|
|
|
return Promise.all([gasPricePromise, noncePromise, toPromise]).then(function(results) {
|
2017-03-01 10:36:14 +03:00
|
|
|
var signedTransaction = self.sign({
|
2017-05-20 22:42:16 +03:00
|
|
|
to: results[2],
|
2017-03-01 10:36:14 +03:00
|
|
|
data: data,
|
|
|
|
gasLimit: gasLimit,
|
|
|
|
gasPrice: results[0],
|
|
|
|
nonce: results[1],
|
2017-03-08 09:47:29 +03:00
|
|
|
value: value,
|
|
|
|
chainId: chainId
|
2016-08-03 00:43:18 +03:00
|
|
|
});
|
2017-03-01 10:36:14 +03:00
|
|
|
|
2017-04-05 00:19:18 +03:00
|
|
|
return self.provider.sendTransaction(signedTransaction).then(function(hash) {
|
|
|
|
var transaction = Wallet.parseTransaction(signedTransaction);
|
|
|
|
transaction.hash = hash;
|
|
|
|
return transaction;
|
|
|
|
});
|
2016-08-03 00:43:18 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-05-20 22:42:16 +03:00
|
|
|
utils.defineProperty(Wallet.prototype, 'send', function(addressOrName, amountWei, options) {
|
2016-08-03 09:26:36 +03:00
|
|
|
if (!options) { options = {}; }
|
|
|
|
|
|
|
|
return this.sendTransaction({
|
2017-05-20 22:42:16 +03:00
|
|
|
to: addressOrName,
|
2016-08-03 09:26:36 +03:00
|
|
|
gasLimit: options.gasLimit,
|
|
|
|
gasPrice: options.gasPrice,
|
|
|
|
nonce: options.nonce,
|
|
|
|
value: amountWei,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-10-19 09:52:55 +03:00
|
|
|
// @TODO: this is starting to get used various places; move to utils?
|
|
|
|
function hexPad(value, length) {
|
|
|
|
while (value.length < 2 * length + 2) {
|
|
|
|
value = '0x0' + value.substring(2);
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getHash(message) {
|
|
|
|
var payload = utils.concat([
|
|
|
|
utils.toUtf8Bytes('\x19Ethereum Signed Message:\n'),
|
|
|
|
utils.toUtf8Bytes(String(message.length)),
|
2017-12-02 06:25:19 +03:00
|
|
|
((typeof(message) === 'string') ? utils.toUtf8Bytes(message): message)
|
2017-10-19 09:52:55 +03:00
|
|
|
]);
|
|
|
|
return utils.keccak256(payload);
|
|
|
|
}
|
|
|
|
|
|
|
|
utils.defineProperty(Wallet.prototype, 'signMessage', function(message) {
|
|
|
|
var signingKey = new SigningKey(this.privateKey);
|
|
|
|
var sig = signingKey.signDigest(getHash(message));
|
|
|
|
|
2017-12-05 09:59:37 +03:00
|
|
|
return (hexPad(sig.r, 32) + hexPad(sig.s, 32).substring(2) + (sig.recoveryParam ? '1c': '1b'));
|
2017-10-19 09:52:55 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
utils.defineProperty(Wallet, 'verifyMessage', function(message, signature) {
|
|
|
|
signature = utils.hexlify(signature);
|
|
|
|
if (signature.length != 132) { throw new Error('invalid signature'); }
|
|
|
|
var digest = getHash(message);
|
|
|
|
var recoveryParam = parseInt(signature.substring(130), 16) - 27;
|
|
|
|
if (recoveryParam < 0) { throw new Error('invalid signature'); }
|
|
|
|
return SigningKey.recover(
|
|
|
|
digest,
|
|
|
|
signature.substring(0, 66),
|
|
|
|
'0x' + signature.substring(66, 130),
|
|
|
|
parseInt(signature.substring(130), 16) - 27
|
|
|
|
);
|
|
|
|
});
|
2017-02-24 22:41:24 +03:00
|
|
|
|
|
|
|
utils.defineProperty(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 = {}; }
|
|
|
|
|
|
|
|
return secretStorage.encrypt(this.privateKey, password, options, progressCallback);
|
|
|
|
});
|
|
|
|
|
2017-03-08 09:47:29 +03:00
|
|
|
|
|
|
|
utils.defineProperty(Wallet, 'isEncryptedWallet', function(json) {
|
2017-02-24 22:41:24 +03:00
|
|
|
return (secretStorage.isValidWallet(json) || secretStorage.isCrowdsaleWallet(json));
|
|
|
|
});
|
|
|
|
|
2017-03-08 09:47:29 +03:00
|
|
|
|
|
|
|
|
|
|
|
utils.defineProperty(Wallet, 'createRandom', function(options) {
|
|
|
|
var entropy = utils.randomBytes(16);
|
2017-05-10 01:53:32 +03:00
|
|
|
|
|
|
|
if (!options) { options = { }; }
|
|
|
|
|
2017-03-08 09:47:29 +03:00
|
|
|
if (options.extraEntropy) {
|
|
|
|
entropy = utils.keccak256(utils.concat([entropy, options.extraEntropy])).substring(0, 34);
|
|
|
|
}
|
|
|
|
var mnemonic = HDNode.entropyToMnemonic(entropy);
|
|
|
|
return Wallet.fromMnemonic(mnemonic, options.path);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
utils.defineProperty(Wallet, 'fromEncryptedWallet', function(json, password, progressCallback) {
|
2017-02-24 22:41:24 +03:00
|
|
|
if (progressCallback && typeof(progressCallback) !== 'function') {
|
|
|
|
throw new Error('invalid callback');
|
|
|
|
}
|
2017-03-08 09:47:29 +03:00
|
|
|
|
2017-02-24 22:41:24 +03:00
|
|
|
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) {
|
|
|
|
resolve(new Wallet(signingKey));
|
|
|
|
}, function(error) {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
|
|
|
|
} else {
|
|
|
|
reject('invalid wallet JSON');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-03-08 09:47:29 +03:00
|
|
|
utils.defineProperty(Wallet, 'fromMnemonic', function(mnemonic, path) {
|
|
|
|
if (!path) { path = defaultPath; }
|
2017-02-24 22:41:24 +03:00
|
|
|
|
2017-03-08 09:47:29 +03:00
|
|
|
var hdnode = HDNode.fromMnemonic(mnemonic).derivePath(path);
|
|
|
|
|
|
|
|
var wallet = new Wallet(hdnode.privateKey);
|
|
|
|
utils.defineProperty(wallet, 'mnemonic', mnemonic);
|
|
|
|
utils.defineProperty(wallet, 'path', path);
|
|
|
|
|
|
|
|
return wallet;
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
utils.defineProperty(Wallet, 'fromBrainWallet', function(username, password, progressCallback) {
|
2017-02-24 22:41:24 +03:00
|
|
|
if (progressCallback && typeof(progressCallback) !== 'function') {
|
|
|
|
throw new Error('invalid callback');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof(username) === 'string') {
|
|
|
|
username = utils.toUtf8Bytes(username, 'NFKC');
|
|
|
|
} else {
|
|
|
|
username = utils.arrayify(username, 'password');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof(password) === 'string') {
|
|
|
|
password = utils.toUtf8Bytes(password, 'NFKC');
|
|
|
|
} else {
|
|
|
|
password = utils.arrayify(password, 'password');
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
scrypt(password, username, (1 << 18), 8, 1, 32, function(error, progress, key) {
|
|
|
|
if (error) {
|
|
|
|
reject(error);
|
2017-04-05 00:19:18 +03:00
|
|
|
|
2017-02-24 22:41:24 +03:00
|
|
|
} else if (key) {
|
2017-02-27 08:02:40 +03:00
|
|
|
resolve(new Wallet(utils.hexlify(key)));
|
2017-04-05 00:19:18 +03:00
|
|
|
|
2017-02-24 22:41:24 +03:00
|
|
|
} else if (progressCallback) {
|
2017-04-05 00:19:18 +03:00
|
|
|
return progressCallback(progress);
|
2017-02-24 22:41:24 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2016-07-27 00:57:11 +03:00
|
|
|
});
|
|
|
|
|
2017-02-24 22:41:24 +03:00
|
|
|
//utils.defineProperty(Wallet, 'isCrowdsaleWallet', secretStorage.isCrowdsaleWallet);
|
|
|
|
|
|
|
|
//utils.defineProperty(Wallet, 'decryptCrowdsale', function(json, password) {
|
|
|
|
// return new Wallet(secretStorage.decryptCrowdsale(json, password));
|
|
|
|
//});
|
|
|
|
|
2016-07-22 00:03:32 +03:00
|
|
|
module.exports = Wallet;
|