---
eip: 6865
label: ERC-6865
title: On-Chain EIP-712 Visualization
status: Draft
type: Standards Track
category: ERC
created: 2023-04-10
requires: 712
official: https://eips.ethereum.org/EIPS/eip-6865
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-6865.md
canonical: https://eips.huatuo.xyz/zh/eip-6865/
lang: zh
tldr: 让将要使用某个 EIP-712 签名的合约自己来解释它：通过一个 view 函数返回用户将交出哪些资产、收到哪些资产以及持续多长时间，使钱包能在签名前展示清晰的摘要。
tags: [eip712, signatures, wallets, security, ux]
---

# ERC-6865: On-Chain EIP-712 Visualization

> Visualize structured data highlighting the potential consequences for users' assets

**TL;DR:** 让将要使用某个 EIP-712 签名的合约自己来解释它：通过一个 view 函数返回用户将交出哪些资产、收到哪些资产以及持续多长时间，使钱包能在签名前展示清晰的摘要。

## 它解决什么问题？

[EIP-712](/eip-712/) 类型化签名支撑着免 gas 授权、NFT 市场挂单和许多 DeFi 订单。钱包展示的是原始的类型化字段，大多数用户看不懂：一个看似无害的"Order"，可能会把有价值的 NFT 白白送出去。钓鱼网站正是利用了这一点。

钱包可以尝试在链下解码每个协议，但这无法扩展（协议太多）、容易出错（误读逻辑比什么都不显示更糟），而且协议一升级就会过时。

ERC-6865 把这项工作转交给协议：验证签名的合约同时公开一个函数，用来描述该签名会对用户的资产产生什么影响。

## 它如何运作

EIP-712 domain 中指定的 `verifyingContract` 实现：

```solidity
function visualizeEIP712Message(bytes memory encodedMessage, bytes32 domainHash)
    external view returns (Result memory);
```

- `encodedMessage` 是用 `abi.encode` 进行 ABI 编码后的类型化消息。
- `domainHash` 是 EIP-712 的 domain separator 哈希，合约可借此拒绝它不支持的版本或协议。

它返回：

```solidity
struct Liveness { uint256 from; uint256 to; }               // validity window (timestamps)
struct UserAssetMovement {
    address assetTokenAddress;  // ERC-20/721/1155 address; address(0) = native ETH
    uint256 id;                 // NFT id (ignored for fungibles)
    uint256[] amounts;          // amount at `from` ... amount at `to`
}
struct Result {
    UserAssetMovement[] assetsIn;   // minimum the user receives
    UserAssetMovement[] assetsOut;  // maximum the user gives
    Liveness liveness;
}
```

`amounts` 是数组，这样就能表达随时间变化的价格（例如荷兰式拍卖）：第一项是 `liveness.from` 时的数量，最后一项是 `liveness.to` 时的数量。通常它只有一项。

当被请求签名（`eth_signTypedData`）时，钱包会在 domain 中的 `chainId` 和 `verifyingContract` 上调用该函数，并展示"你交出 X，至少收到 Y，有效期至 Z"。如果 domain 缺少这些字段，钱包应忽略此提案。

## 示例代码

钱包侧的调用（ethers v6）：

```ts
import { AbiCoder, Contract, JsonRpcProvider, TypedDataEncoder } from "ethers";

const abi = [
  "function visualizeEIP712Message(bytes encodedMessage, bytes32 domainHash) view returns " +
  "((address assetTokenAddress,uint256 id,uint256[] amounts)[] assetsIn," +
  " (address assetTokenAddress,uint256 id,uint256[] amounts)[] assetsOut," +
  " (uint256 from,uint256 to) liveness)",
];

async function preview(rpcUrl: string, domain: any, encodedMessage: string) {
  const provider = new JsonRpcProvider(rpcUrl);
  const verifier = new Contract(domain.verifyingContract, abi, provider);
  const domainHash = TypedDataEncoder.hashDomain(domain);

  const r = await verifier.visualizeEIP712Message(encodedMessage, domainHash);
  for (const a of r.assetsOut) console.log("You give:", a.assetTokenAddress, a.id, a.amounts);
  for (const a of r.assetsIn)  console.log("You get :", a.assetTokenAddress, a.id, a.amounts);
  console.log("Valid:", new Date(Number(r.liveness.from) * 1000), "→", new Date(Number(r.liveness.to) * 1000));
}
// encodedMessage is built per protocol with AbiCoder.defaultAbiCoder().encode([...types], [...values])
```

## 需要注意

- **状态：Draft。** 接口仍可能变化，不应假定钱包已提供支持。
- 结果的可信度取决于 `verifyingContract`。恶意合约可以返回让人放心但虚假的摘要，因此钱包应结合合约的声誉来权衡，不能把它当作安全的证明。
- 已经部署且不可升级的协议无法添加该函数；规范提到未来可能为它们设立一个注册表。
- [EIP-5267](/eip-5267/)（获取合约的 EIP-712 domain）是对钱包而言的互补构件。


---

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