flipbip_scene_1.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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 "../helpers/flipbip_haptic.h"
  9. //#include "../helpers/flipbip_speaker.h"
  10. #include "../helpers/flipbip_led.h"
  11. #include "../helpers/flipbip_string.h"
  12. #include "../helpers/flipbip_file.h"
  13. #include <string.h>
  14. #include "../crypto/rand.h"
  15. #include "../crypto/bip32.h"
  16. #include "../crypto/bip39.h"
  17. #include "../crypto/curves.h"
  18. #include "../crypto/memzero.h"
  19. struct FlipBipScene1 {
  20. View* view;
  21. FlipBipScene1Callback callback;
  22. void* context;
  23. };
  24. typedef struct {
  25. int page;
  26. int strength;
  27. uint32_t coin;
  28. bool overwrite;
  29. bool mnemonic_only;
  30. CONFIDENTIAL const char* mnemonic;
  31. CONFIDENTIAL uint8_t seed[64];
  32. CONFIDENTIAL const HDNode* node;
  33. CONFIDENTIAL const char* xprv_root;
  34. CONFIDENTIAL const char* xprv_account;
  35. CONFIDENTIAL const char* xpub_account;
  36. CONFIDENTIAL const char* xprv_extended;
  37. CONFIDENTIAL const char* xpub_extended;
  38. } FlipBipScene1Model;
  39. static CONFIDENTIAL char s_disp_text1[30 + 1];
  40. static CONFIDENTIAL char s_disp_text2[30 + 1];
  41. static CONFIDENTIAL char s_disp_text3[30 + 1];
  42. static CONFIDENTIAL char s_disp_text4[30 + 1];
  43. static CONFIDENTIAL char s_disp_text5[30 + 1];
  44. static CONFIDENTIAL char s_disp_text6[30 + 1];
  45. static bool s_busy = false;
  46. void flipbip_scene_1_set_callback(
  47. FlipBipScene1* instance,
  48. FlipBipScene1Callback callback,
  49. void* context) {
  50. furi_assert(instance);
  51. furi_assert(callback);
  52. instance->callback = callback;
  53. instance->context = context;
  54. }
  55. static void flipbip_scene_1_draw_generic(const char* text, size_t line_len) {
  56. // Split the text into parts
  57. for (size_t si = 1; si <= 6; si++) {
  58. char *ptr = NULL;
  59. if (si == 1) ptr = s_disp_text1;
  60. else if (si == 2) ptr = s_disp_text2;
  61. else if (si == 3) ptr = s_disp_text3;
  62. else if (si == 4) ptr = s_disp_text4;
  63. else if (si == 5) ptr = s_disp_text5;
  64. else if (si == 6) ptr = s_disp_text6;
  65. memzero(ptr, 30 + 1);
  66. if (line_len > 30) {
  67. strncpy(ptr, text + ((si - 1) * 30), 30);
  68. } else {
  69. strncpy(ptr, text + ((si - 1) * line_len), line_len);
  70. }
  71. }
  72. }
  73. static void flipbip_scene_1_draw_mnemonic(const char* mnemonic) {
  74. // Delineate sections of the mnemonic every 4 words
  75. char *mnemonic_working = malloc(strlen(mnemonic) + 1);
  76. strcpy(mnemonic_working, mnemonic);
  77. int word = 0;
  78. for (size_t i = 0; i < strlen(mnemonic_working); i++) {
  79. if (mnemonic_working[i] == ' ') {
  80. word++;
  81. if (word % 4 == 0) {
  82. mnemonic_working[i] = ',';
  83. }
  84. }
  85. }
  86. // Split the mnemonic into parts
  87. char *mnemonic_part = flipbip_strtok(mnemonic_working, ",");
  88. int mi = 0;
  89. while(mnemonic_part != NULL)
  90. {
  91. char *ptr = NULL;
  92. mi++;
  93. if (mi == 1) ptr = s_disp_text1;
  94. else if (mi == 2) ptr = s_disp_text2;
  95. else if (mi == 3) ptr = s_disp_text3;
  96. else if (mi == 4) ptr = s_disp_text4;
  97. else if (mi == 5) ptr = s_disp_text5;
  98. else if (mi == 6) ptr = s_disp_text6;
  99. memzero(ptr, 30 + 1);
  100. if (strlen(mnemonic_part) > 30) {
  101. strncpy(ptr, mnemonic_part, 30);
  102. } else {
  103. strncpy(ptr, mnemonic_part, strlen(mnemonic_part));
  104. }
  105. mnemonic_part = flipbip_strtok(NULL, ",");
  106. }
  107. // Free the working mnemonic memory
  108. memzero(mnemonic_working, strlen(mnemonic_working));
  109. free(mnemonic_working);
  110. }
  111. static void flipbip_scene_1_draw_seed(FlipBipScene1Model* const model) {
  112. char *seed_working = malloc(64 * 2 + 1);
  113. // Convert the seed to a hex string
  114. for (size_t i = 0; i < 64; i++) {
  115. flipbip_btox(model->seed[i], seed_working + (i * 2));
  116. }
  117. flipbip_scene_1_draw_generic(seed_working, 22);
  118. // Free the working seed memory
  119. memzero(seed_working, sizeof(seed_working));
  120. free(seed_working);
  121. }
  122. static void flipbip_scene_1_draw_address(const HDNode* node, uint32_t addr_type, uint32_t addr_index) {
  123. s_busy = true;
  124. // buffer for key serialization
  125. const size_t buflen = 128;
  126. char buf[128 + 1];
  127. HDNode *addr_node = malloc(sizeof(HDNode));
  128. memcpy(addr_node, node, sizeof(HDNode));
  129. hdnode_private_ckd(addr_node, addr_index);
  130. hdnode_fill_public_key(addr_node);
  131. if (addr_type == 0) { // BTC
  132. // BTC style address
  133. const char addr_version = 0x00;
  134. //const char wif_version = 0x80;
  135. ecdsa_get_address(addr_node->public_key, addr_version, HASHER_SHA2_RIPEMD, HASHER_SHA2D, buf, buflen);
  136. char *address = malloc(buflen + 1);
  137. strncpy(address, buf, buflen);
  138. flipbip_scene_1_draw_generic(address, 12);
  139. memzero(address, buflen + 1);
  140. free(address);
  141. //ecdsa_get_wif(addr_node->private_key, wif_version, HASHER_SHA2D, buf, buflen);
  142. //char *wif = malloc(buflen + 1);
  143. //strncpy(wif, buf, buflen);
  144. } else if (addr_type == 60) { // ETH
  145. // ETH style address
  146. hdnode_get_ethereum_pubkeyhash(addr_node, (uint8_t *)buf);
  147. char *address = malloc(42 + 1);
  148. memcpy(address, "0x", 2);
  149. // Convert the hash to a hex string
  150. for (size_t i = 0; i < 20; i++) {
  151. flipbip_btox(buf[i], address + 2 + (i * 2));
  152. }
  153. flipbip_scene_1_draw_generic(address, 12);
  154. memzero(address, 42 + 1);
  155. free(address);
  156. }
  157. memzero(addr_node, sizeof(HDNode));
  158. free(addr_node);
  159. s_busy = false;
  160. }
  161. static void flipbip_scene_1_clear_text() {
  162. memzero((void*)s_disp_text1, 30 + 1);
  163. memzero((void*)s_disp_text2, 30 + 1);
  164. memzero((void*)s_disp_text3, 30 + 1);
  165. memzero((void*)s_disp_text4, 30 + 1);
  166. memzero((void*)s_disp_text5, 30 + 1);
  167. memzero((void*)s_disp_text6, 30 + 1);
  168. }
  169. void flipbip_scene_1_draw(Canvas* canvas, FlipBipScene1Model* model) {
  170. //UNUSED(model);
  171. canvas_clear(canvas);
  172. canvas_set_color(canvas, ColorBlack);
  173. flipbip_scene_1_clear_text();
  174. if (model->page == 1) {
  175. const char* info = "-Scroll pages with up/down-"
  176. "p1,2) Mnemonic/Seed "
  177. "p3) xprv Root Key "
  178. "p4,5) xprv/xpub Accnt Keys"
  179. "p6,7) xprv/xpub Extnd Keys"
  180. "p8+) Receive Addresses ";
  181. flipbip_scene_1_draw_generic(info, 27);
  182. }
  183. else if (model->page == 2) {
  184. flipbip_scene_1_draw_mnemonic(model->mnemonic);
  185. }
  186. else if (model->page == 3) {
  187. flipbip_scene_1_draw_seed(model);
  188. }
  189. else if (model->page == 4) {
  190. flipbip_scene_1_draw_generic(model->xprv_root, 20);
  191. }
  192. else if (model->page == 5) {
  193. flipbip_scene_1_draw_generic(model->xprv_account, 20);
  194. }
  195. else if (model->page == 6) {
  196. flipbip_scene_1_draw_generic(model->xpub_account, 20);
  197. }
  198. else if (model->page == 7) {
  199. flipbip_scene_1_draw_generic(model->xprv_extended, 20);
  200. }
  201. else if (model->page == 8) {
  202. flipbip_scene_1_draw_generic(model->xpub_extended, 20);
  203. }
  204. else if (model->page >= 9 && model->page <= 13) {
  205. flipbip_scene_1_draw_address(model->node, model->coin, model->page - 9);
  206. }
  207. if (model->page == 0) {
  208. canvas_set_font(canvas, FontPrimary);
  209. canvas_draw_str(canvas, 1, 10, "Loading...");
  210. canvas_draw_str(canvas, 6, 30, "m/44'/x'/0'/0");
  211. } else if (model->page >= 9 && model->page <= 13) {
  212. canvas_set_font(canvas, FontSecondary);
  213. const char * receive_text;
  214. if (model->coin == 0) { // BTC
  215. receive_text = "BTC receive address:";
  216. }
  217. else if (model->coin == 60) { // ETH
  218. receive_text = "ETH receive address:";
  219. }
  220. else {
  221. receive_text = "Receive address:";
  222. }
  223. canvas_draw_str_aligned(canvas, 1, 2, AlignLeft, AlignTop, receive_text);
  224. canvas_set_font(canvas, FontPrimary);
  225. canvas_draw_str(canvas, 6, 22, s_disp_text1);
  226. canvas_draw_str(canvas, 6, 34, s_disp_text2);
  227. canvas_draw_str(canvas, 6, 46, s_disp_text3);
  228. canvas_draw_str(canvas, 6, 58, s_disp_text4);
  229. }
  230. else {
  231. canvas_set_font(canvas, FontSecondary);
  232. canvas_draw_str_aligned(canvas, 1, 2, AlignLeft, AlignTop, s_disp_text1);
  233. canvas_draw_str_aligned(canvas, 1, 12, AlignLeft, AlignTop, s_disp_text2);
  234. canvas_draw_str_aligned(canvas, 1, 22, AlignLeft, AlignTop, s_disp_text3);
  235. canvas_draw_str_aligned(canvas, 1, 32, AlignLeft, AlignTop, s_disp_text4);
  236. canvas_draw_str_aligned(canvas, 1, 42, AlignLeft, AlignTop, s_disp_text5);
  237. canvas_draw_str_aligned(canvas, 1, 52, AlignLeft, AlignTop, s_disp_text6);
  238. }
  239. }
  240. static int flipbip_scene_1_model_init(FlipBipScene1Model* const model, const int strength, const uint32_t coin, const bool overwrite) {
  241. model->page = 0;
  242. model->mnemonic_only = false;
  243. model->strength = strength;
  244. model->coin = coin;
  245. model->overwrite = overwrite;
  246. // Allocate memory for mnemonic
  247. char* mnemonic = malloc(256);
  248. memzero(mnemonic, 256);
  249. // Check if the mnemonic key & data is already saved in persistent storage, or overwrite is true
  250. if (overwrite || (!flipbip_has_settings(true) && !flipbip_has_settings(false))) {
  251. // Generate a random mnemonic using trezor-crypto
  252. const char* mnemonic_gen = mnemonic_generate(strength);
  253. // Save the mnemonic to persistent storage
  254. if (!flipbip_save_settings_secure(mnemonic_gen)) return 1; // 1 = save error
  255. // Clear the generated mnemonic from memory
  256. mnemonic_clear();
  257. model->mnemonic_only = true;
  258. }
  259. // Load the mnemonic from persistent storage
  260. if (!flipbip_load_settings_secure(mnemonic)) return 2; // 2 = load error
  261. model->mnemonic = mnemonic;
  262. // Check if the mnemonic is valid
  263. if (mnemonic_check(model->mnemonic) == 0) return 3; // 3 = mnemonic check error
  264. // if we are only generating the mnemonic, return
  265. if (model->mnemonic_only) {
  266. return -1; // -1 = mnemonic only, return from parent
  267. }
  268. // test mnemonic
  269. //model->mnemonic = "wealth budget salt video delay obey neutral tail sure soda hold rubber joy movie boat raccoon tornado noise off inmate payment patch group topple";
  270. // Generate a BIP39 seed from the mnemonic
  271. mnemonic_to_seed(model->mnemonic, "", model->seed, 0);
  272. // Generate a BIP32 root HD node from the mnemonic
  273. HDNode *root = malloc(sizeof(HDNode));
  274. hdnode_from_seed(model->seed, 64, SECP256K1_NAME, root);
  275. // m/44'/0'/0'/0 or m/44'/60'/0'/0
  276. const uint32_t purpose = 44;
  277. //const uint32_t coin = 0; // BTC
  278. //const uint32_t coin = 60; // ETH
  279. const uint32_t account = 0;
  280. const uint32_t change = 0;
  281. // constants for BTC / ETH
  282. const uint32_t version_public = 0x0488b21e;
  283. const uint32_t version_private = 0x0488ade4;
  284. // "xprv_magic": 76066276,
  285. // "xpub_magic": 76067358,
  286. // "xpub_magic_segwit_p2sh": 77429938,
  287. // "xpub_magic_segwit_native": 78792518,
  288. // "xpub_magic_multisig_segwit_p2sh": 43365439,
  289. // "xpub_magic_multisig_segwit_native": 44728019,
  290. // buffer for key serialization
  291. const size_t buflen = 128;
  292. char buf[128 + 1];
  293. // root
  294. uint32_t fingerprint = 0;
  295. hdnode_serialize_private(root, fingerprint, version_private, buf, buflen);
  296. char *xprv_root = malloc(buflen + 1);
  297. strncpy(xprv_root, buf, buflen);
  298. model->xprv_root = xprv_root;
  299. HDNode *node = root;
  300. // purpose m/44'
  301. fingerprint = hdnode_fingerprint(node);
  302. hdnode_private_ckd_prime(node, purpose); // purpose
  303. // coin m/44'/0' or m/44'/60'
  304. fingerprint = hdnode_fingerprint(node);
  305. hdnode_private_ckd_prime(node, model->coin); // coin
  306. // account m/44'/0'/0' or m/44'/60'/0'
  307. fingerprint = hdnode_fingerprint(node);
  308. hdnode_private_ckd_prime(node, account); // account
  309. hdnode_serialize_private(node, fingerprint, version_private, buf, buflen);
  310. char *xprv_acc = malloc(buflen + 1);
  311. strncpy(xprv_acc, buf, buflen);
  312. model->xprv_account = xprv_acc;
  313. hdnode_serialize_public(node, fingerprint, version_public, buf, buflen);
  314. char *xpub_acc = malloc(buflen + 1);
  315. strncpy(xpub_acc, buf, buflen);
  316. model->xpub_account = xpub_acc;
  317. // external/internal (change) m/44'/0'/0'/0 or m/44'/60'/0'/0
  318. fingerprint = hdnode_fingerprint(node);
  319. hdnode_private_ckd(node, change); // external/internal (change)
  320. hdnode_serialize_private(node, fingerprint, version_private, buf, buflen);
  321. char *xprv_ext = malloc(buflen + 1);
  322. strncpy(xprv_ext, buf, buflen);
  323. model->xprv_extended = xprv_ext;
  324. hdnode_serialize_public(node, fingerprint, version_public, buf, buflen);
  325. char *xpub_ext = malloc(buflen + 1);
  326. strncpy(xpub_ext, buf, buflen);
  327. model->xpub_extended = xpub_ext;
  328. model->node = node;
  329. model->page = 1;
  330. #if USE_BIP39_CACHE
  331. // Clear the BIP39 cache
  332. bip39_cache_clear();
  333. #endif
  334. // 0 = success
  335. return 0;
  336. }
  337. bool flipbip_scene_1_input(InputEvent* event, void* context) {
  338. furi_assert(context);
  339. FlipBipScene1* instance = context;
  340. // Ignore input if busy
  341. if (s_busy) {
  342. return false;
  343. }
  344. if (event->type == InputTypeRelease) {
  345. switch(event->key) {
  346. case InputKeyBack:
  347. with_view_model(
  348. instance->view,
  349. FlipBipScene1Model * model,
  350. {
  351. UNUSED(model);
  352. instance->callback(FlipBipCustomEventScene1Back, instance->context);
  353. },
  354. true);
  355. break;
  356. case InputKeyRight:
  357. case InputKeyDown:
  358. case InputKeyOk:
  359. with_view_model(
  360. instance->view,
  361. FlipBipScene1Model* model,
  362. {
  363. //UNUSED(model);
  364. model->page = (model->page + 1) % 14;
  365. if (model->page == 0) {
  366. model->page = 1;
  367. }
  368. },
  369. true);
  370. break;
  371. case InputKeyLeft:
  372. case InputKeyUp:
  373. with_view_model(
  374. instance->view,
  375. FlipBipScene1Model* model,
  376. {
  377. //UNUSED(model);
  378. model->page = (model->page - 1) % 14;
  379. if (model->page == 0) {
  380. model->page = 13;
  381. }
  382. },
  383. true);
  384. break;
  385. case InputKeyMAX:
  386. break;
  387. }
  388. }
  389. return true;
  390. }
  391. void flipbip_scene_1_exit(void* context) {
  392. furi_assert(context);
  393. FlipBipScene1* instance = (FlipBipScene1*)context;
  394. with_view_model(
  395. instance->view,
  396. FlipBipScene1Model * model,
  397. {
  398. model->page = 0;
  399. model->strength = 0;
  400. model->coin = 0;
  401. memzero(model->seed, 64);
  402. // if mnemonic_only is true, then we don't need to free the data here
  403. if (!model->mnemonic_only) {
  404. memzero((void*)model->mnemonic, strlen(model->mnemonic));
  405. free((void*)model->mnemonic);
  406. memzero((void*)model->node, sizeof(HDNode));
  407. free((void*)model->node);
  408. memzero((void*)model->xprv_root, strlen(model->xprv_root));
  409. memzero((void*)model->xprv_account, strlen(model->xprv_account));
  410. memzero((void*)model->xpub_account, strlen(model->xpub_account));
  411. memzero((void*)model->xprv_extended, strlen(model->xprv_extended));
  412. memzero((void*)model->xpub_extended, strlen(model->xpub_extended));
  413. free((void*)model->xprv_root);
  414. free((void*)model->xprv_account);
  415. free((void*)model->xpub_account);
  416. free((void*)model->xprv_extended);
  417. free((void*)model->xpub_extended);
  418. }
  419. },
  420. true
  421. );
  422. flipbip_scene_1_clear_text();
  423. }
  424. void flipbip_scene_1_enter(void* context) {
  425. furi_assert(context);
  426. FlipBipScene1* instance = (FlipBipScene1*)context;
  427. FlipBip* app = instance->context;
  428. // BIP39 Strength setting
  429. int strength_setting = app->bip39_strength;
  430. int strength = 256; // FlipBipStrength256 // 24 words (256 bit)
  431. if (strength_setting == FlipBipStrength128) strength = 128; // 12 words (128 bit)
  432. else if (strength_setting == FlipBipStrength192) strength = 192; // 18 words (192 bit)
  433. // BIP44 Coin setting
  434. int coin_setting = app->bip44_coin;
  435. uint32_t coin = 0; //FlipBipCoinBTC0 // BTC (0)
  436. if (coin_setting == FlipBipCoinETH60) coin = 60; // ETH (60)
  437. // Overwrite the saved seed with a new one setting
  438. bool overwrite = app->overwrite_saved_seed != 0;
  439. flipbip_play_happy_bump(app);
  440. flipbip_led_set_rgb(app, 255, 0, 0);
  441. with_view_model(
  442. instance->view,
  443. FlipBipScene1Model * model,
  444. {
  445. s_busy = true;
  446. const int status = flipbip_scene_1_model_init(model, strength, coin, overwrite);
  447. // nonzero status, free the mnemonic
  448. if (status != 0) {
  449. memzero((void*)model->mnemonic, strlen(model->mnemonic));
  450. free((void*)model->mnemonic);
  451. }
  452. // if error, set the error message
  453. if (status == 1) {
  454. model->mnemonic = "ERROR:,Save error";
  455. model->page = 2;
  456. } else if (status == 2) {
  457. model->mnemonic = "ERROR:,Load error";
  458. model->page = 2;
  459. } else if (status == 3) {
  460. model->mnemonic = "ERROR:,Mnemonic check failed";
  461. model->page = 2;
  462. }
  463. s_busy = false;
  464. // if overwrite is set and mnemonic generated, return from scene immediately
  465. if (status == -1) {
  466. instance->callback(FlipBipCustomEventScene1Back, instance->context);
  467. }
  468. },
  469. true
  470. );
  471. }
  472. FlipBipScene1* flipbip_scene_1_alloc() {
  473. FlipBipScene1* instance = malloc(sizeof(FlipBipScene1));
  474. instance->view = view_alloc();
  475. view_allocate_model(instance->view, ViewModelTypeLocking, sizeof(FlipBipScene1Model));
  476. view_set_context(instance->view, instance); // furi_assert crashes in events without this
  477. view_set_draw_callback(instance->view, (ViewDrawCallback)flipbip_scene_1_draw);
  478. view_set_input_callback(instance->view, flipbip_scene_1_input);
  479. view_set_enter_callback(instance->view, flipbip_scene_1_enter);
  480. view_set_exit_callback(instance->view, flipbip_scene_1_exit);
  481. return instance;
  482. }
  483. void flipbip_scene_1_free(FlipBipScene1* instance) {
  484. furi_assert(instance);
  485. with_view_model(
  486. instance->view,
  487. FlipBipScene1Model * model,
  488. {
  489. UNUSED(model);
  490. },
  491. true);
  492. flipbip_scene_1_clear_text();
  493. view_free(instance->view);
  494. free(instance);
  495. }
  496. View* flipbip_scene_1_get_view(FlipBipScene1* instance) {
  497. furi_assert(instance);
  498. return instance->view;
  499. }