flipbip_scene_1.c 13 KB

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