0

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.

0

If you just need a command line, you can try pip3 install btc-address-dump.

Here is an example:

$ btc_address_dump "olympic wine chicken argue unaware bundle tunnel grid spider slot spell need"
mnemonic = olympic wine chicken argue unaware bundle tunnel grid spider slot spell need
private key (hex) = c7ac679b56f50bfd54dd924fe45a8dca7a1c2dced254b03dac22afc03adb9127
private key (WIF) = 5KLDyKtrScLYsKMJzVCt8Mf6Nn9DEV7V3fg8njfSZnqe7ZEMqzK
private key (WIF compressed) = L3urFcPsE2yHf5zeQjVSfUB8j8FEzX5cnmhjNsJfqjKgowPP4tmg
public key (uncompressed) = 044cd0aaeca3b636078583408e75edd77307b5190ca7a48bb9fbc1f2576c17dff1087190d91e26af594e3f8ecd3f4d3596c03c45d3b235da916903c930c6593cc4
public key (compressed) = 024cd0aaeca3b636078583408e75edd77307b5190ca7a48bb9fbc1f2576c17dff1
legacy address (p2pkh uncompressed) = 1Q5RqZctfcNkTPad2tuJSREByd2gB8xs63
legacy address (p2pkh compressed) = 12W36tm2jnreFiYdrzfE6cvRaKRbicEpnA
p2sh-segwit address (p2sh p2wpkh) = 3AzXxVUqdzvzEqVmdtmeVqRwc98uqwyh76
bech32 address (p2wpkh) = bc1qzp6lz6zy9p7k68r0a7lzfpkdxvj3yapynzuatt
1
  • This looks great thanks. I got errors though when I run import btc_address_dump. This should work with python3.8.5 right? 🦄 Feb 1 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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