Skip to content

Commit

Permalink
Updated parseTransaction to format parameters more meaningfully.
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Sep 16, 2016
1 parent d8013ca commit 736b08e
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 12 deletions.
40 changes: 35 additions & 5 deletions dist/ethers-wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -2226,12 +2226,42 @@ utils.defineProperty(Wallet, 'parseTransaction', function(rawTransaction) {
raw.push(signedTransaction[index]);
});

transaction.v = signedTransaction[6];
transaction.r = signedTransaction[7];
transaction.s = signedTransaction[8];
if (transaction.to) {
if (transaction.to.length === 0) {
delete transaction.to;
} else {
transaction.to = utils.getAddress('0x' + transaction.to.toString('hex'));
}
}

['gasPrice', 'gasLimit', 'nonce', 'value'].forEach(function(name) {
if (!transaction[name]) { return; }
if (transaction[name].length === 0) {
transaction[name] = new utils.BN(0);
} else {
transaction[name] = new utils.BN(transaction[name].toString('hex'), 16);
}
});

var digest = utils.sha3(rlp.encode(raw));
transaction.from = SigningKey.recover(digest, transaction.r, transaction.s, transaction.v[0] - 27);
/* @TODO: Maybe? In the future, all nonces stored as numbers? (obviously, major version change)
if (transaction.nonce) {
transaction.nonce = transaction.nonce.toNumber()
}
*/

if (signedTransaction.length > 6 && signedTransaction[6].length === 1 &&
signedTransaction[7].length >= 1 && signedTransaction[7].length <= 32 &&
signedTransaction[8].length >= 1 && signedTransaction[7].length <= 32) {

transaction.v = signedTransaction[6][0];
transaction.r = signedTransaction[7];
transaction.s = signedTransaction[8];

var digest = utils.sha3(rlp.encode(raw));
try {
transaction.from = SigningKey.recover(digest, transaction.r, transaction.s, transaction.v - 27);
} catch (error) { }
}

return transaction;
});
Expand Down
2 changes: 1 addition & 1 deletion dist/ethers-wallet.min.js

Large diffs are not rendered by default.

40 changes: 35 additions & 5 deletions lib/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,42 @@ utils.defineProperty(Wallet, 'parseTransaction', function(rawTransaction) {
raw.push(signedTransaction[index]);
});

transaction.v = signedTransaction[6];
transaction.r = signedTransaction[7];
transaction.s = signedTransaction[8];
if (transaction.to) {
if (transaction.to.length === 0) {
delete transaction.to;
} else {
transaction.to = utils.getAddress('0x' + transaction.to.toString('hex'));
}
}

['gasPrice', 'gasLimit', 'nonce', 'value'].forEach(function(name) {
if (!transaction[name]) { return; }
if (transaction[name].length === 0) {
transaction[name] = new utils.BN(0);
} else {
transaction[name] = new utils.BN(transaction[name].toString('hex'), 16);
}
});

var digest = utils.sha3(rlp.encode(raw));
transaction.from = SigningKey.recover(digest, transaction.r, transaction.s, transaction.v[0] - 27);
/* @TODO: Maybe? In the future, all nonces stored as numbers? (obviously, major version change)
if (transaction.nonce) {
transaction.nonce = transaction.nonce.toNumber()
}
*/

if (signedTransaction.length > 6 && signedTransaction[6].length === 1 &&
signedTransaction[7].length >= 1 && signedTransaction[7].length <= 32 &&
signedTransaction[8].length >= 1 && signedTransaction[7].length <= 32) {

transaction.v = signedTransaction[6][0];
transaction.r = signedTransaction[7];
transaction.s = signedTransaction[8];

var digest = utils.sha3(rlp.encode(raw));
try {
transaction.from = SigningKey.recover(digest, transaction.r, transaction.s, transaction.v - 27);
} catch (error) { }
}

return transaction;
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ethers-wallet",
"version": "1.0.3",
"version": "1.0.4",
"description": "Ethereum wallet library.",
"main": "index.js",
"scripts": {
Expand Down
1 change: 1 addition & 0 deletions tests/test-transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module.exports = function(test) {

test.equal(ethers, ethereumLib, 'invalid transaction');

// @TODO: More testing on parsed transaction.
test.equal(wallet.address, Wallet.parseTransaction(ethers).from, 'invalid parseTransaction');
}

Expand Down

0 comments on commit 736b08e

Please sign in to comment.