---
eip: 7632
label: ERC-7632
title: Interfaces for Named Token
status: Draft
type: Standards Track
category: ERC
created: 2024-02-08
requires: 165
official: https://eips.ethereum.org/EIPS/eip-7632
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-7632.md
canonical: https://eips.huatuo.xyz/zh/eip-7632/
lang: zh
tldr: 为以 uint256 id 标识的代币赋予人类可读的字符串名称，提供在名称与 id 之间转换的标准函数，以及每个方法的可选 ByName 版本。
tags: [nft, naming, token, interface, erc721]
---

# ERC-7632: Interfaces for Named Token

> Enable tokens to have a string name and be able to convert between name and id.

**TL;DR:** 为以 uint256 id 标识的代币赋予人类可读的字符串名称，提供在名称与 id 之间转换的标准函数，以及每个方法的可选 ByName 版本。

## 它解决什么问题？

[ERC-721](/eip-721/) 和 [ERC-1155](/eip-1155/) 用 `uint256 tokenId` 来标识代币。对许多系列来说，代币其实有一个**名称**：类似域名的标识、用户名、票券码。市场、浏览器和钱包最终只能显示一长串数字，或者各个项目自己发明查询函数。

ERC-7632 标准化了代币如何暴露其名称，以及如何在名称与 id 之间相互转换，使工具能够一致地展示和操作有名称的代币。

## 它如何运作

兼容的合约必须为每个代币支持一个 `tokenName`，并支持名称与 id 之间的映射，方式有两种：

1. **推荐：** 实现 `IERC_NamedTokenCore`：

   ```solidity
   interface IERC_NamedTokenCore {
     function idToName(uint256 _tokenId) external view returns (string memory);
     function nameToId(string memory _tokenName) external returns (uint256);
   }
   ```

   映射必须是一一对应的：`nameToId(idToName(id)) == id` 且 `idToName(nameToId(name)) == name`。建议在引入名称时发出 `newName(uint256 indexed tokenId, string tokenName)`。

2. **默认规则：** 如果没有实现核心接口，合约可以使用 `tokenId = uint256(keccak256(tokenName))`。

此外，建议每个接受 `tokenId` 的方法都有一个接受 `string tokenName` 的 **`...ByName`** 孪生版本，例如 `ownerOfByName`，行为完全相同。

可选的 `IERC_NamedTokenExtension` 增加了 `isValidTokenName` 和 `normalizeTokenName`，使合约可以声明诸如转为小写之类的规则，这对外观相似的名称很重要。

## 示例代码

使用默认哈希映射的有名称 NFT（Solidity，OpenZeppelin ERC-721）：

```solidity
contract NamedNFT is ERC721 {
    mapping(uint256 => string) private _names;
    event newName(uint256 indexed tokenId, string tokenName);

    constructor() ERC721("Named", "NMD") {}

    function nameToId(string memory name) public pure returns (uint256) {
        return uint256(keccak256(bytes(name)));
    }

    function idToName(uint256 id) external view returns (string memory) {
        _requireOwned(id);
        return _names[id];
    }

    function mintByName(address to, string calldata name) external {
        uint256 id = nameToId(name);
        _names[id] = name;
        _safeMint(to, id);
        emit newName(id, name);
    }

    function ownerOfByName(string calldata name) external view returns (address) {
        return ownerOf(nameToId(name));
    }
}
```

## 需要注意

- 状态：**Draft**。接口名称（`IERC_...`）和一些签名看起来尚未完成；例如，规范将 `isValidTokenName` 声明为返回 `string`，并将 `nameToId` 声明为非 `view`。
- 规范假定名称和 id 都是唯一的。如果没有规范化，两个外观相似的名称可能映射到不同的代币，从而招致钓鱼；如果允许这种情况，请声明规范化规则。
- 使用默认的 keccak 映射时，id 不是连续的，除非像示例那样存储名称，否则无法从 id 还原名称。


---

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