Added transaction parsing with address recovery.

This commit is contained in:
ricmoo 2016-09-16 14:08:36 +08:00
parent b26b1b9c53
commit d8013cae37
6 changed files with 63 additions and 4 deletions

28
dist/ethers-wallet.js vendored

@ -1582,6 +1582,7 @@ var utils = require('./utils.js');
var secp256k1 = new (elliptic.ec)('secp256k1');
function SigningKey(privateKey) {
if (!(this instanceof SigningKey)) { throw new Error('missing new'); }
@ -1603,6 +1604,11 @@ function SigningKey(privateKey) {
});
}
utils.defineProperty(SigningKey, 'recover', function(digest, r, s, recoveryParam) {
var publicKey = secp256k1.recoverPubKey(digest, {r: r, s: s}, recoveryParam);
publicKey = (new Buffer(publicKey.encode('hex', false), 'hex')).slice(1);
return utils.getAddress(utils.sha3(publicKey).slice(12).toString('hex'));
});
module.exports = SigningKey;
@ -2208,6 +2214,28 @@ function Wallet(privateKey, provider) {
});
}
utils.defineProperty(Wallet, 'parseTransaction', function(rawTransaction) {
rawTransaction = utils.hexOrBuffer(rawTransaction, 'rawTransaction');
var signedTransaction = rlp.decode(rawTransaction);
var raw = [];
var transaction = {};
transactionFields.forEach(function(fieldInfo, index) {
transaction[fieldInfo.name] = signedTransaction[index];
raw.push(signedTransaction[index]);
});
transaction.v = signedTransaction[6];
transaction.r = signedTransaction[7];
transaction.s = signedTransaction[8];
var digest = utils.sha3(rlp.encode(raw));
transaction.from = SigningKey.recover(digest, transaction.r, transaction.s, transaction.v[0] - 27);
return transaction;
});
utils.defineProperty(Wallet.prototype, 'getBalance', function(blockNumber) {
var provider = this._provider;

File diff suppressed because one or more lines are too long

@ -7,6 +7,7 @@ var utils = require('./utils.js');
var secp256k1 = new (elliptic.ec)('secp256k1');
function SigningKey(privateKey) {
if (!(this instanceof SigningKey)) { throw new Error('missing new'); }
@ -28,5 +29,10 @@ function SigningKey(privateKey) {
});
}
utils.defineProperty(SigningKey, 'recover', function(digest, r, s, recoveryParam) {
var publicKey = secp256k1.recoverPubKey(digest, {r: r, s: s}, recoveryParam);
publicKey = (new Buffer(publicKey.encode('hex', false), 'hex')).slice(1);
return utils.getAddress(utils.sha3(publicKey).slice(12).toString('hex'));
});
module.exports = SigningKey;

@ -111,6 +111,28 @@ function Wallet(privateKey, provider) {
});
}
utils.defineProperty(Wallet, 'parseTransaction', function(rawTransaction) {
rawTransaction = utils.hexOrBuffer(rawTransaction, 'rawTransaction');
var signedTransaction = rlp.decode(rawTransaction);
var raw = [];
var transaction = {};
transactionFields.forEach(function(fieldInfo, index) {
transaction[fieldInfo.name] = signedTransaction[index];
raw.push(signedTransaction[index]);
});
transaction.v = signedTransaction[6];
transaction.r = signedTransaction[7];
transaction.s = signedTransaction[8];
var digest = utils.sha3(rlp.encode(raw));
transaction.from = SigningKey.recover(digest, transaction.r, transaction.s, transaction.v[0] - 27);
return transaction;
});
utils.defineProperty(Wallet.prototype, 'getBalance', function(blockNumber) {
var provider = this._provider;

@ -1,6 +1,6 @@
{
"name": "ethers-wallet",
"version": "1.0.2",
"version": "1.0.3",
"description": "Ethereum wallet library.",
"main": "index.js",
"scripts": {

@ -17,9 +17,12 @@ module.exports = function(test) {
rawTransaction.sign(privateKey);
var ethereumLib = '0x' + rawTransaction.serialize().toString('hex');
var ethers = (new Wallet(privateKey)).sign(transaction);
var wallet = new Wallet(privateKey);
var ethers = wallet.sign(transaction);
test.equal(ethers, ethereumLib, 'invalid transaction');
test.equal(wallet.address, Wallet.parseTransaction(ethers).from, 'invalid parseTransaction');
}
for (var i = 0; i < 10000; i++) {