• Stars
    star
    201
  • Rank 194,445 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 10 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

JavaScript component for Bitcoin hierarchical deterministic keys (BIP32)

hdkey

NPM Package build status js-standard-style

A JavaScript component for BIP32(hierarchical deterministic keys).

Installation

npm i --save hdkey

Usage

example:

var HDKey = require('hdkey')
var seed = 'a0c42a9c3ac6abf2ba6a9946ae83af18f51bf1c9fa7dacc4c92513cc4dd015834341c775dcd4c0fac73547c5662d81a9e9361a0aac604a73a321bd9103bce8af'
var hdkey = HDKey.fromMasterSeed(Buffer.from(seed, 'hex'))
console.log(hdkey.privateExtendedKey)
// => 'xprv9s21ZrQH143K2SKJK9EYRW3Vsg8tWVHRS54hAJasj1eGsQXeWDHLeuu5hpLHRbeKedDJM4Wj9wHHMmuhPF8dQ3bzyup6R7qmMQ1i1FtzNEW'
console.log(hdkey.publicExtendedKey)
// => 'xpub661MyMwAqRbcEvPmRAmYndzERhyNux1GoHzHxgzVHMBFkCro3kbbCiDZZ5XabZDyXPj5mH3hktvkjhhUdCQxie5e1g4t2GuAWNbPmsSfDp2'

HDKey.fromMasterSeed(seedBuffer[, versions])

Creates an hdkey object from a master seed buffer. Accepts an optional versions object.

var seed = 'a0c42a9c3ac6abf2ba6a9946ae83af18f51bf1c9fa7dacc4c92513cc4dd015834341c775dcd4c0fac73547c5662d81a9e9361a0aac604a73a321bd9103bce8af'
var hdkey = HDKey.fromMasterSeed(Buffer.from(seed, 'hex'))

HDKey.fromExtendedKey(extendedKey[, versions, skipVerification])

Creates an hdkey object from a xprv or xpub extended key string. Accepts an optional versions object & an optional skipVerification boolean. If skipVerification is set to true, then the provided public key's x (and y if uncompressed) coordinate will not will be verified to be on the curve.

var key = 'xprvA2nrNbFZABcdryreWet9Ea4LvTJcGsqrMzxHx98MMrotbir7yrKCEXw7nadnHM8Dq38EGfSh6dqA9QWTyefMLEcBYJUuekgW4BYPJcr9E7j'
var hdkey = HDKey.fromExtendedKey(key)

or

var key = 'xpub6FnCn6nSzZAw5Tw7cgR9bi15UV96gLZhjDstkXXxvCLsUXBGXPdSnLFbdpq8p9HmGsApME5hQTZ3emM2rnY5agb9rXpVGyy3bdW6EEgAtqt'
var hdkey = HDKey.fromExtendedKey(key)

HDKey.fromJSON(obj)

Creates an hdkey object from an object created via hdkey.toJSON().


hdkey.derive(path)

Derives the hdkey at path from the current hdkey.

var seed = 'fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542'
var hdkey = HDKey.fromMasterSeed(Buffer.from(seed, 'hex'))
var childkey = hdkey.derive("m/0/2147483647'/1")

console.log(childkey.privateExtendedKey)
// -> "xprv9zFnWC6h2cLgpmSA46vutJzBcfJ8yaJGg8cX1e5StJh45BBciYTRXSd25UEPVuesF9yog62tGAQtHjXajPPdbRCHuWS6T8XA2ECKADdw4Ef"
console.log(childkey.publicExtendedKey)
// -> "xpub6DF8uhdarytz3FWdA8TvFSvvAh8dP3283MY7p2V4SeE2wyWmG5mg5EwVvmdMVCQcoNJxGoWaU9DCWh89LojfZ537wTfunKau47EL2dhHKon"

Newer, "hardened" derivation paths look like this:

// as defined by BIP-44
var childkey = hdkey.derive("m/44'/0'/0'/0/0");

hdkey.sign(hash)

Signs the buffer hash with the private key using secp256k1 and returns the signature as a buffer.

hdkey.verify(hash, signature)

Verifies that the signature is valid for hash and the hdkey's public key using secp256k1. Returns true for valid, false for invalid. Throws if the hash or signature is the wrong length.

hdkey.wipePrivateData()

Wipes all record of the private key from the hdkey instance. After calling this method, the instance will behave as if it was created via HDKey.fromExtendedKey(xpub).

hdkey.toJSON()

Serializes the hdkey to an object that can be JSON.stringify()ed.

var seed = 'fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542'
var hdkey = HDKey.fromMasterSeed(Buffer.from(seed, 'hex'))

