flipbip_scene_1.c 26 KB

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