flipbip_scene_1.c 26 KB

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