flipbip_scene_1.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. #include "../flipbip.h"
  2. #include <furi.h>
  3. #include <furi_hal.h>
  4. #include <input/input.h>
  5. #include <gui/elements.h>
  6. //#include <dolphin/dolphin.h>
  7. #include <storage/storage.h>
  8. #include <string.h>
  9. #include "FlipBIP_icons.h"
  10. #include "../helpers/flipbip_haptic.h"
  11. #include "../helpers/flipbip_led.h"
  12. #include "../helpers/flipbip_string.h"
  13. #include "../helpers/flipbip_file.h"
  14. // From: /lib/crypto
  15. #include <memzero.h>
  16. #include <rand.h>
  17. #include <curves.h>
  18. #include <bip32.h>
  19. #include <bip39.h>
  20. #define DERIV_PURPOSE 44
  21. #define DERIV_ACCOUNT 0
  22. #define DERIV_CHANGE 0
  23. #define MAX_ADDR_LEN 42 + 1 // 42 = max length of address + null terminator
  24. #define NUM_ADDRS 6
  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 (PAGE_ADDR_BEGIN + NUM_ADDRS - 1)
  36. #define TEXT_LOADING "Loading..."
  37. #define TEXT_NEW_WALLET "New wallet"
  38. #define TEXT_DEFAULT_COIN "Coin"
  39. #define TEXT_RECEIVE_ADDRESS "receive address:"
  40. #define TEXT_DEFAULT_DERIV "m/44'/X'/0'/0"
  41. const char* TEXT_INFO = "-Scroll pages with up/down-"
  42. "p1,2) Mnemonic/Seed "
  43. "p3) xprv Root Key "
  44. "p4,5) xprv/xpub Accnt Keys"
  45. "p6,7) xprv/xpub Extnd Keys"
  46. "p8+) Receive Addresses ";
  47. #define TEXT_SAVE_QR "Save QR"
  48. // bip44_coin, xprv_version, xpub_version, addr_version, wif_version, addr_format
  49. const uint32_t COIN_INFO_ARRAY[3][6] = {
  50. {COIN_BTC, 0x0488ade4, 0x0488b21e, 0x00, 0x80, FlipBipCoinBTC0},
  51. {COIN_ETH, 0x0488ade4, 0x0488b21e, 0x00, 0x80, FlipBipCoinETH60},
  52. {COIN_DOGE, 0x02fac398, 0x02facafd, 0x1e, 0x9e, FlipBipCoinBTC0}};
  53. // coin_name, derivation_path
  54. const char* COIN_TEXT_ARRAY[3][3] = {
  55. {"BTC", "m/44'/0'/0'/0", "bitcoin:"},
  56. {"ETH", "m/44'/60'/0'/0", "ethereum:"},
  57. {"DOGE", "m/44'/3'/0'/0", "dogecoin:"}};
  58. struct FlipBipScene1 {
  59. View* view;
  60. FlipBipScene1Callback callback;
  61. void* context;
  62. };
  63. typedef struct {
  64. int page;
  65. int strength;
  66. uint32_t coin;
  67. bool overwrite;
  68. bool mnemonic_only;
  69. CONFIDENTIAL const char* mnemonic;
  70. CONFIDENTIAL uint8_t seed[64];
  71. CONFIDENTIAL const HDNode* node;
  72. CONFIDENTIAL const char* xprv_root;
  73. CONFIDENTIAL const char* xprv_account;
  74. CONFIDENTIAL const char* xpub_account;
  75. CONFIDENTIAL const char* xprv_extended;
  76. CONFIDENTIAL const char* xpub_extended;
  77. char* recv_addresses[NUM_ADDRS];
  78. } FlipBipScene1Model;
  79. // Node for the receive address
  80. static CONFIDENTIAL HDNode* s_addr_node = NULL;
  81. // Generic display text
  82. static CONFIDENTIAL char* s_disp_text1 = NULL;
  83. static CONFIDENTIAL char* s_disp_text2 = NULL;
  84. static CONFIDENTIAL char* s_disp_text3 = NULL;
  85. static CONFIDENTIAL char* s_disp_text4 = NULL;
  86. static CONFIDENTIAL char* s_disp_text5 = NULL;
  87. static CONFIDENTIAL char* s_disp_text6 = NULL;
  88. // Derivation path text
  89. static const char* s_derivation_text = TEXT_DEFAULT_DERIV;
  90. //static bool s_busy = false;
  91. void flipbip_scene_1_set_callback(
  92. FlipBipScene1* instance,
  93. FlipBipScene1Callback callback,
  94. void* context) {
  95. furi_assert(instance);
  96. furi_assert(callback);
  97. instance->callback = callback;
  98. instance->context = context;
  99. }
  100. static void flipbip_scene_1_init_address(
  101. char* addr_text,
  102. const HDNode* node,
  103. uint32_t coin_type,
  104. uint32_t addr_index) {
  105. //s_busy = true;
  106. // Buffer for address serialization
  107. const size_t buflen = 40;
  108. char buf[40 + 1] = {0};
  109. // Use static node for address generation
  110. memcpy(s_addr_node, node, sizeof(HDNode));
  111. memzero(addr_text, MAX_ADDR_LEN);
  112. hdnode_private_ckd(s_addr_node, addr_index);
  113. hdnode_fill_public_key(s_addr_node);
  114. // coin info
  115. // bip44_coin, xprv_version, xpub_version, addr_version, wif_version, addr_format
  116. uint32_t coin_info[6] = {0};
  117. for(size_t i = 0; i < 6; i++) {
  118. coin_info[i] = COIN_INFO_ARRAY[coin_type][i];
  119. }
  120. if(coin_info[5] == FlipBipCoinBTC0) { // BTC / DOGE style address
  121. // BTC / DOGE style address
  122. ecdsa_get_address(
  123. s_addr_node->public_key, coin_info[3], HASHER_SHA2_RIPEMD, HASHER_SHA2D, buf, buflen);
  124. strcpy(addr_text, buf);
  125. //ecdsa_get_wif(addr_node->private_key, WIF_VERSION, HASHER_SHA2D, buf, buflen);
  126. } else if(coin_info[5] == FlipBipCoinETH60) { // ETH
  127. // ETH style address
  128. hdnode_get_ethereum_pubkeyhash(s_addr_node, (uint8_t*)buf);
  129. addr_text[0] = '0';
  130. addr_text[1] = 'x';
  131. // Convert the hash to a hex string
  132. flipbip_btox((uint8_t*)buf, 20, addr_text + 2);
  133. }
  134. // Clear the address node
  135. memzero(s_addr_node, sizeof(HDNode));
  136. //s_busy = false;
  137. }
  138. static void flipbip_scene_1_draw_generic(const char* text, size_t line_len) {
  139. // Split the text into parts
  140. for(size_t si = 1; si <= 6; si++) {
  141. char* ptr = NULL;
  142. if(si == 1)
  143. ptr = s_disp_text1;
  144. else if(si == 2)
  145. ptr = s_disp_text2;
  146. else if(si == 3)
  147. ptr = s_disp_text3;
  148. else if(si == 4)
  149. ptr = s_disp_text4;
  150. else if(si == 5)
  151. ptr = s_disp_text5;
  152. else if(si == 6)
  153. ptr = s_disp_text6;
  154. memzero(ptr, 30 + 1);
  155. if(line_len > 30) {
  156. strncpy(ptr, text + ((si - 1) * 30), 30);
  157. } else {
  158. strncpy(ptr, text + ((si - 1) * line_len), line_len);
  159. }
  160. }
  161. }
  162. static void flipbip_scene_1_draw_mnemonic(const char* mnemonic) {
  163. // Delineate sections of the mnemonic every 4 words
  164. const size_t mnemonic_working_len = strlen(mnemonic) + 1;
  165. char* mnemonic_working = malloc(mnemonic_working_len);
  166. strcpy(mnemonic_working, mnemonic);
  167. int word = 0;
  168. for(size_t i = 0; i < strlen(mnemonic_working); i++) {
  169. if(mnemonic_working[i] == ' ') {
  170. word++;
  171. if(word % 4 == 0) {
  172. mnemonic_working[i] = ',';
  173. }
  174. }
  175. }
  176. // Split the mnemonic into parts
  177. char* mnemonic_part = flipbip_strtok(mnemonic_working, ",");
  178. int mi = 0;
  179. while(mnemonic_part != NULL) {
  180. char* ptr = NULL;
  181. mi++;
  182. if(mi == 1)
  183. ptr = s_disp_text1;
  184. else if(mi == 2)
  185. ptr = s_disp_text2;
  186. else if(mi == 3)
  187. ptr = s_disp_text3;
  188. else if(mi == 4)
  189. ptr = s_disp_text4;
  190. else if(mi == 5)
  191. ptr = s_disp_text5;
  192. else if(mi == 6)
  193. ptr = s_disp_text6;
  194. memzero(ptr, 30 + 1);
  195. if(strlen(mnemonic_part) > 30) {
  196. strncpy(ptr, mnemonic_part, 30);
  197. } else {
  198. strncpy(ptr, mnemonic_part, strlen(mnemonic_part));
  199. }
  200. mnemonic_part = flipbip_strtok(NULL, ",");
  201. }
  202. // Free the working mnemonic memory
  203. memzero(mnemonic_working, mnemonic_working_len);
  204. free(mnemonic_working);
  205. }
  206. static void flipbip_scene_1_draw_seed(FlipBipScene1Model* const model) {
  207. const size_t seed_working_len = 64 * 2 + 1;
  208. char* seed_working = malloc(seed_working_len);
  209. // Convert the seed to a hex string
  210. flipbip_btox(model->seed, 64, seed_working);
  211. flipbip_scene_1_draw_generic(seed_working, 22);
  212. // Free the working seed memory
  213. memzero(seed_working, seed_working_len);
  214. free(seed_working);
  215. }
  216. static void flipbip_scene_1_clear_text() {
  217. memzero((void*)s_disp_text1, 30 + 1);
  218. memzero((void*)s_disp_text2, 30 + 1);
  219. memzero((void*)s_disp_text3, 30 + 1);
  220. memzero((void*)s_disp_text4, 30 + 1);
  221. memzero((void*)s_disp_text5, 30 + 1);
  222. memzero((void*)s_disp_text6, 30 + 1);
  223. }
  224. void flipbip_scene_1_draw(Canvas* canvas, FlipBipScene1Model* model) {
  225. //UNUSED(model);
  226. canvas_clear(canvas);
  227. canvas_set_color(canvas, ColorBlack);
  228. flipbip_scene_1_clear_text();
  229. if(model->page == PAGE_INFO) {
  230. flipbip_scene_1_draw_generic(TEXT_INFO, 27);
  231. } else if(model->page == PAGE_MNEMONIC) {
  232. flipbip_scene_1_draw_mnemonic(model->mnemonic);
  233. } else if(model->page == PAGE_SEED) {
  234. flipbip_scene_1_draw_seed(model);
  235. } else if(model->page == PAGE_XPRV_ROOT) {
  236. flipbip_scene_1_draw_generic(model->xprv_root, 20);
  237. } else if(model->page == PAGE_XPRV_ACCT) {
  238. flipbip_scene_1_draw_generic(model->xprv_account, 20);
  239. } else if(model->page == PAGE_XPUB_ACCT) {
  240. flipbip_scene_1_draw_generic(model->xpub_account, 20);
  241. } else if(model->page == PAGE_XPRV_EXTD) {
  242. flipbip_scene_1_draw_generic(model->xprv_extended, 20);
  243. } else if(model->page == PAGE_XPUB_EXTD) {
  244. flipbip_scene_1_draw_generic(model->xpub_extended, 20);
  245. } else if(model->page >= PAGE_ADDR_BEGIN && model->page <= PAGE_ADDR_END) {
  246. flipbip_scene_1_draw_generic(model->recv_addresses[model->page - PAGE_ADDR_BEGIN], 12);
  247. elements_button_right(canvas, TEXT_SAVE_QR);
  248. }
  249. if(model->page == PAGE_LOADING) {
  250. canvas_set_font(canvas, FontPrimary);
  251. canvas_draw_str(canvas, 1, 10, TEXT_LOADING);
  252. canvas_draw_str(canvas, 6, 30, s_derivation_text);
  253. canvas_draw_icon(canvas, 86, 25, &I_Keychain_39x36);
  254. } else if(model->page >= PAGE_ADDR_BEGIN && model->page <= PAGE_ADDR_END) {
  255. // draw address header
  256. canvas_set_font(canvas, FontSecondary);
  257. // coin_name, derivation_path
  258. const char* receive_text = COIN_TEXT_ARRAY[model->coin][0];
  259. if(receive_text == NULL) {
  260. receive_text = TEXT_DEFAULT_COIN;
  261. }
  262. const size_t receive_len = strlen(receive_text) * 7;
  263. canvas_draw_str_aligned(canvas, 1, 2, AlignLeft, AlignTop, receive_text);
  264. canvas_draw_str_aligned(canvas, receive_len, 2, AlignLeft, AlignTop, TEXT_RECEIVE_ADDRESS);
  265. // draw address number
  266. const unsigned char addr_num[1] = {(unsigned char)(model->page - PAGE_ADDR_BEGIN)};
  267. char addr_num_text[3];
  268. flipbip_btox(addr_num, 1, addr_num_text);
  269. addr_num_text[0] = '/';
  270. canvas_draw_str_aligned(canvas, 110, 2, AlignLeft, AlignTop, addr_num_text);
  271. // draw address
  272. canvas_set_font(canvas, FontPrimary);
  273. canvas_draw_str(canvas, 6, 22, s_disp_text1);
  274. canvas_draw_str(canvas, 6, 34, s_disp_text2);
  275. canvas_draw_str(canvas, 6, 46, s_disp_text3);
  276. canvas_draw_str(canvas, 6, 58, s_disp_text4);
  277. } else {
  278. canvas_set_font(canvas, FontSecondary);
  279. canvas_draw_str_aligned(canvas, 1, 2, AlignLeft, AlignTop, s_disp_text1);
  280. canvas_draw_str_aligned(canvas, 1, 12, AlignLeft, AlignTop, s_disp_text2);
  281. canvas_draw_str_aligned(canvas, 1, 22, AlignLeft, AlignTop, s_disp_text3);
  282. canvas_draw_str_aligned(canvas, 1, 32, AlignLeft, AlignTop, s_disp_text4);
  283. canvas_draw_str_aligned(canvas, 1, 42, AlignLeft, AlignTop, s_disp_text5);
  284. canvas_draw_str_aligned(canvas, 1, 52, AlignLeft, AlignTop, s_disp_text6);
  285. }
  286. }
  287. static int flipbip_scene_1_model_init(
  288. FlipBipScene1Model* const model,
  289. const int strength,
  290. const uint32_t coin,
  291. const bool overwrite,
  292. const char* passphrase_text) {
  293. model->page = PAGE_LOADING;
  294. model->mnemonic_only = false;
  295. model->strength = strength;
  296. model->coin = coin;
  297. model->overwrite = overwrite;
  298. // Allocate memory for mnemonic
  299. char* mnemonic = malloc(TEXT_BUFFER_SIZE);
  300. memzero(mnemonic, TEXT_BUFFER_SIZE);
  301. // Check if the mnemonic key & data is already saved in persistent storage, or overwrite is true
  302. if(overwrite || (!flipbip_has_settings(FlipBipFileKey, NULL) &&
  303. !flipbip_has_settings(FlipBipFileDat, NULL))) {
  304. // Set mnemonic only mode
  305. model->mnemonic_only = true;
  306. // Generate a random mnemonic using trezor-crypto
  307. const char* mnemonic_gen = mnemonic_generate(strength);
  308. // Check if the mnemonic is valid
  309. if(mnemonic_check(mnemonic_gen) == 0)
  310. return FlipBipStatusMnemonicCheckError; // 13 = mnemonic check error
  311. // Save the mnemonic to persistent storage
  312. else if(!flipbip_save_settings_secure(mnemonic_gen))
  313. return FlipBipStatusSaveError; // 12 = save error
  314. // Clear the generated mnemonic from memory
  315. mnemonic_clear();
  316. }
  317. // Load the mnemonic from persistent storage
  318. if(!flipbip_load_settings_secure(mnemonic)) {
  319. // Set mnemonic only mode for this error for memory cleanup purposes
  320. model->mnemonic_only = true;
  321. return FlipBipStatusLoadError; // 11 = load error
  322. }
  323. model->mnemonic = mnemonic;
  324. // Check if the mnemonic is valid
  325. if(mnemonic_check(model->mnemonic) == 0) {
  326. // Set mnemonic only mode for this error for memory cleanup purposes
  327. model->mnemonic_only = true;
  328. return FlipBipStatusMnemonicCheckError; // 13 = mnemonic check error
  329. }
  330. // test return values
  331. //model->mnemonic_only = true;
  332. //return FlipBipStatusMnemonicCheckError; // 13 = mnemonic check error
  333. // if we are only generating the mnemonic, return
  334. if(model->mnemonic_only) {
  335. return FlipBipStatusReturn; // 10 = mnemonic only, return from parent
  336. }
  337. // Generate a BIP39 seed from the mnemonic
  338. mnemonic_to_seed(model->mnemonic, passphrase_text, model->seed, 0);
  339. // Generate a BIP32 root HD node from the mnemonic
  340. HDNode* root = malloc(sizeof(HDNode));
  341. hdnode_from_seed(model->seed, 64, SECP256K1_NAME, root);
  342. // buffer for key serialization
  343. const size_t buflen = 128;
  344. char buf[128 + 1];
  345. // coin info
  346. // bip44_coin, xprv_version, xpub_version, addr_version, wif_version, addr_format
  347. uint32_t coin_info[6] = {0};
  348. for(size_t i = 0; i < 6; i++) {
  349. coin_info[i] = COIN_INFO_ARRAY[coin][i];
  350. }
  351. // root
  352. uint32_t fingerprint = 0;
  353. hdnode_serialize_private(root, fingerprint, coin_info[1], buf, buflen);
  354. char* xprv_root = malloc(buflen + 1);
  355. strncpy(xprv_root, buf, buflen);
  356. model->xprv_root = xprv_root;
  357. HDNode* node = root;
  358. // purpose m/44'
  359. fingerprint = hdnode_fingerprint(node);
  360. hdnode_private_ckd_prime(node, DERIV_PURPOSE); // purpose
  361. // coin m/44'/0' or m/44'/60'
  362. fingerprint = hdnode_fingerprint(node);
  363. hdnode_private_ckd_prime(node, coin_info[0]); // coin
  364. // account m/44'/0'/0' or m/44'/60'/0'
  365. fingerprint = hdnode_fingerprint(node);
  366. hdnode_private_ckd_prime(node, DERIV_ACCOUNT); // account
  367. hdnode_serialize_private(node, fingerprint, coin_info[1], buf, buflen);
  368. char* xprv_acc = malloc(buflen + 1);
  369. strncpy(xprv_acc, buf, buflen);
  370. model->xprv_account = xprv_acc;
  371. hdnode_serialize_public(node, fingerprint, coin_info[2], buf, buflen);
  372. char* xpub_acc = malloc(buflen + 1);
  373. strncpy(xpub_acc, buf, buflen);
  374. model->xpub_account = xpub_acc;
  375. // external/internal (change) m/44'/0'/0'/0 or m/44'/60'/0'/0
  376. fingerprint = hdnode_fingerprint(node);
  377. hdnode_private_ckd(node, DERIV_CHANGE); // external/internal (change)
  378. hdnode_serialize_private(node, fingerprint, coin_info[1], buf, buflen);
  379. char* xprv_ext = malloc(buflen + 1);
  380. strncpy(xprv_ext, buf, buflen);
  381. model->xprv_extended = xprv_ext;
  382. hdnode_serialize_public(node, fingerprint, coin_info[2], buf, buflen);
  383. char* xpub_ext = malloc(buflen + 1);
  384. strncpy(xpub_ext, buf, buflen);
  385. model->xpub_extended = xpub_ext;
  386. model->node = node;
  387. // Initialize addresses
  388. for(int a = 0; a < NUM_ADDRS; a++) {
  389. model->recv_addresses[a] = malloc(MAX_ADDR_LEN);
  390. memzero(model->recv_addresses[a], MAX_ADDR_LEN);
  391. flipbip_scene_1_init_address(model->recv_addresses[a], node, coin_info[5], a);
  392. }
  393. model->page = PAGE_INFO;
  394. #if USE_BIP39_CACHE
  395. // Clear the BIP39 cache
  396. bip39_cache_clear();
  397. #endif
  398. // 0 = success
  399. return FlipBipStatusSuccess;
  400. }
  401. bool flipbip_scene_1_input(InputEvent* event, void* context) {
  402. furi_assert(context);
  403. FlipBipScene1* instance = context;
  404. // Ignore input if busy
  405. // if(s_busy) {
  406. // return false;
  407. // }
  408. if(event->type == InputTypeRelease) {
  409. switch(event->key) {
  410. case InputKeyBack:
  411. with_view_model(
  412. instance->view,
  413. FlipBipScene1Model * model,
  414. {
  415. UNUSED(model);
  416. instance->callback(FlipBipCustomEventScene1Back, instance->context);
  417. },
  418. true);
  419. break;
  420. case InputKeyDown:
  421. with_view_model(
  422. instance->view,
  423. FlipBipScene1Model * model,
  424. {
  425. //UNUSED(model);
  426. int page = (model->page + 1) % (PAGE_ADDR_END + 1);
  427. if(page == 0) {
  428. page = PAGE_INFO;
  429. }
  430. model->page = page;
  431. },
  432. true);
  433. break;
  434. case InputKeyLeft:
  435. case InputKeyUp:
  436. with_view_model(
  437. instance->view,
  438. FlipBipScene1Model * model,
  439. {
  440. //UNUSED(model);
  441. int page = (model->page - 1) % (PAGE_ADDR_END + 1);
  442. if(page == 0) {
  443. page = PAGE_ADDR_END;
  444. }
  445. model->page = page;
  446. },
  447. true);
  448. break;
  449. case InputKeyRight:
  450. case InputKeyOk:
  451. with_view_model(
  452. instance->view,
  453. FlipBipScene1Model * model,
  454. {
  455. if(model->page >= PAGE_ADDR_BEGIN && model->page <= PAGE_ADDR_END) {
  456. }
  457. },
  458. true);
  459. break;
  460. case InputKeyMAX:
  461. break;
  462. }
  463. }
  464. return true;
  465. }
  466. void flipbip_scene_1_exit(void* context) {
  467. furi_assert(context);
  468. FlipBipScene1* instance = (FlipBipScene1*)context;
  469. with_view_model(
  470. instance->view,
  471. FlipBipScene1Model * model,
  472. {
  473. model->page = PAGE_LOADING;
  474. model->strength = FlipBipStrength256;
  475. model->coin = FlipBipCoinBTC0;
  476. memzero(model->seed, 64);
  477. // if mnemonic_only is true, then we don't need to free the data here
  478. if(!model->mnemonic_only) {
  479. memzero((void*)model->mnemonic, strlen(model->mnemonic));
  480. free((void*)model->mnemonic);
  481. memzero((void*)model->node, sizeof(HDNode));
  482. free((void*)model->node);
  483. memzero((void*)model->xprv_root, strlen(model->xprv_root));
  484. memzero((void*)model->xprv_account, strlen(model->xprv_account));
  485. memzero((void*)model->xpub_account, strlen(model->xpub_account));
  486. memzero((void*)model->xprv_extended, strlen(model->xprv_extended));
  487. memzero((void*)model->xpub_extended, strlen(model->xpub_extended));
  488. free((void*)model->xprv_root);
  489. free((void*)model->xprv_account);
  490. free((void*)model->xpub_account);
  491. free((void*)model->xprv_extended);
  492. free((void*)model->xpub_extended);
  493. for(int a = 0; a < NUM_ADDRS; a++) {
  494. memzero((void*)model->recv_addresses[a], MAX_ADDR_LEN);
  495. free((void*)model->recv_addresses[a]);
  496. }
  497. }
  498. },
  499. true);
  500. flipbip_scene_1_clear_text();
  501. }
  502. void flipbip_scene_1_enter(void* context) {
  503. furi_assert(context);
  504. FlipBipScene1* instance = (FlipBipScene1*)context;
  505. FlipBip* app = instance->context;
  506. // BIP39 Strength setting
  507. int strength = 256; // FlipBipStrength256 // 24 words (256 bit)
  508. if(app->bip39_strength == FlipBipStrength128) {
  509. strength = 128; // 12 words (128 bit)
  510. } else if(app->bip39_strength == FlipBipStrength192) {
  511. strength = 192; // 18 words (192 bit)
  512. }
  513. // BIP39 Passphrase setting
  514. const char* passphrase_text = "";
  515. if(app->passphrase == FlipBipPassphraseOn && strlen(app->passphrase_text) > 0) {
  516. passphrase_text = app->passphrase_text;
  517. }
  518. // BIP44 Coin setting
  519. const uint32_t coin = app->bip44_coin;
  520. // coin_name, derivation_path
  521. s_derivation_text = COIN_TEXT_ARRAY[coin][1];
  522. // Overwrite the saved seed with a new one setting
  523. bool overwrite = app->overwrite_saved_seed != 0;
  524. if(overwrite) {
  525. s_derivation_text = TEXT_NEW_WALLET;
  526. }
  527. flipbip_play_happy_bump(app);
  528. //notification_message(app->notification, &sequence_blink_cyan_100);
  529. flipbip_led_set_rgb(app, 255, 0, 0);
  530. with_view_model(
  531. instance->view,
  532. FlipBipScene1Model * model,
  533. {
  534. // s_busy = true;
  535. const int status =
  536. flipbip_scene_1_model_init(model, strength, coin, overwrite, passphrase_text);
  537. // nonzero status, free the mnemonic
  538. if(status != FlipBipStatusSuccess) {
  539. memzero((void*)model->mnemonic, strlen(model->mnemonic));
  540. free((void*)model->mnemonic);
  541. }
  542. // if error, set the error message
  543. if(status == FlipBipStatusSaveError) {
  544. model->mnemonic = "ERROR:,Save error";
  545. model->page = PAGE_MNEMONIC;
  546. flipbip_play_long_bump(app);
  547. } else if(status == FlipBipStatusLoadError) {
  548. model->mnemonic = "ERROR:,Load error";
  549. model->page = PAGE_MNEMONIC;
  550. flipbip_play_long_bump(app);
  551. } else if(status == FlipBipStatusMnemonicCheckError) {
  552. model->mnemonic = "ERROR:,Mnemonic check error";
  553. model->page = PAGE_MNEMONIC;
  554. flipbip_play_long_bump(app);
  555. }
  556. // s_busy = false;
  557. // if overwrite is set and mnemonic generated, return from scene immediately
  558. if(status == FlipBipStatusReturn) {
  559. instance->callback(FlipBipCustomEventScene1Back, instance->context);
  560. }
  561. },
  562. true);
  563. }
  564. FlipBipScene1* flipbip_scene_1_alloc() {
  565. FlipBipScene1* instance = malloc(sizeof(FlipBipScene1));
  566. instance->view = view_alloc();
  567. view_allocate_model(instance->view, ViewModelTypeLocking, sizeof(FlipBipScene1Model));
  568. view_set_context(instance->view, instance); // furi_assert crashes in events without this
  569. view_set_draw_callback(instance->view, (ViewDrawCallback)flipbip_scene_1_draw);
  570. view_set_input_callback(instance->view, flipbip_scene_1_input);
  571. view_set_enter_callback(instance->view, flipbip_scene_1_enter);
  572. view_set_exit_callback(instance->view, flipbip_scene_1_exit);
  573. // allocate the address node
  574. s_addr_node = (HDNode*)malloc(sizeof(HDNode));
  575. // allocate the display text
  576. s_disp_text1 = (char*)malloc(30 + 1);
  577. s_disp_text2 = (char*)malloc(30 + 1);
  578. s_disp_text3 = (char*)malloc(30 + 1);
  579. s_disp_text4 = (char*)malloc(30 + 1);
  580. s_disp_text5 = (char*)malloc(30 + 1);
  581. s_disp_text6 = (char*)malloc(30 + 1);
  582. return instance;
  583. }
  584. void flipbip_scene_1_free(FlipBipScene1* instance) {
  585. furi_assert(instance);
  586. with_view_model(
  587. instance->view, FlipBipScene1Model * model, { UNUSED(model); }, true);
  588. // free the address node
  589. memzero(s_addr_node, sizeof(HDNode));
  590. free(s_addr_node);
  591. // free the display text
  592. flipbip_scene_1_clear_text();
  593. free(s_disp_text1);
  594. free(s_disp_text2);
  595. free(s_disp_text3);
  596. free(s_disp_text4);
  597. free(s_disp_text5);
  598. free(s_disp_text6);
  599. view_free(instance->view);
  600. free(instance);
  601. }
  602. View* flipbip_scene_1_get_view(FlipBipScene1* instance) {
  603. furi_assert(instance);
  604. return instance->view;
  605. }