# @exodus/keychain 中文文档

规范地址：https://npmcn.com/package/%40exodus/keychain/

版本：12\.1\.0
npm 元数据更新时间：2026-09-21T20:07:20.816Z
中文译文更新时间：2026-11-14T03:53:30.149Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40exodus/keychain
原文对照：https://npmcn.com/package/%40exodus/keychain/?lang=original
项目主页：https://github.com/ExodusMovement/exodus/tree/master/libs/features/keychain
源码仓库：https://github.com/ExodusMovement/exodus

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：67 kB，20 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- 运行依赖：7 个
- 首次发布：2022-06-22T14:56:32.393Z
- 最新版本发布：2026-09-21T20:07:20.607Z
- 近 30 天下载量：43186（2026-10-14 至 2026-11-12，npm 官方下载统计）
- 已发布版本数：79（近 12 个月 8 个）

## 中文简介

设计一个单元,以更加安全地使用私人关键材料

## 安装

```sh
npm install @exodus/keychain
```

也可以使用：`pnpm add @exodus/keychain`、`yarn add @exodus/keychain`、`bun add @exodus/keychain`

## README

# `@exodus/keychain`

键链是一个模块,旨在与私人关键材料更加安全地工作. 它可以与被围住的花园相提并论,私钥不应从中逃出. 使用私人密钥的所有操作,例如签名和加密数据,都应该在模块内执行,其中 `KeyIdentifier`s用于指定用于哪个操作的密钥。 注意"应该",因为我们还没有完全在那里.

在目前状态下,这个库旨在为密码材料的工作提供一个良好的界面. 但是,它有一些安全方面的限制,这些限制已经列入我们的路线图,以便解决:

-   私人关键材料直接传递给资产库,第三方开发人员可以包含代码. 这在我们的行进图上通过重构资产库来接受签名功能而不是密钥来消除.
-   私钥 _能够_ 导出,通过 `keychain.exportKey`
-   `keychain.lock()` 不保证私密密钥完全清除内存

## 安装

```
pnpm add @exodus/keychain
```

## 使用量

