Git Product home page Git Product logo

hdkey's Introduction

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

hdkey's People

Contributors

alcuadrado avatar axic avatar braydonf avatar dcousens avatar ddushkin avatar fanatid avatar homura avatar jprichardson avatar junderw avatar kewde avatar lukechilds avatar mattgstevens avatar nitowa avatar ryanzim avatar twksos avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

hdkey's Issues

What is the case for publicKeyTweakAdd ?

hd.publicKey = Buffer.from(secp256k1.publicKeyTweakAdd(Buffer.from(this.publicKey), IL, true))

In what case would the private key be null here? Is there a case where one wants to derive a bunch of public addresses without having the private key?
Thanks in advance

Peer Dependency issue

Error: Unable to resolve module assert from F:\Projects\PRIVI\Artwork-dapp\node_modules\hdkey\lib\hdkey.js: assert could not be found within the project or in these directories: node_modules

hdkey.fromPrivateKey

I wanted to create a pull request as I've found the need for a feature that I think shouldn't be too difficult to implement. That need being to create an hdkey object from a private key. Currently only a fromExtendedPrivateKey function exists and a setter for privateKey exists that does most of the cryptography that is needed.

However I noticed around that before returning the hdkey, the current version assigns this to some variable and eventually returns that variable. I assume this is what I should do as well? I also noticed that in fromExtendedPrivateKey, there are these lines

  // => version(4) || depth(1) || fingerprint(4) || index(4) || chain(32) || key(33)
  versions = versions || BITCOIN_VERSIONS
  var hdkey = new HDKey(versions)

I am not entirely sure how these differ. Should I instead create the hdkey object through this way by accepting a version parameter?

I apologize for the questions, contributing to open source repositories is something I'm new at but would love to do more of!

Docs: bad derive(path) example?

var childkey = hdkey.derive("m/0/2147483647'/1")

I'm new to all of this, but this looks like a junk / garbage / invalid path.

Shouldn't it be something like m/44'/0'/0'/0/1?

Where

?

Again total n00b, but these docs don't match other docs I'm looking at, so this is confusing to me.

Same value when calling .toJSON()

All HDKeys are returning the same values when calling hdkey.toJSON:

for ( var x = 0; x < 10; x++ ) {
    var str = "str" + x;
    console.log(str)
    var master_seed = HDKey.fromMasterSeed(Buffer.from(str, 'utf8'))
    console.log(master_seed._privateKey)
    console.log(master_seed.toJSON().xpriv)
    console.log(master_seed.toJSON().xpub)
}

Gives this:

