flipbip_scene_1.c 22 KB

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