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