flipbip39_scene_1.c 13 KB

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