diff --git a/packages/eth-json-rpc-middleware/CHANGELOG.md b/packages/eth-json-rpc-middleware/CHANGELOG.md index f010b3086f..1995befb4d 100644 --- a/packages/eth-json-rpc-middleware/CHANGELOG.md +++ b/packages/eth-json-rpc-middleware/CHANGELOG.md @@ -11,6 +11,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Bump `@metamask/superstruct` from `^3.1.0` to `^3.4.1` ([#9754](https://github.com/MetaMask/core/pull/9754)) +### Fixed + +- Accept a numeric `chainId` in `eth_sendTransaction` and `eth_signTransaction` params ([#9965](https://github.com/MetaMask/core/pull/9965)) + - `chainId` is now validated as a quantity (hex string or number), matching `gas`, `nonce`, `value`, and the other numerical fields + - Previously it was restricted to a string, so dapps sending a number were rejected with `-32602 Invalid params` + ## [24.0.0] ### Changed diff --git a/packages/eth-json-rpc-middleware/src/utils/validation.test.ts b/packages/eth-json-rpc-middleware/src/utils/validation.test.ts index 2d4e343a4e..b3aa4d8b08 100644 --- a/packages/eth-json-rpc-middleware/src/utils/validation.test.ts +++ b/packages/eth-json-rpc-middleware/src/utils/validation.test.ts @@ -333,6 +333,37 @@ describe('Validation Utils', () => { ).not.toThrow(); }); + it('does not throw when `chainId` is a number', () => { + expect(() => + validateTransactionParams({ + from: VALID_FROM, + to: VALID_TO, + chainId: 1, + }), + ).not.toThrow(); + }); + + it('does not throw when `chainId` is a hex string', () => { + expect(() => + validateTransactionParams({ + from: VALID_FROM, + to: VALID_TO, + chainId: '0x1', + }), + ).not.toThrow(); + }); + + it('throws for an extraneous top-level key alongside a numeric `chainId`', () => { + expect(() => + validateTransactionParams({ + from: VALID_FROM, + to: VALID_TO, + chainId: 1, + extraKey: 'unexpected', + }), + ).toThrow(/Invalid params/u); + }); + it.each([ ['null', null], ['undefined', undefined], diff --git a/packages/eth-json-rpc-middleware/src/utils/validation.ts b/packages/eth-json-rpc-middleware/src/utils/validation.ts index 4c68d0ed80..81662ac3ad 100644 --- a/packages/eth-json-rpc-middleware/src/utils/validation.ts +++ b/packages/eth-json-rpc-middleware/src/utils/validation.ts @@ -263,7 +263,7 @@ export const TransactionParamsStruct = object({ }), ), ), - chainId: optional(string()), + chainId: optional(QuantityStruct), data: optional(string()), from: string(), gas: optional(QuantityStruct),