flipbip_scene_1.c 26 KB

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