BIP32(BitcoinImprovementProposal32)定义了一种分层确定性钱包(HierarchicalDeterministicWallet,简称HDWallet),该标准描述了一种树状结构,通过主密钥(MasterKey)生成无限个子密钥(ChildKeys)。
BIP32(Bitcoin Improvement Proposal 32)定义了一种分层确定性钱包(Hierarchical Deterministic Wallet, 简称 HD Wallet),该标准描述了一种树状结构,通过主密钥(Master Key)生成无限个子密钥(Child Keys)。这种方法解决了传统钱包需要频繁备份的问题,并提高了钱包管理的便利性和安全性。
传统的比特币钱包使用随机生成的私钥,每个地址对应独立的密钥对(私钥、公钥),存在的问题包括:
为了解决以上问题,BIP32 提出了分层确定性钱包方案。
HD钱包核心是通过 种子(Seed) 生成一个主密钥(Master Key),再通过确定性的派生算法生成树状结构的子密钥,结构清晰,便于管理与备份。
BIP32 描述了两种密钥派生:
私钥派生子私钥 (CKDpriv)
公钥派生子公钥 (CKDpub)
HD 钱包的派生路径类似文件目录结构,格式如下:
ounter(line
m / purpose' / coin_type' / account' / change / address_index
示例(BIP44 标准的比特币地址路径):
ounter(line
m/44'/0'/0'/0/0
用户随机生成助记词(Mnemonic),经过 BIP39 生成种子(Seed)。
通过种子使用 HMAC-SHA512 算法派生主密钥:
ounter(lineounter(lineounter(lineounter(lineounter(lineounter(line
Master Key = HMAC-SHA512(key="Bitcoin seed", data=Seed)
HMAC-SHA512 的结果 (512 bits) 被分为两部分:
左 256 bits 为主私钥 (master private key),记作 k。
右 256 bits 为主链码 (chain code),记作 c。
ounter(line
Master Key = (k, c)
链码用于派生子密钥,增强安全性。
子密钥派生包含两种模式:硬化派生和软化派生
可以从公钥或私钥派生子密钥
公式
ounter(lineounter(lineounter(lineounter(lineounter(lineounter(line
I = HMAC-SHA512(key=c_par, data=(K_par || index))
I_L = left 256 bits of I
I_R = right 256 bits of I
child_private_key = (I_L + k_par) mod n
child_chain_code = I_R
算法流程:
公式
ounter(lineounter(lineounter(lineounter(lineounter(lineounter(line
I = HMAC-SHA512(key=c_par, data=(K_par || index))
I_L = left 256 bits of I
I_R = right 256 bits of I
child_public_key = point(I_L) + K_par
child_chain_code = I_R
说明:
算法流程
函数 CKDpub((K par , c par ), i) → (K i , c i ) 根据父扩展公钥计算子扩展公钥。该函数仅适用于非强化子密钥。
优点:
缺点:
ounter(lineounter(lineounter(lineounter(lineounter(lineounter(line
I = HMAC-SHA512(key=c_par, data=(0x00 || k_par || index))
I_L = left 256 bits of I
I_R = right 256 bits of I
child_private_key = (I_L + k_par) mod n
child_chain_code = I_R
私钥父密钥 → 公钥子密钥算法流程
推荐实践: 硬化派生用于敏感用途,例如账户级别的密钥派生。
ounter(line
助记词 → 种子 → Master Key → 子密钥 → 地址
ounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(line
助记词:"abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon"
↓
种子(seed)
↓
主密钥(m)
↓
子密钥(m/44'/0'/0'/0/0)
↓
地址:"1LqBGSKuX5yYUonjxT5qGfpUsXKYYWeabA"
ounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(line
const bip39 = require('bip39');
const bip32 = require('bip32');
const bitcoin = require('bitcoinjs-lib');
// 生成主密钥function generateMasterNode() {
const mnemonic = bip39.generateMnemonic();
const seed = bip39.mnemonicToSeedSync(mnemonic);
const root = bip32.fromSeed(seed);
console.log('助记词:', mnemonic);
console.log('种子:', seed.toString('hex'));
return root;
}
// 非硬化派生
function deriveNonHardened(root, path) {
const node = root.derivePath(path);
console.log(`\n非硬化路径 ${path}`);
console.log('子私钥 (WIF):', node.toWIF());
console.log('子公钥:', node.publicKey.toString('hex'));
console.log('地址:', bitcoin.payments.p2pkh({ pubkey: node.publicKey }).address);
}
// 硬化派生
function deriveHardened(root, path) {
const node = root.derivePath(path);
console.log(`\n硬化路径 ${path}`);
console.log('子私钥 (WIF):', node.toWIF());
console.log('子公钥:', node.publicKey.toString('hex'));
console.log('地址:', bitcoin.payments.p2pkh({ pubkey: node.publicKey }).address);
}
// 主函数
function main() {
const root = generateMasterNode();
// 非硬化路径
deriveNonHardened(root, "m/0/1/2");
// 硬化路径
deriveHardened(root, "m/0'/1'/2'");
}
main();
BIP32 提供了一种强大的密钥管理结构,通过分层确定性设计,极大提升了加密货币钱包的安全性、便利性和易用性,现已成为加密货币钱包设计的主流标准。
掌握 BIP32 及相关标准对理解钱包底层原理、区块链产品开发有非常重要的意义。
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!