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