flipbip39_scene_1.c 10 KB

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