Skip to content

Commit

Permalink
dont allow uneven r,s,v values
Browse files Browse the repository at this point in the history
  • Loading branch information
frozeman committed Jan 11, 2018
1 parent d252933 commit 746e440
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
29 changes: 19 additions & 10 deletions packages/web3-eth-accounts/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,21 @@ var isNot = function(value) {
return (_.isUndefined(value) || _.isNull(value));
};

var trimLeadingZero = function (hex) {
while (hex && hex.startsWith('0x00')) {
hex = '0x' + hex.slice(4);
}
return hex;
};

var makeEven = function (hex) {
if(hex % 2 === 1) {
hex = hex.replace('0x', '0x0');
}
return hex;
};


var Accounts = function Accounts() {
var _this = this;

Expand Down Expand Up @@ -150,23 +165,17 @@ Accounts.prototype.signTransaction = function signTransaction(tx, privateKey, ca

var rawTx = RLP.decode(rlpEncoded).slice(0, 6).concat(Account.decodeSignature(signature));

var trimLeadingZero = function (hex) {
while (hex && hex.startsWith('0x00')) {
hex = '0x' + hex.slice(4);
}
return hex;
}
rawTx[7] = trimLeadingZero(rawTx[7]);
rawTx[8] = trimLeadingZero(rawTx[8]);

var rawTransaction = RLP.encode(rawTx)
var rawTransaction = RLP.encode(rawTx);

var values = RLP.decode(rawTransaction);
var result = {
messageHash: hash,
v: values[6],
r: values[7],
s: values[8],
v: makeEven(values[6]),
r: makeEven(values[7]),
s: makeEven(values[8]),
rawTransaction: rawTransaction
};
if (_.isFunction(callback)) {
Expand Down
19 changes: 19 additions & 0 deletions test/eth.accounts.signTransaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,24 @@ var assert = chai.assert;
var clone = function (object) { return object ? JSON.parse(JSON.stringify(object)) : []; };

var tests = [
{
address: '0x2c7536E3605D9C16a7a3D7b1898e529396a65c23',
iban: 'XE0556YCRTEZ9JALZBSCXOK4UJ5F3HN03DV',
privateKey: '0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318',
transaction: {
chainId: 1,
nonce: 0,
gasPrice: "20000000000",
gas: 21000,
to: '0xF0109fC8DF283027b6285cc889F5aA624EaC1F55',
toIban: 'XE04S1IRT2PR8A8422TPBL9SR6U0HODDCUT', // will be switched to "to" in the test
value: "1000000000",
data: ""
},
// signature from eth_signTransaction
rawTransaction: "0xf868808504a817c80082520894f0109fc8df283027b6285cc889f5aa624eac1f55843b9aca008026a0afa02d193471bb974081585daabf8a751d4decbb519604ac7df612cc11e9226da04bf1bd55e82cebb2b09ed39bbffe35107ea611fa212c2d9a1f1ada4952077118",
oldSignature: "0xf868808504a817c80082520894f0109fc8df283027b6285cc889f5aa624eac1f55843b9aca008026a0afa02d193471bb974081585daabf8a751d4decbb519604ac7df612cc11e9226da04bf1bd55e82cebb2b09ed39bbffe35107ea611fa212c2d9a1f1ada4952077118"
},
{
address: '0xEB014f8c8B418Db6b45774c326A0E64C78914dC0',
iban: 'XE25RG8S3H5TX5RD7QTL5UPVW90AHN2VYDC',
Expand Down Expand Up @@ -58,6 +76,7 @@ describe("eth", function () {
assert.equal(testAccount.address, test.address);

var tx = testAccount.signTransaction(test.transaction);


assert.equal(tx.rawTransaction, test.rawTransaction);
});
Expand Down

0 comments on commit 746e440

Please sign in to comment.