22
22
#define DERIV_ACCOUNT 0
23
23
#define DERIV_CHANGE 0
24
24
25
+ #define PAGE_LOADING 0
26
+ #define PAGE_INFO 1
27
+ #define PAGE_MNEMONIC 2
28
+ #define PAGE_SEED 3
29
+ #define PAGE_XPRV_ROOT 4
30
+ #define PAGE_XPRV_ACCT 5
31
+ #define PAGE_XPUB_ACCT 6
32
+ #define PAGE_XPRV_EXTD 7
33
+ #define PAGE_XPUB_EXTD 8
34
+ #define PAGE_ADDR_BEGIN 9
35
+ #define PAGE_ADDR_END 14 //18
36
+
25
37
#define TEXT_LOADING "Loading..."
26
38
#define TEXT_NEW_WALLET "New wallet"
27
39
#define TEXT_DEFAULT_COIN "Coin"
@@ -68,12 +80,15 @@ typedef struct {
68
80
CONFIDENTIAL const char * xpub_extended ;
69
81
} FlipBipScene1Model ;
70
82
71
- static CONFIDENTIAL char s_disp_text1 [30 + 1 ];
72
- static CONFIDENTIAL char s_disp_text2 [30 + 1 ];
73
- static CONFIDENTIAL char s_disp_text3 [30 + 1 ];
74
- static CONFIDENTIAL char s_disp_text4 [30 + 1 ];
75
- static CONFIDENTIAL char s_disp_text5 [30 + 1 ];
76
- static CONFIDENTIAL char s_disp_text6 [30 + 1 ];
83
+ static CONFIDENTIAL HDNode * s_addr_node = NULL ;
84
+
85
+ static CONFIDENTIAL char * s_disp_text1 = NULL ;
86
+ static CONFIDENTIAL char * s_disp_text2 = NULL ;
87
+ static CONFIDENTIAL char * s_disp_text3 = NULL ;
88
+ static CONFIDENTIAL char * s_disp_text4 = NULL ;
89
+ static CONFIDENTIAL char * s_disp_text5 = NULL ;
90
+ static CONFIDENTIAL char * s_disp_text6 = NULL ;
91
+
77
92
static const char * s_derivation_text = TEXT_DEFAULT_DERIV ;
78
93
//static bool s_busy = false;
79
94
@@ -116,7 +131,8 @@ static void flipbip_scene_1_draw_generic(const char* text, size_t line_len) {
116
131
117
132
static void flipbip_scene_1_draw_mnemonic (const char * mnemonic ) {
118
133
// Delineate sections of the mnemonic every 4 words
119
- char * mnemonic_working = malloc (strlen (mnemonic ) + 1 );
134
+ const size_t mnemonic_working_len = strlen (mnemonic ) + 1 ;
135
+ char * mnemonic_working = malloc (mnemonic_working_len );
120
136
strcpy (mnemonic_working , mnemonic );
121
137
int word = 0 ;
122
138
for (size_t i = 0 ; i < strlen (mnemonic_working ); i ++ ) {
@@ -159,35 +175,36 @@ static void flipbip_scene_1_draw_mnemonic(const char* mnemonic) {
159
175
}
160
176
161
177
// Free the working mnemonic memory
162
- memzero (mnemonic_working , strlen ( mnemonic_working ) );
178
+ memzero (mnemonic_working , mnemonic_working_len );
163
179
free (mnemonic_working );
164
180
}
165
181
166
182
static void flipbip_scene_1_draw_seed (FlipBipScene1Model * const model ) {
167
- char * seed_working = malloc (64 * 2 + 1 );
183
+ const size_t seed_working_len = 64 * 2 + 1 ;
184
+ char * seed_working = malloc (seed_working_len );
168
185
// Convert the seed to a hex string
169
186
flipbip_btox (model -> seed , 64 , seed_working );
170
187
171
188
flipbip_scene_1_draw_generic (seed_working , 22 );
172
189
173
190
// Free the working seed memory
174
- memzero (seed_working , sizeof ( seed_working ) );
191
+ memzero (seed_working , seed_working_len );
175
192
free (seed_working );
176
193
}
177
194
178
195
static void
179
196
flipbip_scene_1_draw_address (const HDNode * node , uint32_t addr_type , uint32_t addr_index ) {
180
197
//s_busy = true;
181
198
182
- // buffer for key serialization
183
- const size_t buflen = 128 ;
184
- char buf [128 + 1 ];
199
+ // buffer for address serialization
200
+ const size_t buflen = 40 ;
201
+ char buf [40 + 1 ] = { 0 } ;
185
202
186
- HDNode * addr_node = malloc ( sizeof ( HDNode ));
187
- memcpy (addr_node , node , sizeof (HDNode ));
203
+ // use static node for address generation
204
+ memcpy (s_addr_node , node , sizeof (HDNode ));
188
205
189
- hdnode_private_ckd (addr_node , addr_index );
190
- hdnode_fill_public_key (addr_node );
206
+ hdnode_private_ckd (s_addr_node , addr_index );
207
+ hdnode_fill_public_key (s_addr_node );
191
208
192
209
// coin info
193
210
// bip44_coin, xprv_version, xpub_version, addr_version, wif_version, addr_format
@@ -199,32 +216,23 @@ static void
199
216
if (coin_info [5 ] == FlipBipCoinBTC0 ) { // BTC / DOGE style address
200
217
// BTC / DOGE style address
201
218
ecdsa_get_address (
202
- addr_node -> public_key , coin_info [3 ], HASHER_SHA2_RIPEMD , HASHER_SHA2D , buf , buflen );
219
+ s_addr_node -> public_key , coin_info [3 ], HASHER_SHA2_RIPEMD , HASHER_SHA2D , buf , buflen );
203
220
204
- char * address = malloc (buflen + 1 );
205
- strncpy (address , buf , buflen );
206
- flipbip_scene_1_draw_generic (address , 12 );
207
- memzero (address , buflen + 1 );
208
- free (address );
221
+ flipbip_scene_1_draw_generic (buf , 12 );
209
222
210
223
//ecdsa_get_wif(addr_node->private_key, WIF_VERSION, HASHER_SHA2D, buf, buflen);
211
- //char *wif = malloc(buflen + 1);
212
- //strncpy(wif, buf, buflen);
224
+
213
225
} else if (coin_info [5 ] == FlipBipCoinETH60 ) { // ETH
214
226
// ETH style address
215
- hdnode_get_ethereum_pubkeyhash (addr_node , (uint8_t * )buf );
216
- char * address = malloc (42 + 1 );
217
- memcpy (address , "0x" , 2 );
227
+ hdnode_get_ethereum_pubkeyhash (s_addr_node , (uint8_t * )buf );
228
+ char address [42 + 1 ] = {0 };
229
+ address [0 ] = '0' ;
230
+ address [1 ] = 'x' ;
218
231
// Convert the hash to a hex string
219
232
flipbip_btox ((uint8_t * )buf , 20 , address + 2 );
220
233
flipbip_scene_1_draw_generic (address , 12 );
221
- memzero (address , 42 + 1 );
222
- free (address );
223
234
}
224
235
225
- memzero (addr_node , sizeof (HDNode ));
226
- free (addr_node );
227
-
228
236
//s_busy = false;
229
237
}
230
238
@@ -243,32 +251,33 @@ void flipbip_scene_1_draw(Canvas* canvas, FlipBipScene1Model* model) {
243
251
canvas_set_color (canvas , ColorBlack );
244
252
245
253
flipbip_scene_1_clear_text ();
246
- if (model -> page == 1 ) {
254
+ if (model -> page == PAGE_INFO ) {
247
255
flipbip_scene_1_draw_generic (TEXT_INFO , 27 );
248
- } else if (model -> page == 2 ) {
256
+ } else if (model -> page == PAGE_MNEMONIC ) {
249
257
flipbip_scene_1_draw_mnemonic (model -> mnemonic );
250
- } else if (model -> page == 3 ) {
258
+ } else if (model -> page == PAGE_SEED ) {
251
259
flipbip_scene_1_draw_seed (model );
252
- } else if (model -> page == 4 ) {
260
+ } else if (model -> page == PAGE_XPRV_ROOT ) {
253
261
flipbip_scene_1_draw_generic (model -> xprv_root , 20 );
254
- } else if (model -> page == 5 ) {
262
+ } else if (model -> page == PAGE_XPRV_ACCT ) {
255
263
flipbip_scene_1_draw_generic (model -> xprv_account , 20 );
256
- } else if (model -> page == 6 ) {
264
+ } else if (model -> page == PAGE_XPUB_ACCT ) {
257
265
flipbip_scene_1_draw_generic (model -> xpub_account , 20 );
258
- } else if (model -> page == 7 ) {
266
+ } else if (model -> page == PAGE_XPRV_EXTD ) {
259
267
flipbip_scene_1_draw_generic (model -> xprv_extended , 20 );
260
- } else if (model -> page == 8 ) {
268
+ } else if (model -> page == PAGE_XPUB_EXTD ) {
261
269
flipbip_scene_1_draw_generic (model -> xpub_extended , 20 );
262
- } else if (model -> page >= 9 && model -> page <= 13 ) {
263
- flipbip_scene_1_draw_address (model -> node , model -> coin , model -> page - 9 );
270
+ } else if (model -> page >= PAGE_ADDR_BEGIN && model -> page <= PAGE_ADDR_END ) {
271
+ flipbip_scene_1_draw_address (model -> node , model -> coin , model -> page - PAGE_ADDR_BEGIN );
264
272
}
265
273
266
- if (model -> page == 0 ) {
274
+ if (model -> page == PAGE_LOADING ) {
267
275
canvas_set_font (canvas , FontPrimary );
268
276
canvas_draw_str (canvas , 1 , 10 , TEXT_LOADING );
269
277
canvas_draw_str (canvas , 6 , 30 , s_derivation_text );
270
278
canvas_draw_icon (canvas , 86 , 25 , & I_Keychain_39x36 );
271
- } else if (model -> page >= 9 && model -> page <= 13 ) {
279
+ } else if (model -> page >= PAGE_ADDR_BEGIN && model -> page <= PAGE_ADDR_END ) {
280
+ // draw address header
272
281
canvas_set_font (canvas , FontSecondary );
273
282
// coin_name, derivation_path
274
283
const char * receive_text = COIN_TEXT_ARRAY [model -> coin ][0 ];
@@ -278,6 +287,15 @@ void flipbip_scene_1_draw(Canvas* canvas, FlipBipScene1Model* model) {
278
287
const size_t receive_len = strlen (receive_text ) * 7 ;
279
288
canvas_draw_str_aligned (canvas , 1 , 2 , AlignLeft , AlignTop , receive_text );
280
289
canvas_draw_str_aligned (canvas , receive_len , 2 , AlignLeft , AlignTop , TEXT_RECEIVE_ADDRESS );
290
+
291
+ // draw address number
292
+ const unsigned char addr_num [1 ] = {(unsigned char )(model -> page - PAGE_ADDR_BEGIN )};
293
+ char addr_num_text [3 ];
294
+ flipbip_btox (addr_num , 1 , addr_num_text );
295
+ addr_num_text [0 ] = '/' ;
296
+ canvas_draw_str_aligned (canvas , 110 , 2 , AlignLeft , AlignTop , addr_num_text );
297
+
298
+ // draw address
281
299
canvas_set_font (canvas , FontPrimary );
282
300
canvas_draw_str (canvas , 6 , 22 , s_disp_text1 );
283
301
canvas_draw_str (canvas , 6 , 34 , s_disp_text2 );
@@ -300,7 +318,7 @@ static int flipbip_scene_1_model_init(
300
318
const uint32_t coin ,
301
319
const bool overwrite ,
302
320
const char * passphrase_text ) {
303
- model -> page = 0 ;
321
+ model -> page = PAGE_LOADING ;
304
322
model -> mnemonic_only = false;
305
323
model -> strength = strength ;
306
324
model -> coin = coin ;
@@ -414,7 +432,7 @@ static int flipbip_scene_1_model_init(
414
432
415
433
model -> node = node ;
416
434
417
- model -> page = 1 ;
435
+ model -> page = PAGE_INFO ;
418
436
419
437
#if USE_BIP39_CACHE
420
438
// Clear the BIP39 cache
@@ -454,10 +472,11 @@ bool flipbip_scene_1_input(InputEvent* event, void* context) {
454
472
FlipBipScene1Model * model ,
455
473
{
456
474
//UNUSED(model);
457
- model -> page = (model -> page + 1 ) % 14 ;
458
- if (model -> page == 0 ) {
459
- model -> page = 1 ;
475
+ int page = (model -> page + 1 ) % ( PAGE_ADDR_END + 1 ) ;
476
+ if (page == 0 ) {
477
+ page = PAGE_INFO ;
460
478
}
479
+ model -> page = page ;
461
480
},
462
481
true);
463
482
break ;
@@ -468,10 +487,11 @@ bool flipbip_scene_1_input(InputEvent* event, void* context) {
468
487
FlipBipScene1Model * model ,
469
488
{
470
489
//UNUSED(model);
471
- model -> page = (model -> page - 1 ) % 14 ;
472
- if (model -> page == 0 ) {
473
- model -> page = 13 ;
490
+ int page = (model -> page - 1 ) % ( PAGE_ADDR_END + 1 ) ;
491
+ if (page == 0 ) {
492
+ page = PAGE_ADDR_END ;
474
493
}
494
+ model -> page = page ;
475
495
},
476
496
true);
477
497
break ;
@@ -490,8 +510,8 @@ void flipbip_scene_1_exit(void* context) {
490
510
instance -> view ,
491
511
FlipBipScene1Model * model ,
492
512
{
493
- model -> page = 0 ;
494
- model -> strength = 0 ;
513
+ model -> page = PAGE_LOADING ;
514
+ model -> strength = FlipBipStrength256 ;
495
515
model -> coin = FlipBipCoinBTC0 ;
496
516
memzero (model -> seed , 64 );
497
517
// if mnemonic_only is true, then we don't need to free the data here
@@ -570,15 +590,15 @@ void flipbip_scene_1_enter(void* context) {
570
590
// if error, set the error message
571
591
if (status == FlipBipStatusSaveError ) {
572
592
model -> mnemonic = "ERROR:,Save error" ;
573
- model -> page = 2 ;
593
+ model -> page = PAGE_MNEMONIC ;
574
594
flipbip_play_long_bump (app );
575
595
} else if (status == FlipBipStatusLoadError ) {
576
596
model -> mnemonic = "ERROR:,Load error" ;
577
- model -> page = 2 ;
597
+ model -> page = PAGE_MNEMONIC ;
578
598
flipbip_play_long_bump (app );
579
599
} else if (status == FlipBipStatusMnemonicCheckError ) {
580
600
model -> mnemonic = "ERROR:,Mnemonic check error" ;
581
- model -> page = 2 ;
601
+ model -> page = PAGE_MNEMONIC ;
582
602
flipbip_play_long_bump (app );
583
603
}
584
604
@@ -602,6 +622,17 @@ FlipBipScene1* flipbip_scene_1_alloc() {
602
622
view_set_enter_callback (instance -> view , flipbip_scene_1_enter );
603
623
view_set_exit_callback (instance -> view , flipbip_scene_1_exit );
604
624
625
+ // allocate the address node
626
+ s_addr_node = (HDNode * )malloc (sizeof (HDNode ));
627
+
628
+ // allocate the display text
629
+ s_disp_text1 = (char * )malloc (30 + 1 );
630
+ s_disp_text2 = (char * )malloc (30 + 1 );
631
+ s_disp_text3 = (char * )malloc (30 + 1 );
632
+ s_disp_text4 = (char * )malloc (30 + 1 );
633
+ s_disp_text5 = (char * )malloc (30 + 1 );
634
+ s_disp_text6 = (char * )malloc (30 + 1 );
635
+
605
636
return instance ;
606
637
}
607
638
@@ -611,7 +642,18 @@ void flipbip_scene_1_free(FlipBipScene1* instance) {
611
642
with_view_model (
612
643
instance -> view , FlipBipScene1Model * model , { UNUSED (model ); }, true);
613
644
645
+ // free the address node
646
+ memzero (s_addr_node , sizeof (HDNode ));
647
+ free (s_addr_node );
648
+
649
+ // free the display text
614
650
flipbip_scene_1_clear_text ();
651
+ free (s_disp_text1 );
652
+ free (s_disp_text2 );
653
+ free (s_disp_text3 );
654
+ free (s_disp_text4 );
655
+ free (s_disp_text5 );
656
+ free (s_disp_text6 );
615
657
616
658
view_free (instance -> view );
617
659
free (instance );
0 commit comments