Skip to content

Commit

Permalink
Ensure r and s returned by signTransaction to have 64 character (
Browse files Browse the repository at this point in the history
…#6216)

* ensure `r` and `s` having 64 character

* modify tests

* update CHANGELOG.md
  • Loading branch information
Muhammad-Altabba authored Jun 24, 2023
1 parent e8d579c commit 3f49e18
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
4 changes: 4 additions & 0 deletions packages/web3-eth-accounts/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
4 changes: 2 additions & 2 deletions packages/web3-eth-accounts/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
};
Expand Down
6 changes: 3 additions & 3 deletions packages/web3-eth-accounts/test/unit/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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}/);
});
});

0 comments on commit 3f49e18

Please sign in to comment.