flipbip_scene_1.c 24 KB

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