EIPs Explained

EIP-155 · Final · Core · 2016-10-14

EIP-155: Simple replay attack protection

Mixes a chain ID into every transaction signature, so a transaction signed for one Ethereum network cannot be replayed on another.

What problem does it solve?

After the 2016 DAO fork, Ethereum and Ethereum Classic shared the same history, addresses and transaction format. A transaction signed for one chain was also valid on the other, so anyone could copy it across and "replay" it. Users who sent ETH on one chain could lose the matching ETC, and the reverse.

EIP-155 makes signatures chain-specific by including a chain ID in what gets signed. It went live in the Spurious Dragon hard fork (EIP-607) at block 2,675,000. Mainnet's chain ID is 1.

How it works

Before EIP-155, a legacy transaction was signed over six RLP-encoded fields:

(nonce, gasprice, startgas, to, value, data)

With EIP-155 the signer hashes nine fields instead:

(nonce, gasprice, startgas, to, value, data, chainid, 0, 0)

The recovery value v then encodes the chain ID too:

v = {0,1} + CHAIN_ID * 2 + 35      # protected
v = {0,1} + 27                     # old, unprotected

where {0,1} is the parity of the curve point's y-value. On mainnet, protected signatures have v = 37 or 38.

When a node sees v = CHAIN_ID * 2 + 35 or + 36, it rebuilds the nine-field hash to recover the sender. A signature made for chain 1 fails to recover the right sender on chain 61, so replay is impossible. Old v = 27/28 signatures stay valid under the old rules. The scheme is opt-in at the protocol level.

Sample code

The spec's worked example: nonce 9, gas price 20 gwei, gas 21000, sending 1 ETH to 0x3535…3535 on chain 1. The signing payload ends in 018080 (chainid 1, then two empty fields), and the resulting signature has v = 37.

Signing a legacy transaction with ethers v6. The chain ID goes into the signature automatically:

import { Wallet, Transaction, parseUnits, parseEther } from "ethers";

const wallet = new Wallet(process.env.PRIVATE_KEY!);

const signed = await wallet.signTransaction({
  type: 0,                     // legacy transaction
  chainId: 1,                  // EIP-155 chain id
  nonce: 9,
  gasPrice: parseUnits("20", "gwei"),
  gasLimit: 21000,
  to: "0x3535353535353535353535353535353535353535",
  value: parseEther("1"),
});

const tx = Transaction.from(signed);
console.log(tx.chainId, tx.signature!.networkV); // 1n, 37n or 38n

Things to know

  • Typed transactions (EIP-2718 onwards, including EIP-1559 type-2) carry chainId as an explicit field and use a plain yParity of 0 or 1. The v = chainId * 2 + 35 trick only applies to legacy transactions.
  • Unprotected (pre-155) transactions are still valid on-chain. Many node configurations refuse to accept them over RPC by default.
  • Contracts can read the current chain ID with the CHAINID opcode (EIP-1344), and wallets can query it with eth_chainId (EIP-695). Off-chain signatures use the same ID through the EIP-712 domain.
  • Chain IDs are not assigned by this EIP. Each network picks a unique one, tracked in the community ethereum-lists/chains registry.
  • This EIP is Final.

#transactions #signatures #chainid #security #core

Details

Authors
Vitalik Buterin (@vbuterin)
Status
Final
Type
Standards Track / Core
Created
2016-10-14
Required by

Machine-readable: Markdown · JSON