Crypto currency

How can I generate SegWit addresses from a mnemonic using python?

So, I have written a script that generates BIP39 mnemonics. I now want to generate N native SegWit public keys from this mnemonic.

I have found this, but it generates legacy addresses. It uses the module bip32utils.

def bip39(mnemonic_words):
    mobj = mnemonic.Mnemonic("english")
    seed = mobj.to_seed(mnemonic_words)

    bip32_root_key_obj = bip32utils.BIP32Key.fromEntropy(seed)
    bip32_child_key_obj = bip32_root_key_obj.ChildKey(
        44 + bip32utils.BIP32_HARDEN
    ).ChildKey(
        0 + bip32utils.BIP32_HARDEN
    ).ChildKey(
        0 + bip32utils.BIP32_HARDEN
    ).ChildKey(0).ChildKey(0)

    return {
        'mnemonic_words': mnemonic_words,
        'addr': bip32_child_key_obj.Address(),
        'publickey': binascii.hexlify(bip32_child_key_obj.PublicKey()).decode(),
        'privatekey': bip32_child_key_obj.WalletImportFormat(),
        'coin': 'BTC'
    }

if __name__ == '__main__':
    seed = 'input display smile visa surround learn solar hero vacuum parrot cigar devote'
    pprint.pprint(bip39(seed))

I assume I need bip44utils or something, but it doesn’t exists. I have tried looking at bip-utils but I can’t work out how to convert my mnemonic to an address.

Does anyone know how I can convert a mnemonic to a SegWit address (in a way that I can change the derivation path to get multiple addresses)?

EDIT (SOLVED):

I found bitcoinlib can do this.

from bitcoinlib.wallets import Wallet

passphrase="input display smile visa surround learn solar hero vacuum parrot cigar devote"

w = Wallet.create("Wallet1", witness_type="segwit", keys=passphrase, network='bitcoin')
WalletKeys = (w.get_keys(number_of_keys=10))

for k in WalletKeys:
    print(k.address)

And now it works just as I wanted it to.

Most Related Links :
Business News Governmental News Finance News

Source link

Back to top button