flipbip39_scene_1.c 9.4 KB

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