EIPs Explained

EIP-1965 · Stagnant · Core · 2019-04-20

EIP-1965: Method to check if a chainID is valid at a specific block Number

Proposed a precompile that answers whether a chain ID was valid at a given block number, so signed off-chain messages stay replay-protected across forks, including minority forks.

What problem does it solve?

EIP-155 put the chain ID into transactions so a transaction cannot be replayed on another chain. Smart contracts that verify off-chain signatures (for example EIP-712 messages in layer-2 schemes) need the same protection.

EIP-1344 gives contracts the current chain ID. The author argues that is not enough:

  • If a fork changes the chain ID, messages signed honestly before the fork suddenly look invalid.
  • Contracts would need their own cache of past chain IDs, which costs gas and can be wrong about exactly when a change happened.
  • If a minority forks away and picks a new chain ID while the majority does not bother to change, messages for the majority chain could still be replayed on the minority chain.

EIP-1959 proposed an opcode to check a chain ID against history but, by design, did not protect minority-led forks. EIP-1965 adds a block number to close that gap.

How it works

A new precompile takes two 32-byte inputs:

  1. the chain ID to test,
  2. the block number at which to test it,

and returns 0x1 if that chain ID was valid at that block, 0x0 otherwise. A chain ID is valid from when it was introduced up to the block where it was replaced.

Signers include both the chain ID and the block number at signing time in the message. After a fork that changes the chain ID at block X, any message claiming the old chain ID with a block number at or after X is rejected, while older messages remain valid.

Proposed cost: no more than G_blockhash + G_verylow, possibly to be revisited as the chain ID history grows.

Sample code

Pseudocode for how a verifier would use it (the spec does not assign a precompile address, so CHAINID_CHECK is a placeholder):

// Message signed off-chain includes (chainId, signedAtBlock)
function verify(bytes32 digest, uint256 chainId, uint256 signedAtBlock, bytes calldata sig) external view {
    require(signedAtBlock <= block.number, "future block");
    (bool ok, bytes memory out) = CHAINID_CHECK.staticcall(abi.encode(chainId, signedAtBlock));
    require(ok && abi.decode(out, (uint256)) == 1, "chainId not valid at that block");
    // ...then recover the signer from digest/sig as usual
}

Things to know

  • Status is Stagnant; no such precompile exists on mainnet.
  • Contracts today typically compare against block.chainid (EIP-1344) inside an EIP-712 domain separator, recomputing it if the chain ID changes.
  • The spec notes EIP-712 would need updating: chain ID and block number would have to move out of the fixed domain separator into the signed message itself, and wallets would have to set them correctly during fork transitions.

#chainid #replay-protection #precompile #forks #core

Details

Authors
Ronan Sandford (@wighawag)
Status
Stagnant
Type
Standards Track / Core
Created
2019-04-20
Requires

Machine-readable: Markdown · JSON