HD Public Keys (Bip32)

Hierarchical Deterministic Keys(HD Keys) form a key component of how one manages Public keys within a Bitcoin Wallet. If you are not familiar with HD Keys, be sure to read more about them in the Developer Guide Appendix


import org.twostack.bitcoin4j.crypto.*

var seedBytes = HEX.decode("000102030405060708090a0b0c0d0e0f");

//deterministically derive a master private key from a seed value
val rootKey: DeterministicKey = HDKeyDerivation.createMasterPrivateKey(seedBytes)

//initialise a deterministic hierarchy
val dh = DeterministicHierarchy(rootKey)

//create a hardened path to a node
val pathOneString = "m/44H/0H/0H/6"
val pathOne = HDPath.parsePath(pathOneString)

//retrieve deterministic private key at specified path,
//specifying that the path is relative, and to create key if it does not exist
val dkOne: DeterministicKey = dh[pathOne.subList(0, pathOne.size), true, true]

//export our deterministic key to WiF,
//and wrap using with a PrivateKey instance
val pkOne : PrivateKey = PrivateKey.fromWIF(dkOne.getPrivateKeyAsWiF(NetworkType.MAIN))

//since *dkOne* is also an ECKey object, we can grab
// the public key bytes from it and wrap in a PublicKey() instance
val pubKey = PublicKey.fromBytes(dkOne.pubKey)
On this page