bitcore/docs/guide/hierarchical.md

44 lines
1.6 KiB
Markdown
Raw Normal View History

2014-12-23 07:28:31 -08:00
title: HD Keys
description: Hierarichically derived keys support, let's you create and derive extended public and private keys.
---
2014-12-19 14:23:48 -08:00
# HDKeys
2014-12-12 11:26:33 -08:00
## Hierarichically Derived Keys
2014-11-22 12:29:10 -08:00
2014-12-15 21:45:23 -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
var hdPrivateKey = new HDPrivateKey();
var retrieved = new HDPrivateKey('xpriv...');
var derived = privateKey.derive("m/0'");
var derivedByNumber = privateKey.derive(1).derive(2, true);
var derivedByArgument = privateKey.derive("m/1/2'");
assert(derivedByNumber.xprivkey === derivedByArgument.xprivkey);
var address = new Address(privateKey.publicKey, Networks.livenet);
var redeem = new Transaction().from(output).to(target, 10000).sign(derived.privateKey);
```
## 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 = privateKey.hdPublicKey;
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);
```