cryptocoinjs / hdkey

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

Update hdkey.js #2

Closed systemzax closed 6 years ago

systemzax commented 9 years ago

To encode the xpub and xpriv keys, use versions.bip32 instead of versions

jprichardson commented 9 years ago

Why? To match coininfo?

systemzax commented 9 years ago

Exactly. As of now, assuming versions is a version block from any coininfo file, the following code:

var wallet = hdkey.fromMasterSeed(seed, versions); console.log(wallet.publicExtendedKey);

will result in a string like this : //11111111111112oA4BUYZqASE2HUX3hpSakaFaJvFeqAsxemPypwE2YCRymASEDfn6ZbXUR7LayWiuYiCDKZ2ta77dQmfY2oGUHh5RPqtbaA

This is because the publicExtendedKey and privateExtendedKey methods are currently encoding the key with the public and private key bytes (ie: 0x00 for public and 0x80 for private in the case of Bitcoin).

By using the versions.bip32.public and versions.bip32.private bytes instead (ie: 0x0488ade4 for private and 0x0488b21e public), we get the correct output: //xpub68MsdGikGZ2w789ZNAh3w63x6dAeMph9SFLF8NkztQjpvHjtwu7fhNC6H1xVZVBqcZAi1r2E3LC8FecgpLtEkEp6ut7FsHn9SEiG47QAdzg

You can see this commit : https://github.com/systemzax/hdkey/commit/b7b597e9393642ac047caefecf0c4d681c788479

That one passes the travis-ci test.

systemzax commented 9 years ago

Also I think these methods should throw a "not supported exception" if the coininfo version supplied doesn't have bip32 bytes. Or possibly return the default xpub / xpriv bytes (0x0488b21e and 0x0488ade4).

dcousens commented 6 years ago

If you're using coininfo, don't pass the entire object in, instead pass network.bip32...