---
eip: 712
label: EIP-712
title: Typed structured data hashing and signing
status: Final
type: Standards Track
category: Interface
created: 2017-09-12
requires: 155, 191
official: https://eips.ethereum.org/EIPS/eip-712
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-712.md
canonical: https://eips.huatuo.xyz/zh/eip-712/
lang: zh
tldr: 对类型化的结构化数据进行哈希和签名的标准方法，让钱包可以向用户展示可读的字段而不是一串十六进制，合约也能低成本地验证签名。
tags: [signatures, wallet, offchain, permit, interface]
---

# EIP-712: Typed structured data hashing and signing

> A procedure for hashing and signing of typed structured data as opposed to just bytestrings.

**TL;DR:** 对类型化的结构化数据进行哈希和签名的标准方法，让钱包可以向用户展示可读的字段而不是一串十六进制，合约也能低成本地验证签名。

## 它解决什么问题？

链下签名可以节省 gas。你先签署一条消息，之后由别人把它提交给合约。免 gas 授权、订单簿和元交易都依赖于此。但用普通的 `eth_sign` 时，用户只能看到一串看不懂的十六进制，不知道自己在授权什么。每个应用也各自用临时拼凑的方式对数据做哈希，很容易出错。

EIP-712 定义了一种经过同行评审的结构化数据编码方式。钱包可以展示各个字段（“Permit：spender X，value 100，deadline …”），某个应用或某条链上的签名也无法在另一处被重复使用。

## 它如何运作

最终要签名的摘要是：

```
keccak256("\x19\x01" ‖ domainSeparator ‖ hashStruct(message))
```

`0x19 0x01` 前缀遵循 [ERC-191](/eip-191/)，永远不会被误认为是 RLP 交易。

- **`hashStruct(s) = keccak256(typeHash ‖ encodeData(s))`**
- **`typeHash`** 是类型字符串的 `keccak256`，例如 `Mail(address from,address to,string contents)`。被引用的结构体类型会按名称排序后追加在后面。
- **`encodeData`** 把每个成员编码为 32 字节。原子类型采用 ABI 风格。`string` 和 `bytes` 会被哈希。数组会被哈希。嵌套结构体用各自的 `hashStruct` 编码。
- **`domainSeparator`** 是 `EIP712Domain` 的 `hashStruct`，可包含以下任意字段：`name`、`version`、`chainId`（[EIP-155](/eip-155/)）、`verifyingContract`、`salt`。它把签名绑定到某一个应用、版本、链和合约。

RPC 方法是 `eth_signTypedData`，参数为 `{ types, primaryType, domain, message }`。

## 示例代码

用 ethers v6 签名并验证：

```ts
import { Wallet, verifyTypedData } from "ethers";

const wallet = Wallet.createRandom();

const domain = {
  name: "Ether Mail",
  version: "1",
  chainId: 1,
  verifyingContract: "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC",
};
const types = {
  Person: [{ name: "name", type: "string" }, { name: "wallet", type: "address" }],
  Mail: [
    { name: "from", type: "Person" },
    { name: "to", type: "Person" },
    { name: "contents", type: "string" },
  ],
};
const message = {
  from: { name: "Cow", wallet: "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826" },
  to: { name: "Bob", wallet: "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB" },
  contents: "Hello, Bob!",
};

const sig = await wallet.signTypedData(domain, types, message);
console.log(verifyTypedData(domain, types, message, sig) === wallet.address); // true
```

在链上，Solidity 计算出相同的摘要：

```solidity
bytes32 constant MAIL_TYPEHASH = keccak256("Mail(Person from,Person to,string contents)Person(string name,address wallet)");
bytes32 digest = keccak256(abi.encodePacked("\x19\x01", DOMAIN_SEPARATOR, structHash));
address signer = ecrecover(digest, v, r, s);
```

## 需要注意

- **没有内置重放保护。** 请在结构体中加入 nonce 和 deadline，并在链上标记为已使用。
- 钱包通常把它暴露为 `eth_signTypedData_v4`。更早的“v1/v3”变体在处理数组和嵌套结构体的方式上有所不同。
- 著名的使用者包括 [ERC-2612](/eip-2612/) 的 `permit` 和许多 DEX 的订单格式。智能合约钱包用 [ERC-1271](/eip-1271/) 验证这些签名。[ERC-5267](/eip-5267/) 让合约可以公布自己的 domain。
- 类型化数据签名是钓鱼的惯用手段，因为一个已签名的 `Permit` 就能让代币被转空。签名前务必看清内容。
- 请使用经过审计的辅助库（OpenZeppelin 的 `EIP712`），不要自己手写编码。该 EIP 状态为 Final。


---

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