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

Commit 3f5cb38

Browse files
signTransaction fix (#4295)
* fixed v r s * unit test signTransaction * change log * Fix hardfork typo Co-authored-by: Wyatt Barnes <[email protected]>
1 parent c70722b commit 3f5cb38

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -447,3 +447,4 @@ Released with 1.0.0-beta.37 code base.
447447
- lerna from 3.22.1 to 4.0.0 (#4231)
448448
- Dropped build tests in CI for Node v8 and v10, and added support for Node v14
449449
- Change default value for `maxPriorityFeePerGas` from `1 Gwei` to `2.5 Gwei` (#4284)
450+
- Fixed bug in signTransaction (#4295)

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,9 @@ Accounts.prototype.signTransaction = function signTransaction(tx, privateKey, ca
228228

229229
var result = {
230230
messageHash: '0x' + Buffer.from(signedTx.getMessageToSign(true)).toString('hex'),
231-
v: '0x' + Buffer.from(signedTx.v).toString('hex'),
232-
r: '0x' + Buffer.from(signedTx.r).toString('hex'),
233-
s: '0x' + Buffer.from(signedTx.s).toString('hex'),
231+
v: '0x' + signedTx.v.toString('hex'),
232+
r: '0x' + signedTx.r.toString('hex'),
233+
s: '0x' + signedTx.s.toString('hex'),
234234
rawTransaction: rawTransaction,
235235
transactionHash: transactionHash
236236
};

test/e2e.method.signing.js

+40-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var assert = require('assert');
22
var Basic = require('./sources/Basic');
33
var utils = require('./helpers/test.utils');
44
var Web3 = utils.getWeb3();
5+
var {TransactionFactory} = require('@ethereumjs/tx');
56

67
describe('transaction and message signing [ @E2E ]', function() {
78
let web3;
@@ -563,5 +564,43 @@ describe('transaction and message signing [ @E2E ]', function() {
563564
done(error)
564565
}
565566
});
566-
});
567567

568+
it('accounts.signTransaction returning valid v r s values', async function(){
569+
570+
const source = wallet[0].address;
571+
const destination = wallet[1].address;
572+
573+
const txCount = await web3.eth.getTransactionCount(source);
574+
const networkId = await web3.eth.net.getId();
575+
const chainId = await web3.eth.getChainId();
576+
577+
578+
const customCommon = {
579+
baseChain: 'mainnet',
580+
customChain: {
581+
name: 'custom-network',
582+
networkId: networkId,
583+
chainId: chainId,
584+
},
585+
hardfork: 'petersburg',
586+
};
587+
588+
const txObject = {
589+
nonce: web3.utils.toHex(txCount),
590+
to: destination,
591+
value: web3.utils.toHex(web3.utils.toWei('0.1', 'ether')),
592+
gasLimit: web3.utils.toHex(21000),
593+
gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei')),
594+
common: customCommon
595+
};
596+
597+
const signed = await web3.eth.accounts.signTransaction(txObject, wallet[0].privateKey);
598+
599+
const data = Buffer.from(signed.rawTransaction.slice(2), "hex")
600+
const tx = TransactionFactory.fromSerializedData(data);
601+
602+
assert(signed.v === ('0x' + tx.v.toString('hex')));
603+
assert(signed.r === ('0x' + tx.r.toString('hex')));
604+
assert(signed.s === ('0x' + tx.s.toString('hex')));
605+
});
606+
});

0 commit comments

Comments
 (0)