0

I need a python script to verify, in an air gapped system, that my 24-word (or 12-word) mnemonic seed indeed generates the xpub that my wallet is showing me.

Is there a simple script that does that, having minimal dependencies?

UPDATE:

based on the answer below using https://github.com/trezor/python-mnemonic, I did

def main() -> None:
    import sys
    m = Mnemonic("english")
    seed = Mnemonic.to_seed("my 12 words")
    pubkey = Mnemonic.to_hd_master_key(seed)
    print(pubkey)

but I get

xprv...

and no pubkey.

How do I get the master pub key?

3 Answers 3

1
+50

Using this library which is a pure python library with no dependencies (disclaimer: I am the author)

>>> from cryptotools import Xprv

>>> xprv = Xprv.from_mnemonic('assist excess fox blossom trouble cry must segment arrive stereo weather april pudding tuna change')

>>> xpub = xprv.to_xpub()

>>> xpub.encode()
'xpub661MyMwAqRbcGSaJ8ymCnwWbZzUgRB9XSAGoAvJc92oorJqzfyTPNQCEzqetVjofVs6DjXEPgR62vGP8UsSL4HFXLcRHdBbcDZ7DgyZea59'
6
  • is it possible to convert the xpub to ypub and zpub? Commented Jun 3, 2021 at 3:43
  • @GuerlandoOCs yes try adding the parameter addresstype='P2WPKH-P2SH' or addresstype='P2WPKH' in the from_mnemonic() method
    – Mike D
    Commented Jun 3, 2021 at 20:55
  • shouldn't the method to_xpub be called to_pubkey then? xpub is a little misleading Commented Jun 5, 2021 at 6:51
  • I'm getting cannot import name 'Xprv' from cryptotools on my raspberry pi. Any thoughts on that? Thank you very much Commented Jun 5, 2021 at 6:56
  • Did you install the wrong library perhaps? Dont do pip install cryptotools thats a different kibrary
    – Mike D
    Commented Jun 5, 2021 at 11:07
1

Yes, I'd say the simplest way is to use python bip39 implementation of trezor, this one file should be enough with methods:

to_seed - to convert list of the words into seed and

to_hd_master_key - to generate master xpub/xpriv

It doesn't support further derivations (which require a bit more dependencies), for which I'd suggest using bip32 library.

4
  • I did ` def main() -> None: import sys m = Mnemonic("english") seed = Mnemonic.to_seed("my 12 words") pubkey = Mnemonic.to_hd_master_key(seed) print(pubkey)` Commented Jun 2, 2021 at 0:08
  • but I got only xprv9s2.... What am I missing? Commented Jun 2, 2021 at 0:08
  • I did an update, please take a look Commented Jun 2, 2021 at 0:10
  • I think it does not generate master pubkey, only priv key. Commented Jun 2, 2021 at 0:11
1

The following code using the python-bip32 lib can show this information.

from bip32 import BIP32, HARDENED_INDEX

bip32 = BIP32.from_seed(bytes.fromhex("01"))

# get master xpriv and xpub
master_xpriv = bip32.get_master_xpriv()
master_xpub = bip32.get_master_xpub()

print("master_xpriv: " + master_xpriv)
print("master_xpub: " + master_xpub)

# get from specif path 
xpriv_from_path = bip32.get_xpriv_from_path("m/84'/0'/0'/0/1")
xpub_from_path = bip32.get_xpub_from_path("m/84'/0'/0'/0/1")

print("xpriv_from_path: " + xpriv_from_path)
print("xpub_from_path: " + xpub_from_path)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.