当前位置:首页 > 知识 >

深入了解钱包层级,学习更高级的区块链应用技能

HD钱包优化算法决定只需提供根扩展私钥,整棵树的任意连接点的扩展私钥就可以被推算出来。

让我们一起看看如何使用JavaScript库bitcoinjs-lib来计算HD地址:

``` const bitcoin = require("bitcoinjs-lib"); let xprv = "xprv9s21ZrQH143K4EKMS3q1vbJo564QAbs98BfXQME6nk8UCrnXnv8vWg9qmtup3kTug96p5E3AvarBhPMScQDqMhEEm41rpYEdXBL8qzVZtwz", root = bitcoin.HDNode.fromBase58(xprv);

// m/0: var m_0 = root.derive(0); console.log("xprv m/0: " + m_0.toBase58()); console.log("xpub m/0: " + m_0.neutered().toBase58()); console.log("prv m/0: " + m_0.keyPair.toWIF()); console.log("pub m/0: " + m_0.keyPair.getAddress());

// m/1: var m_1 = root.derive(1); console.log("xprv m/1: " + m_1.toBase58()); console.log("xpub m/1: " + m_1.neutered().toBase58()); console.log("prv m/1: " + m_1.keyPair.toWIF()); console.log("pub m/1: " + m_1.keyPair.getAddress()); ```

注意,以“xprv”开头的字符是512位扩展私钥的Base58编号,编解码后的结果即为初始扩展私钥。

在没有“xprv”的情况下,可以立即推算出某一xpub的子公钥:

``` const bitcoin = require("bitcoinjs-lib"); let xprv = "xprv9s21ZrQH143K4EKMS3q1vbJo564QAbs98BfXQME6nk8UCrnXnv8vWg9qmtup3kTug96p5E3AvarBhPMScQDqMhEEm41rpYEdXBL8qzVZtwz", root = bitcoin.HDNode.fromBase58(xprv);

// m/0: let m_0 = root.derive(0), xprv_m_0 = m_0.toBase58(), xpub_m_0 = m_0.neutered().toBase58(); // Method 1: Get public address of m/0/99 from xprv of m/0: let pub_99a = bitcoin.HDNode.fromBase58(xprv_m_0).derive(99).getAddress();

// Method 2: Get public address of m/0/99 from xpub of m/0: let pub_99b = bitcoin.HDNode.fromBase58(xpub_m_0).derive(99).getAddress();

// Check if the two public addresses are the same: console.log(pub_99a); console.log(pub_99b); ```

但是无法从xpub推算出硬底化子公钥:

``` const bitcoin = require("bitcoinjs-lib"); let xprv = "xprv9s21ZrQH143K4EKMS3q1vbJo564QAbs98BfXQME6nk8UCrnXnv8vWg9qmtup3kTug96p5E3AvarBhPMScQDqMhEEm41rpYEdXBL8qzVZtwz", root = bitcoin.HDNode.fromBase58(xprv);

// m/0: let m_0 = root.derive(0), xprv_m_0 = m_0.toBase58(), xpub_m_0 = m_0.neutered().toBase58();

// Get public address of m/0/99 from xprv of m/0: let pub_99a = bitcoin.HDNode.fromBase58(xprv_m_0).deriveHardened(99).getAddress(); console.log(pub_99a);

// Get public address of m/0/99 from xpub of m/0: bitcoin.HDNode.fromBase58(xpub_m_0).deriveHardened(99).getAddress(); ```

BIP-44是比特币HD钱包的标准,它定义了一个简单的规范来派生私钥:

``` m / purpose' / coin_type' / account' / change / address_index ```

其中,“purpose”恒为“44”,“coin_type”由SLIP-44中定义,例如0对应BTC,2对应LTC,60对应ETH等。“account”表示客户的某个“帐户”,由客户指定的数据库索引。“change=0”表示外部买卖,“change=1”表示内部结构买卖。“address_index”是真正派生的数据库索引,从0到231。

例如,某个BTC钱包为客户创建的一组HD地址如下:

``` m/44'/0'/0'/0/0 m/44'/0'/0'/0/1 m/44'/0'/0'/0/2 m/44'/0'/0'/0/3 ... ```

如果这是以太币钱包,则其客户的HD地址如下:

``` m/44'/2'/0'/0/0 m/44'/2'/0'/0/1 m/44'/2'/0'/0/2 m/44'/2'/0'/0/3 ... ```

经过BIP-44标准化的HD钱包可以管理其它货币的全部方法。同一根扩展私钥在不同钱包上派生的一组地址完全相同。

猜你喜欢

微信二维码

微信