dchest / tweetnacl-js

Port of TweetNaCl cryptographic library to JavaScript
https://tweetnacl.js.org
The Unlicense
1.75k stars 292 forks source link

Hierarchical Deterministic (HD) style extended keys #233

Closed basedwon closed 2 years ago

basedwon commented 2 years ago

I've been scouring the web to no avail trying to find someone that's implemented an HD concept with TweetNacl. There are a few npm libs that do it, but nothing leveraging nacl that I could find. Basically, I want to be able to create child keys from a master seed like a key derivation function. Which is easy if we're just seeding new private keys. But I want to be able to make public keys from a master public key. So you could keep the private key stored away while deriving public keys that the original private key could still authenticate/decrypt. Basically the bip32 for bitcoin, only with tweetnacl. Any ideas?

CMEONE commented 2 years ago

You could do any sort of Hierarchical Deterministic key generation that you want without any libraries implemented using your own concept. Note that this might be a bit dangerous as "cooking your own crypto" is generally frowned upon unless you are extremely experienced in cryptography. If you want to follow a specific standard, I'm sure you can find a library that does what you need as long as it's actually possible.

All you really need though is some way to generate a bunch of keys deterministically, you can do this from a hash for example (generate a master private key, to create a new private/public keypair you can just hash the master private key x number of times to generate a new key). The approach I just described is sort of how HD keypairs work but not fully, so I highly recommend following a standard (like BIP32/BIP39) or porting another audited and heavily tested implementation to work with TweetNaCl to ensure that your implementation is secure and resistant against various attacks.

But I want to be able to make public keys from a master public key.

If you want to determine the public key from the master public key, this cryptography StackExchange post may help you with that, the answers there describe how you can achieve this derivation and link to other resources that you can use to implement this feature.