Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 736b08e

Browse files
committed
Updated parseTransaction to format parameters more meaningfully.
1 parent d8013ca commit 736b08e

File tree

5 files changed

+73
-12
lines changed

5 files changed

+73
-12
lines changed

dist/ethers-wallet.js

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2226,12 +2226,42 @@ utils.defineProperty(Wallet, 'parseTransaction', function(rawTransaction) {
22262226
raw.push(signedTransaction[index]);
22272227
});
22282228

2229-
transaction.v = signedTransaction[6];
2230-
transaction.r = signedTransaction[7];
2231-
transaction.s = signedTransaction[8];
2229+
if (transaction.to) {
2230+
if (transaction.to.length === 0) {
2231+
delete transaction.to;
2232+
} else {
2233+
transaction.to = utils.getAddress('0x' + transaction.to.toString('hex'));
2234+
}
2235+
}
2236+
2237+
['gasPrice', 'gasLimit', 'nonce', 'value'].forEach(function(name) {
2238+
if (!transaction[name]) { return; }
2239+
if (transaction[name].length === 0) {
2240+
transaction[name] = new utils.BN(0);
2241+
} else {
2242+
transaction[name] = new utils.BN(transaction[name].toString('hex'), 16);
2243+
}
2244+
});
22322245

2233-
var digest = utils.sha3(rlp.encode(raw));
2234-
transaction.from = SigningKey.recover(digest, transaction.r, transaction.s, transaction.v[0] - 27);
2246+
/* @TODO: Maybe? In the future, all nonces stored as numbers? (obviously, major version change)
2247+
if (transaction.nonce) {
2248+
transaction.nonce = transaction.nonce.toNumber()
2249+
}
2250+
*/
2251+
2252+
if (signedTransaction.length > 6 && signedTransaction[6].length === 1 &&
2253+
signedTransaction[7].length >= 1 && signedTransaction[7].length <= 32 &&
2254+
signedTransaction[8].length >= 1 && signedTransaction[7].length <= 32) {
2255+
2256+
transaction.v = signedTransaction[6][0];
2257+
transaction.r = signedTransaction[7];
2258+
transaction.s = signedTransaction[8];
2259+
2260+
var digest = utils.sha3(rlp.encode(raw));
2261+
try {
2262+
transaction.from = SigningKey.recover(digest, transaction.r, transaction.s, transaction.v - 27);
2263+
} catch (error) { }
2264+
}
22352265

22362266
return transaction;
22372267
});

dist/ethers-wallet.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/wallet.js

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,42 @@ utils.defineProperty(Wallet, 'parseTransaction', function(rawTransaction) {
123123
raw.push(signedTransaction[index]);
124124
});
125125

126-
transaction.v = signedTransaction[6];
127-
transaction.r = signedTransaction[7];
128-
transaction.s = signedTransaction[8];
126+
if (transaction.to) {
127+
if (transaction.to.length === 0) {
128+
delete transaction.to;
129+
} else {
130+
transaction.to = utils.getAddress('0x' + transaction.to.toString('hex'));
131+
}
132+
}
133+
134+
['gasPrice', 'gasLimit', 'nonce', 'value'].forEach(function(name) {
135+
if (!transaction[name]) { return; }
136+
if (transaction[name].length === 0) {
137+
transaction[name] = new utils.BN(0);
138+
} else {
139+
transaction[name] = new utils.BN(transaction[name].toString('hex'), 16);
140+
}
141+
});
129142

130-
var digest = utils.sha3(rlp.encode(raw));
131-
transaction.from = SigningKey.recover(digest, transaction.r, transaction.s, transaction.v[0] - 27);
143+
/* @TODO: Maybe? In the future, all nonces stored as numbers? (obviously, major version change)
144+
if (transaction.nonce) {
145+
transaction.nonce = transaction.nonce.toNumber()
146+
}
147+
*/
148+
149+
if (signedTransaction.length > 6 && signedTransaction[6].length === 1 &&
150+
signedTransaction[7].length >= 1 && signedTransaction[7].length <= 32 &&
151+
signedTransaction[8].length >= 1 && signedTransaction[7].length <= 32) {
152+
153+
transaction.v = signedTransaction[6][0];
154+
transaction.r = signedTransaction[7];
155+
transaction.s = signedTransaction[8];
156+
157+
var digest = utils.sha3(rlp.encode(raw));
158+
try {
159+
transaction.from = SigningKey.recover(digest, transaction.r, transaction.s, transaction.v - 27);
160+
} catch (error) { }
161+
}
132162

133163
return transaction;
134164
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ethers-wallet",
3-
"version": "1.0.3",
3+
"version": "1.0.4",
44
"description": "Ethereum wallet library.",
55
"main": "index.js",
66
"scripts": {

tests/test-transactions.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ module.exports = function(test) {
2222

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

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

0 commit comments

Comments
 (0)