str0
<Buffer 03 b0 3d f9 c1 9f 4f 58 a8 f7 bb 41 22 71 d0 5f 6d 72 d0 bb 04 7d 22 1f 63 af e8 f9 7b 8d 4d 2c>
xprv9s21ZrQH143K3t8ge8XqwgswkXbf32VeDH8ceyJunpgpVvcVDv3uyoMmfEcATSqisyXy6Co1JCTajPQcPPGfDnrGJNq5g7dFxTdYqPPKxnq
xpub661MyMwAqRbcGND9kA4rJppgJZS9SVDVaW4DTMiXMADoNiwdmTNAXbgFWY5DafTTXTer2LcihLiGUABXZtQhKRbn3qrWY7iZE4AYcbfAy8P
str1
<Buffer a7 d3 d7 34 3a fd 3b 95 cd b7 b3 f7 51 53 ff 92 d8 bf e1 ff f3 38 f2 3b 7c 49 a8 bb c1 60 0d 79>
xprv9s21ZrQH143K3uJGmpzxLf2wWDsweuR7be1yBcursKEUGmyHJS5MhjuF2JRh5YuGuhV5wEXLyagr3SChp6NBcp2xWfnYuu36kpY9P5WnhPm
xpub661MyMwAqRbcGPNjsrXxhnyg4FiS4N8xxrwZz1KURemT9aJRqyPcFYDisaTwCz7o1NBX6RxFwiEy4gpySo479xrCTQVTsLYUKqjpgLaQwW4
str2
<Buffer e8 a1 77 87 94 b6 fe a0 79 be cb 10 70 9d e7 f7 90 2a a1 dc 0d ec 4d 6e f1 42 c2 ab f7 53 bc a0>
xprv9s21ZrQH143K2wS7QZRBbMjfqU6yQeNWGMNC6S5gwhwhdCgY22vNuKQXF7FiSosyEUDgYpxf73Bp52ZU5smTBtAmFnpP12G9uPJMbySvrX9
xpub661MyMwAqRbcFRWaWaxBxVgQPVwTp76MdaHntpVJW3UgW11gZaEdT7j16PcneS7eZRNZV51c8DRw8T8bi6VzDzzcmQyjPCpmRhTxHYxcSUh
str3
<Buffer 7d 36 d8 76 b5 94 94 ea 28 b0 76 2a 49 11 9a 84 ef bf d5 1a 39 17 9e 6e cf bc 64 71 e1 70 0a 6b>
xprv9s21ZrQH143K4ZTzvH9QMXmxfNAupUqSGktbGzNdvKmhrsdGf45pt2yHxKgzMo5Ayi3nVyeYeKtETRfz2cf3MttTLqTckfM8YPjZi9Fcm2Y
xpub661MyMwAqRbcH3YU2JgQifihDQ1QDwZHdypC5NnFUfJgjfxRCbQ5RqHmodFEMdPtMx3BJCmew9vBQzfP4qDRZ9h5rUdqsHLFGAgQiS9oWYy
str4
<Buffer 6d 70 37 57 b8 fa b9 c6 f2 04 64 ee eb b3 35 5c ae d6 39 eb 51 61 e8 bd 92 18 6d b2 39 c0 62 14>
xprv9s21ZrQH143K4W6LbQ5MR4MCSynaEXsUbx4Dx348i937Ky8VNVeyPjWFuutbjWrZtHezwaVdgyiEgAoCWFHtbqj1Vr7PPMiMLJsfp4LkbeE
xpub661MyMwAqRbcGzAohRcMnCHw11d4dzbKyAypkRTkGUa6CmTdv2yDwXpjmCbqF8ZThqLgRafbyqMbM9ZTHscM5Cuw9e5uYBktMs8xocc1Peo
str5
<Buffer 9e d7 d5 e8 37 5c cb 9f 3f 15 c5 bf 1a 8e 43 18 a9 01 c9 d9 5d 8d 9f c5 5c 21 97 59 fb 54 ab f6>
xprv9s21ZrQH143K4TpWQ4FxaHRpAw4dBbFb47Qr1jchWZBbdHoSQD1KCzHVoX1vbgp4e1z3tZB6dVJQBxcCePPccdKWfwgJPXqGjqRMRnSQWVp
xpub661MyMwAqRbcGwtyW5nxwRNYixu7b3ySRLLSp82K4tiaW68awkKZknbyepMnsAmGhSC7wpYJjuAgS8XrrCJXLsrSiD3XqiNF5QgiHY1eUNr
str6
<Buffer 83 d7 7f c7 13 50 41 9a fc 40 cd 30 be 91 5b 5f f8 e2 ca 2c 8f cb c4 56 10 84 7f c5 ea 57 f4 26>
xprv9s21ZrQH143K3kF58W37E9nXhLh3GzURx2RWKTME8Vnha5PwNTDy1NV3epJXxAHwrLGNyaNGba1XJfgp8yPYZy8Z9mxFDAyHgqat25vozhE
xpub661MyMwAqRbcGEKYEXa7bHjGFNXXgTCHKFM77qkqgqKgSsj5uzYDZAoXW5VcpXmU6WP7wTbmtKTqVDotUXsBKxB7MpHGjaPT8iwXUQp8xeS
str7
<Buffer 85 28 d3 6e c5 ba 3f a3 b9 da 13 e7 51 a1 ff 32 3e bd 60 3c 82 27 54 6c 0d 6a 99 29 37 0d 15 52>
xprv9s21ZrQH143K2iJNZsYKD9Ywmt1rjSZUjhQ82CU4xWp4QcPSfWSyfXZe8bUPHrbfY8HxhcWVXz9HwX39EDbaom2nzTrkqCGaFYmS5TEzrDe
xpub661MyMwAqRbcFCNqfu5KaHVgKurM8uHL6vKipasgWrM3HQibD3mEDKt7ysrkT88CFhNAQsex73roiAnHwR2UyXjDrZnRUnFx9eg2wm8RpYb
str8
<Buffer ab 1a 5b c8 1d 0c c8 51 4c 4f 53 1f 45 e4 d3 8a cc be 87 4c 52 35 ee a4 b1 2e 1a 17 6b 94 a0 83>
xprv9s21ZrQH143K2i8TAAeAQoxZy5QEgg7iPLdUezkb63EjtwMQ8d82EjPYrDW2WVXG8sW8FqKSdjdK9U3GyVFxr5ihic9WUCFWZcP6FxC1vLb
xpub661MyMwAqRbcFCCvGCBAmwuJX7Ej68qZkZZ5TPACeNmimjgYgASGnXi2hV2m7oWTkz8s7BpEQP6YGKpsbZmLgosueujuWLFsy48xDcEemY9
str9
<Buffer 96 4c ca 7b d1 7a 6e 8c a5 9f 29 c9 cf 48 e4 0f f9 79 55 3b a3 2b cf 3e ff 1a 91 28 8d 1b f6 e1>
xprv9s21ZrQH143K2LgcC2P3nUcJYxmxmn5tvbnG1d4TSdJW2XT2VXbv9bVEScMzpMcA8PiuVyAZSnjHUfmcLWzB89V9pQDRgrxRierJ9QbkH3C
xpub661MyMwAqRbcEpm5J3v49cZ36zcTBEokHphrp1U4zxqUuKnB34vAhPoiHuGMcQpt9o6SGFe5WboBeHNCWiSdNutyMs65xcsbF3SSCdLYrJX

