flipbip_scene_1.c 17 KB

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