vbuterin / pybitcointools

SImple, common-sense Bitcoin-themed Python ECC library
1.3k stars 860 forks source link

[Bounty] Recover the extended BIP32 master private key from its extended BIP32 public key and a WIF private key #171

Closed mayerwin closed 7 years ago

mayerwin commented 7 years ago

I have posted this question with a 50 reputation bounty here: https://bitcoin.stackexchange.com/questions/55383/recover-the-extended-bip32-master-private-key-from-its-extended-bip32-public-key I will also consider rewarding the accepted answer with BTC.

I am trying to recover the extended BIP32 master private key from its extended BIP32 public key (which I already know) and a WIF private key (which I obtained by decrypting a BIP38 seed with a passphrase).

I followed the steps described in this article from Vitalik Buterin, but to use the crack_bip32_privkey function in pybitcointools, I need to have a private key in BIP32 format (not WIF). I can see how to obtain a BIP32 private master key from a BIP32 seed (with bip32_master_key), but not how to do the same from a private key in WIF format.

How can I achieve this (if I understood things correctly), with pybitcointools?

quinnoaj commented 7 years ago

you mean the xprv?

quinnoaj commented 7 years ago

Have you able to run from pybitcointools.deterministic successfully?

mayerwin commented 7 years ago

Fyi the issue has been resolved, see the StackExchange answer.

quinnoaj commented 7 years ago

I'm encoutering the same problem though when I tried the answer given by Andrew I got some error with the pybitcointools.deterministic

mayerwin commented 7 years ago

I didn't get any error, though you may want to check which Python version you are running.

quinnoaj commented 7 years ago

I have tried it both to python2.7 and 3.5 and got the same error no module named pybitcointools.deterministic :(

mayerwin commented 7 years ago

Try bitcoin.deterministic.

quinnoaj commented 7 years ago

wow bitcoin.deterministic works thanks. But print final_key have error missing parentheses. when I try to use print(final_key) it throws indention error. when I try to adjust the indention it throws 'final key' is not defined.

it's weird.

mayerwin commented 7 years ago

Please post your full code. You're probably not far off. Parentheses are necessary for Python 3.5+. final_key should be defined.

mayerwin commented 7 years ago

Please paste your code within "Insert code" markup so it preserves any indentation.

quinnoaj commented 7 years ago

>from bitcoin.deterministic import raw_crack_bip32_privkey, bip32_deserialize, bip32_serialize, bip32_privtopub >from bitcoin.main import decode_privkey, encode_privkey

>parent_pub = 'xpub661MyMwAqRbcEnKbXcCqD2GT1di5zQxVqoHPAgHNe8dv5JP8gWmDproS6kFHJnLZd23tWevhdn4urGJ6b264DfTGKr8zjmYDjyDTi9U7iyT' >wif_key = encode_privkey(decode_privkey('KyqcQVzcp7cHEMEDHQaz5eaE5azsRHaE4ukkeqwM2vdiQwBYtxeb'), 'bin_compressed') >for i in xrange(2**31 -1): > priv = (b'\x04\x88\xAD\xE4', 1, 0, i, b'', wif_key) > pkey = raw_crack_bip32_privkey(bip32_deserialize(parent_pub), priv) > final_key = bip32_serialize(pkey) > if bip32_privtopub(final_key) == parent_pub: > print (final_key)