flipchess_scene_1.c 26 KB

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