flipbip_scene_1.c 26 KB

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