{"eip":1965,"title":"Method to check if a chainID is valid at a specific block Number","description":"This EIP adds a precompile that returns whether a specific chainID (EIP-155 unique identifier) is valid at a specific blockNumber. ChainID are assumed to be valid up to the blockNumber at which they get replaced by a new chainID.","authors":["Ronan Sandford (@wighawag)"],"status":"Stagnant","type":"Standards Track","category":"Core","created":"2019-04-20","requires":[155],"discussionsTo":"https://ethereum-magicians.org/t/eip-1965-valid-chainid-for-specific-blocknumber-protect-all-forks/3181","repo":"EIPs","label":"EIP-1965","officialUrl":"https://eips.ethereum.org/EIPS/eip-1965","sourceUrl":"https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1965.md","url":"https://eips.huatuo.xyz/eip-1965/","tldr":{"en":"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.","ja":"あるチェーン ID が特定のブロック番号の時点で有効だったかを答えるプリコンパイルの提案。署名済みのオフチェーンメッセージを、少数派フォークを含むフォークをまたいでもリプレイ攻撃から守る。","ko":"체인 ID가 특정 블록 번호 시점에 유효했는지 알려 주는 프리컴파일을 제안했다. 서명된 오프체인 메시지가 소수파 포크를 포함한 포크 이후에도 리플레이 공격으로부터 보호되도록 한다.","zh":"提议新增一个预编译合约，用来回答某个链 ID 在给定区块号时是否有效，使链下签名消息在分叉（包括少数派分叉）之后依然能防重放。"},"tags":["chainid","replay-protection","precompile","forks","core"],"explainers":{"en":{"lang":"en","tldr":"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.","tags":["chainid","replay-protection","precompile","forks","core"],"related":[155,1344,1959,712],"updated":"2026-10-19","url":"https://eips.huatuo.xyz/eip-1965/","markdown":"## What problem does it solve?\n\n[EIP-155](/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](/eip-712/) messages in layer-2 schemes) need the same protection.\n\n[EIP-1344](/eip-1344/) gives contracts the *current* chain ID. The author argues that is not enough:\n\n- If a fork changes the chain ID, messages signed honestly before the fork suddenly look invalid.\n- Contracts would need their own cache of past chain IDs, which costs gas and can be wrong about exactly when a change happened.\n- 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.\n\n[EIP-1959](/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.\n\n## How it works\n\nA new precompile takes two 32-byte inputs:\n\n1. the chain ID to test,\n2. the block number at which to test it,\n\nand 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.\n\nSigners 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.\n\nProposed cost: no more than `G_blockhash + G_verylow`, possibly to be revisited as the chain ID history grows.\n\n## Sample code\n\nPseudocode for how a verifier would use it (the spec does not assign a precompile address, so `CHAINID_CHECK` is a placeholder):\n\n```solidity\n// Message signed off-chain includes (chainId, signedAtBlock)\nfunction verify(bytes32 digest, uint256 chainId, uint256 signedAtBlock, bytes calldata sig) external view {\n    require(signedAtBlock \u003c= block.number, \"future block\");\n    (bool ok, bytes memory out) = CHAINID_CHECK.staticcall(abi.encode(chainId, signedAtBlock));\n    require(ok \u0026\u0026 abi.decode(out, (uint256)) == 1, \"chainId not valid at that block\");\n    // ...then recover the signer from digest/sig as usual\n}\n```\n\n## Things to know\n\n- Status is **Stagnant**; no such precompile exists on mainnet.\n- Contracts today typically compare against `block.chainid` ([EIP-1344](/eip-1344/)) inside an EIP-712 domain separator, recomputing it if the chain ID changes.\n- 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.\n"},"ja":{"lang":"ja","tldr":"あるチェーン ID が特定のブロック番号の時点で有効だったかを答えるプリコンパイルの提案。署名済みのオフチェーンメッセージを、少数派フォークを含むフォークをまたいでもリプレイ攻撃から守る。","tags":["chainid","replay-protection","precompile","forks","core"],"related":[155,1344,1959,712],"updated":"2026-10-19","url":"https://eips.huatuo.xyz/ja/eip-1965/","markdown":"## 何を解決するのか\n\n[EIP-155](/eip-155/) はトランザクションにチェーン ID を含め、別のチェーンでリプレイされないようにしました。*オフチェーン*署名を検証するスマートコントラクト（たとえばレイヤー 2 の仕組みにおける [EIP-712](/eip-712/) メッセージ）にも同じ保護が必要です。\n\n[EIP-1344](/eip-1344/) はコントラクトに*現在の*チェーン ID を提供します。著者はそれだけでは不十分だと主張しています。\n\n- フォークでチェーン ID が変わると、フォーク前に正しく署名されたメッセージが突然無効に見えてしまいます。\n- コントラクトが過去のチェーン ID を独自にキャッシュする必要があり、ガスがかかるうえ、変更がいつ起きたかを正確に把握できない可能性があります。\n- *少数派*がフォークして新しいチェーン ID を選んでも、多数派がチェーン ID を変更しなければ、多数派チェーン向けのメッセージが少数派チェーン上でリプレイされる可能性があります。\n\n[EIP-1959](/eip-1959/) はチェーン ID を履歴と照合するオペコードを提案しましたが、設計上、少数派主導のフォークは保護しませんでした。EIP-1965 はブロック番号を加えてこの隙間を埋めます。\n\n## 仕組み\n\n新しいプリコンパイルは 32 バイトの入力を 2 つ受け取ります。\n\n1. 検証するチェーン ID\n2. 検証するブロック番号\n\nそして、そのチェーン ID がそのブロックで有効だった場合は `0x1`、そうでなければ `0x0` を返します。チェーン ID は、導入された時点から置き換えられたブロックまで有効です。\n\n署名者は、署名時点のチェーン ID とブロック番号の両方をメッセージに含めます。ブロック X でチェーン ID が変わるフォークの後は、古いチェーン ID を名乗りブロック番号が X 以降のメッセージはすべて拒否され、それより前のメッセージは有効なままです。\n\n提案されたコストは `G_blockhash + G_verylow` 以下で、チェーン ID の履歴が増えるにつれて見直される可能性があります。\n\n## サンプルコード\n\n検証側がこれをどう使うかの擬似コードです（仕様ではプリコンパイルのアドレスが割り当てられていないため、`CHAINID_CHECK` はプレースホルダーです）。\n\n```solidity\n// Message signed off-chain includes (chainId, signedAtBlock)\nfunction verify(bytes32 digest, uint256 chainId, uint256 signedAtBlock, bytes calldata sig) external view {\n    require(signedAtBlock \u003c= block.number, \"future block\");\n    (bool ok, bytes memory out) = CHAINID_CHECK.staticcall(abi.encode(chainId, signedAtBlock));\n    require(ok \u0026\u0026 abi.decode(out, (uint256)) == 1, \"chainId not valid at that block\");\n    // ...then recover the signer from digest/sig as usual\n}\n```\n\n## 知っておくべきこと\n\n- ステータスは **Stagnant** で、このようなプリコンパイルはメインネットに存在しません。\n- 現在のコントラクトは通常、EIP-712 のドメインセパレーター内で `block.chainid`（[EIP-1344](/eip-1344/)）と比較し、チェーン ID が変わった場合は再計算します。\n- 仕様では、EIP-712 の更新が必要になると述べています。チェーン ID とブロック番号は固定のドメインセパレーターから署名対象のメッセージ自体へ移す必要があり、ウォレットはフォークの移行期間中にそれらを正しく設定しなければなりません。\n"},"ko":{"lang":"ko","tldr":"체인 ID가 특정 블록 번호 시점에 유효했는지 알려 주는 프리컴파일을 제안했다. 서명된 오프체인 메시지가 소수파 포크를 포함한 포크 이후에도 리플레이 공격으로부터 보호되도록 한다.","tags":["chainid","replay-protection","precompile","forks","core"],"related":[155,1344,1959,712],"updated":"2026-10-19","url":"https://eips.huatuo.xyz/ko/eip-1965/","markdown":"## 어떤 문제를 해결하나요?\n\n[EIP-155](/eip-155/)는 트랜잭션에 체인 ID를 넣어 다른 체인에서 재사용(리플레이)할 수 없게 했습니다. *오프체인* 서명을 검증하는 스마트 컨트랙트(예: 레이어 2 방식의 [EIP-712](/eip-712/) 메시지)에도 같은 보호가 필요합니다.\n\n[EIP-1344](/eip-1344/)는 컨트랙트에 *현재* 체인 ID를 알려 줍니다. 작성자는 이것만으로는 부족하다고 주장합니다.\n\n- 포크로 체인 ID가 바뀌면, 포크 전에 정직하게 서명된 메시지가 갑자기 유효하지 않은 것처럼 보입니다.\n- 컨트랙트가 과거 체인 ID를 직접 캐시해야 하는데, 가스가 들고 변경이 정확히 언제 일어났는지 틀릴 수도 있습니다.\n- *소수파*가 포크해 나가 새 체인 ID를 고르는데 다수파가 체인 ID를 바꾸지 않으면, 다수파 체인용 메시지가 소수파 체인에서 리플레이될 수 있습니다.\n\n[EIP-1959](/eip-1959/)는 체인 ID를 이력과 대조하는 opcode를 제안했지만, 설계상 소수파가 주도하는 포크는 보호하지 않았습니다. EIP-1965는 블록 번호를 추가해 이 틈을 메웁니다.\n\n## 어떻게 동작하나요?\n\n새 프리컴파일은 32바이트 입력 두 개를 받습니다.\n\n1. 검사할 체인 ID,\n2. 검사할 블록 번호,\n\n그리고 해당 체인 ID가 그 블록에서 유효했다면 `0x1`, 아니면 `0x0`을 반환합니다. 체인 ID는 도입된 시점부터 교체된 블록까지 유효합니다.\n\n서명자는 서명 시점의 체인 ID와 블록 번호를 모두 메시지에 포함합니다. 블록 X에서 체인 ID를 바꾸는 포크가 일어난 뒤에는, 이전 체인 ID를 주장하면서 블록 번호가 X 이상인 메시지는 모두 거부되고, 그보다 오래된 메시지는 계속 유효합니다.\n\n제안된 비용: `G_blockhash + G_verylow` 이하이며, 체인 ID 이력이 늘어남에 따라 다시 검토될 수 있습니다.\n\n## 샘플 코드\n\n검증자가 이를 사용하는 방식의 의사 코드입니다(사양은 프리컴파일 주소를 지정하지 않으므로 `CHAINID_CHECK`는 자리표시자입니다).\n\n```solidity\n// Message signed off-chain includes (chainId, signedAtBlock)\nfunction verify(bytes32 digest, uint256 chainId, uint256 signedAtBlock, bytes calldata sig) external view {\n    require(signedAtBlock \u003c= block.number, \"future block\");\n    (bool ok, bytes memory out) = CHAINID_CHECK.staticcall(abi.encode(chainId, signedAtBlock));\n    require(ok \u0026\u0026 abi.decode(out, (uint256)) == 1, \"chainId not valid at that block\");\n    // ...then recover the signer from digest/sig as usual\n}\n```\n\n## 알아둘 점\n\n- 상태는 **Stagnant**이며, 이런 프리컴파일은 메인넷에 존재하지 않습니다.\n- 오늘날 컨트랙트는 보통 EIP-712 도메인 구분자 안에서 `block.chainid`([EIP-1344](/eip-1344/))와 비교하고, 체인 ID가 바뀌면 다시 계산합니다.\n- 사양은 EIP-712도 업데이트가 필요하다고 언급합니다. 체인 ID와 블록 번호를 고정된 도메인 구분자에서 서명 대상 메시지 자체로 옮겨야 하며, 지갑은 포크 전환 기간 동안 이를 올바르게 설정해야 합니다.\n"},"zh":{"lang":"zh","tldr":"提议新增一个预编译合约，用来回答某个链 ID 在给定区块号时是否有效，使链下签名消息在分叉（包括少数派分叉）之后依然能防重放。","tags":["chainid","replay-protection","precompile","forks","core"],"related":[155,1344,1959,712],"updated":"2026-10-19","url":"https://eips.huatuo.xyz/zh/eip-1965/","markdown":"## 它解决什么问题？\n\n[EIP-155](/eip-155/) 把链 ID 放进交易，使交易无法在另一条链上重放。验证*链下*签名的智能合约（例如二层方案中的 [EIP-712](/eip-712/) 消息）也需要同样的保护。\n\n[EIP-1344](/eip-1344/) 为合约提供*当前*链 ID。作者认为这还不够：\n\n- 如果分叉改变了链 ID，分叉前诚实签名的消息会突然看起来无效。\n- 合约需要自己缓存过去的链 ID，这既消耗 gas，也可能搞错变更发生的确切时间。\n- 如果*少数派*分叉出去并选了新的链 ID，而多数派没有费心更改，那么针对多数派链的消息仍可能在少数派链上被重放。\n\n[EIP-1959](/eip-1959/) 提议了一个对照历史检查链 ID 的 opcode，但按设计并不保护由少数派主导的分叉。EIP-1965 加入区块号来弥补这个缺口。\n\n## 它如何运作\n\n新的预编译合约接收两个 32 字节的输入：\n\n1. 要检验的链 ID，\n2. 检验所用的区块号，\n\n如果该链 ID 在该区块有效则返回 `0x1`，否则返回 `0x0`。链 ID 从引入时起到被替换的区块为止有效。\n\n签名者在消息中同时包含签名时的链 ID 和区块号。在区块 X 处改变链 ID 的分叉发生后，任何声称使用旧链 ID 且区块号大于或等于 X 的消息都会被拒绝，而更早的消息仍然有效。\n\n提议的成本：不超过 `G_blockhash + G_verylow`，随着链 ID 历史增长可能需要重新审视。\n\n## 示例代码\n\n验证者如何使用它的伪代码（规范没有分配预编译地址，因此 `CHAINID_CHECK` 只是占位符）：\n\n```solidity\n// Message signed off-chain includes (chainId, signedAtBlock)\nfunction verify(bytes32 digest, uint256 chainId, uint256 signedAtBlock, bytes calldata sig) external view {\n    require(signedAtBlock \u003c= block.number, \"future block\");\n    (bool ok, bytes memory out) = CHAINID_CHECK.staticcall(abi.encode(chainId, signedAtBlock));\n    require(ok \u0026\u0026 abi.decode(out, (uint256)) == 1, \"chainId not valid at that block\");\n    // ...then recover the signer from digest/sig as usual\n}\n```\n\n## 需要注意\n\n- 状态为 **Stagnant**；主网上不存在这样的预编译合约。\n- 如今合约通常在 EIP-712 域分隔符中与 `block.chainid`（[EIP-1344](/eip-1344/)）比较，链 ID 变化时重新计算。\n- 规范指出 EIP-712 需要更新：链 ID 和区块号必须从固定的域分隔符移到被签名的消息本身中，钱包也必须在分叉过渡期间正确设置它们。\n"}}}
