flipbip_scene_1.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. #include "../flipbip.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/flipbip_haptic.h"
  8. //#include "../helpers/flipbip_speaker.h"
  9. #include "../helpers/flipbip_led.h"
  10. #include "../helpers/flipbip_string.h"
  11. #include <string.h>
  12. // #include "../helpers/printf.h"
  13. #include "../crypto/bip32.h"
  14. #include "../crypto/bip39.h"
  15. #include "../crypto/curves.h"
  16. #include "../crypto/memzero.h"
  17. struct FlipBipScene1 {
  18. View* view;
  19. FlipBipScene1Callback callback;
  20. void* context;
  21. };
  22. typedef struct {
  23. int page;
  24. int strength;
  25. // for crypto
  26. CONFIDENTIAL const char* mnemonic;
  27. CONFIDENTIAL uint8_t seed[64];
  28. CONFIDENTIAL const HDNode* node;
  29. CONFIDENTIAL const char* xprv_root;
  30. CONFIDENTIAL const char* xprv_account;
  31. CONFIDENTIAL const char* xpub_account;
  32. CONFIDENTIAL const char* xprv_extended;
  33. CONFIDENTIAL const char* xpub_extended;
  34. // for display
  35. CONFIDENTIAL const char* seed1;
  36. CONFIDENTIAL const char* seed2;
  37. CONFIDENTIAL const char* seed3;
  38. CONFIDENTIAL const char* seed4;
  39. CONFIDENTIAL const char* seed5;
  40. CONFIDENTIAL const char* seed6;
  41. CONFIDENTIAL const char* mnemonic1;
  42. CONFIDENTIAL const char* mnemonic2;
  43. CONFIDENTIAL const char* mnemonic3;
  44. CONFIDENTIAL const char* mnemonic4;
  45. CONFIDENTIAL const char* mnemonic5;
  46. CONFIDENTIAL const char* mnemonic6;
  47. } FlipBipScene1Model;
  48. void flipbip_scene_1_set_callback(
  49. FlipBipScene1* instance,
  50. FlipBipScene1Callback callback,
  51. void* context) {
  52. furi_assert(instance);
  53. furi_assert(callback);
  54. instance->callback = callback;
  55. instance->context = context;
  56. }
  57. static void flipbip_scene_1_draw_mnemonic(FlipBipScene1Model* const model) {
  58. // Delineate sections of the mnemonic every 4 words
  59. char *mnemonic_working = malloc(strlen(model->mnemonic) + 1);
  60. strcpy(mnemonic_working, model->mnemonic);
  61. int word = 0;
  62. for (size_t i = 0; i < strlen(mnemonic_working); i++) {
  63. if (mnemonic_working[i] == ' ') {
  64. word++;
  65. if (word % 4 == 0) {
  66. mnemonic_working[i] = ',';
  67. }
  68. }
  69. }
  70. // Split the mnemonic into parts
  71. char *mnemonic_part = flipbip_strtok(mnemonic_working, ",");
  72. int mi = 0;
  73. while(mnemonic_part != NULL)
  74. {
  75. char *ptr = malloc(strlen(mnemonic_part) + 1);
  76. strcpy(ptr, mnemonic_part);
  77. mi++;
  78. if (mi == 1) model->mnemonic1 = ptr;
  79. else if (mi == 2) model->mnemonic2 = ptr;
  80. else if (mi == 3) model->mnemonic3 = ptr;
  81. else if (mi == 4) model->mnemonic4 = ptr;
  82. else if (mi == 5) model->mnemonic5 = ptr;
  83. else if (mi == 6) model->mnemonic6 = ptr;
  84. mnemonic_part = flipbip_strtok(NULL, ",");
  85. }
  86. // Free the working mnemonic memory
  87. memzero(mnemonic_working, strlen(mnemonic_working));
  88. free(mnemonic_working);
  89. }
  90. void flipbip_scene_1_draw(Canvas* canvas, FlipBipScene1Model* model) {
  91. //UNUSED(model);
  92. canvas_clear(canvas);
  93. canvas_set_color(canvas, ColorBlack);
  94. //canvas_set_font(canvas, FontPrimary);
  95. //canvas_draw_str_aligned(canvas, 0, 10, AlignLeft, AlignTop, "This is Scene 1");
  96. canvas_set_font(canvas, FontSecondary);
  97. //canvas_draw_str_aligned(canvas, 1, 2, AlignLeft, AlignTop, model->strength == 128 ? "128-bit" : "256-bit");
  98. //canvas_draw_str_aligned(canvas, 1, 2, AlignLeft, AlignTop, model->seed);
  99. // Mnenomic
  100. if (model->page == 0) {
  101. canvas_draw_str_aligned(canvas, 1, 2, AlignLeft, AlignTop, model->mnemonic1);
  102. canvas_draw_str_aligned(canvas, 1, 12, AlignLeft, AlignTop, model->mnemonic2);
  103. canvas_draw_str_aligned(canvas, 1, 22, AlignLeft, AlignTop, model->mnemonic3);
  104. canvas_draw_str_aligned(canvas, 1, 32, AlignLeft, AlignTop, model->mnemonic4);
  105. canvas_draw_str_aligned(canvas, 1, 42, AlignLeft, AlignTop, model->mnemonic5);
  106. canvas_draw_str_aligned(canvas, 1, 52, AlignLeft, AlignTop, model->mnemonic6);
  107. }
  108. // Seed
  109. else {
  110. canvas_draw_str_aligned(canvas, 1, 2, AlignLeft, AlignTop, model->seed1);
  111. canvas_draw_str_aligned(canvas, 1, 12, AlignLeft, AlignTop, model->seed2);
  112. canvas_draw_str_aligned(canvas, 1, 22, AlignLeft, AlignTop, model->seed3);
  113. canvas_draw_str_aligned(canvas, 1, 32, AlignLeft, AlignTop, model->seed4);
  114. canvas_draw_str_aligned(canvas, 1, 42, AlignLeft, AlignTop, model->seed5);
  115. canvas_draw_str_aligned(canvas, 1, 52, AlignLeft, AlignTop, model->seed6);
  116. }
  117. }
  118. static void flipbip_scene_1_model_init(FlipBipScene1Model* const model, const int strength) {
  119. model->page = 0;
  120. // Generate a random mnemonic using trezor-crypto
  121. model->strength = strength;
  122. //const char *mnemonic = mnemonic_generate(model->strength);
  123. model->mnemonic = "wealth budget salt video delay obey neutral tail sure soda hold rubber joy movie boat raccoon tornado noise off inmate payment patch group topple";
  124. flipbip_scene_1_draw_mnemonic(model);
  125. // Generate a BIP39 seed from the mnemonic
  126. //uint8_t seedbytes[64];
  127. mnemonic_to_seed(model->mnemonic, "", model->seed, 0);
  128. // BEGIN DRAW SEED
  129. char *seed_working = malloc(64 * 2 + 1);
  130. // Convert the seed to a hex string
  131. for (size_t i = 0; i < 64; i++) {
  132. flipbip_itoa(model->seed[i], seed_working + (i * 2), 64 * 2 + 1, 16);
  133. //sprintf(seed + (i * 2), "%.2x", seedbytes[i]);
  134. }
  135. // Split the seed into parts
  136. for (size_t si = 1; si <= 6; si++) {
  137. char *ptr = malloc(22 + 1);
  138. strncpy(ptr, seed_working + ((si - 1) * 22), 22);
  139. if (si == 1) model->seed1 = ptr;
  140. else if (si == 2) model->seed2 = ptr;
  141. else if (si == 3) model->seed3 = ptr;
  142. else if (si == 4) model->seed4 = ptr;
  143. else if (si == 5) model->seed5 = ptr;
  144. else if (si == 6) model->seed6 = ptr;
  145. }
  146. // Free the working seed memory
  147. memzero(seed_working, sizeof(seed_working));
  148. free(seed_working);
  149. // END DRAW SEED
  150. // Generate a BIP32 root key from the mnemonic
  151. // //bool root_set = false;
  152. HDNode *root = malloc(sizeof(HDNode));
  153. hdnode_from_seed(model->seed, 64, SECP256K1_NAME, root);
  154. // //root_set = true;
  155. // m/44'/0'/0'/0
  156. uint32_t purpose = 44;
  157. uint32_t coin = 0; // Bitcoin
  158. uint32_t account = 0;
  159. uint32_t change = 0;
  160. // constants for Bitcoin
  161. const uint32_t version_public = 0x0488b21e;
  162. const uint32_t version_private = 0x0488ade4;
  163. const char addr_version = 0x00, wif_version = 0x80;
  164. // buffer for key serialization
  165. const size_t buflen = 128;
  166. char buf[128 + 1];
  167. // root
  168. uint32_t fingerprint = 0;
  169. hdnode_serialize_private(root, fingerprint, version_private, buf, buflen);
  170. char *xprv_root = malloc(buflen + 1);
  171. strncpy(xprv_root, buf, buflen);
  172. model->xprv_root = xprv_root;
  173. HDNode *node = root;
  174. // purpose m/44'
  175. fingerprint = hdnode_fingerprint(node);
  176. hdnode_private_ckd_prime(node, purpose); // purpose
  177. // coin m/44'/0'
  178. fingerprint = hdnode_fingerprint(node);
  179. hdnode_private_ckd_prime(node, coin); // coin
  180. // account m/44'/0'/0'
  181. fingerprint = hdnode_fingerprint(node);
  182. hdnode_private_ckd_prime(node, account); // account
  183. hdnode_serialize_private(node, fingerprint, version_private, buf, buflen);
  184. char *xprv_acc = malloc(buflen + 1);
  185. strncpy(xprv_acc, buf, buflen);
  186. model->xprv_account = xprv_acc;
  187. hdnode_serialize_public(node, fingerprint, version_public, buf, buflen);
  188. char *xpub_acc = malloc(buflen + 1);
  189. strncpy(xpub_acc, buf, buflen);
  190. model->xpub_account = xpub_acc;
  191. // external/internal (change) m/44'/0'/0'/0
  192. fingerprint = hdnode_fingerprint(node);
  193. hdnode_private_ckd(node, change); // external/internal (change)
  194. hdnode_serialize_private(node, fingerprint, version_private, buf, buflen);
  195. char *xprv_ext = malloc(buflen + 1);
  196. strncpy(xprv_ext, buf, buflen);
  197. model->xprv_extended = xprv_ext;
  198. hdnode_serialize_public(node, fingerprint, version_public, buf, buflen);
  199. char *xpub_ext = malloc(buflen + 1);
  200. strncpy(xpub_ext, buf, buflen);
  201. model->xpub_extended = xpub_ext;
  202. model->node = node;
  203. // BEGIN DRAW ADDRESS
  204. for (int i = 0; i < 3; i++) {
  205. HDNode *addr_node = malloc(sizeof(HDNode));
  206. memcpy(addr_node, node, sizeof(HDNode));
  207. hdnode_private_ckd(addr_node, i);
  208. hdnode_fill_public_key(addr_node);
  209. ecdsa_get_address(addr_node->public_key, addr_version, HASHER_SHA2_RIPEMD, HASHER_SHA2D, buf, buflen);
  210. char *address = malloc(buflen + 1);
  211. strncpy(address, buf, buflen);
  212. model->seed5 = address;
  213. ecdsa_get_wif(addr_node->private_key, wif_version, HASHER_SHA2D, buf, buflen);
  214. char *wif = malloc(buflen + 1);
  215. strncpy(wif, buf, buflen);
  216. model->seed6 = wif;
  217. memzero(addr_node, sizeof(HDNode));
  218. free(addr_node);
  219. }
  220. // END DRAW ADDRESS
  221. // Clear the root node
  222. // memzero(root, sizeof(HDNode));
  223. // free(root);
  224. // Clear the mnemonic
  225. //mnemonic_clear();
  226. #if USE_BIP39_CACHE
  227. // Clear the BIP39 cache
  228. bip39_cache_clear();
  229. #endif
  230. }
  231. bool flipbip_scene_1_input(InputEvent* event, void* context) {
  232. furi_assert(context);
  233. FlipBipScene1* instance = context;
  234. if (event->type == InputTypeRelease) {
  235. switch(event->key) {
  236. case InputKeyBack:
  237. with_view_model(
  238. instance->view,
  239. FlipBipScene1Model * model,
  240. {
  241. UNUSED(model);
  242. instance->callback(FlipBipCustomEventScene1Back, instance->context);
  243. },
  244. true);
  245. break;
  246. case InputKeyLeft:
  247. case InputKeyRight:
  248. case InputKeyUp:
  249. case InputKeyDown:
  250. case InputKeyOk:
  251. with_view_model(
  252. instance->view,
  253. FlipBipScene1Model* model,
  254. {
  255. //UNUSED(model);
  256. model->page = (model->page + 1) % 2;
  257. },
  258. true);
  259. break;
  260. case InputKeyMAX:
  261. break;
  262. }
  263. }
  264. return true;
  265. }
  266. void flipbip_scene_1_exit(void* context) {
  267. furi_assert(context);
  268. FlipBipScene1* instance = (FlipBipScene1*)context;
  269. with_view_model(
  270. instance->view,
  271. FlipBipScene1Model * model,
  272. {
  273. // Clear the mnemonic from memory
  274. model->page = 0;
  275. model->strength = 0;
  276. memzero((void*)model->seed1, strlen(model->seed1));
  277. memzero((void*)model->seed2, strlen(model->seed2));
  278. memzero((void*)model->seed3, strlen(model->seed3));
  279. memzero((void*)model->seed4, strlen(model->seed4));
  280. memzero((void*)model->seed5, strlen(model->seed5));
  281. memzero((void*)model->seed6, strlen(model->seed6));
  282. memzero((void*)model->mnemonic1, strlen(model->mnemonic1));
  283. memzero((void*)model->mnemonic2, strlen(model->mnemonic2));
  284. memzero((void*)model->mnemonic3, strlen(model->mnemonic3));
  285. memzero((void*)model->mnemonic4, strlen(model->mnemonic4));
  286. memzero((void*)model->mnemonic5, strlen(model->mnemonic5));
  287. memzero((void*)model->mnemonic6, strlen(model->mnemonic6));
  288. },
  289. true
  290. );
  291. }
  292. void flipbip_scene_1_enter(void* context) {
  293. furi_assert(context);
  294. FlipBipScene1* instance = (FlipBipScene1*)context;
  295. FlipBip* app = instance->context;
  296. int strength_setting = app->bip39_strength;
  297. int strength = 256;
  298. if (strength_setting == 0) strength = 128;
  299. else if (strength_setting == 1) strength = 192;
  300. flipbip_play_happy_bump(app);
  301. flipbip_led_set_rgb(app, 255, 0, 0);
  302. with_view_model(
  303. instance->view,
  304. FlipBipScene1Model * model,
  305. {
  306. flipbip_scene_1_model_init(model, strength);
  307. },
  308. true
  309. );
  310. }
  311. FlipBipScene1* flipbip_scene_1_alloc() {
  312. FlipBipScene1* instance = malloc(sizeof(FlipBipScene1));
  313. instance->view = view_alloc();
  314. view_allocate_model(instance->view, ViewModelTypeLocking, sizeof(FlipBipScene1Model));
  315. view_set_context(instance->view, instance); // furi_assert crashes in events without this
  316. view_set_draw_callback(instance->view, (ViewDrawCallback)flipbip_scene_1_draw);
  317. view_set_input_callback(instance->view, flipbip_scene_1_input);
  318. view_set_enter_callback(instance->view, flipbip_scene_1_enter);
  319. view_set_exit_callback(instance->view, flipbip_scene_1_exit);
  320. // FlipBip* app = instance->context;
  321. // int strength_setting = app->bip39_strength;
  322. // int strength = 256;
  323. // if (strength_setting == 0) strength = 128;
  324. // else if (strength_setting == 1) strength = 192;
  325. // with_view_model(
  326. // instance->view,
  327. // FlipBipScene1Model * model,
  328. // {
  329. // flipbip_scene_1_model_init(model, strength);
  330. // },
  331. // true
  332. // );
  333. return instance;
  334. }
  335. void flipbip_scene_1_free(FlipBipScene1* instance) {
  336. furi_assert(instance);
  337. with_view_model(
  338. instance->view,
  339. FlipBipScene1Model * model,
  340. {
  341. //UNUSED(model);
  342. free((void*)model->seed1);
  343. free((void*)model->seed2);
  344. free((void*)model->seed3);
  345. free((void*)model->seed4);
  346. free((void*)model->seed5);
  347. free((void*)model->seed6);
  348. free((void*)model->mnemonic1);
  349. free((void*)model->mnemonic2);
  350. free((void*)model->mnemonic3);
  351. free((void*)model->mnemonic4);
  352. free((void*)model->mnemonic5);
  353. free((void*)model->mnemonic6);
  354. },
  355. true);
  356. view_free(instance->view);
  357. free(instance);
  358. }
  359. View* flipbip_scene_1_get_view(FlipBipScene1* instance) {
  360. furi_assert(instance);
  361. return instance->view;
  362. }