---
eip: 1191
label: ERC-1191
title: Add chain id to mixed-case checksum address encoding
status: Last Call
type: Standards Track
category: ERC
created: 2018-03-18
requires: 55, 155
official: https://eips.ethereum.org/EIPS/eip-1191
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-1191.md
canonical: https://eips.huatuo.xyz/zh/eip-1191/
lang: zh
tldr: 在 EIP-55 的大小写混合地址校验和中混入链 ID，使钱包能在地址是从另一个 EVM 网络复制而来时发出警告。
tags: [address, checksum, wallet, chain-id]
---

# ERC-1191: Add chain id to mixed-case checksum address encoding

> This EIP extends EIP-55 by optionally adding a chain id defined by EIP-155 to the checksum calculation.

**TL;DR:** 在 EIP-55 的大小写混合地址校验和中混入链 ID，使钱包能在地址是从另一个 EVM 网络复制而来时发出警告。

## 它解决什么问题？

[EIP-55](/eip-55/) 把校验和编码在地址十六进制字母的大小写中，以发现输入错误。但以太坊地址本身不携带它属于哪个网络的信息，因此一条链上完全有效的地址，在另一条链上看起来同样有效。以太坊兼容网络的用户可能因向为其他网络生成、或从其他网络复制来的地址转账而损失资金。

ERC-1191 允许网络选择启用一种同时依赖其链 ID（来自 [EIP-155](/eip-155/)）的校验和。这样，同样的 20 个字节在每条已启用的链上会有不同的大小写，钱包可以标出不匹配。

## 它如何运作

- 从 EIP-55 算法开始：对小写的十六进制地址做 keccak-256 哈希，然后把对应哈希半字节（nibble）大于等于 8 的每个字母改为大写。
- 如果链 ID 属于**选择启用**该变体的网络，哈希输入就变成十进制链 ID 后面跟着**包含** `0x` 前缀的小写地址，例如 `"30" + "0x27b1fdb0..."`。
- 对于其他任何链 ID，使用普通的 EIP-55 结果，因此以太坊主网的地址不会改变。

规范中列为使用该方案的网络是 RSK 主网（链 ID 30）和 RSK 测试网（31）。

## 示例代码

使用 ethers v6 基础函数的 TypeScript：

```ts
import { keccak256, toUtf8Bytes } from "ethers";

const ADOPTED_EIP1191 = new Set([30, 31]); // per the spec's usage table

export function checksum1191(address: string, chainId: number): string {
  const lower = address.toLowerCase();             // "0x..."
  const hex = lower.slice(2);
  const input = ADOPTED_EIP1191.has(chainId) ? `${chainId}${lower}` : hex;
  const hash = keccak256(toUtf8Bytes(input)).slice(2);

  let out = "0x";
  for (let i = 0; i < hex.length; i++) {
    out += parseInt(hash[i], 16) >= 8 ? hex[i].toUpperCase() : hex[i];
  }
  return out;
}

// From the spec's test vectors:
checksum1191("0x27b1fdb04752bbc536007a920d24acb045561c26", 30);
// -> "0x27b1FdB04752BBc536007A920D24ACB045561c26"
```

## 需要注意

- 状态：**Last Call**（规范中的截止日期为 2019-11-18）。它从未进入 Final。
- 是否启用以网络为单位。在没有采用它的链上，输出与 EIP-55 完全相同，因此在那里无法提供跨链保护。
- 校验和不匹配应被视为“该地址可能是为另一个网络准备的”这一警告，而不是它有误的证据。
- 规范列出了若干已有实现的钱包和库；依赖之前请先检查您自己的工具。


---

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