flipbip_scene_1.c 22 KB

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