flipbip_scene_1.c 15 KB

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