ERC-5169 · Final(最终) · ERC · 2022-05-03
ERC-5169: Client Script URI for Token Contracts
让代币合约通过 scriptURI() 公布其官方客户端脚本的获取位置,使钱包可以直接从合约加载可信的、代币专属的功能。
它解决什么问题?
许多代币不只是躺在钱包里:NFT 可能会打开一扇办公室的门,门票可能需要签到,合约可能有钱包不会显示的 mint 函数。项目通过客户端脚本或迷你 dApp 来提供这些功能。问题在于,钱包或用户如何知道哪个脚本才是某个代币的官方脚本,因为假脚本可能构造恶意交易。
ERC-5169 把答案锚定在代币合约本身:调用 scriptURI(),就能得到合约所有者认可的脚本所在位置。
它如何运作
interface IERC5169 {
event ScriptUpdate(string[] newScriptURI);
function scriptURI() external view returns (string[] memory);
function setScriptURI(string[] memory newScriptURI) external;
}
scriptURI()返回一个 URI 数组(任何 RFC 3986 URI:IPFS、GitHub gist、云存储……),因此同一个脚本可以同时镜像到中心化和去中心化的托管位置。- 合约保存一个
owner,设置为部署者;只有该 owner 可以调用setScriptURI。 - 每次更新都会触发
ScriptUpdate,因此钱包可以刷新缓存的脚本。 - 脚本的格式没有固定。例如有"TokenScript"、针对单一代币精简的 dApp,或硬件钱包扩展。
- 完整性由客户端负责: 任何使用该脚本的人都必须检查它位于不可变的位置、URI 中包含脚本的哈希,或者它带有符合作者配套提案"Authenticity for Client Script"的签名。
把脚本存放在链下,可以避免高昂的存储费用,也避免每次调整脚本都频繁使用管理员密钥。
示例代码
合约侧(OpenZeppelin v5 Ownable):
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
interface IERC5169 {
event ScriptUpdate(string[] newScriptURI);
function scriptURI() external view returns (string[] memory);
function setScriptURI(string[] memory newScriptURI) external;
}
contract ScriptedToken is IERC5169, Ownable {
string[] private _scriptURI;
constructor() Ownable(msg.sender) {}
function scriptURI() external view returns (string[] memory) {
return _scriptURI;
}
function setScriptURI(string[] memory newScriptURI) external onlyOwner {
_scriptURI = newScriptURI;
emit ScriptUpdate(newScriptURI);
}
}
钱包侧(ethers v6),在获取并验证脚本之前先读取列表:
import { Contract } from "ethers";
const token = new Contract(tokenAddress,
["function scriptURI() view returns (string[])"], provider);
const uris: string[] = await token.scriptURI();
// e.g. ["ipfs://<cid>", "https://example.com/token.tsml"]
// Prefer content-addressed URIs; otherwise verify a hash or signature before running.