flipbip_scene_1.c 19 KB

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