---
eip: 7871
label: ERC-7871
title: Wallet Signing API
status: Draft
type: Standards Track
category: ERC
created: 2024-01-29
requires: 191, 712, 5792
official: https://eips.ethereum.org/EIPS/eip-7871
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-7871.md
canonical: https://eips.huatuo.xyz/zh/eip-7871/
lang: zh
tldr: 提议一个统一的 wallet_sign JSON-RPC 方法，用 EIP-5792 风格的 capabilities 涵盖所有 EIP-191 签名类型（personal_sign、EIP-712 类型化数据、validator 数据）。
tags: [wallet, json-rpc, signatures, eip-712, draft]
---

# ERC-7871: Wallet Signing API

> Adds a JSON-RPC method for requesting a signature from a wallet

**TL;DR:** 提议一个统一的 wallet_sign JSON-RPC 方法，用 EIP-5792 风格的 capabilities 涵盖所有 EIP-191 签名类型（personal_sign、EIP-712 类型化数据、validator 数据）。

## 它解决什么问题？

如今，dapp 请求链下签名时必须在多个 RPC 方法之间选择：`personal_sign`、`eth_sign`、`eth_signTypedData_v4` 及其变体。每个钱包和库支持的集合都略有不同，每出现一种新的签名格式，就又需要一个新方法。

ERC-7871 把它们整合成一个方法 `wallet_sign`。被签署数据的类型是一个参数，而不是方法名，所以未来的 [EIP-191](/eip-191/) `signed_data` 版本可以在不增加新 RPC 方法的情况下加入。它还借用了 [EIP-5792](/eip-5792/) 的 `capabilities` 机制，让钱包可以声明并接受额外的签名功能。

## 它如何运作

`wallet_sign` 接受：

- `version`：`wallet_sign` 接口本身的版本（例如 `"1.0"`）。
- `address`（可选）：如果提供，钱包必须恰好用该地址签名。
- `request.type`：一字节的 EIP-191 版本。
- `request.data`：该版本对应的载荷。
- `capabilities`（可选）：EIP-5792 风格的功能标志，可通过 `wallet_getCapabilities` 发现。

为现有 EIP-191 版本定义的请求结构：

| `type` | 含义 | `data` |
| --- | --- | --- |
| `0x00` | 带指定验证者的数据 | `{ validator, data }` |
| `0x01` | [EIP-712](/eip-712/) 类型化数据 | 完整的类型化数据对象 |
| `0x45` | Personal sign | `{ message }`（UTF-8 字符串） |

返回结果是 `{ signature, capabilities? }`。新的 `signed_data` 版本预计会在各自的 ERC 中定义其请求结构。

## 示例代码

通过 [EIP-1193](/eip-1193/) provider 调用 `wallet_sign`（TypeScript）：

```ts
const provider = (window as any).ethereum;

// Personal-sign style message
const { signature } = await provider.request({
  method: "wallet_sign",
  params: [{
    version: "1.0",
    request: { type: "0x45", data: { message: "Hello world" } },
  }],
});

// EIP-712 typed data from a specific account
const typed = await provider.request({
  method: "wallet_sign",
  params: [{
    version: "1.0",
    address: account,
    request: {
      type: "0x01",
      data: {
        domain: { name: "Example", version: "1", chainId: 1 },
        types: { Mail: [{ name: "contents", type: "string" }] },
        primaryType: "Mail",
        message: { contents: "hi" },
      },
    },
  }],
});
```

## 需要注意

- **状态：Draft。** Rationale、Backwards Compatibility 和 Security Considerations 部分仍然是空的，接口也可能变化。
- 目前还不是可以依赖的替代方案：请检查钱包的支持情况（例如通过 `wallet_getCapabilities`），并回退到 `personal_sign` / `eth_signTypedData_v4`。
- 规范没有明确把参数包在数组里；上面的数组形式遵循通常的 JSON-RPC 惯例，不同钱包可能有所差异。
- 签名仍是标准的 EIP-191 / EIP-712 签名；链上或链下的验证方式不变。


---

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