Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit ac28015

Browse files
committed
Return r, s, and v without leading 0's
1 parent 746e440 commit ac28015

File tree

2 files changed

+43
-8
lines changed

2 files changed

+43
-8
lines changed

packages/web3-eth-accounts/src/index.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ var isNot = function(value) {
4242
};
4343

4444
var trimLeadingZero = function (hex) {
45-
while (hex && hex.startsWith('0x00')) {
46-
hex = '0x' + hex.slice(4);
45+
while (hex && hex.startsWith('0x0')) {
46+
hex = '0x' + hex.slice(3);
4747
}
4848
return hex;
4949
};
5050

5151
var makeEven = function (hex) {
52-
if(hex % 2 === 1) {
52+
if(hex.length % 2 === 1) {
5353
hex = hex.replace('0x', '0x0');
5454
}
5555
return hex;
@@ -165,17 +165,17 @@ Accounts.prototype.signTransaction = function signTransaction(tx, privateKey, ca
165165

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

168-
rawTx[7] = trimLeadingZero(rawTx[7]);
169-
rawTx[8] = trimLeadingZero(rawTx[8]);
168+
rawTx[7] = makeEven(trimLeadingZero(rawTx[7]));
169+
rawTx[8] = makeEven(trimLeadingZero(rawTx[8]));
170170

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

173173
var values = RLP.decode(rawTransaction);
174174
var result = {
175175
messageHash: hash,
176-
v: makeEven(values[6]),
177-
r: makeEven(values[7]),
178-
s: makeEven(values[8]),
176+
v: trimLeadingZero(values[6]),
177+
r: trimLeadingZero(values[7]),
178+
s: trimLeadingZero(values[8]),
179179
rawTransaction: rawTransaction
180180
};
181181
if (_.isFunction(callback)) {

test/eth.accounts.signTransaction.js

+35
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,27 @@ var tests = [
2626
rawTransaction: "0xf868808504a817c80082520894f0109fc8df283027b6285cc889f5aa624eac1f55843b9aca008026a0afa02d193471bb974081585daabf8a751d4decbb519604ac7df612cc11e9226da04bf1bd55e82cebb2b09ed39bbffe35107ea611fa212c2d9a1f1ada4952077118",
2727
oldSignature: "0xf868808504a817c80082520894f0109fc8df283027b6285cc889f5aa624eac1f55843b9aca008026a0afa02d193471bb974081585daabf8a751d4decbb519604ac7df612cc11e9226da04bf1bd55e82cebb2b09ed39bbffe35107ea611fa212c2d9a1f1ada4952077118"
2828
},
29+
{
30+
address: '0x2c7536E3605D9C16a7a3D7b1898e529396a65c23',
31+
iban: 'XE0556YCRTEZ9JALZBSCXOK4UJ5F3HN03DV',
32+
privateKey: '0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318',
33+
transaction: {
34+
chainId: 1,
35+
nonce: 0,
36+
gasPrice: "234567897654321",
37+
gas: 2000000,
38+
to: '0xF0109fC8DF283027b6285cc889F5aA624EaC1F55',
39+
toIban: 'XE04S1IRT2PR8A8422TPBL9SR6U0HODDCUT', // will be switched to "to" in the test
40+
value: "1000000000",
41+
data: ""
42+
},
43+
// expected r and s values from signature
44+
r: "0x9ebb6ca057a0535d6186462bc0b465b561c94a295bdb0621fc19208ab149a9c",
45+
s: "0x440ffd775ce91a833ab410777204d5341a6f9fa91216a6f3ee2c051fea6a0428",
46+
// signature from eth_signTransaction
47+
rawTransaction: "0xf86a8086d55698372431831e848094f0109fc8df283027b6285cc889f5aa624eac1f55843b9aca008025a009ebb6ca057a0535d6186462bc0b465b561c94a295bdb0621fc19208ab149a9ca0440ffd775ce91a833ab410777204d5341a6f9fa91216a6f3ee2c051fea6a0428",
48+
oldSignature: "0xf86a8086d55698372431831e848094f0109fc8df283027b6285cc889f5aa624eac1f55843b9aca008025a009ebb6ca057a0535d6186462bc0b465b561c94a295bdb0621fc19208ab149a9ca0440ffd775ce91a833ab410777204d5341a6f9fa91216a6f3ee2c051fea6a0428"
49+
},
2950
{
3051
address: '0xEB014f8c8B418Db6b45774c326A0E64C78914dC0',
3152
iban: 'XE25RG8S3H5TX5RD7QTL5UPVW90AHN2VYDC',
@@ -82,6 +103,20 @@ describe("eth", function () {
82103
});
83104
});
84105

106+
tests.forEach(function (test, i) {
107+
it("signTransaction must produce r and s as quantities, not data", function() {
108+
if (!('r' in test)) {
109+
return ;
110+
}
111+
var ethAccounts = new Accounts();
112+
113+
var signature = ethAccounts.signTransaction(test.transaction, test.privateKey);
114+
115+
assert.equal(signature.r, test.r);
116+
assert.equal(signature.s, test.s);
117+
});
118+
});
119+
85120
tests.forEach(function (test, i) {
86121
it("signTransaction using the iban as \"to\" must compare to eth_signTransaction", function() {
87122
var ethAccounts = new Accounts();

0 commit comments

Comments
 (0)