<p>You can try <a href="https://github.com/mcdallas/cryptotools" rel="nofollow noreferrer">this</a> (disclaimer: I wrote it) and do the following:</p>

<pre><code>from btctools import Xprv

&gt;&gt;&gt; xprv = Xprv.from_mnemonic('impulse prize erode winner pupil fun off addict ...')
&gt;&gt;&gt; xpub = xprv.to_xpub()
&gt;&gt;&gt; xpub.encode()    
'xpub661MyMwAqRbcFJwcugErEvzSgeKwGgHeLyzWKKK4C3edVRDbCU8o2pueJ5S26cXgPQQVRxQXyuoKrScnNKzohc4YNefs4zXg8tAhSLfnFo7'

&gt;&gt;&gt; xpub/0/0/1 
Xpub(path=M/0/0/1, key=039ec9190c3469b70f55a50466c7514b881ebe83b6215e94ed727854e1724ca811)

&gt;&gt;&gt; xpub.address('P2PKH') 
'1Fh2JwDc87R2BSXmcRYQPgRR5ntn5LuabK'
</code></pre>

CC BY-SA 4.0
2018-10-27T13:00:49.243Z
2018-10-27T13:00:49.243Z
Mike D
<p>Hello I have the following code that generates a Public Key, Private Key and BTC Address.</p>

<pre><code>import hashlib
import ecdsa
from base58 import b58encode_check, b58decode_check
import os


def pubkey_to_address(pubkey: bytes) -&gt; str:
    if 'ripemd160' not in hashlib.algorithms_available:
        raise RuntimeError('missing ripemd160 hash algorithm')

    sha = hashlib.sha256(pubkey).digest()
    ripe = hashlib.new('ripemd160', sha).digest()
    return b58encode_check(b'\x00' + ripe)


def generate_new_keys() -&gt; list:
    signing_key = ecdsa.SigningKey.generate(curve=ecdsa.SECP256k1)
    verifying_key = signing_key.get_verifying_key()
    my_address = pubkey_to_address(verifying_key.to_string())

    return signing_key.to_string(), verifying_key, my_address
</code></pre>

<p>So when I use this function (generate_new_keys) I will get all the important keys.</p>

<p>Now I'd like to get my Extended Public Key to generate more subaddresses. Any idea where to start? </p>

<p>I've tried this library but It seems unsafe for BTC: <a href="https://github.com/ranaroussi/pywallet" rel="nofollow noreferrer">https://github.com/ranaroussi/pywallet</a></p>

<wallet><public-key>
How to get the Extended Public Key
Marcos Aguayo
# How to get the Extended Public Key

(Asked by: username_0 on 2018-10-27)

Hello I have the following code that generates a Public Key, Private Key and BTC Address.

```
import hashlib
import ecdsa
from base58 import b58encode_check, b58decode_check
import os

def pubkey_to_address(pubkey: bytes) -> str:
    if 'ripemd160' not in hashlib.algorithms_available:
        raise RuntimeError('missing ripemd160 hash algorithm')

    sha = hashlib.sha256(pubkey).digest()
    ripe = hashlib.new('ripemd160', sha).digest()
    return b58encode_check(b'\x00' + ripe)

def generate_new_keys() -> list:
    signing_key = ecdsa.SigningKey.generate(curve=ecdsa.SECP256k1)
    verifying_key = signing_key.get_verifying_key()
    my_address = pubkey_to_address(verifying_key.to_string())

    return signing_key.to_string(), verifying_key, my_address

```

So when I use this function (generate\_new\_keys) I will get all the important keys.

Now I'd like to get my Extended Public Key to generate more subaddresses. Any idea where to start?

I've tried this library but It seems unsafe for BTC: <https://github.com/ranaroussi/pywallet>

**Tags:** wallet, public-key


---

## 1 answers

### Answer by username_1 on 2018-10-27. Score: 2

You can try [this](https://github.com/mcdallas/cryptotools) (disclaimer: I wrote it) and do the following:

```
from btctools import Xprv

>>> xprv = Xprv.from_mnemonic('impulse prize erode winner pupil fun off addict ...')
>>> xpub = xprv.to_xpub()
>>> xpub.encode()    
'xpub661MyMwAqRbcFJwcugErEvzSgeKwGgHeLyzWKKK4C3edVRDbCU8o2pueJ5S26cXgPQQVRxQXyuoKrScnNKzohc4YNefs4zXg8tAhSLfnFo7'

>>> xpub/0/0/1 
Xpub(path=M/0/0/1, key=039ec9190c3469b70f55a50466c7514b881ebe83b6215e94ed727854e1724ca811)

>>> xpub.address('P2PKH') 
'1Fh2JwDc87R2BSXmcRYQPgRR5ntn5LuabK'

```


---

