diff --git a/packages/web3-eth-accounts/CHANGELOG.md b/packages/web3-eth-accounts/CHANGELOG.md index d9571feba9b..90a5cc590d4 100644 --- a/packages/web3-eth-accounts/CHANGELOG.md +++ b/packages/web3-eth-accounts/CHANGELOG.md @@ -107,3 +107,7 @@ Documentation: [Migration Guide from 1.x](https://docs.web3js.org/guides/web3_upgrade_guide/x/) ## [Unreleased] + +### Fixed + +- Fixed "The `r` and `s` returned by `signTransaction` to does not always consist of 64 characters #6207" (#6216) diff --git a/packages/web3-eth-accounts/src/account.ts b/packages/web3-eth-accounts/src/account.ts index 7925033b736..64e18911901 100644 --- a/packages/web3-eth-accounts/src/account.ts +++ b/packages/web3-eth-accounts/src/account.ts @@ -271,8 +271,8 @@ export const signTransaction = async ( return { messageHash: bytesToHex(signedTx.getMessageToSign(true)), v: `0x${signedTx.v.toString(16)}`, - r: `0x${signedTx.r.toString(16)}`, - s: `0x${signedTx.s.toString(16)}`, + r: `0x${signedTx.r.toString(16).padStart(64, '0')}`, + s: `0x${signedTx.s.toString(16).padStart(64, '0')}`, rawTransaction: rawTx, transactionHash: bytesToHex(txHash), }; diff --git a/packages/web3-eth-accounts/test/unit/account.test.ts b/packages/web3-eth-accounts/test/unit/account.test.ts index 3bccc516b71..9b493fea298 100644 --- a/packages/web3-eth-accounts/test/unit/account.test.ts +++ b/packages/web3-eth-accounts/test/unit/account.test.ts @@ -104,9 +104,9 @@ describe('accounts', () => { expect(signedResult.messageHash).toBeDefined(); expect(signedResult.rawTransaction).toBeDefined(); expect(signedResult.transactionHash).toBeDefined(); - expect(signedResult.r).toBeDefined(); - expect(signedResult.s).toBeDefined(); - expect(signedResult.v).toBeDefined(); + expect(signedResult.r).toMatch(/0[xX][0-9a-fA-F]{64}/); + expect(signedResult.s).toMatch(/0[xX][0-9a-fA-F]{64}/); + expect(signedResult.v).toMatch(/0[xX][0-9a-fA-F]+/); }); it.each(transactionsTestData)('Recover transaction', async txData => { diff --git a/packages/web3-eth/test/integration/web3_eth/sign_transaction.test.ts b/packages/web3-eth/test/integration/web3_eth/sign_transaction.test.ts index 27be403c7ae..cf1f7452f77 100644 --- a/packages/web3-eth/test/integration/web3_eth/sign_transaction.test.ts +++ b/packages/web3-eth/test/integration/web3_eth/sign_transaction.test.ts @@ -61,8 +61,8 @@ describe('Web3Eth.signTransaction', () => { // Pulling out of toMatchObject to be compatiable with Cypress expect(response.raw).toMatch(/0[xX][0-9a-fA-F]+/); expect(typeof (response.tx as TransactionLegacySignedAPI).v).toBe('bigint'); - expect(response.tx.r).toMatch(/0[xX][0-9a-fA-F]+/); - expect(response.tx.s).toMatch(/0[xX][0-9a-fA-F]+/); + expect(response.tx.r).toMatch(/0[xX][0-9a-fA-F]{64}/); + expect(response.tx.s).toMatch(/0[xX][0-9a-fA-F]{64}/); }); it('should sign a contract deployment', async () => { @@ -90,7 +90,7 @@ describe('Web3Eth.signTransaction', () => { // Pulling out of toMatchObject to be compatiable with Cypress expect(response.raw).toMatch(/0[xX][0-9a-fA-F]+/); expect(typeof (response.tx as TransactionLegacySignedAPI).v).toBe('bigint'); - expect(response.tx.r).toMatch(/0[xX][0-9a-fA-F]+/); - expect(response.tx.s).toMatch(/0[xX][0-9a-fA-F]+/); + expect(response.tx.r).toMatch(/0[xX][0-9a-fA-F]{64}/); + expect(response.tx.s).toMatch(/0[xX][0-9a-fA-F]{64}/); }); });