---
eip: 1186
label: EIP-1186
title: RPC-Method to get Merkle Proofs - eth_getProof
status: Stagnant
type: Standards Track
category: Interface
created: 2018-06-24
requires: 1474
official: https://eips.ethereum.org/EIPS/eip-1186
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1186.md
canonical: https://eips.huatuo.xyz/zh/eip-1186/
lang: zh
tldr: 定义 eth_getProof JSON-RPC 方法，返回账户状态及 Merkle 证明，使持有可信状态根的任何人都无需信任节点即可验证。
tags: [json-rpc, merkle, proof, light-client, state]
---

# EIP-1186: RPC-Method to get Merkle Proofs - eth_getProof

> One of the great features of Ethereum is the fact, that you can verify all data of the state. But in order to allow verification of accounts outside the client, we need an additional function delivering us the required proof. These proofs are important to secure Layer2-Technologies.

**TL;DR:** 定义 eth_getProof JSON-RPC 方法，返回账户状态及 Merkle 证明，使持有可信状态根的任何人都无需信任节点即可验证。

## 它解决什么问题？

`eth_getBalance` 或 `eth_getStorageAt` 这类普通 RPC 调用会返回一个值，并要求你信任节点。以太坊的状态存放在 Merkle Patricia trie 中，因此每个值原本*都可以*对照区块头中的 `stateRoot` 来证明，但标准 RPC 没有提供获取这种证明的方法。

EIP-1186 新增了 `eth_getProof`。有了它，手机应用、IoT 设备、跨链桥或 Layer 2 只要知道一个可信的区块头，就可以向不受信任的节点索取数据，并自行检查返回的答案。

## 它如何运作

**参数**

1. 账户地址（20 字节）。
2. 要证明的存储键数组（每个 32 字节）。
3. 区块号，或 `"latest"` 之类的标签。

**结果**

| 字段 | 含义 |
| --- | --- |
| `balance`、`nonce` | 账户的值 |
| `codeHash` | 账户代码的哈希 |
| `storageHash` | 账户存储 trie 的根 |
| `accountProof` | 沿着 `keccak256(address)` 从 `stateRoot` 向下到该账户的 RLP 编码 trie 节点 |
| `storageProof` | 对每个请求的键：`key`、`value`，以及沿着 `keccak256(key)` 从 `storageHash` 向下的 `proof` 节点 |

验证时，取区块的 `stateRoot`，沿 `accountProof` 走一遍以还原账户记录 `[nonce, balance, storageHash, codeHash]`，再从该 `storageHash` 出发沿每条存储证明走一遍。

证明同样适用于**不存在**的值：证明沿路径走到尽头，并显示它终止于一个空的分支槽，或终止于另一条路径的叶子。

## 示例代码

用 ethers v6 请求证明：

```ts
import { JsonRpcProvider, zeroPadValue } from "ethers";

const provider = new JsonRpcProvider(process.env.RPC_URL);

const slot0 = zeroPadValue("0x00", 32);
const proof = await provider.send("eth_getProof", [
  "0x7F0d15C7FAae65896648C8273B6d7E43f58Fa842", // account
  [slot0],                                       // storage keys
  "latest",
]);

console.log(proof.balance, proof.nonce, proof.storageHash);
console.log("account proof nodes:", proof.accountProof.length);
console.log("slot 0 =", proof.storageProof[0].value);
```

原始 JSON-RPC 请求：

```json
{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "eth_getProof",
  "params": ["0x7F0d15C7FAae65896648C8273B6d7E43f58Fa842", ["0x0000000000000000000000000000000000000000000000000000000000000000"], "latest"]
}
```

要真正完成验证，请把 `accountProof` 和区块头的 `stateRoot` 交给 Merkle Patricia trie 证明验证器（例如你所用语言的 trie 库中的验证器）。

## 需要注意

- **状态：作为 EIP 是 Stagnant**，但 `eth_getProof` 已被执行层客户端广泛实现，并被跨链桥、轻客户端和存储证明工具所使用。
- 证明的可信程度取决于你用来核对它的区块头。请从你信任的来源获取 `stateRoot`，而不要从同一个节点获取。
- 对于没有代码的账户，`codeHash` 是空字节的哈希，即 `0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470`。
- 许多节点只保留近期状态，因此获取旧区块的证明可能需要归档节点。
- 规范曾建议把区块哈希参数作为一项可能的改进；[EIP-1898](/eip-1898/) 为读取状态的 RPC 方法普遍加入了这种区块标识符。


---

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