13
13
// #include "../helpers/printf.h"
14
14
#include "../crypto/bip32.h"
15
15
#include "../crypto/bip39.h"
16
- // #include "../crypto/ecdsa.h"
17
16
#include "../crypto/curves.h"
18
17
#include "../crypto/memzero.h"
19
18
@@ -27,18 +26,30 @@ struct FlipBipScene1 {
27
26
typedef struct {
28
27
int page ;
29
28
int strength ;
30
- const char * seed1 ;
31
- const char * seed2 ;
32
- const char * seed3 ;
33
- const char * seed4 ;
34
- const char * seed5 ;
35
- const char * seed6 ;
36
- const char * mnemonic1 ;
37
- const char * mnemonic2 ;
38
- const char * mnemonic3 ;
39
- const char * mnemonic4 ;
40
- const char * mnemonic5 ;
41
- const char * mnemonic6 ;
29
+
30
+ // for crypto
31
+ CONFIDENTIAL const char * mnemonic ;
32
+ CONFIDENTIAL uint8_t seed [64 ];
33
+ CONFIDENTIAL const HDNode * node ;
34
+ CONFIDENTIAL const char * xprv_root ;
35
+ CONFIDENTIAL const char * xprv_account ;
36
+ CONFIDENTIAL const char * xpub_account ;
37
+ CONFIDENTIAL const char * xprv_extended ;
38
+ CONFIDENTIAL const char * xpub_extended ;
39
+
40
+ // for display
41
+ CONFIDENTIAL const char * seed1 ;
42
+ CONFIDENTIAL const char * seed2 ;
43
+ CONFIDENTIAL const char * seed3 ;
44
+ CONFIDENTIAL const char * seed4 ;
45
+ CONFIDENTIAL const char * seed5 ;
46
+ CONFIDENTIAL const char * seed6 ;
47
+ CONFIDENTIAL const char * mnemonic1 ;
48
+ CONFIDENTIAL const char * mnemonic2 ;
49
+ CONFIDENTIAL const char * mnemonic3 ;
50
+ CONFIDENTIAL const char * mnemonic4 ;
51
+ CONFIDENTIAL const char * mnemonic5 ;
52
+ CONFIDENTIAL const char * mnemonic6 ;
42
53
} FlipBipScene1Model ;
43
54
44
55
void flipbip_scene_1_set_callback (
@@ -91,74 +102,90 @@ static void flipbip_scene_1_model_init(FlipBipScene1Model* const model, const in
91
102
// Generate a random mnemonic using trezor-crypto
92
103
model -> strength = strength ;
93
104
//const char *mnemonic = mnemonic_generate(model->strength);
94
- const char * mnemonic = "wealth budget salt video delay obey neutral tail sure soda hold rubber joy movie boat raccoon tornado noise off inmate payment patch group topple" ;
105
+ model -> mnemonic = "wealth budget salt video delay obey neutral tail sure soda hold rubber joy movie boat raccoon tornado noise off inmate payment patch group topple" ;
106
+
107
+ // START DRAW MNEMONIC
95
108
96
109
// Delineate sections of the mnemonic every 4 words
97
- char * mnemo = malloc (strlen (mnemonic ) + 1 );
98
- strcpy (mnemo , mnemonic );
110
+ char * mnemonic_working = malloc (strlen (model -> mnemonic ) + 1 );
111
+ strcpy (mnemonic_working , model -> mnemonic );
99
112
int word = 0 ;
100
- for (size_t i = 0 ; i < strlen (mnemo ); i ++ ) {
101
- if (mnemo [i ] == ' ' ) {
113
+ for (size_t i = 0 ; i < strlen (mnemonic_working ); i ++ ) {
114
+ if (mnemonic_working [i ] == ' ' ) {
102
115
word ++ ;
103
116
if (word % 4 == 0 ) {
104
- mnemo [i ] = ',' ;
117
+ mnemonic_working [i ] = ',' ;
105
118
}
106
119
}
107
120
}
108
121
109
122
// Split the mnemonic into parts
110
- char * mnemopart = flipbip_strtok (mnemo , "," );
111
- int partnum = 0 ;
112
- while (mnemopart != NULL )
123
+ char * mnemonic_part = flipbip_strtok (mnemonic_working , "," );
124
+ int mi = 0 ;
125
+ while (mnemonic_part != NULL )
113
126
{
114
- char * partptr = malloc (strlen (mnemopart ) + 1 );
115
- strcpy (partptr , mnemopart );
116
- partnum ++ ;
127
+ char * ptr = malloc (strlen (mnemonic_part ) + 1 );
128
+ strcpy (ptr , mnemonic_part );
129
+ mi ++ ;
117
130
118
- if ( partnum == 1 ) model -> mnemonic1 = partptr ;
119
- if (partnum == 2 ) model -> mnemonic2 = partptr ;
120
- if (partnum == 3 ) model -> mnemonic3 = partptr ;
121
- if (partnum == 4 ) model -> mnemonic4 = partptr ;
122
- if (partnum == 5 ) model -> mnemonic5 = partptr ;
123
- if (partnum == 6 ) model -> mnemonic6 = partptr ;
124
-
125
- mnemopart = flipbip_strtok (NULL , "," );
131
+ if ( mi == 1 ) model -> mnemonic1 = ptr ;
132
+ else if (mi == 2 ) model -> mnemonic2 = ptr ;
133
+ else if (mi == 3 ) model -> mnemonic3 = ptr ;
134
+ else if (mi == 4 ) model -> mnemonic4 = ptr ;
135
+ else if (mi == 5 ) model -> mnemonic5 = ptr ;
136
+ else if (mi == 6 ) model -> mnemonic6 = ptr ;
137
+
138
+ mnemonic_part = flipbip_strtok (NULL , "," );
126
139
}
127
140
141
+ // Free the working mnemonic memory
142
+ memzero (mnemonic_working , strlen (mnemonic_working ));
143
+ free (mnemonic_working );
144
+
145
+ // END DRAW MNEMONIC
146
+
128
147
// Generate a BIP39 seed from the mnemonic
129
- uint8_t seedbytes [64 ];
130
- mnemonic_to_seed (mnemonic , "" , seedbytes , 0 );
131
- char * seed = malloc (64 * 2 + 1 );
148
+ //uint8_t seedbytes[64];
149
+ mnemonic_to_seed (model -> mnemonic , "" , model -> seed , 0 );
132
150
151
+ // BEGIN DRAW SEED
152
+
153
+ char * seed_working = malloc (64 * 2 + 1 );
133
154
// Convert the seed to a hex string
134
155
for (size_t i = 0 ; i < 64 ; i ++ ) {
135
- flipbip_itoa (seedbytes [i ], seed + (i * 2 ), 64 * 2 + 1 , 16 );
156
+ flipbip_itoa (model -> seed [i ], seed_working + (i * 2 ), 64 * 2 + 1 , 16 );
136
157
//sprintf(seed + (i * 2), "%.2x", seedbytes[i]);
137
158
}
138
159
139
160
// Split the seed into parts
140
- for (size_t seedpartnum = 1 ; seedpartnum <= 6 ; seedpartnum ++ ) {
141
- char * seedpartptr = malloc (22 + 1 );
142
- strncpy (seedpartptr , seed + ((seedpartnum - 1 ) * 22 ), 22 );
161
+ for (size_t si = 1 ; si <= 6 ; si ++ ) {
162
+ char * ptr = malloc (22 + 1 );
163
+ strncpy (ptr , seed_working + ((si - 1 ) * 22 ), 22 );
143
164
144
- if ( seedpartnum == 1 ) model -> seed1 = seedpartptr ;
145
- if (seedpartnum == 2 ) model -> seed2 = seedpartptr ;
146
- if (seedpartnum == 3 ) model -> seed3 = seedpartptr ;
147
- if (seedpartnum == 4 ) model -> seed4 = seedpartptr ;
148
- if (seedpartnum == 5 ) model -> seed5 = seedpartptr ;
149
- if (seedpartnum == 6 ) model -> seed6 = seedpartptr ;
165
+ if ( si == 1 ) model -> seed1 = ptr ;
166
+ else if (si == 2 ) model -> seed2 = ptr ;
167
+ else if (si == 3 ) model -> seed3 = ptr ;
168
+ else if (si == 4 ) model -> seed4 = ptr ;
169
+ else if (si == 5 ) model -> seed5 = ptr ;
170
+ else if (si == 6 ) model -> seed6 = ptr ;
150
171
}
151
172
173
+ // Free the working seed memory
174
+ memzero (seed_working , sizeof (seed_working ));
175
+ free (seed_working );
176
+
177
+ // END DRAW SEED
178
+
152
179
// WIP / TODO: Generate a BIP32 root key from the mnemonic
153
180
154
181
// //bool root_set = false;
155
182
HDNode * root = malloc (sizeof (HDNode ));
156
- hdnode_from_seed (seedbytes , 64 , SECP256K1_NAME , root );
183
+ hdnode_from_seed (model -> seed , 64 , SECP256K1_NAME , root );
157
184
// //root_set = true;
158
185
159
186
// m/44'/0'/0'/0
160
187
uint32_t purpose = 44 ;
161
- uint32_t coin = 0 ;
188
+ uint32_t coin = 0 ; // Bitcoin
162
189
uint32_t account = 0 ;
163
190
uint32_t change = 0 ;
164
191
@@ -174,10 +201,9 @@ static void flipbip_scene_1_model_init(FlipBipScene1Model* const model, const in
174
201
// root
175
202
uint32_t fingerprint = 0 ;
176
203
hdnode_serialize_private (root , fingerprint , version_private , buf , buflen );
177
- char * xprv_root = malloc (22 + 1 );
178
- strncpy (xprv_root , buf , 22 );
179
- model -> seed2 = xprv_root ;
180
-
204
+ char * xprv_root = malloc (buflen + 1 );
205
+ strncpy (xprv_root , buf , buflen );
206
+ model -> xprv_root = xprv_root ;
181
207
182
208
HDNode * node = root ;
183
209
@@ -194,62 +220,59 @@ static void flipbip_scene_1_model_init(FlipBipScene1Model* const model, const in
194
220
hdnode_private_ckd_prime (node , account ); // account
195
221
196
222
hdnode_serialize_private (node , fingerprint , version_private , buf , buflen );
197
- char * xprv_acc = malloc (22 + 1 );
198
- strncpy (xprv_acc , buf , 22 );
199
- model -> seed3 = xprv_acc ;
223
+ char * xprv_acc = malloc (buflen + 1 );
224
+ strncpy (xprv_acc , buf , buflen );
225
+ model -> xprv_account = xprv_acc ;
200
226
201
227
hdnode_serialize_public (node , fingerprint , version_public , buf , buflen );
202
- char * xpub_acc = malloc (22 + 1 );
203
- strncpy (xpub_acc , buf , 22 );
204
- model -> seed4 = xpub_acc ;
228
+ char * xpub_acc = malloc (buflen + 1 );
229
+ strncpy (xpub_acc , buf , buflen );
230
+ model -> xpub_account = xpub_acc ;
205
231
206
- // external / internal (change) m/44'/0'/0'/0
232
+ // external/ internal (change) m/44'/0'/0'/0
207
233
fingerprint = hdnode_fingerprint (node );
208
- hdnode_private_ckd (node , change ); // change
234
+ hdnode_private_ckd (node , change ); // external/internal ( change)
209
235
210
236
hdnode_serialize_private (node , fingerprint , version_private , buf , buflen );
211
- char * xprv_chg = malloc (22 + 1 );
212
- strncpy (xprv_chg , buf , 22 );
213
- model -> seed5 = xprv_chg ;
237
+ char * xprv_ext = malloc (buflen + 1 );
238
+ strncpy (xprv_ext , buf , buflen );
239
+ model -> xprv_extended = xprv_ext ;
214
240
215
241
hdnode_serialize_public (node , fingerprint , version_public , buf , buflen );
216
- char * xpub_chg = malloc (22 + 1 );
217
- strncpy (xpub_chg , buf , 22 );
218
- model -> seed6 = xpub_chg ;
242
+ char * xpub_ext = malloc (buflen + 1 );
243
+ strncpy (xpub_ext , buf , buflen );
244
+ model -> xpub_extended = xpub_ext ;
245
+
246
+ model -> node = node ;
219
247
220
- for (int i = 0 ; i < 5 ; i ++ ) {
248
+ // BEGIN DRAW ADDRESS
249
+
250
+ for (int i = 0 ; i < 3 ; i ++ ) {
221
251
HDNode * addr_node = malloc (sizeof (HDNode ));
222
252
memcpy (addr_node , node , sizeof (HDNode ));
223
253
224
254
hdnode_private_ckd (addr_node , i );
225
255
hdnode_fill_public_key (addr_node );
226
256
ecdsa_get_address (addr_node -> public_key , addr_version , HASHER_SHA2_RIPEMD , HASHER_SHA2D , buf , buflen );
227
- char * address = malloc (22 + 1 );
228
- strncpy (address , buf , 22 );
257
+ char * address = malloc (buflen + 1 );
258
+ strncpy (address , buf , buflen );
229
259
model -> seed5 = address ;
230
260
ecdsa_get_wif (addr_node -> private_key , wif_version , HASHER_SHA2D , buf , buflen );
231
- char * wif = malloc (22 + 1 );
232
- strncpy (wif , buf , 22 );
261
+ char * wif = malloc (buflen + 1 );
262
+ strncpy (wif , buf , buflen );
233
263
model -> seed6 = wif ;
234
- // list->setItem(i, 0, new QTableWidgetItem(address));
235
- // list->setItem(i, 1, new QTableWidgetItem(wif));
236
- // list->setItem(i, 2, new QTableWidgetItem("0.0"));
237
264
memzero (addr_node , sizeof (HDNode ));
238
265
free (addr_node );
239
266
}
240
267
268
+ // END DRAW ADDRESS
269
+
241
270
// Clear the root node
242
- memzero (root , sizeof (HDNode ));
243
- free (root );
271
+ // memzero(root, sizeof(HDNode));
272
+ // free(root);
244
273
245
274
// Clear the mnemonic
246
- mnemonic_clear ();
247
- memzero (mnemo , strlen (mnemo ));
248
- free (mnemo );
249
-
250
- // Clear the seed
251
- memzero (seed , sizeof (seed ));
252
- free (seed );
275
+ //mnemonic_clear();
253
276
254
277
#if USE_BIP39_CACHE
255
278
// Clear the BIP39 cache
0 commit comments