<p>I am trying to use the cryptowallet library <a href="https://github.com/ElementsProject/libwally-core" rel="nofollow noreferrer">LibWally</a> to convert a 12-word mnemonic into a set of derived addresses.  I believe my mistake is not with the library itself, but a process flow/algorithmic one, so even if you don't aren't familiar with the library, you may be able to help.</p>
<p>First I validate and convert the mnemonic to a seed with the following code:</p>
<pre><code>    char *mnemonic = &quot;abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about&quot;;
    bip39_mnemonic_to_seed((const char *)mnemonic, &quot;&quot;,
                          (unsigned char *)bytes_output, HMAC_SHA512_LEN,
                           &amp;size_written)
     wally_hex_from_bytes((unsigned char*)bytes_output, size_written, &amp;hex_output)
</code></pre>
<p>This gives me the proper seed value; at least it matches what I see on <a href="https://iancoleman.io/bip39/" rel="nofollow noreferrer">Ian Coleman's Site</a>.  Next I take the seed value in bytes_input (not the hex coded value) and call:</p>
<pre><code>bip32_key_from_seed( (unsigned char*) bytes_output, 
    BIP32_ENTROPY_LEN_128,
    BIP32_VER_MAIN_PRIVATE,
    0,
    &amp;key_structure)
</code></pre>
<p>And then retrieve the private key with:</p>
<pre><code>bip32_key_to_base58(&amp;key_structure, BIP32_FLAG_KEY_PRIVATE, &amp;output_privkey);
</code></pre>
<p>The value in privKey does not match the Bip32 root key shown in Ian's site, though interestingly enough, it matches on the first 16 characters, before diverging (and winding up 4 chars longer).</p>
<p>Am I missing a step here?  Or doing something else wrong?</p>
<p>Edit: Adding some output data.  Using the phrase given above: my (and IanColeman's) seed is:</p>
<p>5eb00bbddcf069084889a8ab9155568165f5c453ccb85e70811aaed6f6da5fc19a5ac40b389cd370d086206dec8aa6c43daea6690f20ad3d8d48b2d2ce9e38e4</p>
<p>Root Key results:</p>
<p>Mine: xprv9s21ZrQH143K2fH5FXCJ4B1iTN2wJSq1h9z2wTZkytm3YgVRxP621bDZ7yqo4KdNWVNcMEyWBHrjThJ9jXMabog7R1RTWFNdSAz2mhX2VjL</p>
<p>IanColeman: xprv9s21ZrQH143K3GJpoapnV8SFfukcVBSfeCficPSGfubmSFDxo1kuHnLisriDvSnRRuL2Qrg5ggqHKNVpxR86QEC8w35uxmGoggxtQTPvfUu</p>
<p>&quot;</p>

CC BY-SA 4.0
2020-09-10T14:31:58.333Z
I added some output data above. Do you mean "to" base58, the function I'm already calling?   Also, the docs for bip32_key_from_seed says it creates a new master root key.  But the output is an "extkey", an extended key structure w/ public/private pair on it.  It feels like I should first have to create the root key, then use that to create the extended key, no?
Endymio
CC BY-SA 4.0
2020-09-10T15:38:39.357Z
The extended private key on Coleman's site doesn't match the return value from my to_base58() call either.  After the same "xprv9" prefix, it diverges.
Endymio
CC BY-SA 4.0
2020-09-12T17:31:28.013Z
The Trezor API in that link is so different from libWally that its difficult to see what I might be doing wrong.  But if I can't get this to work, I may have to switch to that.   I could probably just write the code from scratch myself, but all the online BIP32 references I've seen are somewhat opaque for those not well-versed in elliptic-curve mathematics.
Endymio
<wallet><bip32-hd-wallets><mnemonic-seed><c><libwally>
Howto convert mnemonic->seed->root key->derived addresses with c library (libWally)
Endymio
# Howto convert mnemonic->seed->root key->derived addresses with c library (libWally)

(Asked by: username_0 on 2020-09-09)

I am trying to use the cryptowallet library [LibWally](https://github.com/ElementsProject/libwally-core) to convert a 12-word mnemonic into a set of derived addresses. I believe my mistake is not with the library itself, but a process flow/algorithmic one, so even if you don't aren't familiar with the library, you may be able to help.

First I validate and convert the mnemonic to a seed with the following code:

```
    char *mnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
    bip39_mnemonic_to_seed((const char *)mnemonic, "",
                          (unsigned char *)bytes_output, HMAC_SHA512_LEN,
                           &size_written)
     wally_hex_from_bytes((unsigned char*)bytes_output, size_written, &hex_output)

```

This gives me the proper seed value; at least it matches what I see on [Ian Coleman's Site](https://iancoleman.io/bip39/). Next I take the seed value in bytes\_input (not the hex coded value) and call:

```
bip32_key_from_seed( (unsigned char*) bytes_output, 
    BIP32_ENTROPY_LEN_128,
    BIP32_VER_MAIN_PRIVATE,
    0,
    &key_structure)

```

And then retrieve the private key with:

```
bip32_key_to_base58(&key_structure, BIP32_FLAG_KEY_PRIVATE, &output_privkey);

```

The value in privKey does not match the Bip32 root key shown in Ian's site, though interestingly enough, it matches on the first 16 characters, before diverging (and winding up 4 chars longer).

Am I missing a step here? Or doing something else wrong?

Edit: Adding some output data. Using the phrase given above: my (and IanColeman's) seed is:

5eb00bbddcf069084889a8ab9155568165f5c453ccb85e70811aaed6f6da5fc19a5ac40b389cd370d086206dec8aa6c43daea6690f20ad3d8d48b2d2ce9e38e4

Root Key results:

Mine: xprv9s21ZrQH143K2fH5FXCJ4B1iTN2wJSq1h9z2wTZkytm3YgVRxP621bDZ7yqo4KdNWVNcMEyWBHrjThJ9jXMabog7R1RTWFNdSAz2mhX2VjL

IanColeman: xprv9s21ZrQH143K3GJpoapnV8SFfukcVBSfeCficPSGfubmSFDxo1kuHnLisriDvSnRRuL2Qrg5ggqHKNVpxR86QEC8w35uxmGoggxtQTPvfUu

"

**Tags:** wallet, bip32-hd-wallets, mnemonic-seed, c, libwally

> **Comments:**
> **username_0**: I added some output data above. Do you mean "to" base58, the function I'm already calling?   Also, the docs for bip32_key_from_seed says it creates a new master root key.  But the output is an "extkey", an extended key structure w/ public/private pair on it.  It feels like I should first have to create the root key, then use that to create the extended key, no?  
> **username_0**: The extended private key on Coleman's site doesn't match the return value from my to_base58() call either.  After the same "xprv9" prefix, it diverges.  
> **username_0**: The Trezor API in that link is so different from libWally that its difficult to see what I might be doing wrong.  But if I can't get this to work, I may have to switch to that.   I could probably just write the code from scratch myself, but all the online BIP32 references I've seen are somewhat opaque for those not well-versed in elliptic-curve mathematics.  

---

## 0 answers

