From 9d1b60d50c872c76d897a6778e0747d8fde5b7dc Mon Sep 17 00:00:00 2001 From: Shannon Appelcline Date: Tue, 11 Aug 2020 09:16:02 -1000 Subject: [PATCH] Create 16_2_genmnemonic.c --- src/16_2_genmnemonic.c | 70 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/16_2_genmnemonic.c diff --git a/src/16_2_genmnemonic.c b/src/16_2_genmnemonic.c new file mode 100644 index 0000000..34ade15 --- /dev/null +++ b/src/16_2_genmnemonic.c @@ -0,0 +1,70 @@ +#include +#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. Validate the Mnemonic */ + + lw_response = bip39_mnemonic_validate(NULL,mnem); + + if (lw_response) { + + printf("Error: Mnemonic did not validate: %d\n",lw_response); + exit(-1); + + } else { + + printf("Mnemonic validated!\n"); + + } + + /* 5. Translate into Seed */ + + size_t seed_len; + unsigned char *seed; + + lw_response = bip39_mnemonic_to_seed(mnem,NULL,seed,BIP39_SEED_LEN_512,&seed_len); + + if (lw_response) { + + printf("Error: bip39_mnemonic_to_seed failed: %d\n",lw_response); + exit(-1); + + } + + +}