flipbip39_scene_1.c 12 KB

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