flipbip_scene_1.c 26 KB

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