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 <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. if(s_warn_insecure) {
  285. canvas_set_font(canvas, FontSecondary);
  286. canvas_draw_str(canvas, 2, 50, WARN_INSECURE_TEXT_1);
  287. canvas_draw_str(canvas, 2, 60, WARN_INSECURE_TEXT_2);
  288. }
  289. } else if(model->page >= PAGE_ADDR_BEGIN && model->page <= PAGE_ADDR_END) {
  290. // draw address header
  291. canvas_set_font(canvas, FontSecondary);
  292. // coin_name, derivation_path
  293. const char* receive_text = COIN_TEXT_ARRAY[model->coin][0];
  294. if(receive_text == NULL) {
  295. receive_text = TEXT_DEFAULT_COIN;
  296. }
  297. const size_t receive_len = strlen(receive_text) * 7;
  298. canvas_draw_str_aligned(canvas, 2, 2, AlignLeft, AlignTop, receive_text);
  299. canvas_draw_str_aligned(
  300. canvas, receive_len + 1, 2, AlignLeft, AlignTop, TEXT_RECEIVE_ADDRESS);
  301. // draw address number
  302. const unsigned char addr_num[1] = {(unsigned char)(model->page - PAGE_ADDR_BEGIN)};
  303. char addr_num_text[3] = {0};
  304. flipbip_btox(addr_num, 1, addr_num_text);
  305. addr_num_text[0] = '/';
  306. canvas_draw_str_aligned(canvas, 125, 2, AlignRight, AlignTop, addr_num_text);
  307. // draw QR code file path
  308. char addr_name_text[14] = {0};
  309. strcpy(addr_name_text, COIN_TEXT_ARRAY[model->coin][0]);
  310. flipbip_btox(addr_num, 1, addr_name_text + strlen(addr_name_text));
  311. strcpy(addr_name_text + strlen(addr_name_text), TEXT_QRFILE_EXT);
  312. //elements_button_right(canvas, addr_name_text);
  313. canvas_draw_str_aligned(canvas, 125, 53, AlignRight, AlignTop, addr_name_text);
  314. // draw address
  315. canvas_set_font(canvas, FontPrimary);
  316. canvas_draw_str(canvas, 7, 22, s_disp_text1);
  317. canvas_draw_str(canvas, 7, 34, s_disp_text2);
  318. canvas_draw_str(canvas, 7, 46, s_disp_text3);
  319. canvas_draw_str(canvas, 7, 58, s_disp_text4);
  320. } else {
  321. canvas_set_font(canvas, FontSecondary);
  322. canvas_draw_str_aligned(canvas, 1, 2, AlignLeft, AlignTop, s_disp_text1);
  323. canvas_draw_str_aligned(canvas, 1, 12, AlignLeft, AlignTop, s_disp_text2);
  324. canvas_draw_str_aligned(canvas, 1, 22, AlignLeft, AlignTop, s_disp_text3);
  325. canvas_draw_str_aligned(canvas, 1, 32, AlignLeft, AlignTop, s_disp_text4);
  326. canvas_draw_str_aligned(canvas, 1, 42, AlignLeft, AlignTop, s_disp_text5);
  327. canvas_draw_str_aligned(canvas, 1, 52, AlignLeft, AlignTop, s_disp_text6);
  328. }
  329. }
  330. static int flipbip_scene_1_model_init(
  331. FlipBipScene1Model* const model,
  332. const int strength,
  333. const uint32_t coin,
  334. const bool overwrite,
  335. const char* passphrase_text) {
  336. model->page = PAGE_LOADING;
  337. model->mnemonic_only = false;
  338. model->strength = strength;
  339. model->coin = coin;
  340. model->overwrite = overwrite;
  341. // Allocate memory for mnemonic
  342. char* mnemonic = malloc(TEXT_BUFFER_SIZE);
  343. memzero(mnemonic, TEXT_BUFFER_SIZE);
  344. // Check if the mnemonic key & data is already saved in persistent storage, or overwrite is true
  345. if(overwrite || (!flipbip_has_file(FlipBipFileKey, NULL, false) &&
  346. !flipbip_has_file(FlipBipFileDat, NULL, false))) {
  347. // Set mnemonic only mode
  348. model->mnemonic_only = true;
  349. // Generate a random mnemonic using trezor-crypto
  350. const char* mnemonic_gen = mnemonic_generate(strength);
  351. // Check if the mnemonic is valid
  352. if(mnemonic_check(mnemonic_gen) == 0)
  353. return FlipBipStatusMnemonicCheckError; // 13 = mnemonic check error
  354. // Save the mnemonic to persistent storage
  355. else if(!flipbip_save_file_secure(mnemonic_gen))
  356. return FlipBipStatusSaveError; // 12 = save error
  357. // Clear the generated mnemonic from memory
  358. mnemonic_clear();
  359. }
  360. // Load the mnemonic from persistent storage
  361. if(!flipbip_load_file_secure(mnemonic)) {
  362. // Set mnemonic only mode for this error for memory cleanup purposes
  363. model->mnemonic_only = true;
  364. return FlipBipStatusLoadError; // 11 = load error
  365. }
  366. model->mnemonic = mnemonic;
  367. // Check if the mnemonic is valid
  368. if(mnemonic_check(model->mnemonic) == 0) {
  369. // Set mnemonic only mode for this error for memory cleanup purposes
  370. model->mnemonic_only = true;
  371. return FlipBipStatusMnemonicCheckError; // 13 = mnemonic check error
  372. }
  373. // test return values
  374. //model->mnemonic_only = true;
  375. //return FlipBipStatusMnemonicCheckError; // 13 = mnemonic check error
  376. // if we are only generating the mnemonic, return
  377. if(model->mnemonic_only) {
  378. return FlipBipStatusReturn; // 10 = mnemonic only, return from parent
  379. }
  380. // Generate a BIP39 seed from the mnemonic
  381. mnemonic_to_seed(model->mnemonic, passphrase_text, model->seed, 0);
  382. // Generate a BIP32 root HD node from the mnemonic
  383. HDNode* root = malloc(sizeof(HDNode));
  384. hdnode_from_seed(model->seed, 64, SECP256K1_NAME, root);
  385. // buffer for key serialization
  386. const size_t buflen = 128;
  387. char buf[128 + 1] = {0};
  388. // coin info
  389. // bip44_coin, xprv_version, xpub_version, addr_version, wif_version, addr_format
  390. uint32_t coin_info[6] = {0};
  391. for(size_t i = 0; i < 6; i++) {
  392. coin_info[i] = COIN_INFO_ARRAY[coin][i];
  393. }
  394. // root
  395. uint32_t fingerprint = 0;
  396. hdnode_serialize_private(root, fingerprint, coin_info[1], buf, buflen);
  397. char* xprv_root = malloc(buflen + 1);
  398. strncpy(xprv_root, buf, buflen);
  399. model->xprv_root = xprv_root;
  400. HDNode* node = root;
  401. // purpose m/44'
  402. fingerprint = hdnode_fingerprint(node);
  403. hdnode_private_ckd_prime(node, DERIV_PURPOSE); // purpose
  404. // coin m/44'/0' or m/44'/60'
  405. fingerprint = hdnode_fingerprint(node);
  406. hdnode_private_ckd_prime(node, coin_info[0]); // coin
  407. // account m/44'/0'/0' or m/44'/60'/0'
  408. fingerprint = hdnode_fingerprint(node);
  409. hdnode_private_ckd_prime(node, DERIV_ACCOUNT); // account
  410. hdnode_serialize_private(node, fingerprint, coin_info[1], buf, buflen);
  411. char* xprv_acc = malloc(buflen + 1);
  412. strncpy(xprv_acc, buf, buflen);
  413. model->xprv_account = xprv_acc;
  414. hdnode_serialize_public(node, fingerprint, coin_info[2], buf, buflen);
  415. char* xpub_acc = malloc(buflen + 1);
  416. strncpy(xpub_acc, buf, buflen);
  417. model->xpub_account = xpub_acc;
  418. // external/internal (change) m/44'/0'/0'/0 or m/44'/60'/0'/0
  419. fingerprint = hdnode_fingerprint(node);
  420. hdnode_private_ckd(node, DERIV_CHANGE); // external/internal (change)
  421. hdnode_serialize_private(node, fingerprint, coin_info[1], buf, buflen);
  422. char* xprv_ext = malloc(buflen + 1);
  423. strncpy(xprv_ext, buf, buflen);
  424. model->xprv_extended = xprv_ext;
  425. hdnode_serialize_public(node, fingerprint, coin_info[2], buf, buflen);
  426. char* xpub_ext = malloc(buflen + 1);
  427. strncpy(xpub_ext, buf, buflen);
  428. model->xpub_extended = xpub_ext;
  429. model->node = node;
  430. // Initialize addresses
  431. for(uint8_t a = 0; a < NUM_ADDRS; a++) {
  432. model->recv_addresses[a] = malloc(MAX_ADDR_BUF);
  433. memzero(model->recv_addresses[a], MAX_ADDR_BUF);
  434. flipbip_scene_1_init_address(model->recv_addresses[a], node, coin, a);
  435. // Save QR code file
  436. memzero(buf, buflen);
  437. strcpy(buf, COIN_TEXT_ARRAY[coin][0]);
  438. const unsigned char addr_num[1] = {a};
  439. flipbip_btox(addr_num, 1, buf + strlen(buf));
  440. strcpy(buf + strlen(buf), TEXT_QRFILE_EXT);
  441. flipbip_save_qrfile(COIN_TEXT_ARRAY[coin][2], model->recv_addresses[a], buf);
  442. memzero(buf, buflen);
  443. }
  444. model->page = PAGE_INFO;
  445. #if USE_BIP39_CACHE
  446. // Clear the BIP39 cache
  447. bip39_cache_clear();
  448. #endif
  449. // 0 = success
  450. return FlipBipStatusSuccess;
  451. }
  452. bool flipbip_scene_1_input(InputEvent* event, void* context) {
  453. furi_assert(context);
  454. FlipBipScene1* instance = context;
  455. // Ignore input if busy
  456. // if(s_busy) {
  457. // return false;
  458. // }
  459. if(event->type == InputTypeRelease) {
  460. switch(event->key) {
  461. case InputKeyBack:
  462. with_view_model(
  463. instance->view,
  464. FlipBipScene1Model * model,
  465. {
  466. UNUSED(model);
  467. instance->callback(FlipBipCustomEventScene1Back, instance->context);
  468. },
  469. true);
  470. break;
  471. case InputKeyRight:
  472. case InputKeyDown:
  473. with_view_model(
  474. instance->view,
  475. FlipBipScene1Model * model,
  476. {
  477. //UNUSED(model);
  478. int page = (model->page + 1) % (PAGE_ADDR_END + 1);
  479. if(page == 0) {
  480. page = PAGE_INFO;
  481. }
  482. model->page = page;
  483. },
  484. true);
  485. break;
  486. case InputKeyLeft:
  487. case InputKeyUp:
  488. with_view_model(
  489. instance->view,
  490. FlipBipScene1Model * model,
  491. {
  492. //UNUSED(model);
  493. int page = (model->page - 1) % (PAGE_ADDR_END + 1);
  494. if(page == 0) {
  495. page = PAGE_ADDR_END;
  496. }
  497. model->page = page;
  498. },
  499. true);
  500. break;
  501. // case InputKeyRight:
  502. case InputKeyOk:
  503. // with_view_model(
  504. // instance->view,
  505. // FlipBipScene1Model * model,
  506. // {
  507. // if(model->page >= PAGE_ADDR_BEGIN && model->page <= PAGE_ADDR_END) {
  508. // }
  509. // },
  510. // true);
  511. // break;
  512. // case InputKeyLeft:
  513. case InputKeyMAX:
  514. break;
  515. }
  516. }
  517. return true;
  518. }
  519. void flipbip_scene_1_exit(void* context) {
  520. furi_assert(context);
  521. FlipBipScene1* instance = (FlipBipScene1*)context;
  522. with_view_model(
  523. instance->view,
  524. FlipBipScene1Model * model,
  525. {
  526. model->page = PAGE_LOADING;
  527. model->strength = FlipBipStrength256;
  528. model->coin = FlipBipCoinBTC0;
  529. memzero(model->seed, 64);
  530. // if mnemonic_only is true, then we don't need to free the data here
  531. if(!model->mnemonic_only) {
  532. memzero((void*)model->mnemonic, strlen(model->mnemonic));
  533. free((void*)model->mnemonic);
  534. memzero((void*)model->node, sizeof(HDNode));
  535. free((void*)model->node);
  536. memzero((void*)model->xprv_root, strlen(model->xprv_root));
  537. memzero((void*)model->xprv_account, strlen(model->xprv_account));
  538. memzero((void*)model->xpub_account, strlen(model->xpub_account));
  539. memzero((void*)model->xprv_extended, strlen(model->xprv_extended));
  540. memzero((void*)model->xpub_extended, strlen(model->xpub_extended));
  541. free((void*)model->xprv_root);
  542. free((void*)model->xprv_account);
  543. free((void*)model->xpub_account);
  544. free((void*)model->xprv_extended);
  545. free((void*)model->xpub_extended);
  546. for(int a = 0; a < NUM_ADDRS; a++) {
  547. memzero((void*)model->recv_addresses[a], MAX_ADDR_BUF);
  548. free((void*)model->recv_addresses[a]);
  549. }
  550. }
  551. },
  552. true);
  553. flipbip_scene_1_clear_text();
  554. }
  555. void flipbip_scene_1_enter(void* context) {
  556. furi_assert(context);
  557. FlipBipScene1* instance = (FlipBipScene1*)context;
  558. FlipBip* app = instance->context;
  559. // BIP39 Strength setting
  560. int strength = 256; // FlipBipStrength256 // 24 words (256 bit)
  561. if(app->bip39_strength == FlipBipStrength128) {
  562. strength = 128; // 12 words (128 bit)
  563. } else if(app->bip39_strength == FlipBipStrength192) {
  564. strength = 192; // 18 words (192 bit)
  565. }
  566. // BIP39 Passphrase setting
  567. const char* passphrase_text = "";
  568. if(app->passphrase == FlipBipPassphraseOn && strlen(app->passphrase_text) > 0) {
  569. passphrase_text = app->passphrase_text;
  570. s_warn_insecure = false;
  571. } else {
  572. s_warn_insecure = true;
  573. }
  574. // BIP44 Coin setting
  575. const uint32_t coin = app->bip44_coin;
  576. // coin_name, derivation_path
  577. s_derivation_text = COIN_TEXT_ARRAY[coin][1];
  578. // Overwrite the saved seed with a new one setting
  579. bool overwrite = app->overwrite_saved_seed != 0;
  580. if(overwrite) {
  581. s_derivation_text = TEXT_NEW_WALLET;
  582. }
  583. // Wait a beat to allow the display time to update to the loading screen
  584. furi_thread_flags_wait(0, FuriFlagWaitAny, 20);
  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. // calling strlen on mnemonic here can cause a crash, don't.
  598. // it wasn't loaded properly anyways, no need to zero the memory
  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. }