flipbip39_scene_1.c 9.5 KB

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