ERC-7053 · Final(最终) · ERC · 2023-05-22
ERC-7053: Interoperable Digital Media Indexing
一个简单的数字媒体链上索引:任何人都可以用文件的内容标识符(CID)和一些数据调用 commit(),触发的 Commit 事件就构成了该文件跨合约、可检索的历史记录。
它解决什么问题?
照片、视频和其他数字媒体会被复制、编辑、铸造成 NFT,并被许多系统引用。每个系统往往各自追踪文件,因此没有一个通用的键来回答"这个确切的文件被谁、在哪条链上记录过什么?"。来源(provenance)和元数据因此变得零散。
ERC-7053 提出了一个通用的键,即文件的内容标识符(Content Identifier,CID),以及针对它记录日志的标准方式。由于 CID 是内容的密码学哈希,任何拥有该文件的人都可以重新计算它,并查询跨合约、跨 EVM 链针对它提交的所有记录。
它如何运作
- 为媒体文件计算 CID。规范要求使用基于密码学哈希的内容地址,并说明它应与去中心化存储上使用的标识符相同,这样 CID 同时也能定位文件及其元数据。
- 在符合规范的合约上调用
commit,传入 CID 和一个自由格式的commitData字符串(例如指向元数据的指针、操作描述或编辑记录)。 - 合约触发
Commit事件并返回区块号。参考实现还会按 CID 保存区块号,以便通过getCommits读回。
event Commit(address indexed recorder, string indexed assetCid, string commitData);
function commit(string memory assetCid, string memory commitData)
public returns (uint256 blockNumber);
一个文件的历史就是其 CID 对应的 Commit 事件的有序列表,可能跨越许多合约和链。在合约出现之前铸造的 NFT 也可以被索引,只需计算其 tokenURI 所指文件的 CID。
示例代码
提交记录并随后查询文件的历史(ethers v6):
import { Contract, id } from "ethers";
const abi = [
"event Commit(address indexed recorder, string indexed assetCid, string commitData)",
"function commit(string assetCid, string commitData) returns (uint256)",
];
const registry = new Contract(registryAddress, abi, signer);
const cid = "bafy..."; // CID of the media file
await (await registry.commit(cid, JSON.stringify({ action: "captured" }))).wait();
// Indexed strings are stored as keccak256 hashes in the log topic,
// so filter by the hash of the CID.
const logs = await registry.queryFilter(registry.filters.Commit(null, id(cid)));
for (const l of logs) console.log(l.blockNumber, l.args.recorder, l.args.commitData);
需要注意
- 状态:Final。
- 合约不会验证任何内容:它不检查 CID 是否有效,也不检查
commitData是否属实。任何人都可以针对任何 CID 提交任何内容,因此信任来自recorder是谁,而不是记录本身存在。 - 由于
assetCid是 indexed 的string,日志 topic 中保存的是它的哈希而非文本。你可以按已知的 CID 过滤,但无法仅凭 topic 读回 CID;如果需要,请把它放在commitData或存储中。 - 索引器在根据日志构建历史时,应处理重组(reorg)以及可能漏掉的事件。