---
eip: 2520
label: ERC-2520
title: Multiple contenthash records for ENS
status: Stagnant
type: Standards Track
category: ERC
created: 2020-02-18
requires: 1577
official: https://eips.ethereum.org/EIPS/eip-2520
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-2520.md
canonical: https://eips.huatuo.xyz/zh/eip-2520/
lang: zh
tldr: 扩展 ENS 的 contenthash 记录，使一个名称可以按协议为键，同时指向多个托管系统（IPFS、Swarm、Onion 等）上的内容。
tags: [ens, contenthash, ipfs, swarm, resolver]
---

# ERC-2520: Multiple contenthash records for ENS

> ENS support for multiple contenthash records on a single ENS name.

**TL;DR:** 扩展 ENS 的 contenthash 记录，使一个名称可以按协议为键，同时指向多个托管系统（IPFS、Swarm、Onion 等）上的内容。

## 它解决什么问题？

[EIP-1577](/eip-1577/) 为 ENS 名称提供了 `contenthash` 记录，用来告诉浏览器或网关去中心化网站的位置（IPFS CID、Swarm 哈希等）。但一个名称只能保存**一个** contenthash。同时发布到 IPFS 和 Swarm 的网站所有者只能二选一，而只支持 Swarm 的网关无法使用 IPFS 记录。

ERC-2520 让解析器为每个协议存储一个 contenthash，外加一个默认值，这样不同的应用都可以获取各自能理解的记录。

## 它如何运作

解析器保留 EIP-1577 的原有函数，并增加带有 `proto` 参数的重载：

- `setContenthash(node, proto, hash)` 为该协议存储一个哈希。调用原来的两参数版本则存储**默认**记录。
- `contenthash(node, proto)` 返回该协议的哈希。原来的单参数版本返回默认记录。
- 支持此功能的解析器在 `supportsInterface(0x6de03e07)`（[ERC-165](/eip-165/)）上返回 `true`。

给客户端的指引：

- 只支持一种系统的应用（例如 IPFS 网关）**应当**请求特定协议的记录。
- 支持多种系统的应用（规范中的例子是 MetaMask）**应当**请求默认记录，与 EIP-1577 完全一样。

为保持设计简单，有意不支持每个协议多条记录和优先级排序。

## 示例代码

接口和一个最小解析器，遵循规范的参考实现（无访问控制，仅作演示）：

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

contract MultiContentHashResolver {
    bytes4 constant MULTI_CONTENT_HASH_INTERFACE_ID = 0x6de03e07;
    event ContenthashChanged(bytes32 indexed node, bytes hash);

    mapping(bytes32 => mapping(bytes => bytes)) private hashes;

    // In production, restrict to the name's owner (`authorised(node)`).
    function setContenthash(bytes32 node, bytes calldata proto, bytes calldata hash) external {
        hashes[node][proto] = hash;
        emit ContenthashChanged(node, hash);
    }

    function contenthash(bytes32 node, bytes calldata proto) external view returns (bytes memory) {
        return hashes[node][proto];
    }

    function supportsInterface(bytes4 id) external pure returns (bool) {
        return id == MULTI_CONTENT_HASH_INTERFACE_ID;
    }
}
```

## 需要注意

- **状态：Stagnant**，并且它建立在同为 Stagnant 的 [EIP-1577](/eip-1577/) 之上。标准的 ENS 公共解析器没有实现这一扩展，因此大多数工具只能看到单个默认记录。
- 规范没有定义 `proto` 键的具体编码；实现者需要在链外就此达成一致。
- 规范的安全考虑部分仍为「TBD」。与任何解析器一样，写入必须限制为该名称的所有者或获批准的运营者。
- 由于是重载，它向后兼容：旧客户端继续工作，只会收到默认记录。


---

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