---
eip: 2304
label: ERC-2304
title: Multichain address resolution for ENS
status: Stagnant
type: Standards Track
category: ERC
created: 2019-09-09
requires: 137
official: https://eips.ethereum.org/EIPS/eip-2304
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-2304.md
canonical: https://eips.huatuo.xyz/zh/eip-2304/
lang: zh
tldr: 为 ENS 解析器增加 `addr(node, coinType)` 函数，让同一个名称不仅能指向以太坊地址，还能指向比特币、莱特币等其他链上的地址。
tags: [ens, naming, multichain, resolver, addresses]
---

# ERC-2304: Multichain address resolution for ENS

> This EIP introduces new overloads for the addr field for ENS resolvers, which permit resolution of addresses for other blockchains via ENS.

**TL;DR:** 为 ENS 解析器增加 `addr(node, coinType)` 函数，让同一个名称不仅能指向以太坊地址，还能指向比特币、莱特币等其他链上的地址。

## 它解决什么问题？

最初的 ENS 规范（[EIP-137](/eip-137/)）让 `alice.eth` 这样的名称通过 `addr(bytes32 node)` 解析为单个以太坊地址。多币种钱包想要更多：用户应该可以输入 `alice.eth` 并发送 BTC、LTC 或 DOGE。没有共同标准，每个钱包都会发明自己的记录格式，名称在不同客户端之间就无法通用。

ERC-2304 规范了非以太坊地址如何存入 ENS 解析器并从中读取。

## 它如何运作

- 新的解析器函数 `addr(bytes32 node, uint coinType) returns (bytes)` 返回指定链的地址。其 [ERC-165](/eip-165/) 接口 ID 为 `0xf1cb7e06`。
- `coinType` 是 SLIP-44 中的币种类型索引（例如比特币为 `0`，莱特币为 `2`，狗狗币为 `3`，以太坊为 `60`，以太坊经典为 `61`）。
- 返回值是地址的**原生二进制形式**，而不是人类可读的文本。对于比特币系的链，这意味着 scriptPubkey 字节（例如 P2PKH 地址存储为 `76a914<20-byte hash>88ac`）；对于以太坊，则是去掉 `0x` 前缀的原始 20 字节。如果名称没有该币种的地址，则返回空 bytes。
- 解析器必须在每次变更时触发 `AddressChanged(bytes32 indexed node, uint coinType, bytes newAddress)` 事件。推荐的 setter 是 `setAddr(bytes32 node, uint coinType, bytes addr)`。
- 向后兼容：如果解析器同时支持旧的 `addr(bytes32)`，它必须返回与 `addr(node, 60)` 相同的值，并且旧的 `AddrChanged` 事件和新的 `AddressChanged` 事件必须一起触发。

文本与二进制之间的转换由客户端负责。把存储的字节转换回以太坊风格的地址时，必须应用 [EIP-55](/eip-55/) 校验和。

## 示例代码

解析器的存储与访问函数（已简化，省略了访问控制）：

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

contract MultiCoinResolver {
    uint256 constant COIN_TYPE_ETH = 60;
    event AddressChanged(bytes32 indexed node, uint256 coinType, bytes newAddress);

    mapping(bytes32 => mapping(uint256 => bytes)) private _addresses;

    function setAddr(bytes32 node, uint256 coinType, bytes calldata a) external {
        // real resolvers check that msg.sender controls `node`
        _addresses[node][coinType] = a;
        emit AddressChanged(node, coinType, a);
    }

    function addr(bytes32 node, uint256 coinType) external view returns (bytes memory) {
        return _addresses[node][coinType]; // empty bytes if unset
    }

    function supportsInterface(bytes4 id) external pure returns (bool) {
        return id == 0xf1cb7e06 || id == 0x01ffc9a7;
    }
}
```

用 ethers v6 读取比特币地址，它会替你解码二进制格式：

```ts
import { JsonRpcProvider } from "ethers";

const provider = new JsonRpcProvider(process.env.RPC_URL);
const resolver = await provider.getResolver("alice.eth");
const btc = await resolver?.getAddress(0); // coinType 0 = Bitcoin
console.log(btc);
```

## 需要注意

- 在 EIP 仓库中状态为 **Stagnant**。ENS 项目在自己的 ENSIP 流程中维护这一方案，ENS 解析器和库在实践中实现的也是它。
- 始终存储二进制形式，而不是显示用的字符串，并且在解析用户输入时验证校验和；编码错误的比特币 scriptPubkey 可能导致资金无法花费。
- 规范为列出的每条链都提供了测试向量。编写编码器/解码器时请使用它们。
- 对于没有自己解析器的名称（例如 `user.wallet.eth`）的解析，请参阅 [ERC-2544](/eip-2544/) 的通配符解析。


---

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