---
eip: 725
label: ERC-725
title: General data key/value store and execution
status: Draft
type: Standards Track
category: ERC
created: 2017-10-02
requires: 165, 173
official: https://eips.ethereum.org/EIPS/eip-725
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-725.md
canonical: https://eips.huatuo.xyz/zh/eip-725/
lang: zh
tldr: 智能合约账户的两个构件：ERC725X 是通用的仅所有者可调用的执行函数，ERC725Y 是以 bytes32 为键、用于可升级元数据的数据存储。
tags: [account, smart-wallet, identity, key-value, draft]
---

# ERC-725: General data key/value store and execution

> An interface for a smart contract based account with attachable data key/value store

**TL;DR:** 智能合约账户的两个构件：ERC725X 是通用的仅所有者可调用的执行函数，ERC725Y 是以 bytes32 为键、用于可升级元数据的数据存储。

## 它解决什么问题？

智能合约可以充当账户（它可以持有资产并调用其他合约），也可以携带关于自身的信息（资料、权限、链接）。在 ERC-725 之前，每个项目都为这两件事发明了自己的函数名，因此钱包和 dapp 无法以统一的方式对待这些合约。

ERC-725 定义了两个小而可分离的接口：

- **ERC725X**："代表我执行操作"。一个可以调用、部署或委托的函数。
- **ERC725Y**："存储关于我的任何信息"。一个键为 `bytes32`、值为原始 `bytes` 的键值存储。

两者合在一起构成灵活的合约账户；单独使用时，ERC725Y 可以为代币、NFT 或任何其他合约添加可升级的元数据。

## 它如何运作

**所有权。** 合约有一个所有者（EOA 或另一个合约），遵循 [ERC-173](/eip-173/)（`owner()`、`transferOwnership()`）。只有所有者可以执行操作或写入数据。

**ERC725X**（接口 ID `0x7545acac`）：

| `operationType` | 动作 |
| --- | --- |
| `0` | `CALL` |
| `1` | `CREATE` |
| `2` | `CREATE2`（salt 附加在 `data` 后面，见 [EIP-1014](/eip-1014/)） |
| `3` | `STATICCALL` |
| `4` | `DELEGATECALL`（危险） |

`execute(operationType, target, value, data)` 运行一个操作，`executeBatch(...)` 运行多个操作。事件 `Executed` 和 `ContractCreated` 记录发生了什么。

**ERC725Y**（接口 ID `0x629aa694`）：`getData(key)`、`getDataBatch(keys)`、`setData(key, value)`、`setDataBatch(keys, values)`，以及 `DataChanged` 事件。键通常是描述性名称的哈希，例如 `keccak256("MyStandardKey")`，这样不同标准之间不会冲突。

两个接口都必须通过 [ERC-165](/eip-165/) 声明。

## 示例代码

使用 ERC-725 合约（Solidity）：

```solidity
interface IERC725X {
    function execute(uint256 operationType, address target, uint256 value, bytes calldata data)
        external payable returns (bytes memory);
}
interface IERC725Y {
    function getData(bytes32 dataKey) external view returns (bytes memory);
    function setData(bytes32 dataKey, bytes calldata dataValue) external;
}

contract Owner {
    function demo(address account, address token, address to) external {
        // Send 100 token units from the account (operation 0 = CALL)
        IERC725X(account).execute(
            0, token, 0,
            abi.encodeWithSignature("transfer(address,uint256)", to, 100)
        );

        // Attach a profile pointer to the account
        bytes32 key = keccak256("ProfileURI");
        IERC725Y(account).setData(key, bytes("ipfs://..."));
    }
}
```

在链下读取数据（ethers v6）：

```ts
import { Contract, keccak256, toUtf8Bytes, toUtf8String } from "ethers";

const acct = new Contract(addr, ["function getData(bytes32) view returns (bytes)"], provider);
const raw = await acct.getData(keccak256(toUtf8Bytes("ProfileURI")));
console.log(toUtf8String(raw));
```

## 需要注意

- **该提案仍处于 Draft 状态。** 接口多年来有所变化：旧版本使用重载的 `getData(bytes32[])` 和 `execute(...[])`，它们在 2023 年被重命名为 `...Batch` 函数。请通过合约的 ERC-165 接口 ID 来确认它实现的是哪个版本。
- `DELEGATECALL`（操作 `4`）会在账户自身的存储中运行外部代码。它可能覆盖所有者或数据，或使账户自毁。只应委托给你完全信任的代码。
- 通用执行意味着重入攻击和调用链攻击需要格外小心。
- 基于 ERC-725 构建的完整账户配置文件在 EIP 流程之外被标准化为 `LSP0-ERC725Account`，其中 `LSP2` 定义了带类型的数据键。
- 关于使用更广泛的智能账户路线，请参阅 [ERC-4337](/eip-4337/)。关于由 NFT 拥有的账户，请参阅 [ERC-6551](/eip-6551/)。


---

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