What's going on? Did I do something wrong?

Deriving from hardened extended keys

Hey guys - I've been playing with this module a bit and finding it really useful. Thank you!

I'm just wondering if I might be missing something. I'd like to be able to derive child nodes from a hardened extended key, which could be generated on startup.

i.e.
derive a node such as m/44'/1'/0' and create an extended, hardened, public key that could be used to derive paths such as m/44'/1'/0'/n

If it's not currently possible let me know and I might give it a shot.

Replacing the module crypto

Hi,

I'm opening this issue to know if you are open to replacing the use of crypto for more browser-friendly libraries like create-hmac and randombytes.

I've had a hard time using this library with Rollup, which led me to bundling my own version here: https://github.com/ethereum/js-ethereum-cryptography/tree/master/hdkey-without-crypto-config

I'd love not to have to do that, and the needed change is really small. I can prepare a PR for this, making sure that the builtin implementations are used on node.

Getting same output for any input to "fromMasterSeed"

I was excited to find your module but it looks like there might be a minor issue.

When I run the following script, it doesn't seem that the seed is being regenerated on each occurrence:

var HDKey = require('hdkey')
for ( var i = 0; i < 10; i++ ) {
var str = "str" + i;
console.log(str)
var master_seed = HDKey.fromMasterSeed(new Buffer.from(str, 'hex'))
console.log(master_seed._privateKey)
}

which produces

