---
eip: 5169
label: ERC-5169
title: Client Script URI for Token Contracts
status: Final
type: Standards Track
category: ERC
created: 2022-05-03
requires: 20, 165, 721, 777, 1155
official: https://eips.ethereum.org/EIPS/eip-5169
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-5169.md
canonical: https://eips.huatuo.xyz/zh/eip-5169/
lang: zh
tldr: 让代币合约通过 scriptURI() 公布其官方客户端脚本的获取位置，使钱包可以直接从合约加载可信的、代币专属的功能。
tags: [token, nft, wallet, scripts, metadata, tokenscript]
---

# ERC-5169: Client Script URI for Token Contracts

> Add a scriptURI to point to an executable script associated with the functionality of the token.

**TL;DR:** 让代币合约通过 scriptURI() 公布其官方客户端脚本的获取位置，使钱包可以直接从合约加载可信的、代币专属的功能。

## 它解决什么问题？

许多代币不只是躺在钱包里：NFT 可能会打开一扇办公室的门，门票可能需要签到，合约可能有钱包不会显示的 `mint` 函数。项目通过客户端脚本或迷你 dApp 来提供这些功能。问题在于，钱包或用户如何知道哪个脚本才是某个代币的**官方**脚本，因为假脚本可能构造恶意交易。

ERC-5169 把答案锚定在代币合约本身：调用 `scriptURI()`，就能得到合约所有者认可的脚本所在位置。

## 它如何运作

```solidity
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`）：

```solidity
// 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），在获取并验证脚本之前先读取列表：

```ts
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.
```

## 需要注意

- **状态：Final。**
- `scriptURI` 告诉你的是*合约所有者*认可什么，而不是脚本是否安全。所有者密钥一旦被攻破，就可以让所有钱包指向恶意脚本，因此对待更新要像对待合约升级一样谨慎。
- 仅有 `https://` URI 不提供任何完整性保证；在执行任何内容之前，需要内容寻址存储（IPFS CID）或签名校验。
- 如果脚本调用外部服务器 API，本标准对是否应信任这些服务器没有任何规定。
- 它可以与 [ERC-20](/eip-20/)、[ERC-721](/eip-721/)、[ERC-777](/eip-777/) 和 [ERC-1155](/eip-1155/) 代币配合使用，而不改变它们的行为。


---

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