Skip to content

Commit f8f744e

Browse files
committed
pregenerate addresses
1 parent 3f93a8f commit f8f744e

File tree

1 file changed

+68
-58
lines changed

1 file changed

+68
-58
lines changed

views/flipbip_scene_1.c

Lines changed: 68 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
#define DERIV_ACCOUNT 0
2323
#define DERIV_CHANGE 0
2424

25+
#define MAX_ADDR_LEN 42 + 1 // 42 = max length of address + null terminator
26+
#define NUM_ADDRS 6
27+
2528
#define PAGE_LOADING 0
2629
#define PAGE_INFO 1
2730
#define PAGE_MNEMONIC 2
@@ -32,9 +35,7 @@
3235
#define PAGE_XPRV_EXTD 7
3336
#define PAGE_XPUB_EXTD 8
3437
#define PAGE_ADDR_BEGIN 9
35-
#define PAGE_ADDR_END 14 //18
36-
37-
#define MAX_ADDR_LEN 42 + 1 // 42 = max length of address + null terminator
38+
#define PAGE_ADDR_END (PAGE_ADDR_BEGIN + NUM_ADDRS - 1)
3839

3940
#define TEXT_LOADING "Loading..."
4041
#define TEXT_NEW_WALLET "New wallet"
@@ -85,11 +86,11 @@ typedef struct {
8586
CONFIDENTIAL const char* xpub_account;
8687
CONFIDENTIAL const char* xprv_extended;
8788
CONFIDENTIAL const char* xpub_extended;
89+
char* recv_addresses[NUM_ADDRS];
8890
} FlipBipScene1Model;
8991

9092
// Node for the receive address
9193
static CONFIDENTIAL HDNode* s_addr_node = NULL;
92-
static char* s_addr_text = NULL;
9394
// Generic display text
9495
static CONFIDENTIAL char* s_disp_text1 = NULL;
9596
static CONFIDENTIAL char* s_disp_text2 = NULL;
@@ -111,6 +112,54 @@ void flipbip_scene_1_set_callback(
111112
instance->context = context;
112113
}
113114

