cryptocoinjs / hdkey

JavaScript component for Bitcoin hierarchical deterministic keys (BIP32)
MIT License
201 stars 74 forks source link

hdkey.fromPrivateKey #14

Closed arshbot closed 4 years ago

arshbot commented 6 years ago

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!

junderw commented 4 years ago

You can't create a BIP32 compliant HDkey with just a private key.

The only data you can supply is:

  1. A seed to create the master node. (root node)
  2. An encoding of the extended private key. (Provided in this library, as you have noticed)

Supplying supplying a 32 byte private key can not create a valid HDkey object unless you run it through the seed to master node steps, in which case your HDkey would be using the HASH of your private key, not your private key.

So the answer is unfortunately, there is no way to create an HDkey object that is BIP32 compliant with just a private key.