Update 16_2_Using_BIP39_in_Libwally.md

This commit is contained in:
Shannon Appelcline 2020-08-05 15:38:37 -10:00 committed by GitHub
parent c05da44c84
commit 579d4fc4f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,4 +6,55 @@ One of Libwally's greatest powers is that it can lay bare the underlying work of
> :book: ***What is a Mnemonic Code?*** Bitcoin addresses (and their corresponding private keys) are long, untintelligible lists of characters and numbers, which are not only impossible to remember, but also easy to mess up. Mnemonic codes were a solution for this that allow users to remember 12 (or 24) words in their language. These codes can then be used to fully restore a BIP32 seed that's the basis of an HD wallet.
_In process_
#include <stdio.h>
#include "sodium.h"
#include "wally_core.h"
#include "wally_bip39.h"
int main(void) {
int lw_response;
/* 1. Initialize Libwally */
lw_response = wally_init(0);
if (lw_response) {
printf("Error: Wally_init failed: %d\n",lw_response);
exit(-1);
}
/* 2. Generate Entropy */
unsigned char entropy[16];
randombytes_buf(entropy, 16);
/* 3. Translate into Mnemonic */
char *mnem = NULL;
lw_response = bip39_mnemonic_from_bytes(NULL,entropy,16,&mnem);
if (lw_response) {
printf("Error: bip39_mnemonic_from_bytes failed: %d\n",lw_response);
exit(-1);
}
printf("Mnemonic: %s\n",mnem);
/* 4. Translate into Seed */
size_t seed_len;
unsigned char seed[BIP39_SEED_LEN_512];
lw_response = bip39_mnemonic_to_seed(mnem,NULL,&seed,BIP39_SEED_LEN_512,&seed_len);
printf("Seed: %x\n",seed);
}