115+
static void flipbip_scene_1_init_address(
116+
char* addr_text,
117+
const HDNode* node,
118+
uint32_t coin_type,
119+
uint32_t addr_index) {
120+
//s_busy = true;
121+
122+
// Buffer for address serialization
123+
const size_t buflen = 40;
124+
char buf[40 + 1] = {0};
125+
126+
// Use static node for address generation
127+
memcpy(s_addr_node, node, sizeof(HDNode));
128+
memzero(addr_text, MAX_ADDR_LEN);
129+
130+
hdnode_private_ckd(s_addr_node, addr_index);
131+
hdnode_fill_public_key(s_addr_node);
132+
133+
// coin info
134+
// bip44_coin, xprv_version, xpub_version, addr_version, wif_version, addr_format
135+
uint32_t coin_info[6] = {0};
136+
for(size_t i = 0; i < 6; i++) {
137+
coin_info[i] = COIN_INFO_ARRAY[coin_type][i];
138+
}
139+
140+
if(coin_info[5] == FlipBipCoinBTC0) { // BTC / DOGE style address
141+
// BTC / DOGE style address
142+
ecdsa_get_address(
143+
s_addr_node->public_key, coin_info[3], HASHER_SHA2_RIPEMD, HASHER_SHA2D, buf, buflen);
144+
strcpy(addr_text, buf);
145+
146+
//ecdsa_get_wif(addr_node->private_key, WIF_VERSION, HASHER_SHA2D, buf, buflen);
147+
148+
} else if(coin_info[5] == FlipBipCoinETH60) { // ETH
149+
// ETH style address
150+
hdnode_get_ethereum_pubkeyhash(s_addr_node, (uint8_t*)buf);
151+
addr_text[0] = '0';
152+
addr_text[1] = 'x';
153+
// Convert the hash to a hex string
154+
flipbip_btox((uint8_t*)buf, 20, addr_text + 2);
155+
}
156+
157+
// Clear the address node
158+
memzero(s_addr_node, sizeof(HDNode));
159+
160+
//s_busy = false;
161+
}
162+
114163
static void flipbip_scene_1_draw_generic(const char* text, size_t line_len) {
115164
// Split the text into parts
116165
for(size_t si = 1; si <= 6; si++) {
@@ -201,55 +250,6 @@ static void flipbip_scene_1_draw_seed(FlipBipScene1Model* const model) {
201250
free(seed_working);
202251
}
203252

204-
static void
205-
flipbip_scene_1_draw_address(const HDNode* node, uint32_t addr_type, uint32_t addr_index) {
206-
//s_busy = true;
207-
208-
// Buffer for address serialization
209-
const size_t buflen = 40;
210-
char buf[40 + 1] = {0};
211-
212-
// Use static node for address generation
213-
memcpy(s_addr_node, node, sizeof(HDNode));
214-
memzero(s_addr_text, MAX_ADDR_LEN);
215-
216-
hdnode_private_ckd(s_addr_node, addr_index);
217-
hdnode_fill_public_key(s_addr_node);
218-
219-
// coin info
220-
// bip44_coin, xprv_version, xpub_version, addr_version, wif_version, addr_format
221-
uint32_t coin_info[6] = {0};
222-
for(size_t i = 0; i < 6; i++) {
223-
coin_info[i] = COIN_INFO_ARRAY[addr_type][i];
224-
}
225-
226-
if(coin_info[5] == FlipBipCoinBTC0) { // BTC / DOGE style address
227-
// BTC / DOGE style address
228-
ecdsa_get_address(
229-
s_addr_node->public_key, coin_info[3], HASHER_SHA2_RIPEMD, HASHER_SHA2D, buf, buflen);
230-
231-
strcpy(s_addr_text, buf);
232-
flipbip_scene_1_draw_generic(s_addr_text, 12);
233-
234-
//ecdsa_get_wif(addr_node->private_key, WIF_VERSION, HASHER_SHA2D, buf, buflen);
235-
236-
} else if(coin_info[5] == FlipBipCoinETH60) { // ETH
237-
// ETH style address
238-
hdnode_get_ethereum_pubkeyhash(s_addr_node, (uint8_t*)buf);
239-
s_addr_text[0] = '0';
240-
s_addr_text[1] = 'x';
241-
// Convert the hash to a hex string
242-
flipbip_btox((uint8_t*)buf, 20, s_addr_text + 2);
243-
244-
flipbip_scene_1_draw_generic(s_addr_text, 12);
245-
}
246-
247-
// Clear the address node
248-
memzero(s_addr_node, sizeof(HDNode));
249-
250-
//s_busy = false;
251-
}
252-
253253
static void flipbip_scene_1_clear_text() {
254254
memzero((void*)s_disp_text1, 30 + 1);
255255
memzero((void*)s_disp_text2, 30 + 1);
@@ -282,7 +282,7 @@ void flipbip_scene_1_draw(Canvas* canvas, FlipBipScene1Model* model) {
282282
} else if(model->page == PAGE_XPUB_EXTD) {
283283
flipbip_scene_1_draw_generic(model->xpub_extended, 20);
284284
} else if(model->page >= PAGE_ADDR_BEGIN && model->page <= PAGE_ADDR_END) {
285-
flipbip_scene_1_draw_address(model->node, model->coin, model->page - PAGE_ADDR_BEGIN);
285+
flipbip_scene_1_draw_generic(model->recv_addresses[model->page - PAGE_ADDR_BEGIN], 12);
286286
elements_button_right(canvas, TEXT_SAVE_QR);
287287
}
288288

@@ -448,6 +448,13 @@ static int flipbip_scene_1_model_init(
448448

449449
model->node = node;
450450

451+
// Initialize addresses
452+
for(int a = 0; a < NUM_ADDRS; a++) {
453+
model->recv_addresses[a] = malloc(MAX_ADDR_LEN);
454+
memzero(model->recv_addresses[a], MAX_ADDR_LEN);
455+
flipbip_scene_1_init_address(model->recv_addresses[a], node, coin_info[5], a);
456+
}
457+
451458
model->page = PAGE_INFO;
452459

453460
#if USE_BIP39_CACHE
@@ -519,7 +526,9 @@ bool flipbip_scene_1_input(InputEvent* event, void* context) {
519526
char qrbuf[90] = {0};
520527
strcpy(qrbuf, TEXT_QRFILE);
521528
strcpy(qrbuf + strlen(qrbuf), COIN_TEXT_ARRAY[model->coin][2]);
522-
strcpy(qrbuf + strlen(qrbuf), s_addr_text);
529+
strcpy(
530+
qrbuf + strlen(qrbuf),
531+
model->recv_addresses[model->page - PAGE_ADDR_BEGIN]);
523532
flipbip_save_settings(qrbuf, FlipBipFileOther, TEXT_QRFILE_EXT, false);
524533
}
525534
},
@@ -560,6 +569,10 @@ void flipbip_scene_1_exit(void* context) {
560569
free((void*)model->xpub_account);
561570
free((void*)model->xprv_extended);
562571
free((void*)model->xpub_extended);
572+
for(int a = 0; a < NUM_ADDRS; a++) {
573+
memzero((void*)model->recv_addresses[a], MAX_ADDR_LEN);
574+
free((void*)model->recv_addresses[a]);
575+
}
563576
}
564577
},
565578
true);
@@ -654,7 +667,6 @@ FlipBipScene1* flipbip_scene_1_alloc() {
654667

655668
// allocate the address node
656669
s_addr_node = (HDNode*)malloc(sizeof(HDNode));
657-
s_addr_text = (char*)malloc(MAX_ADDR_LEN);
658670

659671
// allocate the display text
660672
s_disp_text1 = (char*)malloc(30 + 1);
@@ -676,8 +688,6 @@ void flipbip_scene_1_free(FlipBipScene1* instance) {
676688
// free the address node
677689
memzero(s_addr_node, sizeof(HDNode));
678690
free(s_addr_node);
679-
memzero(s_addr_text, MAX_ADDR_LEN);
680-
free(s_addr_text);
681691

682692
// free the display text
683693
flipbip_scene_1_clear_text();

0 commit comments

Comments
 (0)