---
eip: 1062
label: ERC-1062
title: Formalize IPFS hash into ENS(Ethereum Name Service) resolver
status: Stagnant
type: Standards Track
category: ERC
created: 2018-05-02
official: https://eips.ethereum.org/EIPS/eip-1062
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-1062.md
canonical: https://eips.huatuo.xyz/zh/eip-1062/
lang: zh
tldr: 一项早期提案，用于把 IPFS 内容哈希存入 ENS 解析器，让 mysite.eth 这样人类可读的名称可以指向去中心化网站。
tags: [ens, ipfs, resolver, storage, naming]
---

# ERC-1062: Formalize IPFS hash into ENS(Ethereum Name Service) resolver

> To specify the mapping protocol between resources stored on IPFS and ENS(Ethereum Naming Service).

**TL;DR:** 一项早期提案，用于把 IPFS 内容哈希存入 ENS 解析器，让 mysite.eth 这样人类可读的名称可以指向去中心化网站。

## 它解决什么问题？

IPFS 通过内容的哈希来寻址，哈希是一长串 Base58 字符串，例如 `QmXo...`。这对完整性很有利，但对人类毫无用处：没有人会记住或输入它。ENS（[EIP-137](/eip-137/)）已经能把 `mysite.eth` 这样易读的名称映射到存放在解析器合约中的数据。

ERC-1062 提出了一种把 IPFS 哈希放进 ENS 解析器并再取出来的标准方式，这样浏览器或扩展只需输入 `.eth` 名称，就能加载一个完全去中心化的网站。其目标是在人类可读的名称与内容寻址存储之间搭建一座简单的桥梁。

## 它如何运作

主要的难点在于编码。IPFS 以 Base58 字符串显示哈希，而以太坊合约和 JSON-RPC 处理的是以十六进制显示的原始字节。该提案通过底层的二进制*multihash*来连接两者：

1. **存储：** 把 Base58 的 IPFS 哈希解码为字节，再把这些字节写入解析器。
2. **读取：** 从解析器读取字节，再编码回 Base58，得到 IPFS 哈希。

它依赖解析器的两个方法：

| 函数 | 用途 |
| --- | --- |
| `setMultihash(bytes32 node, bytes hash)` | ENS 名称的所有者存入 multihash 字节 |
| `multihash(bytes32 node)` | 任何人都可以读回存入的字节 |

`node` 是该名称通常的 ENS namehash。只有该 node 的所有者才能调用 setter。

经典的 IPFS（CIDv0）哈希解码后的字节以 `0x12 0x20` 开头：`0x12` 表示 SHA-256，`0x20` 表示 32 字节的摘要，后面跟着摘要本身。

## 示例代码

用 ethers v6 在 Base58 与十六进制之间转换（规范自己的示例使用 `multihashes` npm 库）：

```ts
import { decodeBase58, encodeBase58, getBytes, toBeHex } from "ethers";

// IPFS Base58 hash -> 0x-hex bytes for the resolver
export const toHex = (ipfsHash: string): string => toBeHex(decodeBase58(ipfsHash));

// 0x-hex bytes from the resolver -> IPFS Base58 hash
export const toBase58 = (hex: string): string => encodeBase58(getBytes(hex));
```

所述的解析器接口：

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

interface IMultihashResolver {
    function setMultihash(bytes32 node, bytes calldata hash) external; // only node owner
    function multihash(bytes32 node) external view returns (bytes memory);
}
```

## 需要注意

- **状态：Stagnant。** 该提案从未最终确定。
- 生态系统最终统一采用了 [EIP-1577](/eip-1577/) 的 `contenthash` 解析器字段。它可以用自描述的前缀保存 IPFS、Swarm 和其他内容标识符，也是如今支持 ENS 的钱包、网关和库所读取的字段（例如 ethers 的 `resolver.getContentHash()`）。
- `multihash` 只存储哈希，不说明它属于哪个存储网络。这一局限是人们更倾向于使用更通用字段的原因之一。
- 如果你今天要在某个 ENS 名称后发布网站，请设置 `contenthash`，而不是 `multihash`。


---

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