-
Notifications
You must be signed in to change notification settings - Fork 0
/
16_2_genmnemonic.c
81 lines (48 loc) · 1.47 KB
/
16_2_genmnemonic.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#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. Validate a 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 */
unsigned char seed[BIP39_SEED_LEN_512];
size_t seed_len;
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);
}
/* 6. Print the Seed */
char *seed_hex;
wally_hex_from_bytes(seed,sizeof(seed),&seed_hex);
printf("Seed: %s\n",seed_hex);
/* Always cleanup: the docs clearly tell us what to free */
wally_free_string(mnem);
wally_free_string(seed_hex);
wally_cleanup(0);
}