flipbip_scene_1.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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 "FlipBIP_icons.h"
  9. #include "../helpers/flipbip_haptic.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. canvas_draw_icon(canvas, 86, 25, &I_Keychain_39x36);
  217. } else if(model->page >= 9 && model->page <= 13) {
  218. canvas_set_font(canvas, FontSecondary);
  219. const char* receive_text;
  220. if(model->coin == 0) { // BTC
  221. receive_text = "BTC receive address:";
  222. } else if(model->coin == 60) { // ETH
  223. receive_text = "ETH receive address:";
  224. } else {
  225. receive_text = "Receive address:";
  226. }
  227. canvas_draw_str_aligned(canvas, 1, 2, AlignLeft, AlignTop, receive_text);
  228. canvas_set_font(canvas, FontPrimary);
  229. canvas_draw_str(canvas, 6, 22, s_disp_text1);
  230. canvas_draw_str(canvas, 6, 34, s_disp_text2);
  231. canvas_draw_str(canvas, 6, 46, s_disp_text3);
  232. canvas_draw_str(canvas, 6, 58, s_disp_text4);
  233. } else {
  234. canvas_set_font(canvas, FontSecondary);
  235. canvas_draw_str_aligned(canvas, 1, 2, AlignLeft, AlignTop, s_disp_text1);
  236. canvas_draw_str_aligned(canvas, 1, 12, AlignLeft, AlignTop, s_disp_text2);
  237. canvas_draw_str_aligned(canvas, 1, 22, AlignLeft, AlignTop, s_disp_text3);
  238. canvas_draw_str_aligned(canvas, 1, 32, AlignLeft, AlignTop, s_disp_text4);
  239. canvas_draw_str_aligned(canvas, 1, 42, AlignLeft, AlignTop, s_disp_text5);
  240. canvas_draw_str_aligned(canvas, 1, 52, AlignLeft, AlignTop, s_disp_text6);
  241. }
  242. }
  243. static int flipbip_scene_1_model_init(
  244. FlipBipScene1Model* const model,
  245. const int strength,
  246. const uint32_t coin,
  247. const bool overwrite) {
  248. model->page = 0;
  249. model->mnemonic_only = false;
  250. model->strength = strength;
  251. model->coin = coin;
  252. model->overwrite = overwrite;
  253. // Allocate memory for mnemonic
  254. char* mnemonic = malloc(256);
  255. memzero(mnemonic, 256);
  256. // Check if the mnemonic key & data is already saved in persistent storage, or overwrite is true
  257. if(overwrite || (!flipbip_has_settings(true) && !flipbip_has_settings(false))) {
  258. // Generate a random mnemonic using trezor-crypto
  259. const char* mnemonic_gen = mnemonic_generate(strength);
  260. // Save the mnemonic to persistent storage
  261. if(!flipbip_save_settings_secure(mnemonic_gen)) return 1; // 1 = save error
  262. // Clear the generated mnemonic from memory
  263. mnemonic_clear();
  264. model->mnemonic_only = true;
  265. }
  266. // Load the mnemonic from persistent storage
  267. if(!flipbip_load_settings_secure(mnemonic)) return 2; // 2 = load error
  268. model->mnemonic = mnemonic;
  269. // Check if the mnemonic is valid
  270. if(mnemonic_check(model->mnemonic) == 0) return 3; // 3 = mnemonic check error
  271. // if we are only generating the mnemonic, return
  272. if(model->mnemonic_only) {
  273. return -1; // -1 = mnemonic only, return from parent
  274. }
  275. // test mnemonic
  276. //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";
  277. // Generate a BIP39 seed from the mnemonic
  278. mnemonic_to_seed(model->mnemonic, "", model->seed, 0);
  279. // Generate a BIP32 root HD node from the mnemonic
  280. HDNode* root = malloc(sizeof(HDNode));
  281. hdnode_from_seed(model->seed, 64, SECP256K1_NAME, root);
  282. // m/44'/0'/0'/0 or m/44'/60'/0'/0
  283. const uint32_t purpose = 44;
  284. //const uint32_t coin = 0; // BTC
  285. //const uint32_t coin = 60; // ETH
  286. const uint32_t account = 0;
  287. const uint32_t change = 0;
  288. // constants for BTC / ETH
  289. const uint32_t version_public = 0x0488b21e;
  290. const uint32_t version_private = 0x0488ade4;
  291. // "xprv_magic": 76066276,
  292. // "xpub_magic": 76067358,
  293. // "xpub_magic_segwit_p2sh": 77429938,
  294. // "xpub_magic_segwit_native": 78792518,
  295. // "xpub_magic_multisig_segwit_p2sh": 43365439,
  296. // "xpub_magic_multisig_segwit_native": 44728019,
  297. // buffer for key serialization
  298. const size_t buflen = 128;
  299. char buf[128 + 1];
  300. // root
  301. uint32_t fingerprint = 0;
  302. hdnode_serialize_private(root, fingerprint, version_private, buf, buflen);
  303. char* xprv_root = malloc(buflen + 1);
  304. strncpy(xprv_root, buf, buflen);
  305. model->xprv_root = xprv_root;
  306. HDNode* node = root;
  307. // purpose m/44'
  308. fingerprint = hdnode_fingerprint(node);
  309. hdnode_private_ckd_prime(node, purpose); // purpose
  310. // coin m/44'/0' or m/44'/60'
  311. fingerprint = hdnode_fingerprint(node);
  312. hdnode_private_ckd_prime(node, model->coin); // coin
  313. // account m/44'/0'/0' or m/44'/60'/0'
  314. fingerprint = hdnode_fingerprint(node);
  315. hdnode_private_ckd_prime(node, account); // account
  316. hdnode_serialize_private(node, fingerprint, version_private, buf, buflen);
  317. char* xprv_acc = malloc(buflen + 1);
  318. strncpy(xprv_acc, buf, buflen);
  319. model->xprv_account = xprv_acc;
  320. hdnode_serialize_public(node, fingerprint, version_public, buf, buflen);
  321. char* xpub_acc = malloc(buflen + 1);
  322. strncpy(xpub_acc, buf, buflen);
  323. model->xpub_account = xpub_acc;
  324. // external/internal (change) m/44'/0'/0'/0 or m/44'/60'/0'/0
  325. fingerprint = hdnode_fingerprint(node);
  326. hdnode_private_ckd(node, change); // external/internal (change)
  327. hdnode_serialize_private(node, fingerprint, version_private, buf, buflen);
  328. char* xprv_ext = malloc(buflen + 1);
  329. strncpy(xprv_ext, buf, buflen);
  330. model->xprv_extended = xprv_ext;
  331. hdnode_serialize_public(node, fingerprint, version_public, buf, buflen);
  332. char* xpub_ext = malloc(buflen + 1);
  333. strncpy(xpub_ext, buf, buflen);
  334. model->xpub_extended = xpub_ext;
  335. model->node = node;
  336. model->page = 1;
  337. #if USE_BIP39_CACHE
  338. // Clear the BIP39 cache
  339. bip39_cache_clear();
  340. #endif
  341. // 0 = success
  342. return 0;
  343. }
  344. bool flipbip_scene_1_input(InputEvent* event, void* context) {
  345. furi_assert(context);
  346. FlipBipScene1* instance = context;
  347. // Ignore input if busy
  348. if(s_busy) {
  349. return false;
  350. }
  351. if(event->type == InputTypeRelease) {
  352. switch(event->key) {
  353. case InputKeyBack:
  354. with_view_model(
  355. instance->view,
  356. FlipBipScene1Model * model,
  357. {
  358. UNUSED(model);
  359. instance->callback(FlipBipCustomEventScene1Back, instance->context);
  360. },
  361. true);
  362. break;
  363. case InputKeyRight:
  364. case InputKeyDown:
  365. case InputKeyOk:
  366. with_view_model(
  367. instance->view,
  368. FlipBipScene1Model * model,
  369. {
  370. //UNUSED(model);
  371. model->page = (model->page + 1) % 14;
  372. if(model->page == 0) {
  373. model->page = 1;
  374. }
  375. },
  376. true);
  377. break;
  378. case InputKeyLeft:
  379. case InputKeyUp:
  380. with_view_model(
  381. instance->view,
  382. FlipBipScene1Model * model,
  383. {
  384. //UNUSED(model);
  385. model->page = (model->page - 1) % 14;
  386. if(model->page == 0) {
  387. model->page = 13;
  388. }
  389. },
  390. true);
  391. break;
  392. case InputKeyMAX:
  393. break;
  394. }
  395. }
  396. return true;
  397. }
  398. void flipbip_scene_1_exit(void* context) {
  399. furi_assert(context);
  400. FlipBipScene1* instance = (FlipBipScene1*)context;
  401. with_view_model(
  402. instance->view,
  403. FlipBipScene1Model * model,
  404. {
  405. model->page = 0;
  406. model->strength = 0;
  407. model->coin = 0;
  408. memzero(model->seed, 64);
  409. // if mnemonic_only is true, then we don't need to free the data here
  410. if(!model->mnemonic_only) {
  411. memzero((void*)model->mnemonic, strlen(model->mnemonic));
  412. free((void*)model->mnemonic);
  413. memzero((void*)model->node, sizeof(HDNode));
  414. free((void*)model->node);
  415. memzero((void*)model->xprv_root, strlen(model->xprv_root));
  416. memzero((void*)model->xprv_account, strlen(model->xprv_account));
  417. memzero((void*)model->xpub_account, strlen(model->xpub_account));
  418. memzero((void*)model->xprv_extended, strlen(model->xprv_extended));
  419. memzero((void*)model->xpub_extended, strlen(model->xpub_extended));
  420. free((void*)model->xprv_root);
  421. free((void*)model->xprv_account);
  422. free((void*)model->xpub_account);
  423. free((void*)model->xprv_extended);
  424. free((void*)model->xpub_extended);
  425. }
  426. },
  427. true);
  428. flipbip_scene_1_clear_text();
  429. }
  430. void flipbip_scene_1_enter(void* context) {
  431. furi_assert(context);
  432. FlipBipScene1* instance = (FlipBipScene1*)context;
  433. FlipBip* app = instance->context;
  434. // BIP39 Strength setting
  435. int strength_setting = app->bip39_strength;
  436. int strength = 256; // FlipBipStrength256 // 24 words (256 bit)
  437. if(strength_setting == FlipBipStrength128)
  438. strength = 128; // 12 words (128 bit)
  439. else if(strength_setting == FlipBipStrength192)
  440. strength = 192; // 18 words (192 bit)
  441. // BIP44 Coin setting
  442. int coin_setting = app->bip44_coin;
  443. uint32_t coin = 0; //FlipBipCoinBTC0 // BTC (0)
  444. if(coin_setting == FlipBipCoinETH60) coin = 60; // ETH (60)
  445. // Overwrite the saved seed with a new one setting
  446. bool overwrite = app->overwrite_saved_seed != 0;
  447. flipbip_play_happy_bump(app);
  448. flipbip_led_set_rgb(app, 255, 0, 0);
  449. with_view_model(
  450. instance->view,
  451. FlipBipScene1Model * model,
  452. {
  453. s_busy = true;
  454. const int status = flipbip_scene_1_model_init(model, strength, coin, overwrite);
  455. // nonzero status, free the mnemonic
  456. if(status != 0) {
  457. memzero((void*)model->mnemonic, strlen(model->mnemonic));
  458. free((void*)model->mnemonic);
  459. }
  460. // if error, set the error message
  461. if(status == 1) {
  462. model->mnemonic = "ERROR:,Save error";
  463. model->page = 2;
  464. } else if(status == 2) {
  465. model->mnemonic = "ERROR:,Load error";
  466. model->page = 2;
  467. } else if(status == 3) {
  468. model->mnemonic = "ERROR:,Mnemonic check failed";
  469. model->page = 2;
  470. }
  471. s_busy = false;
  472. // if overwrite is set and mnemonic generated, return from scene immediately
  473. if(status == -1) {
  474. instance->callback(FlipBipCustomEventScene1Back, instance->context);
  475. }
  476. },
  477. true);
  478. }
  479. FlipBipScene1* flipbip_scene_1_alloc() {
  480. FlipBipScene1* instance = malloc(sizeof(FlipBipScene1));
  481. instance->view = view_alloc();
  482. view_allocate_model(instance->view, ViewModelTypeLocking, sizeof(FlipBipScene1Model));
  483. view_set_context(instance->view, instance); // furi_assert crashes in events without this
  484. view_set_draw_callback(instance->view, (ViewDrawCallback)flipbip_scene_1_draw);
  485. view_set_input_callback(instance->view, flipbip_scene_1_input);
  486. view_set_enter_callback(instance->view, flipbip_scene_1_enter);
  487. view_set_exit_callback(instance->view, flipbip_scene_1_exit);
  488. return instance;
  489. }
  490. void flipbip_scene_1_free(FlipBipScene1* instance) {
  491. furi_assert(instance);
  492. with_view_model(
  493. instance->view, FlipBipScene1Model * model, { UNUSED(model); }, true);
  494. flipbip_scene_1_clear_text();
  495. view_free(instance->view);
  496. free(instance);
  497. }
  498. View* flipbip_scene_1_get_view(FlipBipScene1* instance) {
  499. furi_assert(instance);
  500. return instance->view;
  501. }