Skip to content

Commit

Permalink
signTransaction fix (#4295)
Browse files Browse the repository at this point in the history
* fixed v r s

* unit test signTransaction

* change log

* Fix hardfork typo

Co-authored-by: Wyatt Barnes <[email protected]>
  • Loading branch information
jdevcs and spacesailor24 authored Sep 10, 2021
1 parent c70722b commit 3f5cb38
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -447,3 +447,4 @@ Released with 1.0.0-beta.37 code base.
- lerna from 3.22.1 to 4.0.0 (#4231)
- Dropped build tests in CI for Node v8 and v10, and added support for Node v14
- Change default value for `maxPriorityFeePerGas` from `1 Gwei` to `2.5 Gwei` (#4284)
- Fixed bug in signTransaction (#4295)
6 changes: 3 additions & 3 deletions packages/web3-eth-accounts/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,9 @@ Accounts.prototype.signTransaction = function signTransaction(tx, privateKey, ca

var result = {
messageHash: '0x' + Buffer.from(signedTx.getMessageToSign(true)).toString('hex'),
v: '0x' + Buffer.from(signedTx.v).toString('hex'),
r: '0x' + Buffer.from(signedTx.r).toString('hex'),
s: '0x' + Buffer.from(signedTx.s).toString('hex'),
v: '0x' + signedTx.v.toString('hex'),
r: '0x' + signedTx.r.toString('hex'),
s: '0x' + signedTx.s.toString('hex'),
rawTransaction: rawTransaction,
transactionHash: transactionHash
};
Expand Down
41 changes: 40 additions & 1 deletion test/e2e.method.signing.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var assert = require('assert');
var Basic = require('./sources/Basic');
var utils = require('./helpers/test.utils');
var Web3 = utils.getWeb3();
var {TransactionFactory} = require('@ethereumjs/tx');

describe('transaction and message signing [ @E2E ]', function() {
let web3;
Expand Down Expand Up @@ -563,5 +564,43 @@ describe('transaction and message signing [ @E2E ]', function() {
done(error)
}
});
});

it('accounts.signTransaction returning valid v r s values', async function(){

const source = wallet[0].address;
const destination = wallet[1].address;

const txCount = await web3.eth.getTransactionCount(source);
const networkId = await web3.eth.net.getId();
const chainId = await web3.eth.getChainId();


const customCommon = {
baseChain: 'mainnet',
customChain: {
name: 'custom-network',
networkId: networkId,
chainId: chainId,
},
hardfork: 'petersburg',
};

const txObject = {
nonce: web3.utils.toHex(txCount),
to: destination,
value: web3.utils.toHex(web3.utils.toWei('0.1', 'ether')),
gasLimit: web3.utils.toHex(21000),
gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei')),
common: customCommon
};

const signed = await web3.eth.accounts.signTransaction(txObject, wallet[0].privateKey);

const data = Buffer.from(signed.rawTransaction.slice(2), "hex")
const tx = TransactionFactory.fromSerializedData(data);

assert(signed.v === ('0x' + tx.v.toString('hex')));
assert(signed.r === ('0x' + tx.r.toString('hex')));
assert(signed.s === ('0x' + tx.s.toString('hex')));
});
});

0 comments on commit 3f5cb38

Please sign in to comment.