Monday, 21 January 2019

Going from BIP39 (mnemonic) to BIP32 (public/private keys)

I'm looking to develop some code that creates bitcoin private and public keys from a mnemonic. My current understanding of this process is:

entropy > nmemonic > seed > public/private keys > public address

I am using Trezor's nmemonic library and moneywagon in my code.

import string
from random import SystemRandom, randrange
from binascii import hexlify, unhexlify
from moneywagon import generate_keypair
from mnemonic import mnemonic

def gen_rand():
    foo = SystemRandom()
    length = 32
    chars = string.hexdigits
    return ''.join(foo.choice(chars) for _ in range(length))

mnemo = mnemonic.Mnemonic('english')

entropy = gen_rand()
# entropy = '00000000000000000000000000000000'

words = mnemo.to_mnemonic(unhexlify(entropy))
seed = hexlify(mnemo.to_seed(words, passphrase='apassphrase'))
address = generate_keypair('btc', seed)

print(words)
print(seed)
print(address['public']['address'])
print(address['private']['hex'])

if you comment out the above entropy line and run the code you get.

abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about
b'05de15fb96dc0ab9f03c9d411bf84c586c72e7c30bddd413a304896f9f994ea65e7fcafd2c6b796141e310850e5f30b6abc2e6aec79a8ff81f4ba38fde81c403'
15GyM1xxxxxxxxxxxxxxxxxxxxxxTXrrvG
8ede10xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxcae501

My problem is none of this reflected in this page or this page for generating mnemonic codes and public/private keys.

Where am I going wrong



from Going from BIP39 (mnemonic) to BIP32 (public/private keys)

No comments:

Post a Comment