ERC-7950 · Final(最终) · ERC · 2025-05-22
ERC-7950: Encode chain id with transaction hash
一种极小的字符串格式 `chainId:txHash:tx`,将交易哈希与其链 ID 配对,使任何工具都能判断一笔交易属于哪个网络。
它解决什么问题?
仅凭一个交易哈希,并不能说明它位于哪条链上。EVM 链和 L2 众多,用户把哈希粘贴到区块浏览器、工单或聊天中时,往往要解释它是在哪个网络上发送的,或者工具必须在多条链上搜索。
ERC-7950 定义了同时携带这两部分信息的一个紧凑字符串,这样链接转发服务、钱包或区块浏览器就可以自动把它路由到正确的位置。
它如何运作
该字符串由三个以冒号分隔的部分组成:
chainId:txHash:tx
chainId是由 EIP-155 及其链 ID 注册表定义的十进制链 ID(以太坊主网为 1)。txHash是包含0x前缀的交易哈希。tx是固定的后缀,用来标明该字符串是一个交易引用,从而不会与地址或其他标识符混淆。
整个字符串不区分大小写。链 ID 放在最前面,因为路由查询需要的正是它。
规范中的示例:
1:0xc55e2b90168af6972193c1f86fa4d7d7b31a29c156665d15b9cd48618b5177ef:tx
示例代码
编码和解码(TypeScript,ethers v6):
import { JsonRpcProvider } from "ethers";
export function encodeTxRef(chainId: bigint | number, txHash: string): string {
if (!/^0x[0-9a-fA-F]{64}$/.test(txHash)) throw new Error("bad tx hash");
return `${chainId}:${txHash.toLowerCase()}:tx`;
}
export function decodeTxRef(ref: string): { chainId: bigint; txHash: string } {
const m = /^(\d+):(0x[0-9a-f]{64}):tx$/i.exec(ref.trim());
if (!m) throw new Error("not an ERC-7950 tx reference");
return { chainId: BigInt(m[1]), txHash: m[2].toLowerCase() };
}
// Look up the transaction on the right network
const { chainId, txHash } = decodeTxRef(input);
const provider = new JsonRpcProvider(rpcUrlFor(chainId)); // your chainId -> RPC map
const tx = await provider.getTransaction(txHash);
需要注意
- 状态:Final。
- 顺序是
chainId、哈希、tx;这与 CAIP-10 风格的账户 ID 布局不同,所以不要盲目复用地址解析器。 - 由于该格式不区分大小写,在比较或存储之前请统一规范为小写。
- 该标准只定义了这个字符串。钱包和区块浏览器是否接受它、如何接受它,取决于各自的服务。