flipbip39_scene_1.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. #include "../flipbip39.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/flipbip39_haptic.h"
  8. #include "../helpers/flipbip39_speaker.h"
  9. #include "../helpers/flipbip39_led.h"
  10. #include "../helpers/flipbip39_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 FlipBip39Scene1 {
  19. View* view;
  20. FlipBip39Scene1Callback 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. } FlipBip39Scene1Model;
  39. void flipbip39_scene_1_set_callback(
  40. FlipBip39Scene1* instance,
  41. FlipBip39Scene1Callback callback,
  42. void* context) {
  43. furi_assert(instance);
  44. furi_assert(callback);
  45. instance->callback = callback;
  46. instance->context = context;
  47. }
  48. void flipbip39_scene_1_draw(Canvas* canvas, FlipBip39Scene1Model* 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 flipbip39_scene_1_model_init(FlipBip39Scene1Model* 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 = flipbip39_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 = flipbip39_strtok(NULL, ",");
  108. }
  109. // Generate a BIP39 seed from the mnemonic
  110. uint8_t seedbytes[64];
  111. mnemonic_to_seed(mnemonic, "", seedbytes, 0);
  112. char *seed = malloc(64 * 2 + 1);
  113. // Convert the seed to a hex string
  114. for (size_t i = 0; i < 64; i++) {
  115. sprintf(seed + (i * 2), "%.2x", seedbytes[i]);
  116. }
  117. // Split the seed into parts
  118. for (size_t seedpartnum = 1; seedpartnum <= 6; seedpartnum++) {
  119. char *seedpartptr = malloc(22 + 1);
  120. strncpy(seedpartptr, seed + ((seedpartnum - 1) * 22), 22);
  121. if (seedpartnum == 1) model->seed1 = seedpartptr;
  122. if (seedpartnum == 2) model->seed2 = seedpartptr;
  123. if (seedpartnum == 3) model->seed3 = seedpartptr;
  124. if (seedpartnum == 4) model->seed4 = seedpartptr;
  125. if (seedpartnum == 5) model->seed5 = seedpartptr;
  126. if (seedpartnum == 6) model->seed6 = seedpartptr;
  127. }
  128. // WIP / TODO: Generate a BIP32 root key from the mnemonic
  129. // //bool root_set = false;
  130. // HDNode root;
  131. // hdnode_from_seed(seed, 64, SECP256K1_NAME, &root);
  132. // //root_set = true;
  133. // int arg1 = 1;
  134. // // constants for Bitcoin
  135. // const uint32_t version_public = 0x0488b21e;
  136. // const uint32_t version_private = 0x0488ade4;
  137. // const char addr_version = 0x00, wif_version = 0x80;
  138. // const size_t buflen = 128;
  139. // char buf[buflen + 1];
  140. // HDNode node;
  141. // uint32_t fingerprint;
  142. // // external chain
  143. // for (int chain = 0; chain < 2; chain++) {
  144. // //QTableWidget *list = chain == 0 ? ui->listAddress : ui->listChange;
  145. // node = root;
  146. // hdnode_private_ckd(&node, 44 | 0x80000000);
  147. // hdnode_private_ckd(&node, 0 | 0x80000000); // bitcoin
  148. // hdnode_private_ckd(&node, (arg1 - 1) | 0x80000000);
  149. // fingerprint = hdnode_fingerprint(&node);
  150. // hdnode_serialize_private(&node, fingerprint, version_private, buf, buflen);
  151. // //QString xprv = QString(buf); ui->lineXprv->setText(xprv);
  152. // hdnode_serialize_public(&node, fingerprint, version_public, buf, buflen);
  153. // //QString xpub = QString(buf); ui->lineXpub->setText(xpub);
  154. // hdnode_private_ckd(&node, chain); // external / internal
  155. // for (int i = 0; i < 10; i++) {
  156. // HDNode node2 = node;
  157. // hdnode_private_ckd(&node2, i);
  158. // hdnode_fill_public_key(&node2);
  159. // ecdsa_get_address(node2.public_key, addr_version, HASHER_SHA2_RIPEMD, HASHER_SHA2D, buf, buflen);
  160. // //QString address = QString(buf);
  161. // ecdsa_get_wif(node2.private_key, wif_version, HASHER_SHA2D, buf, buflen);
  162. // //QString wif = QString(buf);
  163. // // list->setItem(i, 0, new QTableWidgetItem(address));
  164. // // list->setItem(i, 1, new QTableWidgetItem(wif));
  165. // // list->setItem(i, 2, new QTableWidgetItem("0.0"));
  166. // }
  167. // }
  168. // Clear the mnemonic
  169. mnemonic_clear();
  170. memzero(mnemo, strlen(mnemo));
  171. free(mnemo);
  172. // Clear the seed
  173. memzero(seed, sizeof(seed));
  174. free(seed);
  175. // Clear the BIP39 cache
  176. bip39_cache_clear();
  177. }
  178. bool flipbip39_scene_1_input(InputEvent* event, void* context) {
  179. furi_assert(context);
  180. FlipBip39Scene1* instance = context;
  181. if (event->type == InputTypeRelease) {
  182. switch(event->key) {
  183. case InputKeyBack:
  184. with_view_model(
  185. instance->view,
  186. FlipBip39Scene1Model * model,
  187. {
  188. UNUSED(model);
  189. instance->callback(FlipBip39CustomEventScene1Back, instance->context);
  190. },
  191. true);
  192. break;
  193. case InputKeyLeft:
  194. case InputKeyRight:
  195. case InputKeyUp:
  196. case InputKeyDown:
  197. case InputKeyOk:
  198. with_view_model(
  199. instance->view,
  200. FlipBip39Scene1Model* model,
  201. {
  202. //UNUSED(model);
  203. model->page = (model->page + 1) % 2;
  204. },
  205. true);
  206. break;
  207. case InputKeyMAX:
  208. break;
  209. }
  210. }
  211. return true;
  212. }
  213. void flipbip39_scene_1_exit(void* context) {
  214. furi_assert(context);
  215. FlipBip39Scene1* instance = (FlipBip39Scene1*)context;
  216. with_view_model(
  217. instance->view,
  218. FlipBip39Scene1Model * model,
  219. {
  220. // Clear the mnemonic from memory
  221. model->page = 0;
  222. model->strength = 0;
  223. memzero((void*)model->seed1, strlen(model->seed1));
  224. memzero((void*)model->seed2, strlen(model->seed2));
  225. memzero((void*)model->seed3, strlen(model->seed3));
  226. memzero((void*)model->seed4, strlen(model->seed4));
  227. memzero((void*)model->seed5, strlen(model->seed5));
  228. memzero((void*)model->seed6, strlen(model->seed6));
  229. memzero((void*)model->mnemonic1, strlen(model->mnemonic1));
  230. memzero((void*)model->mnemonic2, strlen(model->mnemonic2));
  231. memzero((void*)model->mnemonic3, strlen(model->mnemonic3));
  232. memzero((void*)model->mnemonic4, strlen(model->mnemonic4));
  233. memzero((void*)model->mnemonic5, strlen(model->mnemonic5));
  234. memzero((void*)model->mnemonic6, strlen(model->mnemonic6));
  235. },
  236. true
  237. );
  238. }
  239. void flipbip39_scene_1_enter(void* context) {
  240. furi_assert(context);
  241. FlipBip39Scene1* instance = (FlipBip39Scene1*)context;
  242. FlipBip39* app = instance->context;
  243. int strength_setting = app->bip39_strength;
  244. int strength = 256;
  245. if (strength_setting == 0) strength = 128;
  246. else if (strength_setting == 1) strength = 192;
  247. flipbip39_play_happy_bump(app);
  248. flipbip39_led_set_rgb(app, 255, 0, 0);
  249. with_view_model(
  250. instance->view,
  251. FlipBip39Scene1Model * model,
  252. {
  253. flipbip39_scene_1_model_init(model, strength);
  254. },
  255. true
  256. );
  257. }
  258. FlipBip39Scene1* flipbip39_scene_1_alloc() {
  259. FlipBip39Scene1* instance = malloc(sizeof(FlipBip39Scene1));
  260. instance->view = view_alloc();
  261. view_allocate_model(instance->view, ViewModelTypeLocking, sizeof(FlipBip39Scene1Model));
  262. view_set_context(instance->view, instance); // furi_assert crashes in events without this
  263. view_set_draw_callback(instance->view, (ViewDrawCallback)flipbip39_scene_1_draw);
  264. view_set_input_callback(instance->view, flipbip39_scene_1_input);
  265. view_set_enter_callback(instance->view, flipbip39_scene_1_enter);
  266. view_set_exit_callback(instance->view, flipbip39_scene_1_exit);
  267. // FlipBip39* app = instance->context;
  268. // int strength_setting = app->bip39_strength;
  269. // int strength = 256;
  270. // if (strength_setting == 0) strength = 128;
  271. // else if (strength_setting == 1) strength = 192;
  272. // with_view_model(
  273. // instance->view,
  274. // FlipBip39Scene1Model * model,
  275. // {
  276. // flipbip39_scene_1_model_init(model, strength);
  277. // },
  278. // true
  279. // );
  280. return instance;
  281. }
  282. void flipbip39_scene_1_free(FlipBip39Scene1* instance) {
  283. furi_assert(instance);
  284. with_view_model(
  285. instance->view,
  286. FlipBip39Scene1Model * model,
  287. {
  288. //UNUSED(model);
  289. free((void*)model->seed1);
  290. free((void*)model->seed2);
  291. free((void*)model->seed3);
  292. free((void*)model->seed4);
  293. free((void*)model->seed5);
  294. free((void*)model->seed6);
  295. free((void*)model->mnemonic1);
  296. free((void*)model->mnemonic2);
  297. free((void*)model->mnemonic3);
  298. free((void*)model->mnemonic4);
  299. free((void*)model->mnemonic5);
  300. free((void*)model->mnemonic6);
  301. },
  302. true);
  303. view_free(instance->view);
  304. free(instance);
  305. }
  306. View* flipbip39_scene_1_get_view(FlipBip39Scene1* instance) {
  307. furi_assert(instance);
  308. return instance->view;
  309. }