flipbip_scene_1.c 26 KB

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