Skip to content

Commit 602c987

Browse files
committed
scene cleanup
1 parent e10c129 commit 602c987

File tree

1 file changed

+106
-83
lines changed

1 file changed

+106
-83
lines changed

views/flipbip_scene_1.c

+106-83
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
// #include "../helpers/printf.h"
1414
#include "../crypto/bip32.h"
1515
#include "../crypto/bip39.h"
16-
// #include "../crypto/ecdsa.h"
1716
#include "../crypto/curves.h"
1817
#include "../crypto/memzero.h"
1918

@@ -27,18 +26,30 @@ struct FlipBipScene1 {
2726
typedef struct {
2827
int page;
2928
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;
4253
} FlipBipScene1Model;
4354

4455
void flipbip_scene_1_set_callback(
@@ -91,74 +102,90 @@ static void flipbip_scene_1_model_init(FlipBipScene1Model* const model, const in
91102
// Generate a random mnemonic using trezor-crypto
92103
model->strength = strength;
93104
//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
95108

96109
// 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);
99112
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] == ' ') {
102115
word++;
103116
if (word % 4 == 0) {
104-
mnemo[i] = ',';
117+
mnemonic_working[i] = ',';
105118
}
106119
}
107120
}
108121

109122
// 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)
113126
{
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++;
117130

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, ",");
126139
}
127140

141+
// Free the working mnemonic memory
142+
memzero(mnemonic_working, strlen(mnemonic_working));
143+
free(mnemonic_working);
144+
145+
// END DRAW MNEMONIC
146+
128147
// 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);
132150

151+
// BEGIN DRAW SEED
152+
153+
char *seed_working = malloc(64 * 2 + 1);
133154
// Convert the seed to a hex string
134155
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);
136157
//sprintf(seed + (i * 2), "%.2x", seedbytes[i]);
137158
}
138159

139160
// 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);
143164

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;
150171
}
151172

173+
// Free the working seed memory
174+
memzero(seed_working, sizeof(seed_working));
175+
free(seed_working);
176+
177+
// END DRAW SEED
178+
152179
// WIP / TODO: Generate a BIP32 root key from the mnemonic
153180

154181
// //bool root_set = false;
155182
HDNode *root = malloc(sizeof(HDNode));
156-
hdnode_from_seed(seedbytes, 64, SECP256K1_NAME, root);
183+
hdnode_from_seed(model->seed, 64, SECP256K1_NAME, root);
157184
// //root_set = true;
158185

159186
// m/44'/0'/0'/0
160187
uint32_t purpose = 44;
161-
uint32_t coin = 0;
188+
uint32_t coin = 0; // Bitcoin
162189
uint32_t account = 0;
163190
uint32_t change = 0;
164191

@@ -174,10 +201,9 @@ static void flipbip_scene_1_model_init(FlipBipScene1Model* const model, const in
174201
// root
175202
uint32_t fingerprint = 0;
176203
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;
181207

182208
HDNode *node = root;
183209

@@ -194,62 +220,59 @@ static void flipbip_scene_1_model_init(FlipBipScene1Model* const model, const in
194220
hdnode_private_ckd_prime(node, account); // account
195221

196222
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;
200226

201227
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;
205231

206-
// external / internal (change) m/44'/0'/0'/0
232+
// external/internal (change) m/44'/0'/0'/0
207233
fingerprint = hdnode_fingerprint(node);
208-
hdnode_private_ckd(node, change); // change
234+
hdnode_private_ckd(node, change); // external/internal (change)
209235

210236
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;
214240

215241
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;
219247

220-
for (int i = 0; i < 5; i++) {
248+
// BEGIN DRAW ADDRESS
249+
250+
for (int i = 0; i < 3; i++) {
221251
HDNode *addr_node = malloc(sizeof(HDNode));
222252
memcpy(addr_node, node, sizeof(HDNode));
223253

224254
hdnode_private_ckd(addr_node, i);
225255
hdnode_fill_public_key(addr_node);
226256
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);
229259
model->seed5 = address;
230260
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);
233263
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"));
237264
memzero(addr_node, sizeof(HDNode));
238265
free(addr_node);
239266
}
240267

268+
// END DRAW ADDRESS
269+
241270
// Clear the root node
242-
memzero(root, sizeof(HDNode));
243-
free(root);
271+
// memzero(root, sizeof(HDNode));
272+
// free(root);
244273

245274
// 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();
253276

254277
#if USE_BIP39_CACHE
255278
// Clear the BIP39 cache

0 commit comments

Comments
 (0)