---
eip: 2645
label: ERC-2645
title: Hierarchical Deterministic Wallet for Layer-2
status: Stagnant
type: Standards Track
category: ERC
created: 2020-05-13
official: https://eips.ethereum.org/EIPS/eip-2645
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-2645.md
canonical: https://eips.huatuo.xyz/zh/eip-2645/
lang: zh
tldr: 一种 BIP-32/44 风格的派生路径，用于非 secp256k1 曲线的 Layer-2 密钥（如 STARK 密钥）：从与以太坊钱包相同的种子派生，并与你的 L1 地址绑定。
tags: [wallet, keys, layer-2, hd-wallet, starkex]
---

# ERC-2645: Hierarchical Deterministic Wallet for Layer-2

> In the context of Computation Integrity Proof (CIP) Layer-2 solutions such as ZK-Rollups, users are required to sign messages on new elliptic curves optimized for those environments.

**TL;DR:** 一种 BIP-32/44 风格的派生路径，用于非 secp256k1 曲线的 Layer-2 密钥（如 STARK 密钥）：从与以太坊钱包相同的种子派生，并与你的 L1 地址绑定。

## 它解决什么问题？

ZK-rollup 之类的有效性证明 Layer-2 系统，常常要求用户使用在证明内部验证成本低的椭圆曲线密钥签名，而不是以太坊的 secp256k1。用户已经为以太坊钱包备份了一份 BIP-39 助记词；要求他们为每个 L2 再管理一个互不相关的秘密，既不方便又有风险。

ERC-2645 定义了一种标准方式，利用经过充分检验的 BIP-32/BIP-44 机制，从现有种子派生出这些 L2 私钥，并在不同 L2 技术和应用之间做到清晰隔离。这样，丢失 L2 密钥就归结为丢失 L1 钱包，而用户已经知道如何保护它。

## 它如何运作

派生路径（兼容 BIP-43）为：

```
m / purpose' / layer' / application' / eth_address_1' / eth_address_2' / index
```

| 层级 | 取值 |
| --- | --- |
| `purpose` | `2645`（即 EIP 编号） |
| `layer` | `sha256(layerName)` 的低 31 位，例如 `"starkex"` → `579218131` |
| `application` | `sha256(appName)` 的低 31 位，例如 `"starkexdvf"` → `1393043894` |
| `eth_address_1` | 用户以太坊地址的低 31 位 |
| `eth_address_2` | 地址的接下来 31 位 |
| `index` | 允许每个地址有多个密钥（非硬化） |

因此，对于地址 `0x00…00`、DeversiFi 和索引 0，路径为 `m/2645'/579218131'/1393043894'/0'/0'/0`。

BIP-32 生成的是 256 位的 secp256k1 密钥，但 L2 曲线的阶 `n` 不同。为了把它映射成均匀分布的密钥，规范使用了 **grinding**：把 BIP-32 密钥与计数器一起哈希，直到结果小于 256 位内能容纳的 `n` 的最大倍数，然后对 `n` 取模。这样可以避免简单的 `key % n` 引入的偏差。规范指出每一轮失败的概率低于 2⁻⁵。

## 示例代码

用 Python 构造路径并进行 grinding（BIP-32 这一步交给库来完成）：

```python
import hashlib

MASK31 = (1 << 31) - 1

def low31(name: str) -> int:
    return int.from_bytes(hashlib.sha256(name.encode()).digest(), "big") & MASK31

def eip2645_path(layer: str, app: str, eth_address: str, index: int = 0) -> str:
    addr = int(eth_address, 16)
    a1 = addr & MASK31
    a2 = (addr >> 31) & MASK31
    return f"m/2645'/{low31(layer)}'/{low31(app)}'/{a1}'/{a2}'/{index}"

def grind_key(root_key: bytes, curve_order: int) -> int:
    N = 2 ** 256
    limit = N - (N % curve_order)
    i = 0
    while True:
        # spec: key = sha256(root_key | i)
        k = int.from_bytes(hashlib.sha256(root_key + i.to_bytes(1, "big")).digest(), "big")
        if k < limit:
            return k % curve_order
        i += 1

print(eip2645_path("starkex", "starkexdvf", "0x" + "00" * 20))
# m/2645'/579218131'/1393043894'/0'/0'/0
```

## 需要注意

- **状态：Stagnant（停滞）。** 该 ERC 没有推进到 Final；请逐个确认各钱包或 L2 是否遵循这个确切的路径。
- 规范把 grinding 的输入写成 `root_key|i`，但没有规定 `i` 如何编码；各实现必须在这个细节上保持一致（上面的代码片段假定为单个字节）。
- 密钥与某一个 L1 地址绑定。使用不同的 L1 账户会得到完全不同的 L2 密钥。
- 对于以太坊验证者使用的 BLS 密钥，另有一套方案：[EIP-2333](/eip-2333/)。


---

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