Introduction

Hi, I'm cardene, a software engineer and technical writer focused on blockchain and AI.

I've also authored Japanese technical books on specification-driven development with LLMs and decentralized application (DApp) development.

I also build and publish a variety of tools, libraries, and open-source projects.

This article examines EIP-1344, which proposes the CHAINID opcode for retrieving the unique chain identifier defined by EIP-155.

This article translates, summarizes, and supplements the material linked below.

I have also covered other EIPs.

Overview

This Ethereum Improvement Proposal adds an opcode that returns the chain identifier defined by EIP-155. EIP-155 defines a way to bind transaction signatures to a particular chain to prevent replay attacks between chains with different identifiers.

The proposal enables smart contracts to determine which chain they are executing on. This is particularly useful when the same contract operates on different Ethereum networks, such as mainnet and testnets. Knowing the chain ID allows a contract to implement network-specific logic through conditional branches, enabling more flexible and secure designs.

Direct access to the chain ID simplifies the implementation of chain-specific behavior and improves interoperability across the Ethereum ecosystem.

See the following article for EIP-155.

[EIP-155] Preventing Replay Attacks Across Ethereum Networks

Motivation

EIP-155 introduces chain IDs to prevent replay attacks across different chains. Having equivalent functionality inside a contract is particularly valuable for signature verification, including Layer 2 signature schemes using EIP-712.

In this context, a replay attack occurs when a transaction valid on one chain is reused without authorization on another. EIP-155 protects chain-bound transactions by incorporating the chain ID into the signature scheme.

A contract can similarly use the current chain ID as part of its signature domain to verify that a signature was intended for that chain. This helps prevent cross-chain signature reuse when chains have distinct identifiers, particularly in Layer 2 solutions using EIP-712. EIP-712 standardizes signing typed structured data, making the signing process more transparent for users.

Access to the chain ID inside contracts improves security and the reliability of chain-specific transactions. This becomes increasingly important as the Ethereum ecosystem expands across multiple chains and Layer 2 networks.

See the following article for EIP-712.

[EIP-712] Hashing and Signing Typed Structured Data

Specification

The new CHAINID opcode is assigned opcode value 0x46. It takes no stack arguments and pushes the current chain ID onto the stack as a 256-bit value. Its gas cost is G_base.

The opcode reads the chain ID from the chain configuration, matching the identifier defined by EIP-155. Because EIP-155 uses distinct chain IDs to prevent cross-chain transaction replay, this opcode gives smart contracts direct access to the identifier of their execution environment.

Importantly, CHAINID returns the configured identifier even when the executing transaction does not include an EIP-155 chain ID. This ensures contracts can access the current chain ID regardless of the transaction's signature format.

Additional Notes

At the time of the proposal, EIP-712 suggested specifying the chain ID at compile time. This could lead to loss of funds or signature replay following a hard fork or human error. Introducing CHAINID enables signature verification based on the current chain ID instead.

There was no clearly defined procedure for assigning chain IDs to particular networks; the choice depended on client implementers and chain communities. If a community splits into two chains, maintaining the same ID on both is unsafe from a replay-protection perspective.

Application developers therefore need to consider how to handle chain ID updates. A trusted oracle that records changes to chain IDs is one possible mechanism.

However, a global oracle is not appropriate for every scenario. In a Plasma Layer 2 design, for example, an operator submits blocks of Layer 2 transactions to the base chain. These submissions may not align exactly with important base-chain events such as changes to the chain ID. If the operator cannot control how the protocol responds to chain ID updates, significant security risks may arise.

Over-specifying chain ID behavior during a split may have unintended consequences. A simple opcode for accessing the current identifier is therefore preferable.

The proposed CHAINID opcode provides the simplest implementation of this functionality and gives developers flexibility to handle changes globally or locally, according to the needs of individual networks and applications.

Compatibility

This EIP is fully compatible with chains implementing the EIP-155 chain ID domain separator for transaction signatures.

Implementation

The reference implementation for the Trinity Python client is available here.

An example implementation of a trustless chain ID oracle is available here.

Citation

Richard Meissner (@rmeissner), Bryant Eisenbach (@fubuloubu), "EIP-1344: ChainID opcode," Ethereum Improvement Proposals, no. 1344, August 2018. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-1344.

Conclusion

EIP-1344 introduces the CHAINID opcode, allowing contracts to access the chain identifier defined by EIP-155 directly.

If you have questions, feel free to contact me on Twitter.

Twitter @cardene777

I also publish articles on the following platforms.

https://chaldene.net/