见示例 [./模块/ 测试 /例. 整合-测试.js](https://npmcn.com/package/%40exodus/keychain/module/__tests__/example.integration-test.js)。 。 。 .

### 已记录的使用路径

检查 [这里](https://github.com/ExodusMovement/exodus-desktop/tree/master/src/app/_local_modules/constants/bip43#documented-usage-paths)

### 创建密钥标识符

为了与私人密钥互动,您必须首先指定其访问方式. A 类 `KeyIdentifier` 必须创造,因为资产有帮助 `KeyIdentifier` 班级将进行举重.

```js
import KeyIdentifier from '@exodus/key-identifier'

const keyId = new KeyIdentifier({
  assetName: 'solana',
  derivationAlgorithm: 'BIP32',
  derivationPath: "m/44'/501'/0'/0/0",
  keyType: 'nacl',
})
```

### 种子标识符

大多数关键链操作需要用种子标识符传递(`seedId`(a) 除一个 `KeyIdentifier`A级 `seedId` 是种子主密钥的六进制BIP32标识符(参见 [./模块/crypto/种子-id.js](https://npmcn.com/package/%40exodus/keychain/module/crypto/seed-id.js)) (中文(简体) ).

### 添加/ 删除种子

在进行键链操作之前,必须提供 `seed` 通过 `keychain.unlock(seed)`。键链持有单一种子: re-`unlock`同样的种子是天能的,解锁一个 _不同_ 种子替换当前键( 最后写成- wins) – 任何外部私钥通过 `addExternalPrivateKey` 在交换中幸存下来。 通话 `keychain.lock()` 将清除种子和任何衍生的密码材料.

```js
const seed = await mnemonicToSeed({
  mnemonic: 'menu memory fury language physical wonder dog valid smart edge decrease worth',
})

await keychain.unlock(seed)
// ...
keychain.lock()
```

### 锁定状态

键链有两种不同的生命周期原始:

-   **`unlock(seed)` 编号 : `lock()`** ——艰难的道路. `unlock` 安装生成的主密钥和 **将密钥链转换到解锁状态**· ; `lock` 从内存中擦去所有主密钥(从mnemonic-衍生出和外部)并重置锁态. 用于 `wallet.unlock` 编号 : `wallet.lock`。 。 。 .
-   **`lockPrivateKeys()` 编号 : `unlockPrivateKeys()` 编号 : `arePrivateKeysLocked()`** ——软道. 翻转一面内锁旗而无需擦去mnemonic-衍生出的主机,所以解锁不必再去掉. 说明: `lockPrivateKeys()` (单位:千美元) **硬删除** 任何外部( a)`ext:`(通过 `addExternalPrivateKey`他们不转回去 `unlockPrivateKeys()` 并且必须重新添加.

当键链被锁定时,需要私人关键材料的呼叫(`signBuffer`, (中文(简体) ), `exportKey` 与 `exportPrivate: true`,特定资产签注) `private keys are locked`. 纯公共业务`getPublicKey`, (中文(简体) ), `exportKey` 无 `exportPrivate`工作的同时锁定.

### 签名交易

使用 `keychain.signBuffer(...)` 为给定的密钥标识符签名.

```js
import { mnemonicToSeed } from '@exodus/bip39'
import keychainDefinition, { KeyIdentifier } from '..'

const keyId = new KeyIdentifier({
  assetName: 'solana',
  derivationAlgorithm: 'BIP32',
  derivationPath: "m/44'/501'/0'/0/0",
  keyType: 'nacl',
})

const DATA =
  '010001033c8939b872876416b1ba97d04c6a31211e39258a82d0fa45542a1cccc2617d2f2c2e85e395109a73ab754dfdad48d2cdefae040d4653228245df6fe6b6d24f7300000000000000000000000000000000000000000000000000000000000000004f968728ba006a647883abdd1b8eabde24e181c8bb8e769256f9a37e73b8727901020200010c02000000b4ebad0200000000'

const result = await keychain.signBuffer({
  seedId,
  keyId,
  signatureType: 'ed25519',
  data: Buffer.from(DATA, 'hex'),
})

// result.toString('hex') === '810cdc7d804dcfab90147e50c40b0afe1f9d01fa6933739032d761f7fca4226389d348d70478560845ae9e90a940ef4173e17690b9d93122aadd56fa56b8b609'
```

### 加密/解密数据

注: Libsodium术语如下: `encryptSecretBox`编号 :`encryptBox`编号 :`encryptSealedBox`。 。 。 .

#### 加密密钥/ 密钥密钥密钥密钥

```js
const ALICE_KEY = new KeyIdentifier({
  derivationAlgorithm: 'SLIP10',
  derivationPath: `m/0'/2'/0'`,
  keyType: 'nacl',
})

const sodiumEncryptor = keychain.sodium.createEncryptor({ keyId: ALICE_KEY })
const plaintext = 'I really love keychains'
const ciphertext = await sodiumEncryptor.encryptSecretBox({
  seedId,
  data: plaintext,
})

const decrypted = await sodiumEncryptor.decryptSecretBox({
  seedId,
  data: ciphertext,
})

// decrypted.toString() === plaintext
```

#### 加密框/解密框

```js
const aliceSodiumEncryptor = keychain.sodium.createEncryptor({ keyId: ALICE_KEY })
const bobSodiumEncryptor = keychain.sodium.createEncryptor({ keyId: BOB_KEY })
const plaintext = 'I really love keychains'
const {
  box: { publicKey: bobPublicKey },
} = await bobSodiumEncryptor.getSodiumKeysFromSeed({ seedId })
const ciphertext = await aliceSodiumEncryptor.encryptBox({
  seedId,
  data: plaintext,
  toPublicKey: bobPublicKey,
})
const {
  box: { publicKey: alicePublicKey },
} = await aliceSodiumEncryptor.getSodiumKeysFromSeed({ seedId })

const decrypted = await bobSodiumEncryptor.decryptBox({
  seedId,
  data: ciphertext,
  fromPublicKey: alicePublicKey,
})

// decrypted.toString() === plaintext
```

#### 加密密封框/解密密封框

```js
const aliceSodiumEncryptor = keychain.sodium.createEncryptor({ keyId: ALICE_KEY })
const bobSodiumEncryptor = keychain.sodium.createEncryptor({ keyId: BOB_KEY })
const plaintext = 'I really love keychains'
const {
  box: { publicKey: bobPublicKey },
} = await bobSodiumEncryptor.getSodiumKeysFromSeed({ seedId })

const ciphertext = await aliceSodiumEncryptor.encryptSealedBox({
  seedId,
  data: plaintext,
  toPublicKey: bobPublicKey,
})

const decrypted = await bobSodiumEncryptor.decryptSealedBox({
  seedId,
  data: ciphertext,
})

// decrypted.toString() === plaintext
```

### 导出 A 密钥

出口公共和/或私人关键材料.

```js
// { xpub, publicKey }
const publicKey = await keychain.exportKey({ seedId, keyId })
// { xpub, xpriv, publicKey, privateKey }
const privateKey = await keychain.exportKey({ seedId, keyId, exportPrivate: true })
```

### 清除键链实例

切开锁链, _减去任何密码材料_。这相当于重新引用具有相同参数的键链工厂.

### sep256k1 签名符

使用带有曲线的 ECDSA 签名缓冲 `secp256k1`。 。 。 .

```js
const keyId = new KeyIdentifier({
  derivationAlgorithm: 'SLIP10',
  derivationPath: `m/73'/2'/0'`,
  keyType: 'nacl',
})

const signer = keychain.secp256k1.createSigner({ keyId })
const plaintext = Buffer.from('I really love keychains')
const signature = await signer.signBuffer({ seedId, data: plaintext })
```

### ed25519 签名符

使用带有曲线的 EDDSA 签名缓冲器 `ed25519`。 。 。 .

```js
const keyId = new KeyIdentifier({
  derivationAlgorithm: 'SLIP10',
  derivationPath: `m/73'/2'/0'`,
  keyType: 'nacl',
})

const signer = keychain.ed25519.createSigner({ keyId })
const plaintext = Buffer.from('I really love keychains')
const signature = await signer.signBuffer({ seedId, data: plaintext })
```

## 常见问题

### @exodus/keychain 是什么？

设计一个单元,以更加安全地使用私人关键材料

### 如何安装 @exodus/keychain？

在项目目录运行 npm install @exodus/keychain，或使用 pnpm add / yarn add / bun add。

### @exodus/keychain 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @exodus/keychain 支持 ESM 还是 CommonJS？

以 ES Module 形式发布，推荐使用 import 引入。在 CommonJS 项目中，Node\.js 20\.19\+ / 22\.12\+ 可以直接 require\(\) 不含顶层 await 的 ES Module，更早的版本需要改用动态 import\(\)。

### @exodus/keychain 可以商用吗？

@exodus/keychain 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @exodus/keychain 的下载量有多少？

根据 npm 官方下载统计，@exodus/keychain 在 2026/10/14 – 2026/11/12 这 30 天内被下载 43,186 次，日均约 1,440 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @exodus/keychain 还在维护吗？

最新版本 12\.1\.0 发布于 2026/09/22（1 个月前）；至今共发布 79 个版本，其中过去 12 个月发布了 8 个；首次发布于 2022/06/22。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

