flipbip_scene_1.c 21 KB

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