---
eip: 2566
label: EIP-2566
title: Human Readable Parameters for Contract Function Execution
status: Stagnant
type: Standards Track
category: Interface
created: 2020-03-23
official: https://eips.ethereum.org/EIPS/eip-2566
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-2566.md
canonical: https://eips.huatuo.xyz/zh/eip-2566/
lang: zh
tldr: 提出一个新的 RPC 方法 eth_sendTransactionToContractFunction，在 calldata 之外同时发送函数的 ABI，让钱包能向用户准确展示他们批准的是哪个函数和哪些参数。
tags: [json-rpc, wallet, abi, ux, security]
---

# EIP-2566: Human Readable Parameters for Contract Function Execution

> New Ethereum RPC method ethsendTransactionToContractFunction that parallels ethsendTransaction but allows for human-readable contract function execution data to be displayed to users.

**TL;DR:** 提出一个新的 RPC 方法 eth_sendTransactionToContractFunction，在 calldata 之外同时发送函数的 ABI，让钱包能向用户准确展示他们批准的是哪个函数和哪些参数。

## 它解决什么问题？

当 dapp 请求钱包调用合约时，使用的是带有 `data` 字段的 `eth_sendTransaction`：一个 4 字节的函数选择器（签名的 keccak256 哈希的前几个字节），后面跟着 ABI 编码的参数。这段数据人类无法阅读，选择器也无法反推出函数名，所以钱包往往无法告诉用户到底在签署什么。

这个缺口很危险。恶意或被攻破的 dapp 可以，比如，替换代币 `transfer(address,uint256)` 调用中的收款人，而只瞥一眼十六进制数据的用户永远不会发现。

## 它如何运作

该提案新增一个镜像 `eth_sendTransaction` 的 JSON-RPC 方法，并增加一个字段：

- `eth_sendTransactionToContractFunction` 接受通常的交易对象（`from`、`to`、`gas`、`gasPrice`、`value`、`data`、`nonce`）**外加 `abi`**，即被调用函数的 ABI 片段。
- 钱包用 `abi` 中的类型解码 `data` 里的参数，并在确认界面显示函数名、参数名和参数值。
- 钱包还可以根据 ABI 片段重新计算选择器，并检查它是否与 `data` 的前四个字节一致，这样 dapp 就不能给不同的 calldata 附上误导性的 ABI。
- 执行不变：`data` 与 `eth_sendTransaction` 会发送的完全一样，该方法返回交易哈希。

之所以选择新增方法而不是修改 `eth_sendTransaction`，是为了让现有代码继续可用（[EIP-712](/eip-712/) 增加 `eth_signTypedData` 时也是同样的考虑）。

## 示例代码

dapp 通过 [EIP-1193](/eip-1193/) provider 发送的请求（TypeScript，使用 ethers v6 编码）：

```ts
import { Interface } from "ethers";

const fragment = {
  type: "function",
  name: "transfer",
  stateMutability: "nonpayable",
  inputs: [
    { name: "to", type: "address" },
    { name: "value", type: "uint256" },
  ],
  outputs: [{ name: "", type: "bool" }],
};

const iface = new Interface([fragment]);
const data = iface.encodeFunctionData("transfer", [recipient, 10n ** 18n]);

const txHash = await window.ethereum.request({
  method: "eth_sendTransactionToContractFunction",
  params: [{ from: account, to: tokenAddress, data, abi: JSON.stringify(fragment) }],
});
```

钱包端检查 ABI 与 calldata 是否匹配：

```ts
const iface = new Interface([JSON.parse(params.abi)]);
const parsed = iface.parseTransaction({ data: params.data }); // null if selector mismatch
if (!parsed) throw new Error("ABI does not match calldata");
console.log(parsed.name, parsed.args); // show to the user
```

## 需要注意

- **状态：Stagnant。** 该方法不属于标准的以太坊 JSON-RPC API，因此不要指望钱包支持它。
- 实际上钱包用另一种方式解决了同样的问题：用从已验证源码数据库或签名注册表获取的 ABI 解码 calldata，并在签名前模拟交易。
- 正如规范自己所指出的，可读的函数名并不等于安全。合约可以把函数命名为 `transfer` 却做任何事；这只能显示正在调用什么，而不是代码实际做了什么。


---

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