---
eip: 7739
label: ERC-7739
title: Readable Typed Signatures for Smart Accounts
status: Draft
type: Standards Track
category: ERC
created: 2024-05-28
requires: 191, 712, 1271, 5267
official: https://eips.ethereum.org/EIPS/eip-7739
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-7739.md
canonical: https://eips.huatuo.xyz/zh/eip-7739/
lang: zh
tldr: 为 ERC-1271 智能账户设计的防御性重新哈希方案，防止同一所有者的签名在其多个账户之间被重放，同时让被签署的内容在支持 EIP-712 的钱包中仍然可读。
tags: [signatures, smart-accounts, eip-712, erc-1271, security, draft]
---

# ERC-7739: Readable Typed Signatures for Smart Accounts

> A defensive rehashing scheme which prevents signature replays across smart accounts and preserves the readability of the signed contents

**TL;DR:** 为 ERC-1271 智能账户设计的防御性重新哈希方案，防止同一所有者的签名在其多个账户之间被重放，同时让被签署的内容在支持 EIP-712 的钱包中仍然可读。

## 它解决什么问题？

智能账户通过 [ERC-1271](/eip-1271/) 的 `isValidSignature(hash, signature)` 验证签名。朴素的实现只检查账户所有者是否对 `hash` 签了名。如果同一个 EOA 拥有多个智能账户，而应用的 `hash` 不包含账户地址（实践中很常见，规范举的例子是 Permit2），那么为账户 A 生成的签名对账户 B 同样有效。这就是重放攻击。

账户可以通过重新哈希来自保：在验证签名之前，把自己的地址和链 ID 混入哈希。但如果做得不好，钱包最终会要求用户签署一个不透明的 32 字节哈希，这有钓鱼风险。ERC-7739 定义了一种由嵌套的 [EIP-712](/eip-712/) 结构体构成的重新哈希方案，使钱包仍能显示原始的、人类可读的消息。

## 它如何运作

`isValidSignature` 保持原有签名，但会运行两种工作流之一。

**TypedDataSign**（应用的哈希是 EIP-712）。用户在*应用的*域下签署一个包装结构体：

```
TypedDataSign(<Contents> contents, string name, string version,
              uint256 chainId, address verifyingContract, bytes32 salt)<ContentsType>
```

其中 name/version/chainId/verifyingContract/salt 字段来自*账户的* [ERC-5267](/eip-5267/) `eip712Domain()`。由于原始结构体是嵌套字段，钱包可以正常显示它；又因为账户地址包含在内，所以该签名只对这个账户有效。

传到链上的签名按如下方式打包：

```
originalSignature ‖ APP_DOMAIN_SEPARATOR ‖ contentsHash ‖ contentsDescription ‖ uint16(len)
```

账户首先检查 `hash == keccak256(0x1901 ‖ APP_DOMAIN_SEPARATOR ‖ contentsHash)`，然后重建包装哈希，并据此验证签名。`contentsName` 应当经过清理（非空、不以小写字母或 `(` 开头、不含 `, )` 或 NUL），以防范类型名注入手法。

**PersonalSign**（应用的哈希是 [EIP-191](/eip-191/) 个人消息）。用户在*账户的*域下签署 `PersonalSign(bytes prefixed)`，其中 `prefixed` 设为原始哈希。签名中不附加任何额外数据。

如果附加的数据能重建出 `hash`，账户必须使用 TypedDataSign；否则使用 PersonalSign。对于链下调用，或者 `hash` 已经承诺了账户地址的情况，可以跳过重新哈希。

**检测：** `isValidSignature(0x7739…7739, "")` 应返回 `0x77390001`。

## 示例代码

为智能账户构造 TypedDataSign 签名（ethers v6）：

```ts
import { TypedDataEncoder, solidityPacked, toUtf8Bytes } from "ethers";

const appDomain = { name: "MailApp", version: "1", chainId: 1, verifyingContract: appAddr };
const mailTypes = { Mail: [{ name: "from", type: "address" }, { name: "to", type: "address" },
                           { name: "message", type: "string" }] };
const mail = { from: alice, to: bob, message: "hi" };

// Account's own domain, read from eip712Domain() (ERC-5267)
const [, name, version, chainId, verifyingContract, salt] = await account.eip712Domain();

const types = {
  TypedDataSign: [
    { name: "contents", type: "Mail" }, { name: "name", type: "string" },
    { name: "version", type: "string" }, { name: "chainId", type: "uint256" },
    { name: "verifyingContract", type: "address" }, { name: "salt", type: "bytes32" },
  ],
  ...mailTypes,
};
const sig = await owner.signTypedData(appDomain, types,
  { contents: mail, name, version, chainId, verifyingContract, salt });

const contentsType = "Mail(address from,address to,string message)";
const wrapped = solidityPacked(
  ["bytes", "bytes32", "bytes32", "bytes", "uint16"],
  [sig, TypedDataEncoder.hashDomain(appDomain),
   TypedDataEncoder.hashStruct("Mail", mailTypes, mail),
   toUtf8Bytes(contentsType), toUtf8Bytes(contentsType).length],
);
// The app then calls account.isValidSignature(TypedDataEncoder.hash(appDomain, mailTypes, mail), wrapped)
```

## 需要注意

- **状态：Draft。** 诸如支持标识魔数之类的细节仍可能变化（早期草案使用返回 `0xd620c85a` 的 `supportsNestedTypedDataSign()`）。
- 使用该方案的账户，其签名者本身不能再使用同一方案；嵌套的 `TypedDataSign` 类型无法在一次 EIP-712 请求中表达。
- 规范的参考实现是刻意做成功能完备的。不要以为最小化的版本就是安全的；ERC-1271 就犯过这个错误。
- 应用合约保持不变，只有签名请求被包装。规范指出，即使钱包或前端没有更新，也可以通过注入客户端 JavaScript 的方式工作。


---

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