diff --git a/CHANGELOG.md b/CHANGELOG.md index 21d9feb6be1..0e67e901b3a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) \ No newline at end of file diff --git a/packages/web3-eth-accounts/src/index.js b/packages/web3-eth-accounts/src/index.js index 8f997a5c68b..f3fbf7ad5da 100644 --- a/packages/web3-eth-accounts/src/index.js +++ b/packages/web3-eth-accounts/src/index.js @@ -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 }; diff --git a/test/e2e.method.signing.js b/test/e2e.method.signing.js index 757cedf4dc5..bfc2e838c1d 100644 --- a/test/e2e.method.signing.js +++ b/test/e2e.method.signing.js @@ -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; @@ -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'))); + }); +}); \ No newline at end of file