flipbip.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #include "flipbip.h"
  2. #include "helpers/flipbip_file.h"
  3. // From: lib/crypto
  4. #include <memzero.h>
  5. #include <bip39.h>
  6. #define MNEMONIC_MENU_DEFAULT "Import mnemonic seed"
  7. #define MNEMONIC_MENU_SUCCESS "Import seed (success)"
  8. #define MNEMONIC_MENU_FAILURE "Import seed (failed!)"
  9. bool flipbip_custom_event_callback(void* context, uint32_t event) {
  10. furi_assert(context);
  11. FlipBip* app = context;
  12. return scene_manager_handle_custom_event(app->scene_manager, event);
  13. }
  14. void flipbip_tick_event_callback(void* context) {
  15. furi_assert(context);
  16. FlipBip* app = context;
  17. scene_manager_handle_tick_event(app->scene_manager);
  18. }
  19. //leave app if back button pressed
  20. bool flipbip_navigation_event_callback(void* context) {
  21. furi_assert(context);
  22. FlipBip* app = context;
  23. return scene_manager_handle_back_event(app->scene_manager);
  24. }
  25. static void text_input_callback(void* context) {
  26. furi_assert(context);
  27. FlipBip* app = context;
  28. bool handled = false;
  29. // check that there is text in the input
  30. if(strlen(app->input_text) > 0) {
  31. if(app->input_state == FlipBipTextInputPassphrase) {
  32. if(app->passphrase == FlipBipPassphraseOn) {
  33. strcpy(app->passphrase_text, app->input_text);
  34. }
  35. // clear input text
  36. memzero(app->input_text, TEXT_BUFFER_SIZE);
  37. // reset input state
  38. app->input_state = FlipBipTextInputDefault;
  39. handled = true;
  40. // switch back to settings view
  41. view_dispatcher_switch_to_view(app->view_dispatcher, FlipBipViewIdSettings);
  42. } else if(app->input_state == FlipBipTextInputMnemonic) {
  43. if(app->import_from_mnemonic == 1) {
  44. strcpy(app->import_mnemonic_text, app->input_text);
  45. int status = FlipBipStatusSuccess;
  46. // Check if the mnemonic is valid
  47. if(mnemonic_check(app->import_mnemonic_text) == 0)
  48. status = FlipBipStatusMnemonicCheckError; // 13 = mnemonic check error
  49. // Save the mnemonic to persistent storage
  50. else if(!flipbip_save_file_secure(app->import_mnemonic_text))
  51. status = FlipBipStatusSaveError; // 12 = save error
  52. if(status == FlipBipStatusSuccess) {
  53. app->mnemonic_menu_text = MNEMONIC_MENU_SUCCESS;
  54. //notification_message(app->notification, &sequence_blink_cyan_100);
  55. //flipbip_play_happy_bump(app);
  56. } else {
  57. app->mnemonic_menu_text = MNEMONIC_MENU_FAILURE;
  58. //notification_message(app->notification, &sequence_blink_red_100);
  59. //flipbip_play_long_bump(app);
  60. }
  61. memzero(app->import_mnemonic_text, TEXT_BUFFER_SIZE);
  62. }
  63. // clear input text
  64. memzero(app->input_text, TEXT_BUFFER_SIZE);
  65. // reset input state
  66. app->input_state = FlipBipTextInputDefault;
  67. handled = true;
  68. // exit scene 1 instance that's being used for text input and go back to menu
  69. scene_manager_previous_scene(app->scene_manager);
  70. //view_dispatcher_switch_to_view(app->view_dispatcher, FlipBipViewIdMenu);
  71. }
  72. }
  73. if(!handled) {
  74. // clear input text
  75. memzero(app->input_text, TEXT_BUFFER_SIZE);
  76. // reset input state
  77. app->input_state = FlipBipTextInputDefault;
  78. // something went wrong, switch to menu view
  79. view_dispatcher_switch_to_view(app->view_dispatcher, FlipBipViewIdMenu);
  80. }
  81. }
  82. FlipBip* flipbip_app_alloc() {
  83. FlipBip* app = malloc(sizeof(FlipBip));
  84. app->gui = furi_record_open(RECORD_GUI);
  85. //app->notification = furi_record_open(RECORD_NOTIFICATION);
  86. // Turn backlight on, believe me this makes testing your app easier
  87. //notification_message(app->notification, &sequence_display_backlight_on);
  88. // Scene additions
  89. app->view_dispatcher = view_dispatcher_alloc();
  90. view_dispatcher_enable_queue(app->view_dispatcher);
  91. app->scene_manager = scene_manager_alloc(&flipbip_scene_handlers, app);
  92. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  93. view_dispatcher_set_navigation_event_callback(
  94. app->view_dispatcher, flipbip_navigation_event_callback);
  95. view_dispatcher_set_tick_event_callback(
  96. app->view_dispatcher, flipbip_tick_event_callback, 100);
  97. view_dispatcher_set_custom_event_callback(app->view_dispatcher, flipbip_custom_event_callback);
  98. app->submenu = submenu_alloc();
  99. // Settings
  100. app->bip39_strength = FlipBipStrength256; // 256 bits (24 words)
  101. app->passphrase = FlipBipPassphraseOff;
  102. // Main menu
  103. app->bip44_coin = FlipBipCoinBTC0; // 0 (BTC)
  104. app->overwrite_saved_seed = 0;
  105. app->import_from_mnemonic = 0;
  106. app->mnemonic_menu_text = MNEMONIC_MENU_DEFAULT;
  107. // Text input
  108. app->input_state = FlipBipTextInputDefault;
  109. view_dispatcher_add_view(
  110. app->view_dispatcher, FlipBipViewIdMenu, submenu_get_view(app->submenu));
  111. app->flipbip_scene_1 = flipbip_scene_1_alloc();
  112. view_dispatcher_add_view(
  113. app->view_dispatcher, FlipBipViewIdScene1, flipbip_scene_1_get_view(app->flipbip_scene_1));
  114. app->variable_item_list = variable_item_list_alloc();
  115. view_dispatcher_add_view(
  116. app->view_dispatcher,
  117. FlipBipViewIdSettings,
  118. variable_item_list_get_view(app->variable_item_list));
  119. app->text_input = text_input_alloc();
  120. text_input_set_result_callback(
  121. app->text_input,
  122. text_input_callback,
  123. (void*)app,
  124. app->input_text,
  125. TEXT_BUFFER_SIZE,
  126. // clear default text
  127. true);
  128. //text_input_set_header_text(app->text_input, "Input");
  129. view_dispatcher_add_view(
  130. app->view_dispatcher, FlipBipViewIdTextInput, text_input_get_view(app->text_input));
  131. // End Scene Additions
  132. return app;
  133. }
  134. void flipbip_app_free(FlipBip* app) {
  135. furi_assert(app);
  136. // Scene manager
  137. scene_manager_free(app->scene_manager);
  138. text_input_free(app->text_input);
  139. // View Dispatcher
  140. view_dispatcher_remove_view(app->view_dispatcher, FlipBipViewIdMenu);
  141. view_dispatcher_remove_view(app->view_dispatcher, FlipBipViewIdScene1);
  142. view_dispatcher_remove_view(app->view_dispatcher, FlipBipViewIdSettings);
  143. view_dispatcher_remove_view(app->view_dispatcher, FlipBipViewIdTextInput);
  144. submenu_free(app->submenu);
  145. view_dispatcher_free(app->view_dispatcher);
  146. furi_record_close(RECORD_GUI);
  147. app->gui = NULL;
  148. //app->notification = NULL;
  149. //Remove whatever is left
  150. memzero(app, sizeof(FlipBip));
  151. free(app);
  152. }
  153. int32_t flipbip_app(void* p) {
  154. UNUSED(p);
  155. FlipBip* app = flipbip_app_alloc();
  156. // Disabled because causes exit on custom firmwares such as RM
  157. /*if(!furi_hal_region_is_provisioned()) {
  158. flipbip_app_free(app);
  159. return 1;
  160. }*/
  161. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  162. scene_manager_next_scene(app->scene_manager, FlipBipSceneMenu); //Start with menu
  163. furi_hal_power_suppress_charge_enter();
  164. view_dispatcher_run(app->view_dispatcher);
  165. furi_hal_power_suppress_charge_exit();
  166. flipbip_app_free(app);
  167. return 0;
  168. }