console.log(hdkey.toJSON())
// -> {
//      xpriv: 'xprv9s21ZrQH143K31xYSDQpPDxsXRTUcvj2iNHm5NUtrGiGG5e2DtALGdso3pGz6ssrdK4PFmM8NSpSBHNqPqm55Qn3LqFtT2emdEXVYsCzC2U',
//      xpub: 'xpub661MyMwAqRbcFW31YEwpkMuc5THy2PSt5bDMsktWQcFF8syAmRUapSCGu8ED9W6oDMSgv6Zz8idoc4a6mr8BDzTJY47LJhkJ8UB7WEGuduB'
//    }

hdkey.privateKey

Getter/Setter of the hdkey's private key, stored as a buffer.

hdkey.publicKey

Getter/Setter of the hdkey's public key, stored as a buffer.

hdkey.privateExtendedKey

Getter/Setter of the hdkey's xprv, stored as a string.

hdkey.publicExtendedKey

Getter/Setter of the hdkey's xpub, stored as a string.

References

License

MIT

More Repositories

1

secp256k1-node

Node.js binding for an Optimized C library for EC operations on curve secp256k1
JavaScript
341
star
2

base-x

Encode/decode any base
JavaScript
312
star
3

bs58

Base58 encoding/decoding for Bitcoin
JavaScript
212
star
4

coinkey

JavaScript component for private keys, public keys, and addresess for crypto currencies such as Bitcoin, Litecoin, and Dogecoin
JavaScript
149
star
5

cryptocoin

JavaScript library for crypto currencies like Bitcoin and Litecoin.
JavaScript
140
star
6

coininfo

JavaScript component for crypto currency specific information.
JavaScript
116
star
7

keccak

Keccak sponge function family
C
86
star
8

p2p-node

Manage peer-to-peer communications for cryptocurrency implementations
JavaScript
61
star
9

ecurve

JavaScript component for Eliptic Curve Cryptography
JavaScript
58
star
10

awesome-cryptocoinjs

Useful crypto coins JavaScript libraries
58
star
11

bigi

JavaScript Big Integer library based upon Tom Wu's work.
JavaScript
49
star
12

sha256

DEPRECATED - JavaScript component to compute the SHA256 of strings or bytes.
JavaScript
47
star
13

scryptsy

Scrypt KDF is used for BIP38 (encryption of private keys) and proof of work for some crypto currencies.
JavaScript
45
star
14

stealth

Stealth addresses for Bitcoin and other crypto currencies.
JavaScript
42
star
15

ecdsa

DEPRECATED - JavaScript component to Eliptical Curve Cryptography signing and verify.
JavaScript
38
star
16

qr-encode

JavaScript component to encode strings into QR codes.
JavaScript
25
star
17

pbkdf2-sha256

DEPRECATED - Key derivation function primarily used for Scrypt
JavaScript
22
star
18

aes

A JavaScript component for the Advanced Encryption Standard (AES). Important for BIP38
JavaScript
21
star
19

eckey

A JavaScript component for Eliptical curve cryptography for crypto currencies such as Litecoin and Bitcoin
JavaScript
17
star
20

coinstring

DEPRECATED - Create and parse crypto currency addresses and wallet import formats.
JavaScript
16
star
21

scrypt

Scrypt for node and browser
C
14
star
22

binstring

DEPRECATED - Convert binary data to and from various string/integer representations
JavaScript
12
star
23

blake-hash

SHA-3 proposal Blake
C
12
star
24

btc-transaction

Create, parse, serialize Bitcoin transactions.
JavaScript
11
star
25

btc-address

JavaScript component to handle Bitcoin addresses.
JavaScript
10
star
26

crypto-hashing

JavaScript hashing libraries wrapped up into one module. Compatible with Node and the browser.
JavaScript
10
star
27

sha512

DEPRECATED - SHA 512 secure hashing algorithm
JavaScript
9
star
28

cryptocoinjs.com

CryptoCoinJS Docs
HTML
9
star
29

drbg.js

Deterministic Random Bit Generators from NIST SP 800-90A
JavaScript
8
star
30

btc-p2p

Bitcoin peer network manager
JavaScript
8
star
31

btc-blockchain

Bitcoin blockchain management
JavaScript
7
star
32

btc-script

Script support for Bitcoin
JavaScript
7
star
33

eip55

An EIP55 compatible address encoding library
JavaScript
7
star
34

coinmsg

Message signing for Bitcoin and other cryptocurrencies.
JavaScript
7
star
35

btc-scriptinterpreter

JavaScript
6
star
36

crypto-binary

Assemble and disassemble binary messages used in cryptocoin applications
JavaScript
5
star
37

p2p-manager

Manage peer connections to a P2P network
JavaScript
5
star
38

btc-opcode

Opcode support for Bitcoin.
JavaScript
5
star