---
eip: 1344
label: EIP-1344
title: ChainID opcode
status: Final
type: Standards Track
category: Core
created: 2018-08-22
requires: 155
official: https://eips.ethereum.org/EIPS/eip-1344
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1344.md
canonical: https://eips.huatuo.xyz/eip-1344/
lang: en
tldr: 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.
tags: [opcode, evm, chain-id, signatures, istanbul]
---

# EIP-1344: ChainID opcode

> This EIP adds an opcode that returns the current chain's EIP-155 unique identifier.

**TL;DR:** 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](/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](/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 `CHAINID` at `0x46`. 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`):

```solidity
// 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):

```ts
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](/eip-2294/) proposes an explicit upper bound.
- Contract-based signature checks such as [ERC-1271](/eip-1271/) rely on including the chain ID in the signed data to get replay protection.


---

This is an unofficial plain-language summary. Read the full specification: https://eips.ethereum.org/EIPS/eip-1344
