ERC-1577 · Stagnant(停滞) · ERC · 2018-11-13
ERC-1577: contenthash field for ENS
为 ENS 解析器新增 contenthash 记录,使名称可以通过自描述的 multicodec 格式指向 IPFS、Swarm 等网络上的内容。
它解决什么问题?
钱包和浏览器开始把 ENS 名称解析为托管在 IPFS 和 Swarm 等去中心化存储上的网站。较早的解析器字段(content 和 multihash)没有清楚说明某个哈希属于哪个网络,所以应用只能猜测,名称所有者也无法确定自己的内容会如何被加载。
ERC-1577 用一个字段 contenthash 取代了它们,其值自带协议标识符。
它如何运作
- 支持该字段的解析器会从 ERC-165 的
supportsInterface(0xbc1c58d1)返回true。 contenthash(node)返回 multicodec 格式的原始字节:
<protoCode uvarint><value []byte>
- 对于 IPFS(
0xe3)和 Swarm(0xe4),值是不带 base 前缀的 CIDv1:
<protoCode uvarint><cid-version><multicodec-content-type><multihash-content-address>
- 客户端必须(MUST)先读取协议代码,并在支持的情况下用相应的网络解析该地址。
- 旧的
content和multihash字段已被弃用。规范要求设置宽限期,至少持续到 2019 年 3 月 31 日,在此期间客户端会回退使用它们。
拆解规范中的 IPFS 示例:
| 字节 | 含义 |
|---|---|
e3 01 |
IPFS 命名空间(0xe3 的 uvarint 形式) |
01 |
CID 版本 1 |
70 |
内容类型 dag-pb |
12 20 |
sha2-256,32 字节摘要 |
29f2…3f1f |
哈希值 |
示例代码
规范中的示例:
IPFS binary: 0xe3010170122029f2d17be6139079dc48696d1f582a8530eb9805b561eda517e22a892c7e3f1f
text: ipfs://QmRAQB6YaCyidP37UdDnjFY5vQuiBrcqdyoW1CuDgwxkD4
Swarm binary: 0xe40101fa011b20d1de9994b4d039f6548d191eb26786769f580809256b4685ef316805265ea162
text: bzz://d1de9994b4d039f6548d191eb26786769f580809256b4685ef316805265ea162
用 ethers v6 读取名称的内容哈希,它会把常见协议解码为 URL:
import { JsonRpcProvider, Contract, namehash } from "ethers";
const provider = new JsonRpcProvider(process.env.RPC_URL);
const resolver = await provider.getResolver("vitalik.eth");
console.log(await resolver?.getContentHash()); // e.g. "ipfs://..."
// Or call the resolver directly for the raw bytes
const r = new Contract(resolver!.address,
["function contenthash(bytes32 node) view returns (bytes)"], provider);
console.log(await r.contenthash(namehash("vitalik.eth")));
在解析器合约内部:
function contenthash(bytes32 node) external view returns (bytes memory);
event ContenthashChanged(bytes32 indexed node, bytes hash);
需要注意
- 在 EIP 仓库中的状态是 Stagnant,但
contenthash是 ENS 解析器以及支持 ENS 的网关和浏览器用于去中心化网站的记录。 - 只有编码方式被标准化;客户端能否真正获取内容,取决于它是否支持该协议。
- 请使用库(例如规范链接的
content-hash包)来编码值,而不要手工拼装字节。 - 名称解析本身由 ENS (EIP-137) 定义;文本记录见 EIP-634。