bitcore/docs/hierarchical.md

51 lines
1.7 KiB
Markdown
Raw Normal View History

---
2014-12-23 07:35:06 -08:00
title: HDKeys
description: Lets you create and derive extended public and private keys according to the BIP32 standard for Hierarchical Deterministic (HD) keys.
2014-12-23 07:28:31 -08:00
---
2014-12-19 14:23:48 -08:00
# HDKeys
2014-12-12 11:26:33 -08:00
## Hierarchically Derived Keys
2014-11-22 12:29:10 -08:00
Bitcore provides full support for [BIP32](https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki), allowing for many key management schemas that benefit from this property. Please be sure to read and understand the basic concepts and the warnings on that BIP before using these classes.
2014-11-22 12:29:10 -08:00
## HDPrivateKey
2014-12-21 07:21:11 -08:00
An instance of a [PrivateKey](privatekey.md) that also contains information required to derive child keys.
2014-12-15 21:45:23 -08:00
Sample usage:
```javascript
2015-05-11 11:30:58 -07:00
var bitcore = require('bitcore');
var HDPrivateKey = bitcore.HDPrivateKey;
2014-12-15 21:45:23 -08:00
var hdPrivateKey = new HDPrivateKey();
var retrieved = new HDPrivateKey('xpriv...');
2015-05-11 11:30:58 -07:00
var derived = hdPrivateKey.derive("m/0'");
2015-01-22 10:54:05 -08:00
var derivedByNumber = hdPrivateKey.derive(1).derive(2, true);
var derivedByArgument = hdPrivateKey.derive("m/1/2'");
2014-12-15 21:45:23 -08:00
assert(derivedByNumber.xprivkey === derivedByArgument.xprivkey);
2015-05-11 11:30:58 -07:00
var address = derived.privateKey.toAddress();
2015-01-22 10:54:05 -08:00
// obtain HDPublicKey
var hdPublicKey = hdPrivateKey.hdPublicKey;
2014-12-15 21:45:23 -08:00
```
## HDPublicKey
An instance of a PublicKey that can be derived to build extended public keys. Note that hardened paths are not available when deriving an HDPublicKey.
```javascript
var hdPrivateKey = new HDPrivateKey();
var hdPublicKey = hdPrivateKey.hdPublicKey;
2014-12-15 21:45:23 -08:00
try {
new HDPublicKey();
} catch(e) {
console.log("Can't generate a public key without a private key");
}
var address = new Address(hdPublicKey.publicKey, Networks.livenet);
var derivedAddress = new Address(hdPublicKey.derive(100).publicKey, Networks.testnet);
```