<p>Using <a href="https://github.com/mcdallas/cryptotools" rel="nofollow noreferrer">this</a> library which is a pure python library with no dependencies (disclaimer: I am the author)</p>
<pre><code>&gt;&gt;&gt; from cryptotools import Xprv

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

&gt;&gt;&gt; xpub = xprv.to_xpub()

&gt;&gt;&gt; xpub.encode()
'xpub661MyMwAqRbcGSaJ8ymCnwWbZzUgRB9XSAGoAvJc92oorJqzfyTPNQCEzqetVjofVs6DjXEPgR62vGP8UsSL4HFXLcRHdBbcDZ7DgyZea59'
</code></pre>

CC BY-SA 4.0
2021-06-03T03:43:39.717Z
is it possible to convert the xpub to ypub and zpub?
Guerlando OCs
CC BY-SA 4.0
2021-06-03T20:55:05.007Z
@GuerlandoOCs yes try adding the parameter `addresstype='P2WPKH-P2SH'` or `addresstype='P2WPKH'` in the `from_mnemonic()` method
Mike D
CC BY-SA 4.0
2021-06-05T06:51:47.183Z
shouldn't the method `to_xpub` be called `to_pubkey` then? `xpub` is a little misleading
Guerlando OCs
CC BY-SA 4.0
2021-06-05T06:56:07.107Z
I'm getting `cannot import name 'Xprv' from cryptotools` on my raspberry pi. Any thoughts on that? Thank you very much
Guerlando OCs
CC BY-SA 4.0
2021-06-05T11:07:59.957Z
Did you install the wrong library perhaps? Dont do pip install cryptotools thats a different kibrary
Mike D
CC BY-SA 4.0
2021-06-05T18:41:19.723Z
I cloned from git and placed into the raspberry pi sd card to later install it, there's no internet on the rpi
Guerlando OCs
CC BY-SA 4.0
2021-06-02T12:37:13.507Z
2021-06-02T12:37:13.507Z
Mike D
<p>The following code using the <a href="https://github.com/darosior/python-bip32" rel="nofollow noreferrer">python-bip32</a> lib can show this information.</p>
<pre><code>from bip32 import BIP32, HARDENED_INDEX

bip32 = BIP32.from_seed(bytes.fromhex(&quot;01&quot;))

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

print(&quot;master_xpriv: &quot; + master_xpriv)
print(&quot;master_xpub: &quot; + master_xpub)

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

print(&quot;xpriv_from_path: &quot; + xpriv_from_path)
print(&quot;xpub_from_path: &quot; + xpub_from_path)
</code></pre>

CC BY-SA 4.0
2021-06-02T00:55:52.163Z
2021-06-02T00:55:52.163Z
le0
<p>Yes, I'd say the simplest way is to use python bip39 implementation of trezor, this <a href="https://github.com/trezor/python-mnemonic/blob/master/mnemonic/mnemonic.py" rel="nofollow noreferrer">one file</a> should be enough with methods:</p>
<p><code>to_seed</code> - to convert list of the words into seed and</p>
<p><code>to_hd_master_key</code> - to generate master xpub/xpriv</p>
<p>It doesn't support further derivations (which require a bit more dependencies), for which I'd suggest using <a href="https://github.com/darosior/python-bip32/blob/master/bip32/bip32.py" rel="nofollow noreferrer">bip32</a> library.</p>

CC BY-SA 4.0
2021-06-02T00:08:20.673Z
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)`
Guerlando OCs
CC BY-SA 4.0
2021-06-02T00:08:39.920Z
but I got only `xprv9s2...`. What am I missing?
Guerlando OCs
CC BY-SA 4.0
2021-06-02T00:10:18.903Z
I did an update, please take a look
Guerlando OCs
CC BY-SA 4.0
2021-06-02T00:11:28.310Z
I think it does not generate master pubkey, only priv key.
Guerlando OCs
CC BY-SA 4.0
2021-02-23T00:23:59.280Z
2021-02-23T00:23:59.280Z
Tony Sanak
<p>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.</p>
<p>Is there a simple script that does that, having minimal dependencies?</p>
<p><strong>UPDATE:</strong></p>
<p>based on the answer below using <a href="https://github.com/trezor/python-mnemonic" rel="nofollow noreferrer">https://github.com/trezor/python-mnemonic</a>, I did</p>
<pre><code>def main() -&gt; None:
    import sys
    m = Mnemonic(&quot;english&quot;)
    seed = Mnemonic.to_seed(&quot;my 12 words&quot;)
    pubkey = Mnemonic.to_hd_master_key(seed)
    print(pubkey)
</code></pre>
<p>but I get</p>
<pre><code>xprv...
</code></pre>
<p>and no pubkey.</p>
<p>How do I get the master pub key?</p>

<private-key><public-key><python><xpub>
Is there a simple python script for xpub generation from private key
Guerlando OCs
# Is there a simple python script for xpub generation from private key

(Asked by: username_0 on 2021-02-22)

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?

**Tags:** private-key, public-key, python, xpub


---

## 3 answers

### Answer by username_1 on 2021-06-02. Score: 1 | Accepted answer

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'

> **Comments:**
> **username_0**: is it possible to convert the xpub to ypub and zpub?  
> **username_1**: @GuerlandoOCs yes try adding the parameter `addresstype='P2WPKH-P2SH'` or `addresstype='P2WPKH'` in the `from_mnemonic()` method  
> **username_0**: shouldn't the method `to_xpub` be called `to_pubkey` then? `xpub` is a little misleading  
> **username_0**: I'm getting `cannot import name 'Xprv' from cryptotools` on my raspberry pi. Any thoughts on that? Thank you very much  
> **username_1**: Did you install the wrong library perhaps? Dont do pip install cryptotools thats a different kibrary  
> **username_0**: I cloned from git and placed into the raspberry pi sd card to later install it, there's no internet on the rpi  

---

### Answer by username_2 on 2021-06-02. Score: 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)


---

### Answer by username_3 on 2021-02-23. Score: 1

Yes, I'd say the simplest way is to use python bip39 implementation of trezor, this [one file](https://github.com/trezor/python-mnemonic/blob/master/mnemonic/mnemonic.py) 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](https://github.com/darosior/python-bip32/blob/master/bip32/bip32.py) library.

> **Comments:**
> **username_0**: 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)`  
> **username_0**: but I got only `xprv9s2...`. What am I missing?  
> **username_0**: I did an update, please take a look  
> **username_0**: I think it does not generate master pubkey, only priv key.  

---

