Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return r, s, and v without leading 0's #1288

Merged
merged 2 commits into from
Jan 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions packages/web3-eth-accounts/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ var isNot = function(value) {
};

var trimLeadingZero = function (hex) {
while (hex && hex.startsWith('0x00')) {
hex = '0x' + hex.slice(4);
while (hex && hex.startsWith('0x0')) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem with this is what if the rs, value is 0x00001234 Then a even number is made uneven

hex = '0x' + hex.slice(3);
}
return hex;
};

var makeEven = function (hex) {
if(hex % 2 === 1) {
if(hex.length % 2 === 1) {
hex = hex.replace('0x', '0x0');
}
return hex;
Expand Down Expand Up @@ -165,17 +165,17 @@ Accounts.prototype.signTransaction = function signTransaction(tx, privateKey, ca

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

rawTx[7] = trimLeadingZero(rawTx[7]);
rawTx[8] = trimLeadingZero(rawTx[8]);
rawTx[7] = makeEven(trimLeadingZero(rawTx[7]));
rawTx[8] = makeEven(trimLeadingZero(rawTx[8]));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And are you sure passing a modified value into RLP.decode is not going to break anything?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean RLP.encode?

It looks like it dynamically calculates the length, so I don't see any problems: https://github.com/MaiaVictor/eth-lib/blob/master/lib/rlp.js#L27

In fact, I believe leaving out 0 bytes at the beginning of an int in the RLP-encoded data is a more standard approach. (not sure if it's required though)


var rawTransaction = RLP.encode(rawTx);

var values = RLP.decode(rawTransaction);
var result = {
messageHash: hash,
v: makeEven(values[6]),
r: makeEven(values[7]),
s: makeEven(values[8]),
v: trimLeadingZero(values[6]),
r: trimLeadingZero(values[7]),
s: trimLeadingZero(values[8]),
rawTransaction: rawTransaction
};
if (_.isFunction(callback)) {
Expand Down
35 changes: 35 additions & 0 deletions test/eth.accounts.signTransaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,27 @@ var tests = [
rawTransaction: "0xf868808504a817c80082520894f0109fc8df283027b6285cc889f5aa624eac1f55843b9aca008026a0afa02d193471bb974081585daabf8a751d4decbb519604ac7df612cc11e9226da04bf1bd55e82cebb2b09ed39bbffe35107ea611fa212c2d9a1f1ada4952077118",
oldSignature: "0xf868808504a817c80082520894f0109fc8df283027b6285cc889f5aa624eac1f55843b9aca008026a0afa02d193471bb974081585daabf8a751d4decbb519604ac7df612cc11e9226da04bf1bd55e82cebb2b09ed39bbffe35107ea611fa212c2d9a1f1ada4952077118"
},
{
address: '0x2c7536E3605D9C16a7a3D7b1898e529396a65c23',
iban: 'XE0556YCRTEZ9JALZBSCXOK4UJ5F3HN03DV',
privateKey: '0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318',
transaction: {
chainId: 1,
nonce: 0,
gasPrice: "234567897654321",
gas: 2000000,
to: '0xF0109fC8DF283027b6285cc889F5aA624EaC1F55',
toIban: 'XE04S1IRT2PR8A8422TPBL9SR6U0HODDCUT', // will be switched to "to" in the test
value: "1000000000",
data: ""
},
// expected r and s values from signature
r: "0x9ebb6ca057a0535d6186462bc0b465b561c94a295bdb0621fc19208ab149a9c",
s: "0x440ffd775ce91a833ab410777204d5341a6f9fa91216a6f3ee2c051fea6a0428",
// signature from eth_signTransaction
rawTransaction: "0xf86a8086d55698372431831e848094f0109fc8df283027b6285cc889f5aa624eac1f55843b9aca008025a009ebb6ca057a0535d6186462bc0b465b561c94a295bdb0621fc19208ab149a9ca0440ffd775ce91a833ab410777204d5341a6f9fa91216a6f3ee2c051fea6a0428",
oldSignature: "0xf86a8086d55698372431831e848094f0109fc8df283027b6285cc889f5aa624eac1f55843b9aca008025a009ebb6ca057a0535d6186462bc0b465b561c94a295bdb0621fc19208ab149a9ca0440ffd775ce91a833ab410777204d5341a6f9fa91216a6f3ee2c051fea6a0428"
},
{
address: '0xEB014f8c8B418Db6b45774c326A0E64C78914dC0',
iban: 'XE25RG8S3H5TX5RD7QTL5UPVW90AHN2VYDC',
Expand Down Expand Up @@ -82,6 +103,20 @@ describe("eth", function () {
});
});

tests.forEach(function (test, i) {
it("signTransaction must produce r and s as quantities, not data", function() {
if (!('r' in test)) {
return ;
}
var ethAccounts = new Accounts();

var signature = ethAccounts.signTransaction(test.transaction, test.privateKey);

assert.equal(signature.r, test.r);
assert.equal(signature.s, test.s);
});
});

tests.forEach(function (test, i) {
it("signTransaction using the iban as \"to\" must compare to eth_signTransaction", function() {
var ethAccounts = new Accounts();
Expand Down