flipbip_scene_1.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. flipbip_itoa(seedbytes[i], seed + (i * 2), 2, 16);
  117. //sprintf(seed + (i * 2), "%.2x", seedbytes[i]);
  118. }
  119. // Split the seed into parts
  120. for (size_t seedpartnum = 1; seedpartnum <= 6; seedpartnum++) {
  121. char *seedpartptr = malloc(22 + 1);
  122. strncpy(seedpartptr, seed + ((seedpartnum - 1) * 22), 22);
  123. if (seedpartnum == 1) model->seed1 = seedpartptr;
  124. if (seedpartnum == 2) model->seed2 = seedpartptr;
  125. if (seedpartnum == 3) model->seed3 = seedpartptr;
  126. if (seedpartnum == 4) model->seed4 = seedpartptr;
  127. if (seedpartnum == 5) model->seed5 = seedpartptr;
  128. if (seedpartnum == 6) model->seed6 = seedpartptr;
  129. }
  130. // WIP / TODO: Generate a BIP32 root key from the mnemonic
  131. // //bool root_set = false;
  132. HDNode *root = malloc(sizeof(HDNode));
  133. hdnode_from_seed(seedbytes, 64, SECP256K1_NAME, root);
  134. // //root_set = true;
  135. // m/44'/0'/0'/0
  136. uint32_t purpose = 44;
  137. uint32_t coin = 0;
  138. uint32_t account = 0;
  139. uint32_t change = 0;
  140. // constants for Bitcoin
  141. const uint32_t version_public = 0x0488b21e;
  142. const uint32_t version_private = 0x0488ade4;
  143. //const char addr_version = 0x00, wif_version = 0x80;
  144. // buffer for key serialization
  145. const size_t buflen = 128;
  146. char buf[128 + 1];
  147. // root
  148. uint32_t fingerprint = 0;
  149. hdnode_serialize_private(root, fingerprint, version_private, buf, buflen);
  150. char *xprv_root = malloc(22 + 1);
  151. strncpy(xprv_root, buf, 22);
  152. model->seed2 = xprv_root;
  153. HDNode *node = root;
  154. // purpose
  155. fingerprint = hdnode_fingerprint(node);
  156. hdnode_private_ckd_prime(node, purpose); // purpose
  157. // coin
  158. fingerprint = hdnode_fingerprint(node);
  159. hdnode_private_ckd_prime(node, coin); // coin
  160. // account
  161. fingerprint = hdnode_fingerprint(node);
  162. hdnode_private_ckd_prime(node, account); // account
  163. hdnode_serialize_private(node, fingerprint, version_private, buf, buflen);
  164. char *xprv_acc = malloc(22 + 1);
  165. strncpy(xprv_acc, buf, 22);
  166. model->seed3 = xprv_acc;
  167. hdnode_serialize_public(node, fingerprint, version_public, buf, buflen);
  168. char *xpub_acc = malloc(22 + 1);
  169. strncpy(xpub_acc, buf, 22);
  170. model->seed4 = xpub_acc;
  171. // external / internal (change)
  172. fingerprint = hdnode_fingerprint(node);
  173. hdnode_private_ckd(node, change); // change
  174. hdnode_serialize_private(node, fingerprint, version_private, buf, buflen);
  175. char *xprv_chg = malloc(22 + 1);
  176. strncpy(xprv_chg, buf, 22);
  177. model->seed5 = xprv_chg;
  178. hdnode_serialize_public(node, fingerprint, version_public, buf, buflen);
  179. char *xpub_chg = malloc(22 + 1);
  180. strncpy(xpub_chg, buf, 22);
  181. model->seed6 = xpub_chg;
  182. // hdnode_private_ckd(&node, chain); // external / internal
  183. // for (int i = 0; i < 10; i++) {
  184. // HDNode node2 = node;
  185. // hdnode_private_ckd(&node2, i);
  186. // hdnode_fill_public_key(&node2);
  187. // ecdsa_get_address(node2.public_key, addr_version, HASHER_SHA2_RIPEMD, HASHER_SHA2D, buf, buflen);
  188. // //QString address = QString(buf);
  189. // ecdsa_get_wif(node2.private_key, wif_version, HASHER_SHA2D, buf, buflen);
  190. // //QString wif = QString(buf);
  191. // // list->setItem(i, 0, new QTableWidgetItem(address));
  192. // // list->setItem(i, 1, new QTableWidgetItem(wif));
  193. // // list->setItem(i, 2, new QTableWidgetItem("0.0"));
  194. // }
  195. // Clear the root node
  196. memzero(root, sizeof(HDNode));
  197. free(root);
  198. // Clear the mnemonic
  199. mnemonic_clear();
  200. memzero(mnemo, strlen(mnemo));
  201. free(mnemo);
  202. // Clear the seed
  203. memzero(seed, sizeof(seed));
  204. free(seed);
  205. #if USE_BIP39_CACHE
  206. // Clear the BIP39 cache
  207. bip39_cache_clear();
  208. #endif
  209. }
  210. bool flipbip_scene_1_input(InputEvent* event, void* context) {
  211. furi_assert(context);
  212. FlipBipScene1* instance = context;
  213. if (event->type == InputTypeRelease) {
  214. switch(event->key) {
  215. case InputKeyBack:
  216. with_view_model(
  217. instance->view,
  218. FlipBipScene1Model * model,
  219. {
  220. UNUSED(model);
  221. instance->callback(FlipBipCustomEventScene1Back, instance->context);
  222. },
  223. true);
  224. break;
  225. case InputKeyLeft:
  226. case InputKeyRight:
  227. case InputKeyUp:
  228. case InputKeyDown:
  229. case InputKeyOk:
  230. with_view_model(
  231. instance->view,
  232. FlipBipScene1Model* model,
  233. {
  234. //UNUSED(model);
  235. model->page = (model->page + 1) % 2;
  236. },
  237. true);
  238. break;
  239. case InputKeyMAX:
  240. break;
  241. }
  242. }
  243. return true;
  244. }
  245. void flipbip_scene_1_exit(void* context) {
  246. furi_assert(context);
  247. FlipBipScene1* instance = (FlipBipScene1*)context;
  248. with_view_model(
  249. instance->view,
  250. FlipBipScene1Model * model,
  251. {
  252. // Clear the mnemonic from memory
  253. model->page = 0;
  254. model->strength = 0;
  255. memzero((void*)model->seed1, strlen(model->seed1));
  256. memzero((void*)model->seed2, strlen(model->seed2));
  257. memzero((void*)model->seed3, strlen(model->seed3));
  258. memzero((void*)model->seed4, strlen(model->seed4));
  259. memzero((void*)model->seed5, strlen(model->seed5));
  260. memzero((void*)model->seed6, strlen(model->seed6));
  261. memzero((void*)model->mnemonic1, strlen(model->mnemonic1));
  262. memzero((void*)model->mnemonic2, strlen(model->mnemonic2));
  263. memzero((void*)model->mnemonic3, strlen(model->mnemonic3));
  264. memzero((void*)model->mnemonic4, strlen(model->mnemonic4));
  265. memzero((void*)model->mnemonic5, strlen(model->mnemonic5));
  266. memzero((void*)model->mnemonic6, strlen(model->mnemonic6));
  267. },
  268. true
  269. );
  270. }
  271. void flipbip_scene_1_enter(void* context) {
  272. furi_assert(context);
  273. FlipBipScene1* instance = (FlipBipScene1*)context;
  274. FlipBip* app = instance->context;
  275. int strength_setting = app->bip39_strength;
  276. int strength = 256;
  277. if (strength_setting == 0) strength = 128;
  278. else if (strength_setting == 1) strength = 192;
  279. flipbip_play_happy_bump(app);
  280. flipbip_led_set_rgb(app, 255, 0, 0);
  281. with_view_model(
  282. instance->view,
  283. FlipBipScene1Model * model,
  284. {
  285. flipbip_scene_1_model_init(model, strength);
  286. },
  287. true
  288. );
  289. }
  290. FlipBipScene1* flipbip_scene_1_alloc() {
  291. FlipBipScene1* instance = malloc(sizeof(FlipBipScene1));
  292. instance->view = view_alloc();
  293. view_allocate_model(instance->view, ViewModelTypeLocking, sizeof(FlipBipScene1Model));
  294. view_set_context(instance->view, instance); // furi_assert crashes in events without this
  295. view_set_draw_callback(instance->view, (ViewDrawCallback)flipbip_scene_1_draw);
  296. view_set_input_callback(instance->view, flipbip_scene_1_input);
  297. view_set_enter_callback(instance->view, flipbip_scene_1_enter);
  298. view_set_exit_callback(instance->view, flipbip_scene_1_exit);
  299. // FlipBip* app = instance->context;
  300. // int strength_setting = app->bip39_strength;
  301. // int strength = 256;
  302. // if (strength_setting == 0) strength = 128;
  303. // else if (strength_setting == 1) strength = 192;
  304. // with_view_model(
  305. // instance->view,
  306. // FlipBipScene1Model * model,
  307. // {
  308. // flipbip_scene_1_model_init(model, strength);
  309. // },
  310. // true
  311. // );
  312. return instance;
  313. }
  314. void flipbip_scene_1_free(FlipBipScene1* instance) {
  315. furi_assert(instance);
  316. with_view_model(
  317. instance->view,
  318. FlipBipScene1Model * model,
  319. {
  320. //UNUSED(model);
  321. free((void*)model->seed1);
  322. free((void*)model->seed2);
  323. free((void*)model->seed3);
  324. free((void*)model->seed4);
  325. free((void*)model->seed5);
  326. free((void*)model->seed6);
  327. free((void*)model->mnemonic1);
  328. free((void*)model->mnemonic2);
  329. free((void*)model->mnemonic3);
  330. free((void*)model->mnemonic4);
  331. free((void*)model->mnemonic5);
  332. free((void*)model->mnemonic6);
  333. },
  334. true);
  335. view_free(instance->view);
  336. free(instance);
  337. }
  338. View* flipbip_scene_1_get_view(FlipBipScene1* instance) {
  339. furi_assert(instance);
  340. return instance->view;
  341. }