EIP-1344 · Final · Core · 2018-08-22
EIP-1344: ChainID opcode
Added the CHAINID opcode (0x46) so contracts can read the current chain ID at runtime and bind signatures to one chain, preventing cross-chain replay.
What problem does it solve?
EIP-155 put a chain ID into transaction signatures so a transaction signed for one network cannot be replayed on another. Smart contracts that verify their own signatures (meta-transactions, permits, multisigs, L2 schemes using EIP-712) had no such protection: they could only hard-code the chain ID at deploy time.
A hard-coded value breaks if a chain splits and one side changes its ID, and it is an easy place for human error. If the same contract and signer exist on two chains, a signature produced for one could be replayed on the other.
EIP-1344 lets a contract ask the EVM which chain it is running on. It shipped in the Istanbul upgrade.
How it works
- New opcode
CHAINIDat0x46. It takes no stack inputs and pushes the chain ID as a 256-bit value. - Cost:
G_base(2 gas), the same tier as other simple environment opcodes. - The value comes from the client's chain configuration, i.e. the EIP-155 chain ID it accepts in transactions. It is returned even if the current transaction was signed without a chain ID.
In Solidity it is exposed as block.chainid (0.8.0 and later) or chainid() in inline assembly.
Sample code
Computing an EIP-712 domain separator at runtime, and recomputing it if the chain ID ever changes (the pattern used by OpenZeppelin's EIP712):
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract Domain {
bytes32 private constant TYPE_HASH = keccak256(
"EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"
);
bytes32 private immutable _cachedSeparator;
uint256 private immutable _cachedChainId;
constructor() {
_cachedChainId = block.chainid; // CHAINID opcode
_cachedSeparator = _build();
}
function domainSeparator() public view returns (bytes32) {
// After a contentious fork the chain ID may differ: rebuild instead of trusting the cache.
return block.chainid == _cachedChainId ? _cachedSeparator : _build();
}
function _build() private view returns (bytes32) {
return keccak256(abi.encode(
TYPE_HASH, keccak256("MyApp"), keccak256("1"), block.chainid, address(this)
));
}
}
Reading the same value off-chain (ethers v6):
const { chainId } = await provider.getNetwork(); // bigint, e.g. 1n on mainnet
Things to know
- The opcode returns whatever chain ID the node is configured with; it is not derived from the transaction.
- The spec explicitly warns that a chain ID can change after a contentious split. Long-lived signed messages should be designed so that such a change does not silently invalidate or replay them; caching the ID forever is the bug this EIP was meant to avoid.
- Because the value is 256-bit, tooling and RLP encoding may break on huge IDs. EIP-2294 proposes an explicit upper bound.
- Contract-based signature checks such as ERC-1271 rely on including the chain ID in the signed data to get replay protection.