---
eip: 7406
label: ERC-7406
title: Multi-Namespace Onchain Registry
status: Draft
type: Standards Track
category: ERC
created: 2023-07-23
requires: 137
official: https://eips.ethereum.org/EIPS/eip-7406
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-7406.md
canonical: https://eips.huatuo.xyz/zh/eip-7406/
lang: zh
tldr: 一个 ENS 风格的链上注册表草案，每个键都位于某个命名空间之内，因此一个合约就能容纳许多相互独立、所有者和解析器各不相同的映射、版本或记录类型。
tags: [registry, naming, ens, namespace]
---

# ERC-7406: Multi-Namespace Onchain Registry

> An universally accepted multi-namespace registry with mapping structures on the Ethereum

**TL;DR:** 一个 ENS 风格的链上注册表草案，每个键都位于某个命名空间之内，因此一个合约就能容纳许多相互独立、所有者和解析器各不相同的映射、版本或记录类型。

## 它解决什么问题？

ENS 注册表（[ERC-137](/eip-137/)）把单个 `node` 映射到一个所有者和一个解析器。这对名称很好用，但项目常常希望把同样的模式用于其他事物：短链接、会话、带版本的记录、协议配置。在单一扁平键空间下，每增加一种新用例通常都意味着再部署一个注册表。

ERC-7406 增加了一个额外维度，即**命名空间（namespace）**。同一个键可以存在于许多命名空间中，拥有不同的所有者和解析器。命名空间可以代表数据类型（"short-url"、"session"）或版本号，这让注册表能够保留记录的可追溯历史：把版本 2 写入新的命名空间，而不是覆盖版本 1。

## 它如何运作

记录通过 `(namespace, key)` 寻址，两者都是 `bytes32`。注册表公开：

| 函数 | 用途 |
| --- | --- |
| `owner(namespace, key)` | 该键在该命名空间中的当前所有者 |
| `resolver(namespace, key)` | 该键在该命名空间中的解析器合约 |
| `setOwner(namespace, key, newOwner)` | 转移该键；只有当前所有者可以调用；触发 `Transfer` |
| `setResolver(namespace, key, newResolver)` | 把该键指向某个解析器；只有所有者可以调用；触发 `NewResolver` |
| `createNamespace(namespace)` | 创建新的命名空间（例如新版本或新记录类型）；触发 `NewNamespace` |

所有权是按命名空间划分的：同一个键在不同命名空间中可以有不同的所有者和解析器。实际的值（地址、文本、内容哈希）存放在解析器合约中，可以遵循现有的 ERC-137 解析器规范。

规范没有说明谁可以调用 `createNamespace`；这留给实现来决定（例如管理员或治理角色）。

## 示例代码

针对当前 Solidity 编写的最小接口和查询：

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

interface IERC7406 {
    event Transfer(bytes32 indexed namespace, bytes32 indexed key, address owner);
    event NewResolver(bytes32 indexed namespace, bytes32 indexed key, address resolver);
    event NewNamespace(bytes32 namespace);

    function owner(bytes32 namespace, bytes32 key) external view returns (address);
    function resolver(bytes32 namespace, bytes32 key) external view returns (address);
    function setOwner(bytes32 namespace, bytes32 key, address newOwner) external;
    function setResolver(bytes32 namespace, bytes32 key, address newResolver) external;
    function createNamespace(bytes32 namespace) external;
}

contract ShortLinkReader {
    IERC7406 public immutable registry;
    bytes32 public constant NS = keccak256("short-url/v1");

    constructor(IERC7406 r) { registry = r; }

    function resolverFor(string calldata slug) external view returns (address) {
        return registry.resolver(NS, keccak256(bytes(slug)));
    }
}
```

## 需要注意

- **状态：Draft。** 接口可能变化，也没有证据表明其被广泛采用。
- 规范中的参考实现使用了非常老旧的 Solidity 语法（`throw`、`constant` 函数、以合约名命名的构造函数），并含有拼写错误，因此无法直接编译。请把它当作草图，而不是可部署的代码。
- 规范自身的安全性一节很简短。`createNamespace` 的访问控制，以及 `setResolver` 是否应仅限所有者调用，是实现者必须做出并审计的关键设计选择。
- 与 ENS 不同，接口中没有层级化的 `subnode` 逻辑；命名空间是扁平的。


---

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