HDNode stands for Hierarchically Deterministic node which can be used to create a HD wallet.
Create HDNode from Seed Buffer.
- rootSeed
Buffer
: root seed buffer - network
string
optional: network. default: 'mainnet'
HDNode HDNode
// create mnemonic
let mnemonic = bitbox.Mnemonic.generate(128);
// create seed buffer from mnemonic
let seedBuffer = bitbox.Mnemonic.toSeed(mnemonic);
// create HDNode from seed buffer
bitbox.HDNode.fromSeed(seedBuffer);
// generate entropy
let entropy = bitbox.Crypto.randomBytes(32);
// create mnemonic from entropy
let mnemonic = bitbox.Mnemonic.fromEntropy(entropy);
// create seed buffer from mnemonic
let seedBuffer = bitbox.Mnemonic.toSeed(mnemonic);
// create HDNode from seed buffer
bitbox.HDNode.fromSeed(seedBuffer);
Derive non hardened child HDNode
- hdnode
HDNode
- num
number
: number of child node. Ex: 0
HDNode HDNode
// create mnemonic
let mnemonic = bitbox.Mnemonic.generate(128);
// create seed buffer from mnemonic
let seedBuffer = bitbox.Mnemonic.toSeed(mnemonic);
// create HDNode from seed buffer
let hdNode = bitbox.HDNode.fromSeed(seedBuffer);
// derive unhardened child HDNode
bitbox.HDNode.derive(hdNode, 0);
Derive hardened child HDNode
- hdnode
HDNode
- num
number
: number of child node. Ex: 0
HDNode HDNode
// create mnemonic
let mnemonic = bitbox.Mnemonic.generate(128);
// create seed buffer from mnemonic
let seedBuffer = bitbox.Mnemonic.toSeed(mnemonic);
// create HDNode from seed buffer
let hdNode = bitbox.HDNode.fromSeed(seedBuffer);
// derive hardened child HDNode
bitbox.HDNode.deriveHardened(hdNode, 0);
Derive child HDNode from path
- hdnode
HDNode
- path
string
: path of child hd node. Ex: "m/44'/145'/0'"
HDNode HDNode
// create mnemonic
let mnemonic = bitbox.Mnemonic.generate(128);
// create seed buffer from mnemonic
let seedBuffer = bitbox.Mnemonic.toSeed(mnemonic);
// create HDNode from seed buffer
let hdNode = bitbox.HDNode.fromSeed(seedBuffer);
// derive hardened child HDNode
bitbox.HDNode.derivePath(hdNode, "m/44'/145'/0'");
Get legacy address of HDNode
- hdnode
HDNode
legacyAddress string
: legacy base58check encoded address of HDNode
// create mnemonic
let mnemonic = bitbox.Mnemonic.generate(128);
// create seed buffer from mnemonic
let seedBuffer = bitbox.Mnemonic.toSeed(mnemonic);
// create HDNode from seed buffer
let hdNode = bitbox.HDNode.fromSeed(seedBuffer);
// to legacy address
bitbox.HDNode.toLegacyAddress(hdNode);
// 14apxtw2LDQmXWsS5k4JEhG93Jzjswhvma
// generate entropy
let entropy = bitbox.Crypto.randomBytes(32);
// create mnemonic from entropy
let mnemonic = bitbox.Mnemonic.fromEntropy(entropy);
// create seed buffer from mnemonic
let seedBuffer = bitbox.Mnemonic.toSeed(mnemonic);
// create HDNode from seed buffer
let hdNode = bitbox.HDNode.fromSeed(seedBuffer);
// to cash address
bitbox.HDNode.toLegacyAddress(hdNode);
// 14mVsq3H5Ep2Jb6AqoKsmY1BFHKCBGPDLi
Get cash address of HDNode
- hdnode
HDNode
- regtest
boolean
optional: return regtest address
cashAddress string
: cashaddr encoded address of HDNode
// create mnemonic
let mnemonic = bitbox.Mnemonic.generate(128);
// create seed buffer from mnemonic
let seedBuffer = bitbox.Mnemonic.toSeed(mnemonic);
// create HDNode from seed buffer
let hdNode = bitbox.HDNode.fromSeed(seedBuffer);
// to cash address
bitbox.HDNode.toCashAddress(hdNode);
// bitcoincash:qqrz6kqw6nvhwgwrt4g7fggepvewtkr7nukkeqf4rw
// generate entropy
let entropy = bitbox.Crypto.randomBytes(32);
// create mnemonic from entropy
let mnemonic = bitbox.Mnemonic.fromEntropy(entropy);
// create seed buffer from mnemonic
let seedBuffer = bitbox.Mnemonic.toSeed(mnemonic);
// create HDNode from seed buffer
let hdNode = bitbox.HDNode.fromSeed(seedBuffer);
// to cash address
bitbox.HDNode.toCashAddress(hdNode);
// bitcoincash:qq549jxsjv66kw0smdju4es2axnk7hhe9cquhjg4gt
Get private key in wallet import format (WIF) of HDNode
- hdnode
HDNode
privateKeyWIF string
: private key in wallet import format (WIF) of HDNode
// create mnemonic
let mnemonic = bitbox.Mnemonic.generate(128);
// create seed buffer from mnemonic
let seedBuffer = bitbox.Mnemonic.toSeed(mnemonic);
// create HDNode from seed buffer
let hdNode = bitbox.HDNode.fromSeed(seedBuffer);
// to WIF
bitbox.HDNode.toWIF(hdNode);
// L5E8QjFnLukp8BuF4uu9gmvvSrbafioURGdBve5tA3Eq5ptzbMCJ
// generate entropy
let entropy = bitbox.Crypto.randomBytes(32);
// create mnemonic from entropy
let mnemonic = bitbox.Mnemonic.fromEntropy(entropy);
// create seed buffer from mnemonic
let seedBuffer = bitbox.Mnemonic.toSeed(mnemonic);
// create HDNode from seed buffer
let hdNode = bitbox.HDNode.fromSeed(seedBuffer);
// to WIF
bitbox.HDNode.toWIF(hdNode);
// KwobPFhv3AuXc3ps6YtWfMVRpLBDBA7jnJddurfELTyTNcFhZYpJ
Get extended public key of HDNode
- hdnode
HDNode
xpub string
: extended public key of HDNode
// create mnemonic
let mnemonic = bitbox.Mnemonic.generate(128);
// create seed buffer from mnemonic
let seedBuffer = bitbox.Mnemonic.toSeed(mnemonic);
// create HDNode from seed buffer
let hdNode = bitbox.HDNode.fromSeed(seedBuffer);
// to extended public key
bitbox.HDNode.toXPub(hdNode);
// xpub661MyMwAqRbcG4CnhNYoK1r1TKLwQQ1UdC3LHoWFK61rsnzh7Hx35qQ9Z53ucYcE5WvA7GEDXhqqKjSY2e6Y8n7WNVLYHpXCuuX945VPuYn
// generate entropy
let entropy = bitbox.Crypto.randomBytes(32);
// create mnemonic from entropy
let mnemonic = bitbox.Mnemonic.fromEntropy(entropy);
// create seed buffer from mnemonic
let seedBuffer = bitbox.Mnemonic.toSeed(mnemonic);
// create HDNode from seed buffer
let hdNode = bitbox.HDNode.fromSeed(seedBuffer);
// to extended public key
bitbox.HDNode.toXPub(hdNode);
// xpub661MyMwAqRbcFuMLeHkSbTNwNHG9MQyrAZqV1Q4MEAsmj9MYa5sxg8WC2LKqW6EHviHVucBjWi1n38juZpDDeX3U6YrsMeACdcNSTHkM8BQ
Get extended private key of HDNode
- hdnode
HDNode
xpriv string
: extended private key of HDNode
// create mnemonic
let mnemonic = bitbox.Mnemonic.generate(128);
// create seed buffer from mnemonic
let seedBuffer = bitbox.Mnemonic.toSeed(mnemonic);
// create HDNode from seed buffer
let hdNode = bitbox.HDNode.fromSeed(seedBuffer);
// to extended private key
bitbox.HDNode.toXPriv(hdNode);
// xprv9s21ZrQH143K2eMCcbT4qwwRhw6qZaPaEDWB792bnrxQZPoP2JUk4kfEx9eeV1uGTAWAfCqYr4wDWo52qALiukizKwQzvEyNR1fWZJi97Kv
// generate entropy
let entropy = bitbox.Crypto.randomBytes(32);
// create mnemonic from entropy
let mnemonic = bitbox.Mnemonic.fromEntropy(entropy);
// create seed buffer from mnemonic
let seedBuffer = bitbox.Mnemonic.toSeed(mnemonic);
// create HDNode from seed buffer
let hdNode = bitbox.HDNode.fromSeed(seedBuffer);
// to extended private key
bitbox.HDNode.toXPriv(hdNode);
// xprv9s21ZrQH143K2b5GPP6zHz22E6LeCgQXJtwNbC3MA3Kz7Se7tveKo96EhqwFtSkYWkyenVcMqM7uq35PcUNG8cUdpsJEgwKG3dvfP7TmL3v
Get the ECPair of an HDNode
- hdnode
HDNode
keyPair ECPair
: ECPair of an HDNode
// create mnemonic
let mnemonic = bitbox.Mnemonic.generate(128);
// create root seed buffer from mnemonic
let rootSeed= bitbox.Mnemonic.toSeed(mnemonic);
// create HDNode from root seed
let hdNode = bitbox.HDNode.fromSeed(rootSeed);
// create public key buffer from HDNode
bitbox.HDNode.toKeyPair(hdNode);
// generate entropy
let entropy = bitbox.Crypto.randomBytes(32);
// create mnemonic from entropy
let mnemonic = bitbox.Mnemonic.fromEntropy(entropy);
// create seed buffer from mnemonic
let seedBuffer = bitbox.Mnemonic.toSeed(mnemonic);
// create HDNode from seed buffer
let hdNode = bitbox.HDNode.fromSeed(seedBuffer);
// create public key buffer from HDNode
bitbox.HDNode.toKeyPair(hdNode);
Get the public key of an HDNode as a buffer
- hdnode
HDNode
publicKeyBuffer Buffer
: public key of HDNode as a buffer
// create mnemonic
let mnemonic = bitbox.Mnemonic.generate(128);
// create root seed buffer from mnemonic
let rootSeed= bitbox.Mnemonic.toSeed(mnemonic);
// create HDNode from root seed
let hdNode = bitbox.HDNode.fromSeed(rootSeed);
// create public key buffer from HDNode
bitbox.HDNode.toPublicKey(hdNode);
// <Buffer 03 86 d6 d3 db ec 1a 93 8c 2c a2 63 c9 79 8f eb e9 16 09 c5 a2 9b 07 65 c4 79 1f d9 0f fa 4d 27 20>
// generate entropy
let entropy = bitbox.Crypto.randomBytes(32);
// create mnemonic from entropy
let mnemonic = bitbox.Mnemonic.fromEntropy(entropy);
// create seed buffer from mnemonic
let seedBuffer = bitbox.Mnemonic.toSeed(mnemonic);
// create HDNode from seed buffer
let hdNode = bitbox.HDNode.fromSeed(seedBuffer);
// create public key buffer from HDNode
bitbox.HDNode.toPublicKey(hdNode);
// <Buffer 02 d2 26 74 6e 78 03 ac 11 e0 96 c6 24 de e8 dd 62 52 e7 8e 51 56 8a c1 18 62 aa 2a 72 50 1d ea 7d>
hash160 of Node’s public key. The same value you would see in a scriptPubKey.
- hdnode
HDNode
identifier Buffer
// mainnet
let xpub = 'xpub6DWfGUo4cjC8oWmgZdpyFMH6v3oeyADfdUPhsehzn5jX44zpazivha3JxUtkcCvBEB1c6DGaiUmpyz2m1DRfGDEVZ5VxLLW2UNEbZ5iTRvi';
let node = bitbox.HDNode.fromXPub(xpub);
bitbox.HDNode.toIdentifier(node);
// <Buffer cd d4 84 1d 2e 96 bf bf f7 9c d1 f4 a6 75 22 1c 7f 67 88 9c>
// the same as if we hash160ed it's publicKey
let publicKeyBuffer = bitbox.HDNode.toPublicKey(node);
bitbox.Crypto.hash160(publicKeyBuffer);
// <Buffer cd d4 84 1d 2e 96 bf bf f7 9c d1 f4 a6 75 22 1c 7f 67 88 9c>
// testnet
let xpub = 'tpubDCxmZ3qLVVphg6NpsnAjQFqDPwr9HYqSgoAcUYAfqSgo32dL6NA8QXqWsS6XTjoGggohZKvujsAv2F2ugej9qfUYau2jSUB4JaYnfMsx3MJ';
let node = bitbox.HDNode.fromXPub(xpub);
bitbox.HDNode.toIdentifier(node);
// <Buffer e1 8e 20 e3 f8 f1 c0 53 e6 1f 9e 3a 58 8e 71 f5 0b 8d 2d c4>
// the same as if we hash160ed it's publicKey
let publicKeyBuffer = bitbox.HDNode.toPublicKey(node);
bitbox.Crypto.hash160(publicKeyBuffer);
// <Buffer e1 8e 20 e3 f8 f1 c0 53 e6 1f 9e 3a 58 8e 71 f5 0b 8d 2d c4>
Generate HDNode from extended private key
- xpriv
string
: extended private key
hdNode HDNode
// mainnet xpriv
bitbox.HDNode.fromXPriv('xprv9s21ZrQH143K2b5GPP6zHz22E6LeCgQXJtwNbC3MA3Kz7Se7tveKo96EhqwFtSkYWkyenVcMqM7uq35PcUNG8cUdpsJEgwKG3dvfP7TmL3v');
// testnet xpriv
bitbox.HDNode.fromXPriv('tprv8gQ3zr1F5pRHMebqqhorrorYNvUG3XkcZjSWVs2cEtRwwJy1TRhgRx4XcF8dYHM2eyTbTCcdKYNhqgyBQphxwRoVyVKr9zuyoA8WxNDRvom');
Generate HDNode from extended public key
- xpub
string
: extended public key
hdNode HDNode
// mainnet xpub
bitbox.HDNode.fromXPub('xpub661MyMwAqRbcFuMLeHkSbTNwNHG9MQyrAZqV1Q4MEAsmj9MYa5sxg8WC2LKqW6EHviHVucBjWi1n38juZpDDeX3U6YrsMeACdcNSTHkM8BQ');
// testnet xpub
bitbox.HDNode.fromXPub('tpubDD669G3VEC6xF7ddjMUTGDWewwzCCrwX933HnP4ufAELmoDn5pXGcSgPnLodjFvWQwRXkG94f77BatEDA8dfQ99yy97kRYynUpNLENEqTBo');
Check if an HDNode can only derive public keys and children
- hdnode
HDNode
isPublic boolean
// mainnet xpub
let xpub = 'xpub6DWfGUo4cjC8oWmgZdpyFMH6v3oeyADfdUPhsehzn5jX44zpazivha3JxUtkcCvBEB1c6DGaiUmpyz2m1DRfGDEVZ5VxLLW2UNEbZ5iTRvi';
let node = bitbox.HDNode.fromXPub(xpub);
bitbox.HDNode.isPublic(node);
// true
// mainnet xpriv
let xpriv = 'xprv9ys4cvcoU8RoxqkZ7Fgt33te4LPHgcsKwyoZYVorkzp9uonWxWgP9wiSQhPeBUqVHbdAyov4Yi55RywBkDfZKdJFRqA51Anz6v72zGaMGZp';
let node = bitbox.HDNode.fromXPriv(xpriv);
bitbox.HDNode.isPublic(node);
// false
// testnet xpub
let xpub = 'tpubDCxmZ3qLVVphg6NpsnAjQFqDPwr9HYqSgoAcUYAfqSgo32dL6NA8QXqWsS6XTjoGggohZKvujsAv2F2ugej9qfUYau2jSUB4JaYnfMsx3MJ';
let node = bitbox.HDNode.fromXPub(xpub);
bitbox.HDNode.isPublic(node);
// true
// testnet xpriv
let xpriv = 'tprv8ggxJ8SG5EdqakzVUeLa9Gr7sqCdEcJPUNDmtdJscNxfmxoXvU36ZguiUWukJVEWEixAUr8pJabJkCt33wzxFQA587gqN51Lxdxx97zAzuG';
let node = bitbox.HDNode.fromXPriv(xpriv);
bitbox.HDNode.isPublic(node);
// false
Check if an HDNode can derive both public and private keys and children
- hdnode
HDNode
isPrivate boolean
// mainnet xpub
let xpub = 'xpub6DWfGUo4cjC8oWmgZdpyFMH6v3oeyADfdUPhsehzn5jX44zpazivha3JxUtkcCvBEB1c6DGaiUmpyz2m1DRfGDEVZ5VxLLW2UNEbZ5iTRvi';
let node = bitbox.HDNode.fromXPub(xpub);
bitbox.HDNode.isPrivate(node);
// false
// mainnet xpriv
let xpriv = 'xprv9ys4cvcoU8RoxqkZ7Fgt33te4LPHgcsKwyoZYVorkzp9uonWxWgP9wiSQhPeBUqVHbdAyov4Yi55RywBkDfZKdJFRqA51Anz6v72zGaMGZp';
let node = bitbox.HDNode.fromXPriv(xpriv);
bitbox.HDNode.isPrivate(node);
// true
// testnet xpub
let xpub = 'tpubDCxmZ3qLVVphg6NpsnAjQFqDPwr9HYqSgoAcUYAfqSgo32dL6NA8QXqWsS6XTjoGggohZKvujsAv2F2ugej9qfUYau2jSUB4JaYnfMsx3MJ';
let node = bitbox.HDNode.fromXPub(xpub);
bitbox.HDNode.isPrivate(node);
// false
// testnet xpriv
let xpriv = 'tprv8ggxJ8SG5EdqakzVUeLa9Gr7sqCdEcJPUNDmtdJscNxfmxoXvU36ZguiUWukJVEWEixAUr8pJabJkCt33wzxFQA587gqN51Lxdxx97zAzuG';
let node = bitbox.HDNode.fromXPriv(xpriv);
bitbox.HDNode.isPrivate(node);
// true
Sign 32 byte hash encoded as a buffer
- hdnode
HDNode
- buffer
Buffer
signature ECSignature
// mainnet xpriv
let xpriv = 'xprv9z2uWrGjbYPxc728rvtMi4jt4SudRiSfYn6Tdif5XN17pJ1NTbHoHK6JePkPLY1NHXLaQcA6sWudpZDm7DwKhbsGQieAp9wx46Wbio4iXg9';
// hdnode from xpriv
let hdnode = bitbox.HDNode.fromXPriv(xpriv);
// 32 byte buffer
let buf = Buffer.from(bitbox.Crypto.sha256('EARTH'), 'hex');
// sign
bitbox.HDNode.sign(hdnode, buf);
// testnet xpriv
let xpriv = 'tprv8ggxJ8SG5EdqakzVUeLa9Gr7sqCdEcJPUNDmtdJscNxfmxoXvU36ZguiUWukJVEWEixAUr8pJabJkCt33wzxFQA587gqN51Lxdxx97zAzuG';
// hdnode from xpriv
let hdnode = bitbox.HDNode.fromXPriv(xpriv);
// 32 byte buffer
let buf = Buffer.from(bitbox.Crypto.sha256('EARTH'), 'hex');
// sign
bitbox.HDNode.sign(hdnode, buf);
Verify signed 32 byte hash encoded as a buffer
- hdnode
HDNode
- buffer
Buffer
- signature
ECSignature
verified boolean
// mainnet xprivs
let xpriv1 = 'xprv9ys4cvcoU8RoqvzxGj886r4Ey3w1WfVNYH8sMnVPVzyQtaPPM6Q8pHm3D9WPWvEupGEgcJ1xLaGaZDcvKfoAurE2AzHRRRup5FuHzDr8n15';
let xpriv2 = 'xprv9ys4cvcoU8RoxqkZ7Fgt33te4LPHgcsKwyoZYVorkzp9uonWxWgP9wiSQhPeBUqVHbdAyov4Yi55RywBkDfZKdJFRqA51Anz6v72zGaMGZp';
// hdnodes from xprivs
let hdnode1 = bitbox.HDNode.fromXPriv(xpriv1);
let hdnode2 = bitbox.HDNode.fromXPriv(xpriv2);
// 32 byte buffer
let buf = Buffer.from(bitbox.Crypto.sha256('EARTH'), 'hex');
// sign
let signature = bitbox.HDNode.sign(hdnode1, buf);
// verify
bitbox.HDNode.verify(hdnode1, buf, signature);
// true
bitbox.HDNode.verify(hdnode2, buf, signature);
// false
// testnet xprivs
let xpriv1 = 'tprv8ggxJ8SG5EdqakzVUeLa9Gr7sqCdEcJPUNDmtdJscNxfmxoXvU36ZguiUWukJVEWEixAUr8pJabJkCt33wzxFQA587gqN51Lxdxx97zAzuG';
let xpriv2 = 'tprv8ggxJ8SG5EdqiM6Dn63QwHScQ7HS5hXqUMxSD1NEbDyPw6VtoUMFZBAohpTMsPz9cYbpHELmA4Zm79NKRvEvFdhWRX2bSmu7V7PiNb364nv';
// hdnodes from xprivs
let hdnode1 = bitbox.HDNode.fromXPriv(xpriv1);
let hdnode2 = bitbox.HDNode.fromXPriv(xpriv2);
// 32 byte buffer
let buf = Buffer.from(bitbox.Crypto.sha256('EARTH'), 'hex');
// sign
let signature = bitbox.ECPair.sign(hdnode1, buf);
// verify
bitbox.HDNode.verify(hdnode1, buf, signature);
// true
bitbox.HDNode.verify(hdnode2, buf, signature);
// false
Create BIP32 account. Has getChainAddress
and nextChainAddress
helper methods.
- hdNodes
HDNode[]
account: Object
// create mnemonic
let mnemonic = bitbox.Mnemonic.generate(128);
// create root seed buffer
let rootSeedBuffer = bitbox.Mnemonic.toSeed(mnemonic);
// create master hd node
let masterHDNode = bitbox.HDNode.fromSeed(rootSeedBuffer);
// derive child node
let childNode = masterHDNode.derivePath("m/44'/145'/0'/0");
// create account
let account = bitbox.HDNode.createAccount([childNode]);
Returns the latest address from account
- account:
number
address: string
// create mnemonic
let mnemonic = bitbox.Mnemonic.generate(128);
// create root seed buffer
let rootSeedBuffer = bitbox.Mnemonic.toSeed(mnemonic);
// create master hd node
let masterHDNode = bitbox.HDNode.fromSeed(rootSeedBuffer);
// derive child node
let childNode = masterHDNode.derivePath("m/44'/145'/0'/0");
// create account
let account = bitbox.HDNode.createAccount([childNode]);
account.getChainAddress(0)
// 1EsibxXqzxzcor7eS34dSGrZp1kb3nQFFr
Moves the most recent address forward for account
- account:
number
address: string
// create mnemonic
let mnemonic = bitbox.Mnemonic.generate(128);
// create root seed buffer
let rootSeedBuffer = bitbox.Mnemonic.toSeed(mnemonic);
// create master hd node
let masterHDNode = bitbox.HDNode.fromSeed(rootSeedBuffer);
// derive child node
let childNode = masterHDNode.derivePath("m/44'/145'/0'/0");
// create account
let account = bitbox.HDNode.createAccount([childNode]);
account.getChainAddress(0)
// 1EsibxXqzxzcor7eS34dSGrZp1kb3nQFFr
account.nextChainAddress(0)
account.getChainAddress(0)
// 1JBApcAMD3riimPxABg2pNvwLumP9DEpxD