MetaMask / MetaMask/metamask-extension
MAX_SAFE_CHAIN_ID no longer needed
- Dominant language
- TypeScript
- Stars
- 13.2k
- Forks
- 5.6k
- Avg merge
- 2d 5h
- Merged PRs (30d)
- 451
Description
### Describe the bug
# Problem
MetaMask rejects chain ids bigger than `4503599627370476`. It is a problem for networks that want to use a bigger chain id. In order to use them with MetaMask some mapping would have to be added.
# Source of the problem
https://github.com/MetaMask/metamask-extension/pull/10224 introduced `MAX_SAFE_CHAIN_ID` for reasons described in https://gist.github.com/rekmarks/a47bd5f2525936c4b8eee31a16345553. At that time libraries used by MetaMask used basic js number for computations, without the support of big integers.
Two libraries mentioned:
- [ethereumjs-tx@1.3.7](https://github.com/ethereumjs/ethereumjs-tx/blob/e3fc21467ecb997090f63f154b1407094f173bf2/index.js#L120-L127) contained
```
const isValidEIP155V =
vInt === this.getChainId() * 2 + 35 || vInt === this.getChainId() * 2 + 36
```
- [ethereumjs-util@7.0.5](https://github.com/ethereumjs/ethereumjs-util/blob/c5dca47d1ec983edd3b00b559088f9b56e16bc01/src/signature.ts#L16-28) contained
```
v: chainId ? recovery + (chainId * 2 + 35) : recovery + 27,
```
# Solution
Since then those libraries were updated and they accept big integers:
- [@ethereumjs/tx@3.2.1 used by MetaMask's develop branch currently](https://github.com/ethereumjs/ethereumjs-monorepo/blob/%40ethereumjs/tx%403.2.1/packages/tx/src/legacyTransaction.ts#L317)
- [ethereumjs-util@7.0.10 used by MetaMask's develop branch currently](https://github.com/ethereumjs/ethereumjs-monorepo/blob/ethereumjs-util%407.0.10/packages/util/src/signature.ts#L25)
We could use changes in these libraries and limit chain id to fit in uint256, so it has to be lower than `2**256`.
I can try to create a PR with required changes if MetaMask maintainers approve suggested solution.
### Steps to reproduce
1. Go to https://metamask.github.io/api-playground/api-documentation/#wallet_addEthereumChain and click "TRY IT NOW" for the example request.
2. Replace chainId with `0xfffffffffffed` (MAX_SAFE_CHAIN_ID+1).
3. Send the request.
### Error messages or log output
```shell
{
"jsonrpc": "2.0",
"id": 0,
"error": {
"code": 32329,
"message": "Invalid chain ID \"0xfffffffffffed\": numerical value greater than max safe value. Received:\n0xfffffffffffed"
}
}
```
### Version
10.9.3
### Build type
_No response_
### Browser
Chrome
### Operating system
MacOS
### Hardware wallet
_No response_
### Additional context
_No response_
Contributor guide
Research direction
Start at the wallet_addEthereumChain API entry point and trace the MAX_SAFE_CHAIN_ID validation. Review the updated @ethereumjs/tx and ethereumjs-util versions described in the issue, then verify that a chain ID above the current limit is accepted while remaining below the uint256 maximum. Done means the reproduced request no longer returns the numerical-value error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- blockchain
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100