flipbip_scene_1.c 24 KB

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