flipbip_scene_1.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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/ecdsa.h"
  16. #include "../crypto/curves.h"
  17. #include "../crypto/memzero.h"
  18. struct FlipBipScene1 {
  19. View* view;
  20. FlipBipScene1Callback callback;
  21. void* context;
  22. };
  23. typedef struct {
  24. int page;
  25. int strength;
  26. const char* seed1;
  27. const char* seed2;
  28. const char* seed3;
  29. const char* seed4;
  30. const char* seed5;
  31. const char* seed6;
  32. const char* mnemonic1;
  33. const char* mnemonic2;
  34. const char* mnemonic3;
  35. const char* mnemonic4;
  36. const char* mnemonic5;
  37. const char* mnemonic6;
  38. } FlipBipScene1Model;
  39. void flipbip_scene_1_set_callback(
  40. FlipBipScene1* instance,
  41. FlipBipScene1Callback callback,
  42. void* context) {
  43. furi_assert(instance);
  44. furi_assert(callback);
  45. instance->callback = callback;
  46. instance->context = context;
  47. }
  48. void flipbip_scene_1_draw(Canvas* canvas, FlipBipScene1Model* model) {
  49. //UNUSED(model);
  50. canvas_clear(canvas);
  51. canvas_set_color(canvas, ColorBlack);
  52. //canvas_set_font(canvas, FontPrimary);
  53. //canvas_draw_str_aligned(canvas, 0, 10, AlignLeft, AlignTop, "This is Scene 1");
  54. canvas_set_font(canvas, FontSecondary);
  55. //canvas_draw_str_aligned(canvas, 1, 2, AlignLeft, AlignTop, model->strength == 128 ? "128-bit" : "256-bit");
  56. //canvas_draw_str_aligned(canvas, 1, 2, AlignLeft, AlignTop, model->seed);
  57. // Mnenomic
  58. if (model->page == 0) {
  59. canvas_draw_str_aligned(canvas, 1, 2, AlignLeft, AlignTop, model->mnemonic1);
  60. canvas_draw_str_aligned(canvas, 1, 12, AlignLeft, AlignTop, model->mnemonic2);
  61. canvas_draw_str_aligned(canvas, 1, 22, AlignLeft, AlignTop, model->mnemonic3);
  62. canvas_draw_str_aligned(canvas, 1, 32, AlignLeft, AlignTop, model->mnemonic4);
  63. canvas_draw_str_aligned(canvas, 1, 42, AlignLeft, AlignTop, model->mnemonic5);
  64. canvas_draw_str_aligned(canvas, 1, 52, AlignLeft, AlignTop, model->mnemonic6);
  65. }
  66. // Seed
  67. else if (model->page == 1) {
  68. canvas_draw_str_aligned(canvas, 1, 2, AlignLeft, AlignTop, model->seed1);
  69. canvas_draw_str_aligned(canvas, 1, 12, AlignLeft, AlignTop, model->seed2);
  70. canvas_draw_str_aligned(canvas, 1, 22, AlignLeft, AlignTop, model->seed3);
  71. canvas_draw_str_aligned(canvas, 1, 32, AlignLeft, AlignTop, model->seed4);
  72. canvas_draw_str_aligned(canvas, 1, 42, AlignLeft, AlignTop, model->seed5);
  73. canvas_draw_str_aligned(canvas, 1, 52, AlignLeft, AlignTop, model->seed6);
  74. }
  75. }
  76. static void flipbip_scene_1_model_init(FlipBipScene1Model* const model, const int strength) {
  77. model->page = 0;
  78. // Generate a random mnemonic using trezor-crypto
  79. model->strength = strength;
  80. const char *mnemonic = mnemonic_generate(model->strength);
  81. // Delineate sections of the mnemonic every 4 words
  82. char *mnemo = malloc(strlen(mnemonic) + 1);
  83. strcpy(mnemo, mnemonic);
  84. int word = 0;
  85. for (size_t i = 0; i < strlen(mnemo); i++) {
  86. if (mnemo[i] == ' ') {
  87. word++;
  88. if (word % 4 == 0) {
  89. mnemo[i] = ',';
  90. }
  91. }
  92. }
  93. // Split the mnemonic into parts
  94. char *mnemopart = flipbip_strtok(mnemo, ",");
  95. int partnum = 0;
  96. while(mnemopart != NULL)
  97. {
  98. char *partptr = malloc(strlen(mnemopart) + 1);
  99. strcpy(partptr, mnemopart);
  100. partnum++;
  101. if (partnum == 1) model->mnemonic1 = partptr;
  102. if (partnum == 2) model->mnemonic2 = partptr;
  103. if (partnum == 3) model->mnemonic3 = partptr;
  104. if (partnum == 4) model->mnemonic4 = partptr;
  105. if (partnum == 5) model->mnemonic5 = partptr;
  106. if (partnum == 6) model->mnemonic6 = partptr;
  107. mnemopart = flipbip_strtok(NULL, ",");
  108. }
  109. // Generate a BIP39 seed from the mnemonic
  110. uint8_t seedbytes[64];
  111. //mnemonic_to_seed(mnemonic, "", seedbytes, 0);
  112. mnemonic_to_seed("wealth budget salt video delay obey neutral tail sure soda hold rubber joy movie boat raccoon tornado noise off inmate payment patch group topple", "", seedbytes, 0);
  113. char *seed = malloc(64 * 2 + 1);
  114. // Convert the seed to a hex string
  115. for (size_t i = 0; i < 64; i++) {
  116. sprintf(seed + (i * 2), "%.2x", seedbytes[i]);
  117. }
  118. // Split the seed into parts
  119. for (size_t seedpartnum = 1; seedpartnum <= 6; seedpartnum++) {
  120. char *seedpartptr = malloc(22 + 1);
  121. strncpy(seedpartptr, seed + ((seedpartnum - 1) * 22), 22);
  122. if (seedpartnum == 1) model->seed1 = seedpartptr;
  123. if (seedpartnum == 2) model->seed2 = seedpartptr;
  124. if (seedpartnum == 3) model->seed3 = seedpartptr;
  125. if (seedpartnum == 4) model->seed4 = seedpartptr;
  126. if (seedpartnum == 5) model->seed5 = seedpartptr;
  127. if (seedpartnum == 6) model->seed6 = seedpartptr;
  128. }
  129. // WIP / TODO: Generate a BIP32 root key from the mnemonic
  130. // //bool root_set = false;
  131. HDNode *root = malloc(sizeof(HDNode));
  132. hdnode_from_seed(seedbytes, 64, SECP256K1_NAME, root);
  133. // //root_set = true;
  134. // m/44'/0'/0'/0
  135. uint32_t purpose = 44;
  136. uint32_t coin = 0;
  137. uint32_t account = 0;
  138. uint32_t change = 0;
  139. // constants for Bitcoin
  140. const uint32_t version_public = 0x0488b21e;
  141. const uint32_t version_private = 0x0488ade4;
  142. //const char addr_version = 0x00, wif_version = 0x80;
  143. const size_t buflen = 128;
  144. char buf[buflen + 1];
  145. // root
  146. uint32_t fingerprint = 0;
  147. hdnode_serialize_private(root, fingerprint, version_private, buf, buflen);
  148. char *xprv_root = malloc(22 + 1);
  149. strncpy(xprv_root, buf, 22);
  150. model->seed2 = xprv_root;
  151. HDNode *node = root;
  152. // purpose
  153. fingerprint = hdnode_fingerprint(node);
  154. hdnode_private_ckd_prime(node, purpose); // purpose
  155. // coin
  156. fingerprint = hdnode_fingerprint(node);
  157. hdnode_private_ckd_prime(node, coin); // coin
  158. // account
  159. fingerprint = hdnode_fingerprint(node);
  160. hdnode_private_ckd_prime(node, account); // account
  161. hdnode_serialize_private(node, fingerprint, version_private, buf, buflen);
  162. char *xprv_acc = malloc(22 + 1);
  163. strncpy(xprv_acc, buf, 22);
  164. model->seed3 = xprv_acc;
  165. hdnode_serialize_public(node, fingerprint, version_public, buf, buflen);
  166. char *xpub_acc = malloc(22 + 1);
  167. strncpy(xpub_acc, buf, 22);
  168. model->seed4 = xpub_acc;
  169. // external / internal (change)
  170. fingerprint = hdnode_fingerprint(node);
  171. hdnode_private_ckd(node, change); // change
  172. hdnode_serialize_private(node, fingerprint, version_private, buf, buflen);
  173. char *xprv_chg = malloc(22 + 1);
  174. strncpy(xprv_chg, buf, 22);
  175. model->seed5 = xprv_chg;
  176. hdnode_serialize_public(node, fingerprint, version_public, buf, buflen);
  177. char *xpub_chg = malloc(22 + 1);
  178. strncpy(xpub_chg, buf, 22);
  179. model->seed6 = xpub_chg;
  180. // hdnode_private_ckd(&node, chain); // external / internal
  181. // for (int i = 0; i < 10; i++) {
  182. // HDNode node2 = node;
  183. // hdnode_private_ckd(&node2, i);
  184. // hdnode_fill_public_key(&node2);
  185. // ecdsa_get_address(node2.public_key, addr_version, HASHER_SHA2_RIPEMD, HASHER_SHA2D, buf, buflen);
  186. // //QString address = QString(buf);
  187. // ecdsa_get_wif(node2.private_key, wif_version, HASHER_SHA2D, buf, buflen);
  188. // //QString wif = QString(buf);
  189. // // list->setItem(i, 0, new QTableWidgetItem(address));
  190. // // list->setItem(i, 1, new QTableWidgetItem(wif));
  191. // // list->setItem(i, 2, new QTableWidgetItem("0.0"));
  192. // }
  193. // Clear the root node
  194. memzero(root, sizeof(HDNode));
  195. free(root);
  196. // Clear the mnemonic
  197. mnemonic_clear();
  198. memzero(mnemo, strlen(mnemo));
  199. free(mnemo);
  200. // Clear the seed
  201. memzero(seed, sizeof(seed));
  202. free(seed);
  203. // Clear the BIP39 cache
  204. bip39_cache_clear();
  205. }
  206. bool flipbip_scene_1_input(InputEvent* event, void* context) {
  207. furi_assert(context);
  208. FlipBipScene1* instance = context;
  209. if (event->type == InputTypeRelease) {
  210. switch(event->key) {
  211. case InputKeyBack:
  212. with_view_model(
  213. instance->view,
  214. FlipBipScene1Model * model,
  215. {
  216. UNUSED(model);
  217. instance->callback(FlipBipCustomEventScene1Back, instance->context);
  218. },
  219. true);
  220. break;
  221. case InputKeyLeft:
  222. case InputKeyRight:
  223. case InputKeyUp:
  224. case InputKeyDown:
  225. case InputKeyOk:
  226. with_view_model(
  227. instance->view,
  228. FlipBipScene1Model* model,
  229. {
  230. //UNUSED(model);
  231. model->page = (model->page + 1) % 2;
  232. },
  233. true);
  234. break;
  235. case InputKeyMAX:
  236. break;
  237. }
  238. }
  239. return true;
  240. }
  241. void flipbip_scene_1_exit(void* context) {
  242. furi_assert(context);
  243. FlipBipScene1* instance = (FlipBipScene1*)context;
  244. with_view_model(
  245. instance->view,
  246. FlipBipScene1Model * model,
  247. {
  248. // Clear the mnemonic from memory
  249. model->page = 0;
  250. model->strength = 0;
  251. memzero((void*)model->seed1, strlen(model->seed1));
  252. memzero((void*)model->seed2, strlen(model->seed2));
  253. memzero((void*)model->seed3, strlen(model->seed3));
  254. memzero((void*)model->seed4, strlen(model->seed4));
  255. memzero((void*)model->seed5, strlen(model->seed5));
  256. memzero((void*)model->seed6, strlen(model->seed6));
  257. memzero((void*)model->mnemonic1, strlen(model->mnemonic1));
  258. memzero((void*)model->mnemonic2, strlen(model->mnemonic2));
  259. memzero((void*)model->mnemonic3, strlen(model->mnemonic3));
  260. memzero((void*)model->mnemonic4, strlen(model->mnemonic4));
  261. memzero((void*)model->mnemonic5, strlen(model->mnemonic5));
  262. memzero((void*)model->mnemonic6, strlen(model->mnemonic6));
  263. },
  264. true
  265. );
  266. }
  267. void flipbip_scene_1_enter(void* context) {
  268. furi_assert(context);
  269. FlipBipScene1* instance = (FlipBipScene1*)context;
  270. FlipBip* app = instance->context;
  271. int strength_setting = app->bip39_strength;
  272. int strength = 256;
  273. if (strength_setting == 0) strength = 128;
  274. else if (strength_setting == 1) strength = 192;
  275. flipbip_play_happy_bump(app);
  276. flipbip_led_set_rgb(app, 255, 0, 0);
  277. with_view_model(
  278. instance->view,
  279. FlipBipScene1Model * model,
  280. {
  281. flipbip_scene_1_model_init(model, strength);
  282. },
  283. true
  284. );
  285. }
  286. FlipBipScene1* flipbip_scene_1_alloc() {
  287. FlipBipScene1* instance = malloc(sizeof(FlipBipScene1));
  288. instance->view = view_alloc();
  289. view_allocate_model(instance->view, ViewModelTypeLocking, sizeof(FlipBipScene1Model));
  290. view_set_context(instance->view, instance); // furi_assert crashes in events without this
  291. view_set_draw_callback(instance->view, (ViewDrawCallback)flipbip_scene_1_draw);
  292. view_set_input_callback(instance->view, flipbip_scene_1_input);
  293. view_set_enter_callback(instance->view, flipbip_scene_1_enter);
  294. view_set_exit_callback(instance->view, flipbip_scene_1_exit);
  295. // FlipBip* app = instance->context;
  296. // int strength_setting = app->bip39_strength;
  297. // int strength = 256;
  298. // if (strength_setting == 0) strength = 128;
  299. // else if (strength_setting == 1) strength = 192;
  300. // with_view_model(
  301. // instance->view,
  302. // FlipBipScene1Model * model,
  303. // {
  304. // flipbip_scene_1_model_init(model, strength);
  305. // },
  306. // true
  307. // );
  308. return instance;
  309. }
  310. void flipbip_scene_1_free(FlipBipScene1* instance) {
  311. furi_assert(instance);
  312. with_view_model(
  313. instance->view,
  314. FlipBipScene1Model * model,
  315. {
  316. //UNUSED(model);
  317. free((void*)model->seed1);
  318. free((void*)model->seed2);
  319. free((void*)model->seed3);
  320. free((void*)model->seed4);
  321. free((void*)model->seed5);
  322. free((void*)model->seed6);
  323. free((void*)model->mnemonic1);
  324. free((void*)model->mnemonic2);
  325. free((void*)model->mnemonic3);
  326. free((void*)model->mnemonic4);
  327. free((void*)model->mnemonic5);
  328. free((void*)model->mnemonic6);
  329. },
  330. true);
  331. view_free(instance->view);
  332. free(instance);
  333. }
  334. View* flipbip_scene_1_get_view(FlipBipScene1* instance) {
  335. furi_assert(instance);
  336. return instance->view;
  337. }