---
eip: 634
label: ERC-634
title: Storage of text records in ENS
status: Stagnant
type: Standards Track
category: ERC
created: 2017-05-17
requires: 137, 165
official: https://eips.ethereum.org/EIPS/eip-634
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-634.md
canonical: https://eips.huatuo.xyz/zh/eip-634/
lang: zh
tldr: 一种 ENS 解析器配置 text(node, key)，让名称可以存储任意键值文本，例如头像、网站、邮箱或 Twitter 账号，使 ENS 名称成为简单的公开个人资料。
tags: [ens, naming, profile, resolver, metadata]
---

# ERC-634: Storage of text records in ENS

> Profiles for ENS resolvers to store arbitrary text key/value pairs.

**TL;DR:** 一种 ENS 解析器配置 text(node, key)，让名称可以存储任意键值文本，例如头像、网站、邮箱或 Twitter 账号，使 ENS 名称成为简单的公开个人资料。

## 它解决什么问题？

[ENS](/eip-137/) 把 `alice.eth` 这类人类可读的名称映射到地址、内容哈希等机器数据。人们还希望给名称附加人类可读的信息：网站、电子邮箱、头像、社交账号。为每种数据单独增加一个解析器函数，永远跟不上新的需求。

EIP-634 借用了 DNS `TXT` 记录的思路。它增加一个通用函数，对任意键返回一个文本值，因此新增数据类型只需要一个新的键，而无需新的合约接口。

## 它如何运作

支持此配置的解析器需要实现：

```solidity
function text(bytes32 node, string calldata key) external view returns (string memory);
```

- `node` 是名称的 ENS namehash。
- 值可以是任意 UTF-8 字符串。如果键未设置，解析器必须返回 `""`。
- 其 [EIP-165](/eip-165/) 接口 ID 为 `0x59d1d43c`，因此客户端可以先检查是否支持。

键分为三类：

- **全局键**使用小写字母、数字和连字符：`avatar`、`description`、`display`、`email`、`keywords`、`mail`、`notice`、`location`、`phone`（E.164 格式）和 `url`。如果设置了 `display`，它必须在忽略大小写的情况下与名称一致，例如 `ricmoo.eth` 对应 `RicMoo.eth`。否则客户端应忽略它。
- **服务键**使用反向 DNS 记法表示服务自己拥有的命名空间，且至少包含一个点：`com.github`、`com.twitter`、`com.linkedin`、`io.keybase`、`org.telegram`。服务也可以定义层级结构，例如 `com.example.users`。
- **旧版键**（`vnd.github`、`vnd.twitter`、`vnd.peepeth`）来自早期草案。客户端可以把它们作为后备来查询。

## 示例代码

使用 ethers v6 读取文本记录：

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

const provider = new JsonRpcProvider(process.env.RPC_URL);
const resolver = await provider.getResolver("vitalik.eth");
if (resolver) {
  const url = await resolver.getText("url");
  const twitter = await resolver.getText("com.twitter");
  console.log({ url, twitter }); // empty or null when the key is unset
}
```

一个按名称存储记录的最小解析器（无访问控制，仅作演示）：

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

contract TextResolver {
    event TextChanged(bytes32 indexed node, string indexed indexedKey, string key);
    mapping(bytes32 => mapping(string => string)) private texts;

    function setText(bytes32 node, string calldata key, string calldata value) external {
        texts[node][key] = value; // real resolvers check the caller owns `node`
        emit TextChanged(node, key, key);
    }

    function text(bytes32 node, string calldata key) external view returns (string memory) {
        return texts[node][key];
    }

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

## 需要注意

- **此 EIP 的状态是 Stagnant。** 尽管如此，文本记录接口仍被 ENS 解析器实现，并被钱包和应用读取。ENS 现在将其作为 ENSIP-5 维护。
- 文本记录是链上的公开数据。不要存储任何你不愿公开的内容，例如私人邮箱或电话号码。
- 这些值未经验证。`com.twitter` 记录只能说明名称所有者的声明，并不证明他们控制着那个账号。
- `avatar` 的值通常是 URL，但各客户端能解析的格式不同，因此请在目标钱包中测试。


---

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