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

Commit 3f49e18

Browse files
Ensure r and s returned by signTransaction to have 64 character (#6216)
* ensure `r` and `s` having 64 character * modify tests * update CHANGELOG.md
1 parent e8d579c commit 3f49e18

File tree

4 files changed

+13
-9
lines changed

4 files changed

+13
-9
lines changed

packages/web3-eth-accounts/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,7 @@ Documentation:
107107
[Migration Guide from 1.x](https://docs.web3js.org/guides/web3_upgrade_guide/x/)
108108

109109
## [Unreleased]
110+
111+
### Fixed
112+
113+
- Fixed "The `r` and `s` returned by `signTransaction` to does not always consist of 64 characters #6207" (#6216)

packages/web3-eth-accounts/src/account.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,8 @@ export const signTransaction = async (
271271
return {
272272
messageHash: bytesToHex(signedTx.getMessageToSign(true)),
273273
v: `0x${signedTx.v.toString(16)}`,
274-
r: `0x${signedTx.r.toString(16)}`,
275-
s: `0x${signedTx.s.toString(16)}`,
274+
r: `0x${signedTx.r.toString(16).padStart(64, '0')}`,
275+
s: `0x${signedTx.s.toString(16).padStart(64, '0')}`,
276276
rawTransaction: rawTx,
277277
transactionHash: bytesToHex(txHash),
278278
};

packages/web3-eth-accounts/test/unit/account.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@ describe('accounts', () => {
104104
expect(signedResult.messageHash).toBeDefined();
105105
expect(signedResult.rawTransaction).toBeDefined();
106106
expect(signedResult.transactionHash).toBeDefined();
107-
expect(signedResult.r).toBeDefined();
108-
expect(signedResult.s).toBeDefined();
109-
expect(signedResult.v).toBeDefined();
107+
expect(signedResult.r).toMatch(/0[xX][0-9a-fA-F]{64}/);
108+
expect(signedResult.s).toMatch(/0[xX][0-9a-fA-F]{64}/);
109+
expect(signedResult.v).toMatch(/0[xX][0-9a-fA-F]+/);
110110
});
111111

112112
it.each(transactionsTestData)('Recover transaction', async txData => {

packages/web3-eth/test/integration/web3_eth/sign_transaction.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ describe('Web3Eth.signTransaction', () => {
6161
// Pulling out of toMatchObject to be compatiable with Cypress
6262
expect(response.raw).toMatch(/0[xX][0-9a-fA-F]+/);
6363
expect(typeof (response.tx as TransactionLegacySignedAPI).v).toBe('bigint');
64-
expect(response.tx.r).toMatch(/0[xX][0-9a-fA-F]+/);
65-
expect(response.tx.s).toMatch(/0[xX][0-9a-fA-F]+/);
64+
expect(response.tx.r).toMatch(/0[xX][0-9a-fA-F]{64}/);
65+
expect(response.tx.s).toMatch(/0[xX][0-9a-fA-F]{64}/);
6666
});
6767

6868
it('should sign a contract deployment', async () => {
@@ -90,7 +90,7 @@ describe('Web3Eth.signTransaction', () => {
9090
// Pulling out of toMatchObject to be compatiable with Cypress
9191
expect(response.raw).toMatch(/0[xX][0-9a-fA-F]+/);
9292
expect(typeof (response.tx as TransactionLegacySignedAPI).v).toBe('bigint');
93-
expect(response.tx.r).toMatch(/0[xX][0-9a-fA-F]+/);
94-
expect(response.tx.s).toMatch(/0[xX][0-9a-fA-F]+/);
93+
expect(response.tx.r).toMatch(/0[xX][0-9a-fA-F]{64}/);
94+
expect(response.tx.s).toMatch(/0[xX][0-9a-fA-F]{64}/);
9595
});
9696
});

0 commit comments

Comments
 (0)