iancoleman / bip39

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

Private keys differ from Electrum (Bip49 path, identical seed, public keys, adresses) #174

Open JVJ5 opened 6 years ago

JVJ5 commented 6 years ago

Today I generated BIP39 Mnemonic using your site. Then I used this mnemonic to generate BIP39 seed wallet in Electrum.
Just for confirmation I compared following values generated by Electrum and by this site adresses - they match public keys - match private keys - different I cannot understand how this can be possible.
Electrum version is 3.0.6 Example of Mnemonic and keys are there (reproduced for several different mnemonics): https://www.reddit.com/r/Electrum/comments/7dku5r/segwit_wallets_and_electrum/dvh9k8z/ Thank you in advance for possible analysis.

iancoleman commented 6 years ago

I suspect it's to do with Electrum internally using different WIF values in serialize_privkey depending on the script type.

https://github.com/spesmilo/electrum/blob/d71d22d279b57dbe1f1509b7ba37fc421b2f173a/lib/bitcoin.py#L460

extended WIF for segwit (used in 3.0.x; but still used internally) SCRIPT_TYPES = { ... 'p2wpkh-p2sh':2, ... }

This changes the WIF constant from 0x80 to 0x80 + 2 = 0x82 for p2wpkh-p2sh (which is used in BIP49) but seems to be electrum specific - see https://bitcointalk.org/index.php?topic=2898237.msg29839781.

Electrum uses a special format for private keys in order to indicate what type of address they should be used for

I tested this modified WIF value in the tool and it generated the expected privkey from electrum. Since it's specific to electrum it should probably not be included in this tool. I'm open to further discussion though.

SomberNight commented 6 years ago

The WIF was changed in Electrum 3.0 as https://github.com/spesmilo/electrum/blob/3234917ea1356143b837183a5d6009724b1f18c4/RELEASE-NOTES#L181-L194 and then as Bitcoin Core decided to keep the WIF to mean "any", changed again in Electrum 3.1: https://github.com/spesmilo/electrum/blob/3234917ea1356143b837183a5d6009724b1f18c4/RELEASE-NOTES#L72-L75

So e.g. for the p2wpkh-p2sh address 35QN8Mme2EXEndUUJ2AKzoqazS7ZiqCx5s, Electrum 3.0.x exported the private key as: LFuXfxj6RnHUkeTvVpHBkksSneRAuF3drUfZQzhTmaR63w35Rj8X (0x82 base58 prefix byte)

Electrum 3.1.x exports the private key as: p2wpkh-p2sh:KxkGXe1J5UTCocSPPfVQAQgUHn1sEQWBXbRJJ61GncufVGeoMZBq (0x80 base58 prefix byte; so the right part is exactly the traditional WIF)

I don't think changes are necessary given the current format and the traditional WIF are similar enough.