flipbip_scene_1.c 17 KB

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