From 3f49e18d24af3dffe2ed47bf65dcf97c0204fc87 Mon Sep 17 00:00:00 2001 From: Muhammad Altabba <24407834+Muhammad-Altabba@users.noreply.github.com> Date: Sat, 24 Jun 2023 02:58:51 +0200 Subject: [PATCH] Ensure `r` and `s` returned by `signTransaction` to have 64 character (#6216) * ensure `r` and `s` having 64 character * modify tests * update CHANGELOG.md --- packages/web3-eth-accounts/CHANGELOG.md | 4 ++++ packages/web3-eth-accounts/src/account.ts | 4 ++-- packages/web3-eth-accounts/test/unit/account.test.ts | 6 +++--- .../test/integration/web3_eth/sign_transaction.test.ts | 8 ++++---- 4 files changed, 13 insertions(+), 9 deletions(-) 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}/); }); });