Ad

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 is reflected in iancoleman.io/bip39 or bip32jp.github.io for generating mnemonic codes and public/private keys.

Where am I going wrong?

Ad

Answer

Both sites generate the same seed as you, given your mnemonic:

abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about

Also https://bip32jp.github.io/english/ gives this specific mnemonic given your forced entropy of

entropy = '00000000000000000000000000000000'

(you have to choose base 16 encoding, since your call to unhexlify interprets this string as such)

The first site https://iancoleman.io/bip39/#english seems to heuristically determine the string encoding for the entropy and recognising it as binary. This yields consequently to another result.

The values for

address['public']['address']
address['private']['hex']

differ from yours on both pages, since these pages use different derivation algorithms than moneywagon does. Moneywagon uses BIP38 a discouraged algorithm. I assume that is the reason for both sites not to offer it.

Ad
source: stackoverflow.com
Ad