iancoleman / bip39

A web tool for converting BIP39 mnemonic codes
https://iancoleman.io/bip39/
MIT License
3.45k stars 1.43k forks source link

Derive child BIP32 extended private/public key #374

Open andronoob opened 4 years ago

andronoob commented 4 years ago

First, I don't know whether this tool is able to derive child BIP32 extended private/public key?

Second, is BIP32 Root Key used as an input field? This seems to be weird/confusing to me, because the the extended key fed by the user may not be the "root" key. I saw that BIP32 serielization format contains a "depth" field.

Did I misunderstand something?

iancoleman commented 4 years ago

First, yes it can derive this. See bip32 tab. Maybe bip32.org is the tool you're really looking for.

Second, it can be used as an input field, but when using mnemonics as input it will derive this field automatically. You're right users may not be entering the root key, but that's how bip32 works, it's a hierarchy and any depth may serve for further derivation.

The only change I could see doing here is checking if the root key value isn't depth 0 and showing a warning, but the user value it's not 'invalid' as far as further derivation is concerned so I think it's ok as-is and this issue probably doesn't require further work.

I'm open to further comments on this.

andronoob commented 4 years ago

You're right users may not be entering the root key, but that's how bip32 works, it's a hierarchy and any depth may serve for further derivation.

Then I think it would be better to display details of a BIP32 extended key, including the depth.

The only change I could see doing here is checking if the root key value isn't depth 0 and showing a warning

I don't think this requires a warning, since it's supposed to be a good feature, right?

robertclarkson commented 4 years ago

Hi @iancoleman first off thanks for this great tool, it has been very helpful. I'm using it as a testing tool to ensure i don't mess up my wallet functions.

I wonder if you had time to explain to me why the BIP32 Root Key changes when you choose BIP44 or 84.

Using the bitcoinjs-lib I seem to only be able to produce the BIP44 BIP32 root key when I want to generate a BIP84 segwit one.

wigy-opensource-developer commented 4 years ago

BIP32 defines a 4-byte prefix called "version bytes" for encoding extended public and private keys. BIP32 only defines these for BTC mainnet and testnet which end up base58 encoded as xprv, xpub, tprv and tpub. Even when you change to a different coin, their version bytes could be different (say IOP extended public keys start with dyw). BIP84 simply redefined these version bytes so the end-user can see from the first characters what kind of scripts are needed to spend coins that belong to those private keys.

If you decode both the xprv and zprv root keys, you will find out that they only differ in the first 4 bytes (version) and the last 4 bytes (checksum). Starting from mnemonic "steak road wreck man core squeeze bubble angry coffee race manual path defense beach chuckle" you get these bytes:

$ node
Welcome to Node.js v12.16.0.
Type ".help" for more information.
> bs58 = require('bs58')
{
  encode: [Function: encode],
  decodeUnsafe: [Function: decodeUnsafe],
  decode: [Function: decode]
}
> bs58.decode('xprv9s21ZrQH143K37Ty7J6oMLUbXcgwhyzN993gfq5Cg6dxmQEUe8jE4yQAsC9ErtA6Q2tvGdzactm2QyEra11b5nhJkBroxNkXPTWTbU773eD')
<Buffer 04 88 ad e4 00 00 00 00 00 00 00 00 00 69 d3 2e 8f b0 75 df 64 82 49 47 36 13 40 c3 a6 70 3c 3f 2c 62 51 15 65 6a e6 64 a9 a3 33 df 34 00 3e 21 e9 4d ... 32 more bytes>
> bs58.decode('zprvAWgYBBk7JR8GjhrCn1g3mWfbsYyqbDyMyN68EcryS7Pisbrw9T4MK6iSuc4QrhTwDK8XmbBhYDU8BYTz1PqcgG4WUsFf8CPVvudkNey7sso')
<Buffer 04 b2 43 0c 00 00 00 00 00 00 00 00 00 69 d3 2e 8f b0 75 df 64 82 49 47 36 13 40 c3 a6 70 3c 3f 2c 62 51 15 65 6a e6 64 a9 a3 33 df 34 00 3e 21 e9 4d ... 32 more bytes>
robertclarkson commented 4 years ago

Thank you for such a good detailed answer