@exodus/keychain
键链是一个模块,旨在与私人关键材料更加安全地工作. 它可以与被围住的花园相提并论,私钥不应从中逃出. 使用私人密钥的所有操作,例如签名和加密数据,都应该在模块内执行,其中 KeyIdentifiers用于指定用于哪个操作的密钥。 注意"应该",因为我们还没有完全在那里.
在目前状态下,这个库旨在为密码材料的工作提供一个良好的界面. 但是,它有一些安全方面的限制,这些限制已经列入我们的路线图,以便解决:
- 私人关键材料直接传递给资产库,第三方开发人员可以包含代码. 这在我们的行进图上通过重构资产库来接受签名功能而不是密钥来消除.
- 私钥 能够 导出,通过
keychain.exportKey keychain.lock()不保证私密密钥完全清除内存
安装
pnpm add @exodus/keychain
使用量
见示例 ./模块/ 测试 /例. 整合-测试.js。 。 。 .
已记录的使用路径
检查 这里
创建密钥标识符
为了与私人密钥互动,您必须首先指定其访问方式. A 类 KeyIdentifier 必须创造,因为资产有帮助 KeyIdentifier 班级将进行举重.
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) 除一个 KeyIdentifierA级 seedId 是种子主密钥的六进制BIP32标识符(参见 ./模块/crypto/种子-id.js) (中文(简体) ).
添加/ 删除种子
在进行键链操作之前,必须提供 seed 通过 keychain.unlock(seed)。键链持有单一种子: re-unlock同样的种子是天能的,解锁一个 不同 种子替换当前键( 最后写成- wins) – 任何外部私钥通过 addExternalPrivateKey 在交换中幸存下来。 通话 keychain.lock() 将清除种子和任何衍生的密码材料.
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(...) 为给定的密钥标识符签名.
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。 。 。 .
加密密钥/ 密钥密钥密钥密钥
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
加密框/解密框
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
加密密封框/解密密封框
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 密钥
出口公共和/或私人关键材料.
// { 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。 。 。 .
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。 。 。 .
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 })