• Welcome to forex.pm forex forum binary options trade. Please login or sign up.
 

Having trouble creating a Python function that creates non-hardened HD wallet child keys

Started by Bitcoin, Feb 15, 2022, 02:18 pm

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Bitcoin

Having trouble creating a Python function that creates non-hardened HD wallet child keys

I am coding my own keys (not for use in production, strictly for learning). I am successfully creating 512 bit seeds from mnemonic phrases, however I am failing at the step of deriving child keys from these seeds. For reference I am using the following post for testing:


https://learnmeabitcoin.com/technical/hd-wallets


What I will do is use that site to generate mnemonics for me, then using the seed I try to match the child keys in the Child Keys (Basic) section, to no avail. For instance, I have the following phrase:


lash afraid any alter object topic pony deer helmet favorite chuckle carpet

which matches the seed and keys in the post:


Extended key: e8448e8e6ac4ce400951f6d630a0963c40b263208dacee0c53b3a3ccf8be9a1540434b626e34b82f28e4fb60842d5a5470e2f8cc2777a559a3c68ac4e9f2559d
Private key: e8448e8e6ac4ce400951f6d630a0963c40b263208dacee0c53b3a3ccf8be9a15
Chain code: 40434b626e34b82f28e4fb60842d5a5470e2f8cc2777a559a3c68ac4e9f2559d
Public key: 03cf895293b95a0ce2457593b82cc6fd646c757a1e2d393932b70dafddc2d9553b

Here is where I am getting tripped up. My rough python function looks like this:


def create_child_keys(chain_code, public_key, children):
    for n in range(children):
        hex_child_index = hex(n)[2:].zfill(8)
        print(hmac.new(bytes.fromhex(chain_code), msg=bytes.fromhex(public_key + hex_child_index), digestmod=hashlib.sha512).hexdigest())

create_child_keys(extended_key, 3)

This returns different hashes for the first 3 children than the link posted above. For instance, according to learnmeabitcoin the first child should have a private key of:


d547eda1f3d7da655cafec4506d3e333f1add4415968b1994f30bd64b023e2d3

but mine is:


c9dd74741e44fbd9e763f47829f37d1b2165a5ad6e90697c719d95489c409326

Hoping something obvious stands out to those more familiar with this process.


Source: Having trouble creating a Python function that creates non-hardened HD wallet child keys