ERC-8074 · Draft(草案) · ERC · 2025-10-30
ERC-8074: Self-Describing Bytes via EIP-712 Selectors
用取自 EIP-712 类型字符串的 4 字节选择器,外加一个 DataList 包装,为通用 bytes 参数中的数据打上标签,使合约能够识别并解码多种载荷类型。
它解决什么问题?
许多接口为了可扩展性在末尾带有一个 bytes data 参数:ERC-721 和 ERC-1155 中的 safeTransferFrom,以及 ERC-5750 中的通用模式。里面放什么是临时约定的。接收合约只能猜测如何解码,也难以接受多种载荷,而在合约之间转发的载荷可能被各方以不同方式解读。
ERC-8074 增加了一个极小的约定,让这些字节说明自己是什么。
它如何运作
单个载荷
- 取结构体的规范 EIP-712 类型字符串,例如
TransferNote(bytes32 reference,string comment,uint256 deadline)。 - 它的选择器是
bytes4(keccak256(typeString)),此处为0xf91f3a24。 - 编码为
selector ++ abi.encode(fields...)。
多个载荷
- 用
DataList(bytes[] items)(选择器0xae74f986)包装:0xae74f986 ++ abi.encode(items)。 - 每个 item 本身必须是有效的单个载荷。
解码规则
- 读取前 4 个字节。如果是
0xae74f986,则解码bytes[]并处理每个 item。 - 如果是你认识的选择器,就把其余部分按该结构体解码。
- 未知的选择器必须被忽略。顺序无关紧要;生产者应避免重复。
对这个 ERC 一无所知的合约仍然可以把载荷当作不透明的字节处理。
示例代码
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract NoteReceiver {
bytes4 constant TRANSFER_NOTE =
bytes4(keccak256("TransferNote(bytes32 reference,string comment,uint256 deadline)")); // 0xf91f3a24
bytes4 constant DATA_LIST = bytes4(keccak256("DataList(bytes[] items)")); // 0xae74f986
event Note(bytes32 reference, string comment, uint256 deadline);
function encodeNote(bytes32 ref, string calldata c, uint256 d) external pure returns (bytes memory) {
return abi.encodePacked(TRANSFER_NOTE, abi.encode(ref, c, d));
}
function handle(bytes calldata data) public {
if (data.length < 4) return;
bytes4 sel = bytes4(data[:4]);
if (sel == DATA_LIST) {
bytes[] memory items = abi.decode(data[4:], (bytes[]));
require(items.length <= 8, "too many items");
for (uint256 i = 0; i < items.length; i++) this.handle(items[i]);
} else if (sel == TRANSFER_NOTE) {
(bytes32 r, string memory c, uint256 d) = abi.decode(data[4:], (bytes32, string, uint256));
emit Note(r, c, d);
}
// unknown selectors: ignore
}
}
需要注意
- 状态为 Draft。
- 解析
DataList时要限制工作量:规范建议设置诸如最多 8 个 item、总共 8 KB 的限制,并在找到所有预期选择器后停止。 - 4 字节的选择器标识的是一个类型,而不是可信的发送者。当这很重要时,请另行认证数据(例如使用 EIP-712 签名)。
- 类型字符串必须是规范的 EIP-712 格式(逗号后不加空格,参数名完全一致),否则选择器无法匹配。