2

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

1 Answer 1

2

You can try this (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'

Your Answer

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

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