---
eip: 2477
label: ERC-2477
title: Token Metadata Integrity
status: Stagnant
type: Standards Track
category: ERC
created: 2020-01-02
requires: 165, 721, 1155
official: https://eips.ethereum.org/EIPS/eip-2477
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-2477.md
canonical: https://eips.huatuo.xyz/zh/eip-2477/
lang: zh
tldr: 让 NFT 和多代币合约把每个代币元数据（及其 schema）的哈希发布到链上，使客户端能检测链下 JSON 是否被修改或篡改。
tags: [nft, metadata, integrity, erc]
---

# ERC-2477: Token Metadata Integrity

> This specification defines a mechanism by which clients may verify that a fetched token metadata document has been delivered without unexpected manipulation.

**TL;DR:** 让 NFT 和多代币合约把每个代币元数据（及其 schema）的哈希发布到链上，使客户端能检测链下 JSON 是否被修改或篡改。

## 它解决什么问题？

[ERC-721](/eip-721/) 和 [ERC-1155](/eip-1155/) 代币通过 URI 指向其元数据。实际上，这些 URI 大多是普通的 HTTPS 网址。控制服务器的人可以随时更改代币的名称、描述或图片，而钱包或市场无从察觉。

ERC-2477 借用了网页 **Subresource Integrity（SRI）** 的思路：在 URI 之外，合约还公开文档的加密摘要。客户端获取 JSON、计算哈希并进行比较。如果哈希不同，说明内容在摘要发布之后被改动过。

## 它如何运作

合约实现两个 view 函数（ERC-165 接口 ID 为 `0x832a7e0e`）：

| 函数 | 返回 |
| --- | --- |
| `tokenURIIntegrity(tokenId)` | 元数据文档的 `(bytes digest, string hashAlgorithm)` |
| `tokenURISchemaIntegrity(tokenId)` | 该文档 JSON schema 的 `(bytes digest, string hashAlgorithm)` |

对于 ERC-721，`tokenURIIntegrity(21)` 覆盖 `tokenURI(21)` 处的文档；对于 ERC-1155，它覆盖 `uri(16)`。当不可用时，任一值都可以为空（`""`）。

关键规则：

- 合约**必须**同时实现 [ERC-165](/eip-165/)。
- 可以使用任何哈希算法，但每个客户端**必须**支持 `sha256`。SHA-384/512 是可选的，因为它们在链上开销很大。
- 元数据还可以携带 `$schema` 加一个 `$schemaIntegrity` 对象（`digest`、`hashAlgorithm`），从文档内部固定其 schema。
- 允许在完整性值变化时触发事件，但不是必需的。

规范逐一介绍了几种设计，从"只对元数据做哈希"到"在链上和文档中同时对元数据和 schema 做哈希"。对受保护文档的任何改动，都需要更新对应的链上摘要。

## 示例代码

一个为每个代币存储 SHA-256 摘要的最小 ERC-721 扩展：

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

interface IERC2477 {
    function tokenURIIntegrity(uint256 tokenId)
        external view returns (bytes memory digest, string memory hashAlgorithm);
    function tokenURISchemaIntegrity(uint256 tokenId)
        external view returns (bytes memory digest, string memory hashAlgorithm);
}

abstract contract MetadataIntegrity is IERC2477 {
    mapping(uint256 => bytes32) internal _metadataSha256;

    function tokenURIIntegrity(uint256 tokenId)
        external view returns (bytes memory, string memory)
    {
        bytes32 d = _metadataSha256[tokenId];
        if (d == bytes32(0)) return ("", "");
        return (abi.encodePacked(d), "sha256");
    }

    function tokenURISchemaIntegrity(uint256)
        external pure returns (bytes memory, string memory)
    {
        return ("", ""); // schema integrity not provided
    }
}
```

在客户端验证（ethers v6，Node.js）：

```ts
import { Contract, sha256, toUtf8Bytes, hexlify } from "ethers";

const nft = new Contract(address, [
  "function tokenURI(uint256) view returns (string)",
  "function tokenURIIntegrity(uint256) view returns (bytes, string)",
], provider);

const [uri, [digest, algo]] = await Promise.all([nft.tokenURI(id), nft.tokenURIIntegrity(id)]);
const body = await (await fetch(uri)).text();      // hash the exact bytes served
if (algo === "sha256" && sha256(toUtf8Bytes(body)) !== hexlify(digest)) {
  throw new Error("metadata does not match on-chain digest");
}
```

## 需要注意

- **状态：Stagnant（停滞）。** 它没有推进到 Final，不应假定钱包和市场支持它。
- 请对实际返回的原始字节做哈希。重新序列化 JSON（空白或键顺序不同）会改变摘要。
- 完整性不等于不可变：所有者仍然可以更新链上摘要。它只是让改动可以被发现。
- 内容寻址的 URI（例如 IPFS CID）或链上 `data:` URI 无需额外函数就能提供类似的保证。
- 尽管头部把 ERC-721 和 ERC-1155 列为必需，这种做法适用于任何公开元数据 URI 的代币。


---

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