Added EIP155 support.
This commit is contained in:
parent
94ba940382
commit
adc1d93ac2
@ -12,8 +12,9 @@ var Output = [];
|
||||
function addTransaction(privateKey, name, transaction, signature) {
|
||||
var rawTransaction = new ethereumTx(transaction);
|
||||
|
||||
transaction.chainId = 5;
|
||||
var rawTransactionEip155 = new ethereumTx(transaction);
|
||||
rawTransactionEip155._chainId = 5;
|
||||
delete transaction['chainId'];
|
||||
|
||||
var test = {
|
||||
accountAddress: '0x' + ethereumUtil.privateToAddress(privateKey).toString('hex'),
|
||||
@ -40,14 +41,21 @@ function addTransaction(privateKey, name, transaction, signature) {
|
||||
Output.push(test);
|
||||
}
|
||||
|
||||
function trimHex(hex) {
|
||||
while (hex.substring(0, 4) === '0x00' && hex.length > 4) {
|
||||
hex = '0x' + hex.substring(4);
|
||||
}
|
||||
return hex;
|
||||
}
|
||||
|
||||
for (var i = 0; i < 1000; i++) {
|
||||
var transaction = {
|
||||
to: utils.randomHexString('to-' + i, 20),
|
||||
data: utils.randomHexString('data-' + i, 0, 10),
|
||||
gasLimit: utils.randomHexString('gasLimit-' + i, 0, 10),
|
||||
gasPrice: utils.randomHexString('gasPrice-' + i, 0, 10),
|
||||
value: utils.randomHexString('value-' + i, 0, 10),
|
||||
nonce: utils.randomHexString('nonce-' + i, 0, 4),
|
||||
gasLimit: trimHex(utils.randomHexString('gasLimit-' + i, 0, 10)),
|
||||
gasPrice: trimHex(utils.randomHexString('gasPrice-' + i, 0, 10)),
|
||||
value: trimHex(utils.randomHexString('value-' + i, 0, 10)),
|
||||
nonce: trimHex(utils.randomHexString('nonce-' + i, 0, 4)),
|
||||
};
|
||||
|
||||
var privateKey = new Buffer(utils.randomBytes('privateKey-' + i, 32));
|
||||
|
@ -1,6 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
var Wallet = require('../wallet/index.js');
|
||||
var Wallet = require('../wallet/wallet.js');
|
||||
|
||||
function testAccounts(test) {
|
||||
|
||||
@ -113,7 +113,7 @@ function testTransactions(test) {
|
||||
|
||||
if ({gasLimit: 1, gasPrice: 1, value: 1}[key]) {
|
||||
if (utils.isBigNumber(value)) {
|
||||
test.ok(true, 'failed to parse into a big number - ' + key + ' - ' + testcase.name);
|
||||
test.ok(true, 'parse into a big number - ' + key + ' - ' + testcase.name);
|
||||
value = value.toHexString();
|
||||
|
||||
if (!expected || expected === '0x') {
|
||||
@ -121,13 +121,13 @@ function testTransactions(test) {
|
||||
}
|
||||
|
||||
} else {
|
||||
test.ok(false, 'failed to parse into a big number - ' + key + ' - ' + testcase.name);
|
||||
test.ok(false, 'parse into a big number - ' + key + ' - ' + testcase.name);
|
||||
return;
|
||||
}
|
||||
|
||||
} else if (key === 'nonce') {
|
||||
if (typeof(value) === 'number') {
|
||||
test.ok(true, 'failed to parse into a number - nonce - ' + testcase.name);
|
||||
test.ok(true, 'parse into a number - nonce - ' + testcase.name);
|
||||
value = utils.hexlify(value);
|
||||
|
||||
if (!expected || expected === '0x') {
|
||||
@ -135,7 +135,7 @@ function testTransactions(test) {
|
||||
}
|
||||
|
||||
} else {
|
||||
test.ok(false, 'failed to parse into a number - nonce - ' + testcase.name);
|
||||
test.ok(false, 'parse into a number - nonce - ' + testcase.name);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -149,20 +149,38 @@ function testTransactions(test) {
|
||||
try {
|
||||
utils.getAddress(value);
|
||||
} catch (error) {
|
||||
test.ok(false, 'failed to create checksum address - to - ' + testcase.name);
|
||||
test.ok(false, 'create checksum address - to - ' + testcase.name);
|
||||
}
|
||||
value = value.toLowerCase();
|
||||
}
|
||||
}
|
||||
|
||||
test.equal(value, expected, 'failed to parse - ' + key + ' - ' + testcase.name);
|
||||
test.equal(value, expected, 'parse - ' + key + ' - ' + testcase.name);
|
||||
|
||||
transaction[key] = testcase[key];
|
||||
});
|
||||
|
||||
test.equal(parsedTransaction.from, utils.getAddress(testcase.accountAddress), 'compute - from - ' + testcase.name);
|
||||
|
||||
test.equal(parsedTransaction.chainId, 0, 'parse - chainId - ' + testcase.name);
|
||||
|
||||
var signedTransaction = wallet.sign(transaction);
|
||||
test.equal(signedTransaction, testcase.signedTransaction,
|
||||
'failed to sign transaction - ' + testcase.name);
|
||||
test.equal(signedTransaction, testcase.signedTransaction, 'sign transaction - ' + testcase.name);
|
||||
|
||||
// EIP155
|
||||
|
||||
var parsedTransactionChainId5 = Wallet.parseTransaction(testcase.signedTransactionChainId5);
|
||||
['data', 'from', 'nonce', 'to'].forEach(function (key) {
|
||||
test.equal(parsedTransaction[key], parsedTransactionChainId5[key], 'eip155 parse - ' + key + ' - ' + testcase.name);
|
||||
});
|
||||
['gasLimit', 'gasPrice', 'value'].forEach(function (key) {
|
||||
test.ok(parsedTransaction[key].eq(parsedTransactionChainId5[key]), 'eip155 parse - ' + key + ' - ' + testcase.name);
|
||||
});
|
||||
test.equal(parsedTransactionChainId5.chainId, 5, 'eip155 parse - chainId - ' + testcase.name);
|
||||
|
||||
transaction.chainId = 5;
|
||||
var signedTransactionChainId5 = wallet.sign(transaction);
|
||||
test.equal(signedTransactionChainId5, testcase.signedTransactionChainId5, 'eip155 sign transaction - ' + testcase.name);
|
||||
});
|
||||
|
||||
test.done();
|
||||
|
106
wallet/wallet.js
106
wallet/wallet.js
@ -1,16 +1,19 @@
|
||||
'use strict';
|
||||
|
||||
var scrypt = require('scrypt-js');
|
||||
var utils = require('ethers-utils');
|
||||
|
||||
var secretStorage = require('./secret-storage.js');
|
||||
var SigningKey = require('./signing-key.js');
|
||||
var HDNode = require('./hdnode');
|
||||
|
||||
var scrypt = require('scrypt-js');
|
||||
var secretStorage = require('./secret-storage');
|
||||
var SigningKey = require('./signing-key');
|
||||
|
||||
// This ensures we inject a setImmediate into the global space, which
|
||||
// dramatically improves the performance of the scrypt PBKDF.
|
||||
require('setimmediate');
|
||||
|
||||
var defaultPath = "m/44'/60'/0'/0/0";
|
||||
|
||||
var transactionFields = [
|
||||
{name: 'nonce', maxLength: 32, },
|
||||
{name: 'gasPrice', maxLength: 32, },
|
||||
@ -40,7 +43,7 @@ function Wallet(privateKey, provider) {
|
||||
});
|
||||
if (provider) { this.provider = provider; }
|
||||
|
||||
var defaultGasLimit = 2000000;
|
||||
var defaultGasLimit = 1500000;
|
||||
Object.defineProperty(this, 'defaultGasLimit', {
|
||||
enumerable: true,
|
||||
get: function() { return defaultGasLimit; },
|
||||
@ -53,6 +56,10 @@ function Wallet(privateKey, provider) {
|
||||
utils.defineProperty(this, 'address', signingKey.address);
|
||||
|
||||
utils.defineProperty(this, 'sign', function(transaction) {
|
||||
var chainId = transaction.chainId;
|
||||
if (!chainId && this.provider) { chainId = this.provider.chainId; }
|
||||
if (!chainId) { chainId = 0; }
|
||||
|
||||
var raw = [];
|
||||
transactionFields.forEach(function(fieldInfo) {
|
||||
var value = transaction[fieldInfo.name] || ([]);
|
||||
@ -80,21 +87,37 @@ function Wallet(privateKey, provider) {
|
||||
raw.push(utils.hexlify(value));
|
||||
});
|
||||
|
||||
if (chainId) {
|
||||
raw.push(utils.hexlify(chainId));
|
||||
raw.push('0x');
|
||||
raw.push('0x');
|
||||
}
|
||||
|
||||
var digest = utils.keccak256(utils.rlp.encode(raw));
|
||||
|
||||
var signature = signingKey.signDigest(digest);
|
||||
|
||||
raw.push(utils.hexlify([27 + signature.recoveryParam]));
|
||||
var v = 27 + signature.recoveryParam
|
||||
if (chainId) {
|
||||
raw.pop();
|
||||
raw.pop();
|
||||
raw.pop();
|
||||
v += chainId * 2 + 8;
|
||||
}
|
||||
|
||||
raw.push(utils.hexlify(v));
|
||||
raw.push(signature.r);
|
||||
raw.push(signature.s);
|
||||
|
||||
return (utils.rlp.encode(raw));
|
||||
return utils.rlp.encode(raw);
|
||||
});
|
||||
}
|
||||
|
||||
utils.defineProperty(Wallet, 'parseTransaction', function(rawTransaction) {
|
||||
rawTransaction = utils.hexlify(rawTransaction, 'rawTransaction');
|
||||
var signedTransaction = utils.rlp.decode(rawTransaction);
|
||||
if (signedTransaction.length !== 9) { throw new Error('invalid transaction'); }
|
||||
|
||||
var raw = [];
|
||||
|
||||
var transaction = {};
|
||||
@ -126,7 +149,6 @@ utils.defineProperty(Wallet, 'parseTransaction', function(rawTransaction) {
|
||||
transaction.nonce = 0;
|
||||
}
|
||||
|
||||
if (signedTransaction.length > 6) {
|
||||
var v = utils.arrayify(signedTransaction[6]);
|
||||
var r = utils.arrayify(signedTransaction[7]);
|
||||
var s = utils.arrayify(signedTransaction[8]);
|
||||
@ -136,18 +158,37 @@ utils.defineProperty(Wallet, 'parseTransaction', function(rawTransaction) {
|
||||
transaction.r = signedTransaction[7];
|
||||
transaction.s = signedTransaction[8];
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
var digest = utils.keccak256(utils.rlp.encode(raw));
|
||||
try {
|
||||
transaction.from = SigningKey.recover(digest, r, s, transaction.v - 27);
|
||||
transaction.from = SigningKey.recover(digest, r, s, recoveryParam);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return transaction;
|
||||
});
|
||||
|
||||
utils.defineProperty(Wallet.prototype, 'getAddress', function() {
|
||||
return this.address;
|
||||
});
|
||||
|
||||
utils.defineProperty(Wallet.prototype, 'getBalance', function(blockTag) {
|
||||
if (!this.provider) { throw new Error('missing provider'); }
|
||||
return this.provider.getBalance(this.address, blockTag);
|
||||
@ -206,6 +247,8 @@ utils.defineProperty(Wallet.prototype, 'sendTransaction', function(transaction)
|
||||
});
|
||||
});
|
||||
|
||||
var chainId = this.provider.chainId;
|
||||
|
||||
var toAddress = undefined;
|
||||
if (transaction.to) { toAddress = utils.getAddress(transaction.to); }
|
||||
|
||||
@ -219,7 +262,8 @@ utils.defineProperty(Wallet.prototype, 'sendTransaction', function(transaction)
|
||||
gasLimit: gasLimit,
|
||||
gasPrice: results[0],
|
||||
nonce: results[1],
|
||||
value: value
|
||||
value: value,
|
||||
chainId: chainId
|
||||
});
|
||||
|
||||
return self.provider.sendTransaction(signedTransaction);
|
||||
@ -254,14 +298,28 @@ utils.defineProperty(Wallet.prototype, 'encrypt', function(password, options, pr
|
||||
return secretStorage.encrypt(this.privateKey, password, options, progressCallback);
|
||||
});
|
||||
|
||||
utils.defineProperty(Wallet, 'isValidWallet', function(json) {
|
||||
|
||||
utils.defineProperty(Wallet, 'isEncryptedWallet', function(json) {
|
||||
return (secretStorage.isValidWallet(json) || secretStorage.isCrowdsaleWallet(json));
|
||||
});
|
||||
|
||||
utils.defineProperty(Wallet, 'decrypt', function(json, password, progressCallback) {
|
||||
|
||||
|
||||
utils.defineProperty(Wallet, 'createRandom', function(options) {
|
||||
var entropy = utils.randomBytes(16);
|
||||
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) {
|
||||
if (progressCallback && typeof(progressCallback) !== 'function') {
|
||||
throw new Error('invalid callback');
|
||||
}
|
||||
|
||||
return new Promise(function(resolve, reject) {
|
||||
|
||||
if (secretStorage.isCrowdsaleWallet(json)) {
|
||||
@ -286,8 +344,20 @@ utils.defineProperty(Wallet, 'decrypt', function(json, password, progressCallbac
|
||||
});
|
||||
});
|
||||
|
||||
utils.defineProperty(Wallet, 'fromMnemonic', function(mnemonic, path) {
|
||||
if (!path) { path = defaultPath; }
|
||||
|
||||
utils.defineProperty(Wallet, 'summonBrainWallet', function(username, password, progressCallback) {
|
||||
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) {
|
||||
if (progressCallback && typeof(progressCallback) !== 'function') {
|
||||
throw new Error('invalid callback');
|
||||
}
|
||||
@ -324,12 +394,8 @@ utils.defineProperty(Wallet, 'summonBrainWallet', function(username, password, p
|
||||
// return new Wallet(secretStorage.decryptCrowdsale(json, password));
|
||||
//});
|
||||
|
||||
|
||||
utils.defineProperty(Wallet, '_SigningKey', SigningKey);
|
||||
// @TOOD: Move this to ethers.SigningKey, ethers.HDNode and ethers.Wallet
|
||||
utils.defineProperty(Wallet, 'SigningKey', SigningKey);
|
||||
utils.defineProperty(Wallet, 'HDNode', HDNode);
|
||||
|
||||
module.exports = Wallet;
|
||||
|
||||
require('ethers-utils/standalone.js')({
|
||||
Wallet: module.exports
|
||||
});
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user