---
eip: 3788
label: EIP-3788
title: Strict enforcement of chainId
status: Stagnant
type: Standards Track
category: Core
created: 2021-09-02
requires: 155
official: https://eips.ethereum.org/EIPS/eip-3788
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-3788.md
canonical: https://eips.huatuo.xyz/zh/eip-3788/
lang: zh
tldr: 提议让节点拒绝任何 chainId 与自身不一致的交易，堵住 chainId 为 0 的交易可以跨链重放的漏洞。
tags: [transactions, chainid, replay-protection, security, core]
---

# EIP-3788: Strict enforcement of chainId

> Reject transactions that do not explicitly have the same chainId as the node's configuration.

**TL;DR:** 提议让节点拒绝任何 chainId 与自身不一致的交易，堵住 chainId 为 0 的交易可以跨链重放的漏洞。

## 它解决什么问题？

[EIP-155](/eip-155/) 为已签名的交易加入了链 ID，使为某个网络签名的交易无法在另一个网络上重放。但正如这份 EIP 指出的，`chainId = 0` 的交易仍然被视为有效。这是有意保留的，目的是让开发者能够创建在任何链上都能工作的交易。

由于有许多 EVM 兼容网络是由以太坊客户端分叉构建的，这个后门变成了风险。大多数钱包不会向用户显示他们签署的链 ID。恶意（或粗心）的网络可以诱使用户签署 `chainId = 0` 的交易，然后在以太坊主网或其他地方重放它们。

## 它如何运作

从分叉区块 `N` 开始，节点拒绝任何链 ID 与自己配置中的链 ID 不同的交易。特别是，`chainId = 0` 的交易变为无效。

```text
if node.config.chainId != tx.chainId:
    reject(tx)
```

该检查应用于两处：

1. **JSON-RPC**（`eth_sendTransaction`、`eth_sendRawTransaction`）：尽早拒绝，这样用户永远不会有一笔注定失败的交易处于待处理状态。
2. 区块处理期间的**交易验证**：这样此类交易永远不可能被打包进区块。

节点配置的链 ID 是唯一的事实来源。

## 示例代码

类型化交易（[EIP-2718](/eip-2718/)，例如 [EIP-1559](/eip-1559/) 的类型 2）始终带有显式的 `chainId`。使用 ethers v6 时，签名者会根据所连接的 provider 填入它，你也可以把意图写明：

```ts
import { JsonRpcProvider, Wallet, parseEther } from "ethers";

const provider = new JsonRpcProvider(process.env.RPC_URL);
const wallet = new Wallet(process.env.PRIVATE_KEY!, provider);
const { chainId } = await provider.getNetwork();

const tx = await wallet.sendTransaction({
  to: "0x000000000000000000000000000000000000dEaD",
  value: parseEther("0.001"),
  chainId,            // bound to this network only, never 0
});
console.log(tx.chainId); // e.g. 1n on mainnet
```

## 需要注意

- 状态：**Stagnant**。它从未被排入网络升级。
- 规范指出，在分叉区块之后，任何仍提交 `chainId == 0` 交易的应用或工具都会被破坏。
- 它无法阻止有人启动一个复用 `chainId = 1` 或任何其他现有 ID 的网络。只有链 ID 唯一时，重放保护才有效。
- 对钱包来说，即使没有分叉，实用的教训依然成立：始终使用显式且正确的链 ID 签名，并向用户显示它。


---

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