---
eip: 2335
label: ERC-2335
title: BLS12-381 Keystore
status: Review
type: Standards Track
category: ERC
created: 2019-09-30
requires: 2333, 2334
official: https://eips.ethereum.org/EIPS/eip-2335
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-2335.md
canonical: https://eips.huatuo.xyz/zh/eip-2335/
lang: zh
tldr: 以太坊验证者使用的、用密码加密的 BLS12-381 密钥 JSON 密钥库格式，由可替换的 KDF、校验和与密码模块构成，不使用 Keccak。
tags: [bls, keystore, validator, encryption, json]
---

# ERC-2335: BLS12-381 Keystore

> Keystore for storing BLS12-381 keys

**TL;DR:** 以太坊验证者使用的、用密码加密的 BLS12-381 密钥 JSON 密钥库格式，由可替换的 KDF、校验和与密码模块构成，不使用 Keccak。

## 它解决什么问题？

自己持有密钥的人需要一种安全的方式把密钥存放在磁盘上，并在设备之间转移。以太坊已经有用于 secp256k1 账户密钥的"Web3 Secret Storage"密钥库（版本 3），但它对 BLS 密钥有两个缺点：

- 它的校验和使用 **Keccak-256**。BLS 密钥和签名本应是跨链标准，而 Keccak 是以太坊特有的。
- 它是逐个功能叠加演变而来的，缺乏对其构成部分的清晰抽象。

ERC-2335 为 BLS12-381 秘密定义了一种新的密钥库（`"version": 4`）。验证者工具用它来生成 `keystore-*.json` 文件，与 [ERC-2333](/eip-2333/)（密钥派生）和 [ERC-2334](/eip-2334/)（密钥路径）配合使用。

## 它如何运作

`crypto` 部分由三个**模块**组成，每个模块都有 `function`、`params` 和 `message`：

| 模块 | 允许的函数 | 作用 |
| --- | --- | --- |
| `kdf` | `scrypt`、`pbkdf2`（HMAC-SHA256） | 把密码转换为 32 字节的解密密钥 |
| `checksum` | `sha256` | 验证密码是否正确 |
| `cipher` | `aes-128-ctr` | 在 `message` 中保存加密后的秘密 |

解密分三步：

1. **规范化密码**：转换为 NFKD，去除 C0（`0x00–0x1F`）、C1（`0x80–0x9F`）和 `0x7F` 控制码，然后进行 UTF-8 编码。空格保留。
2. **派生并校验**：运行 KDF 得到 `decryption_key`。计算 `SHA256(decryption_key[16:32] | cipher.message)` 并与 `checksum.message` 比较。不一致说明密码错误。
3. **解密**：用 `decryption_key[0:16]` 作为 AES-128-CTR 密钥，配合 `cipher.params.iv` 恢复秘密。

其他顶层字段：

- `pubkey`：公钥，使工具无需询问密码即可显示。当秘密是私钥时为必填。
- `path`：[ERC-2334](/eip-2334/) 派生路径，未知时为 `""`。
- `uuid`：随机的 RFC 4122 UUID。
- `description`：可选的人类可读标签（不保证唯一）。
- `version`：始终为 `4`，这样选择是为了避免与旧的 v3 密钥库混淆。

## 示例代码

密钥库的结构（数值已缩短；完整数值见规范的测试向量）：

```json
{
  "crypto": {
    "kdf": {
      "function": "scrypt",
      "params": { "dklen": 32, "n": 262144, "p": 1, "r": 8, "salt": "d4e5…8fa3" },
      "message": ""
    },
    "checksum": { "function": "sha256", "params": {}, "message": "d221…a484" },
    "cipher": {
      "function": "aes-128-ctr",
      "params": { "iv": "264d…4fe6" },
      "message": "06ae…e20f"
    }
  },
  "description": "validator key",
  "pubkey": "9612…0d07",
  "path": "m/12381/3600/0/0/0",
  "uuid": "1d85ae20-35c5-4611-98e8-aa14a633906f",
  "version": 4
}
```

密码校验与解密（Node.js，scrypt 情形）：

```ts
import { scryptSync, createHash, createDecipheriv } from "node:crypto";

function decrypt(ks: any, password: string): Buffer {
  const pw = Buffer.from(
    password.normalize("NFKD").replace(/[\u0000-\u001f\u007f-\u009f]/g, ""),
    "utf8",
  );
  const { n, r, p, dklen, salt } = ks.crypto.kdf.params;
  const dk = scryptSync(pw, Buffer.from(salt, "hex"), dklen, { N: n, r, p, maxmem: 512 * 1024 * 1024 });

  const cipherMsg = Buffer.from(ks.crypto.cipher.message, "hex");
  const checksum = createHash("sha256").update(Buffer.concat([dk.subarray(16, 32), cipherMsg])).digest("hex");
  if (checksum !== ks.crypto.checksum.message) throw new Error("wrong password");

  const iv = Buffer.from(ks.crypto.cipher.params.iv, "hex");
  const d = createDecipheriv("aes-128-ctr", dk.subarray(0, 16), iv);
  return Buffer.concat([d.update(cipherMsg), d.final()]); // 32-byte BLS secret key
}
```

## 需要注意

- 状态为 **Review**：验证者工具在实践中广泛使用该格式，但该 ERC 本身尚未达到 Final。
- 它有意与 v3 Web3 Secret Storage 格式**不兼容**；v3 解析器无法读取这些文件。
- 密钥库对密钥的保护强度，取决于密码和 KDF 参数。请使用强密码，不要为了节省时间而降低 scrypt/PBKDF2 的工作因子。
- 由于 `pubkey` 以明文存储，任何拿到该文件的人即使没有密码，也能看出它属于哪个验证者。


---

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