flipbip39_scene_1.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 strength;
  25. const char* seed;
  26. const char* mnemonic1;
  27. const char* mnemonic2;
  28. const char* mnemonic3;
  29. const char* mnemonic4;
  30. const char* mnemonic5;
  31. const char* mnemonic6;
  32. } FlipBip39Scene1Model;
  33. void flipbip39_scene_1_set_callback(
  34. FlipBip39Scene1* instance,
  35. FlipBip39Scene1Callback callback,
  36. void* context) {
  37. furi_assert(instance);
  38. furi_assert(callback);
  39. instance->callback = callback;
  40. instance->context = context;
  41. }
  42. void flipbip39_scene_1_draw(Canvas* canvas, FlipBip39Scene1Model* model) {
  43. //UNUSED(model);
  44. canvas_clear(canvas);
  45. canvas_set_color(canvas, ColorBlack);
  46. //canvas_set_font(canvas, FontPrimary);
  47. //canvas_draw_str_aligned(canvas, 0, 10, AlignLeft, AlignTop, "This is Scene 1");
  48. canvas_set_font(canvas, FontSecondary);
  49. //canvas_draw_str_aligned(canvas, 1, 2, AlignLeft, AlignTop, model->strength == 128 ? "128-bit" : "256-bit");
  50. //canvas_draw_str_aligned(canvas, 1, 2, AlignLeft, AlignTop, model->seed);
  51. canvas_draw_str_aligned(canvas, 1, 2, AlignLeft, AlignTop, model->mnemonic1);
  52. canvas_draw_str_aligned(canvas, 1, 12, AlignLeft, AlignTop, model->mnemonic2);
  53. canvas_draw_str_aligned(canvas, 1, 22, AlignLeft, AlignTop, model->mnemonic3);
  54. canvas_draw_str_aligned(canvas, 1, 32, AlignLeft, AlignTop, model->mnemonic4);
  55. canvas_draw_str_aligned(canvas, 1, 42, AlignLeft, AlignTop, model->mnemonic5);
  56. canvas_draw_str_aligned(canvas, 1, 52, AlignLeft, AlignTop, model->mnemonic6);
  57. }
  58. static void flipbip39_scene_1_model_init(FlipBip39Scene1Model* const model, const int strength) {
  59. // Generate a random mnemonic using trezor-crypto
  60. model->strength = strength;
  61. const char* mnemonic = mnemonic_generate(model->strength);
  62. // Delineate sections of the mnemonic every 4 words
  63. char *mnemo = malloc(strlen(mnemonic) + 1);
  64. strcpy(mnemo, mnemonic);
  65. int word = 0;
  66. for (size_t i = 0; i < strlen(mnemo); i++) {
  67. if (mnemo[i] == ' ') {
  68. word++;
  69. if (word % 4 == 0) {
  70. mnemo[i] = ',';
  71. }
  72. }
  73. }
  74. // Split the mnemonic into parts
  75. char *mnemopart = flipbip39_strtok(mnemo, ",");
  76. int partnum = 0;
  77. while(mnemopart != NULL)
  78. {
  79. char *partptr = malloc(strlen(mnemopart) + 1);
  80. strcpy(partptr, mnemopart);
  81. partnum++;
  82. if (partnum == 1) model->mnemonic1 = partptr;
  83. if (partnum == 2) model->mnemonic2 = partptr;
  84. if (partnum == 3) model->mnemonic3 = partptr;
  85. if (partnum == 4) model->mnemonic4 = partptr;
  86. if (partnum == 5) model->mnemonic5 = partptr;
  87. if (partnum == 6) model->mnemonic6 = partptr;
  88. mnemopart = flipbip39_strtok(NULL, ",");
  89. }
  90. // WIP / TODO: Generate a seed from the mnemonic
  91. uint8_t seed[64];
  92. mnemonic_to_seed(mnemonic, "", seed, 0);
  93. char *seedptr = malloc(64*2 + 1);
  94. for (size_t i = 0; i < 64; i++) {
  95. sprintf(seedptr + (i * 2), "%.2x", seed[i]);
  96. }
  97. //strcpy(seedptr, (char*)seed);
  98. model->seed = seedptr;
  99. // for (size_t i = 0; i < 6; i++) {
  100. // char *seedpartptr = malloc(22 + 1);
  101. // strncpy(seedpartptr, seedptr + (i * 22), 22);
  102. // //model->seed = seedpartptr;
  103. // if (i == 0) model->mnemonic1 = seedpartptr;
  104. // if (i == 1) model->mnemonic2 = seedpartptr;
  105. // if (i == 2) model->mnemonic3 = seedpartptr;
  106. // if (i == 3) model->mnemonic4 = seedpartptr;
  107. // if (i == 4) model->mnemonic5 = seedpartptr;
  108. // if (i == 5) model->mnemonic6 = seedpartptr;
  109. // }
  110. // WIP / TODO: Generate a BIP32 root key from the mnemonic
  111. // //bool root_set = false;
  112. // HDNode root;
  113. // hdnode_from_seed(seed, 64, SECP256K1_NAME, &root);
  114. // //root_set = true;
  115. // int arg1 = 1;
  116. // // constants for Bitcoin
  117. // const uint32_t version_public = 0x0488b21e;
  118. // const uint32_t version_private = 0x0488ade4;
  119. // const char addr_version = 0x00, wif_version = 0x80;
  120. // const size_t buflen = 128;
  121. // char buf[buflen + 1];
  122. // HDNode node;
  123. // uint32_t fingerprint;
  124. // // external chain
  125. // for (int chain = 0; chain < 2; chain++) {
  126. // //QTableWidget *list = chain == 0 ? ui->listAddress : ui->listChange;
  127. // node = root;
  128. // hdnode_private_ckd(&node, 44 | 0x80000000);
  129. // hdnode_private_ckd(&node, 0 | 0x80000000); // bitcoin
  130. // hdnode_private_ckd(&node, (arg1 - 1) | 0x80000000);
  131. // fingerprint = hdnode_fingerprint(&node);
  132. // hdnode_serialize_private(&node, fingerprint, version_private, buf, buflen);
  133. // //QString xprv = QString(buf); ui->lineXprv->setText(xprv);
  134. // hdnode_serialize_public(&node, fingerprint, version_public, buf, buflen);
  135. // //QString xpub = QString(buf); ui->lineXpub->setText(xpub);
  136. // hdnode_private_ckd(&node, chain); // external / internal
  137. // for (int i = 0; i < 10; i++) {
  138. // HDNode node2 = node;
  139. // hdnode_private_ckd(&node2, i);
  140. // hdnode_fill_public_key(&node2);
  141. // ecdsa_get_address(node2.public_key, addr_version, HASHER_SHA2_RIPEMD, HASHER_SHA2D, buf, buflen);
  142. // //QString address = QString(buf);
  143. // ecdsa_get_wif(node2.private_key, wif_version, HASHER_SHA2D, buf, buflen);
  144. // //QString wif = QString(buf);
  145. // // list->setItem(i, 0, new QTableWidgetItem(address));
  146. // // list->setItem(i, 1, new QTableWidgetItem(wif));
  147. // // list->setItem(i, 2, new QTableWidgetItem("0.0"));
  148. // }
  149. // }
  150. // Clear the mnemonic
  151. memzero(mnemo, strlen(mnemo));
  152. free(mnemo);
  153. mnemonic_clear();
  154. bip39_cache_clear();
  155. }
  156. bool flipbip39_scene_1_input(InputEvent* event, void* context) {
  157. furi_assert(context);
  158. FlipBip39Scene1* instance = context;
  159. if (event->type == InputTypeRelease) {
  160. switch(event->key) {
  161. case InputKeyBack:
  162. with_view_model(
  163. instance->view,
  164. FlipBip39Scene1Model * model,
  165. {
  166. UNUSED(model);
  167. instance->callback(FlipBip39CustomEventScene1Back, instance->context);
  168. },
  169. true);
  170. break;
  171. case InputKeyLeft:
  172. case InputKeyRight:
  173. case InputKeyUp:
  174. case InputKeyDown:
  175. case InputKeyOk:
  176. with_view_model(
  177. instance->view,
  178. FlipBip39Scene1Model* model,
  179. {
  180. UNUSED(model);
  181. },
  182. true);
  183. break;
  184. case InputKeyMAX:
  185. break;
  186. }
  187. }
  188. return true;
  189. }
  190. void flipbip39_scene_1_exit(void* context) {
  191. furi_assert(context);
  192. FlipBip39Scene1* instance = (FlipBip39Scene1*)context;
  193. with_view_model(
  194. instance->view,
  195. FlipBip39Scene1Model * model,
  196. {
  197. // Clear the mnemonic from memory
  198. model->strength = 0;
  199. memzero((void*)model->seed, strlen(model->seed));
  200. memzero((void*)model->mnemonic1, strlen(model->mnemonic1));
  201. memzero((void*)model->mnemonic2, strlen(model->mnemonic2));
  202. memzero((void*)model->mnemonic3, strlen(model->mnemonic3));
  203. memzero((void*)model->mnemonic4, strlen(model->mnemonic4));
  204. memzero((void*)model->mnemonic5, strlen(model->mnemonic5));
  205. memzero((void*)model->mnemonic6, strlen(model->mnemonic6));
  206. },
  207. true
  208. );
  209. }
  210. void flipbip39_scene_1_enter(void* context) {
  211. furi_assert(context);
  212. FlipBip39Scene1* instance = (FlipBip39Scene1*)context;
  213. FlipBip39* app = instance->context;
  214. int strength_setting = app->bip39_strength;
  215. int strength = 256;
  216. if (strength_setting == 0) strength = 128;
  217. else if (strength_setting == 1) strength = 192;
  218. flipbip39_play_happy_bump(app);
  219. flipbip39_led_set_rgb(app, 255, 0, 0);
  220. with_view_model(
  221. instance->view,
  222. FlipBip39Scene1Model * model,
  223. {
  224. flipbip39_scene_1_model_init(model, strength);
  225. },
  226. true
  227. );
  228. }
  229. FlipBip39Scene1* flipbip39_scene_1_alloc() {
  230. FlipBip39Scene1* instance = malloc(sizeof(FlipBip39Scene1));
  231. instance->view = view_alloc();
  232. view_allocate_model(instance->view, ViewModelTypeLocking, sizeof(FlipBip39Scene1Model));
  233. view_set_context(instance->view, instance); // furi_assert crashes in events without this
  234. view_set_draw_callback(instance->view, (ViewDrawCallback)flipbip39_scene_1_draw);
  235. view_set_input_callback(instance->view, flipbip39_scene_1_input);
  236. view_set_enter_callback(instance->view, flipbip39_scene_1_enter);
  237. view_set_exit_callback(instance->view, flipbip39_scene_1_exit);
  238. // FlipBip39* app = instance->context;
  239. // int strength_setting = app->bip39_strength;
  240. // int strength = 256;
  241. // if (strength_setting == 0) strength = 128;
  242. // else if (strength_setting == 1) strength = 192;
  243. // with_view_model(
  244. // instance->view,
  245. // FlipBip39Scene1Model * model,
  246. // {
  247. // flipbip39_scene_1_model_init(model, strength);
  248. // },
  249. // true
  250. // );
  251. return instance;
  252. }
  253. void flipbip39_scene_1_free(FlipBip39Scene1* instance) {
  254. furi_assert(instance);
  255. with_view_model(
  256. instance->view,
  257. FlipBip39Scene1Model * model,
  258. {
  259. //UNUSED(model);
  260. free((void*)model->seed);
  261. free((void*)model->mnemonic1);
  262. free((void*)model->mnemonic2);
  263. free((void*)model->mnemonic3);
  264. free((void*)model->mnemonic4);
  265. free((void*)model->mnemonic5);
  266. free((void*)model->mnemonic6);
  267. },
  268. true);
  269. view_free(instance->view);
  270. free(instance);
  271. }
  272. View* flipbip39_scene_1_get_view(FlipBip39Scene1* instance) {
  273. furi_assert(instance);
  274. return instance->view;
  275. }