ERC-7632 · Draft · ERC · 2024-02-08
ERC-7632: Interfaces for Named Token
Gives tokens identified by uint256 ids a human-readable string name, with standard functions to convert between name and id and optional ByName versions of every method.
What problem does it solve?
ERC-721 and ERC-1155 identify tokens by a uint256 tokenId. For many collections the token really has a name: a domain-like handle, a username, a ticket code. Marketplaces, explorers and wallets end up showing a long number, or each project invents its own lookup function.
ERC-7632 standardizes how a token exposes its name and how to go from name to id and back, so tools can display and operate on named tokens consistently.
How it works
A compliant contract must support a tokenName for each token and a mapping between name and id, in one of two ways:
-
Recommended: implement
IERC_NamedTokenCore:interface IERC_NamedTokenCore { function idToName(uint256 _tokenId) external view returns (string memory); function nameToId(string memory _tokenName) external returns (uint256); }The mapping must be one-to-one:
nameToId(idToName(id)) == idandidToName(nameToId(name)) == name. EmittingnewName(uint256 indexed tokenId, string tokenName)when a name is introduced is recommended. -
Default rule: if the core interface is not implemented, the contract may use
tokenId = uint256(keccak256(tokenName)).
In addition, every method that takes a tokenId is recommended to have a ...ByName twin that takes string tokenName instead, for example ownerOfByName, and behaves identically.
An optional IERC_NamedTokenExtension adds isValidTokenName and normalizeTokenName, so contracts can declare rules such as lower-casing, which matters for look-alike names.
Sample code
A named NFT using the default hash mapping (Solidity, OpenZeppelin ERC-721):
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));
}
}
Things to know
- Status: Draft. The interface names (
IERC_...) and some signatures look unfinished; for example, the spec declaresisValidTokenNameas returningstringandnameToIdas non-view. - The spec assumes names and ids are both unique. Without normalization, two visually similar names can map to different tokens, which invites phishing; declare a normalization rule if you allow that.
- With the default keccak mapping, ids are not sequential and names cannot be recovered from an id unless you store them, as the sample does.