str0
<Buffer 30 0b 15 5f 75 19 64 27 6c 05 36 23 0b d9 b1 6f e7 a8 65 33 c3 cb aa 75 75 e8 d0 43 1d be df 23>
str1
<Buffer 30 0b 15 5f 75 19 64 27 6c 05 36 23 0b d9 b1 6f e7 a8 65 33 c3 cb aa 75 75 e8 d0 43 1d be df 23>
str2
<Buffer 30 0b 15 5f 75 19 64 27 6c 05 36 23 0b d9 b1 6f e7 a8 65 33 c3 cb aa 75 75 e8 d0 43 1d be df 23>
str3
<Buffer 30 0b 15 5f 75 19 64 27 6c 05 36 23 0b d9 b1 6f e7 a8 65 33 c3 cb aa 75 75 e8 d0 43 1d be df 23>
str4
<Buffer 30 0b 15 5f 75 19 64 27 6c 05 36 23 0b d9 b1 6f e7 a8 65 33 c3 cb aa 75 75 e8 d0 43 1d be df 23>
str5
<Buffer 30 0b 15 5f 75 19 64 27 6c 05 36 23 0b d9 b1 6f e7 a8 65 33 c3 cb aa 75 75 e8 d0 43 1d be df 23>
str6
<Buffer 30 0b 15 5f 75 19 64 27 6c 05 36 23 0b d9 b1 6f e7 a8 65 33 c3 cb aa 75 75 e8 d0 43 1d be df 23>
str7
<Buffer 30 0b 15 5f 75 19 64 27 6c 05 36 23 0b d9 b1 6f e7 a8 65 33 c3 cb aa 75 75 e8 d0 43 1d be df 23>
str8
<Buffer 30 0b 15 5f 75 19 64 27 6c 05 36 23 0b d9 b1 6f e7 a8 65 33 c3 cb aa 75 75 e8 d0 43 1d be df 23>
str9
<Buffer 30 0b 15 5f 75 19 64 27 6c 05 36 23 0b d9 b1 6f e7 a8 65 33 c3 cb aa 75 75 e8 d0 43 1d be df 23>

readme.MD is old

not work
const hdkey = HDKEY.fromMasterSeed(Buffer.from(seed, 'hex')) hdkey.derive("m/44'/1'/0'/0/0")
change to hdkey.derivePath("m/44'/1'/0'/0/0") it works

in browser

TypeError [ERR_INVALID_ARG_TYPE]

Hi I am trying to understand HD Wallet through this tutorial https://medium.com/@harshagoli/so-you-want-to-build-an-ethereum-hd-wallet-cb2b7d7e4998

first I generated the seed with bip39 version 3.0.2

const bip39 = require('bip39');
const mnemonic = bip39.generateMnemonic();
const seed = bip39.mnemonicToSeed(mnemonic);

seed is an instance of Promise which resolves to a Buffer

Promise {
  <Buffer 53 e5 e7 80 f0 83 d5 8e d8 6a 88 8b 24 80 5e b0 5e 91 cf 45 18 b3 1c fd 02 a7 a3 37 5c fc e2 c2 98 15 7c db 90 01 e7 8d c0 56 62 dd af f6 74 73 ff 3b ... 14 more bytes>
}

the next step was to generate the root by hdkey.fromMasterSeed (hdkey version 1.1.2)

const hdkey = require('hdkey');
const root = hdkey.fromMasterSeed(seed);

this threw an error:

> root = hdkey.fromMasterSeed(seed);
Uncaught:
TypeError [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received an instance of Promise
    at Hmac.update (internal/crypto/hash.js:84:11)
    at Function.HDKey.fromMasterSeed (/Users/anhdungle/learning/Ethereum_HD_wallet/node_modules/hdkey/lib/hdkey.js:177:54)
    at repl:1:14
    at Script.runInThisContext (vm.js:131:20)
    at REPLServer.defaultEval (repl.js:432:29)
    at bound (domain.js:429:14)
    at REPLServer.runBound [as eval] (domain.js:442:12)
    at REPLServer.onLine (repl.js:759:10)
    at REPLServer.emit (events.js:327:22)
    at REPLServer.EventEmitter.emit (domain.js:485:12) {
  code: 'ERR_INVALID_ARG_TYPE'
}

Thanks a lot.

privateExtendedKey error handling

Calling privateExtendedKey on a public node will result in the following exception: TypeError: Cannot read property 'length' of null

I think it would be nice either adding an assert into the function with a proper message or to return null.

In comparison privateKey just returns null if it is not available, perhaps that should be followed.

Unable to sign transaction and get privkey object

I'm trying to sign a transaction from by keys generated here

I get an error from assert saying that my transaction length is an invalid message length.
screen shot 2018-04-18 at 5 17 31 pm

I'm guessing this is mostly due to the transaction being invalid
I got the transaction from here

I tried copying the transaction output from here before being signed but I was unable to decode what was in the buffer.

How do you suggest I go about creating a transaction I can import or verify the usage of being able to sign the transaction ?

The Privatekey object from the hdkey isn't logging out either
I'm testing it out here

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.