flipbip.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include "flipbip.h"
  2. #include "helpers/flipbip_file.h"
  3. //#include "helpers/flipbip_haptic.h"
  4. // From: lib/crypto
  5. #include <memzero.h>
  6. #include <bip39.h>
  7. #define MNEMONIC_MENU_DEFAULT "Import mnemonic seed"
  8. #define MNEMONIC_MENU_SUCCESS "Import seed (success)"
  9. #define MNEMONIC_MENU_FAILURE "Import seed (failure)"
  10. bool flipbip_custom_event_callback(void* context, uint32_t event) {
  11. furi_assert(context);
  12. FlipBip* app = context;
  13. return scene_manager_handle_custom_event(app->scene_manager, event);
  14. }
  15. void flipbip_tick_event_callback(void* context) {
  16. furi_assert(context);
  17. FlipBip* app = context;
  18. scene_manager_handle_tick_event(app->scene_manager);
  19. }
  20. //leave app if back button pressed
  21. bool flipbip_navigation_event_callback(void* context) {
  22. furi_assert(context);
  23. FlipBip* app = context;
  24. return scene_manager_handle_back_event(app->scene_manager);
  25. }
  26. static void text_input_callback(void* context) {
  27. furi_assert(context);
  28. FlipBip* app = context;
  29. bool handled = false;
  30. // check that there is text in the input
  31. if(strlen(app->input_text) > 0) {
  32. if(app->input_state == FlipBipTextInputPassphrase) {
  33. if(app->passphrase == FlipBipPassphraseOn) {
  34. strcpy(app->passphrase_text, app->input_text);
  35. }
  36. // clear input text
  37. memzero(app->input_text, TEXT_BUFFER_SIZE);
  38. // reset input state
  39. app->input_state = FlipBipTextInputDefault;
  40. handled = true;
  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. //view_dispatcher_switch_to_view(app->view_dispatcher, FlipBipViewIdMenu);
  79. }
  80. }
  81. FlipBip* flipbip_app_alloc() {
  82. FlipBip* app = malloc(sizeof(FlipBip));
  83. app->gui = furi_record_open(RECORD_GUI);
  84. //app->notification = furi_record_open(RECORD_NOTIFICATION);
  85. //Turn backlight on, believe me this makes testing your app easier
  86. //notification_message(app->notification, &sequence_display_backlight_on);
  87. //Scene additions
  88. app->view_dispatcher = view_dispatcher_alloc();
  89. view_dispatcher_enable_queue(app->view_dispatcher);
  90. app->scene_manager = scene_manager_alloc(&flipbip_scene_handlers, app);
  91. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  92. view_dispatcher_set_navigation_event_callback(
  93. app->view_dispatcher, flipbip_navigation_event_callback);
  94. view_dispatcher_set_tick_event_callback(
  95. app->view_dispatcher, flipbip_tick_event_callback, 100);
  96. view_dispatcher_set_custom_event_callback(app->view_dispatcher, flipbip_custom_event_callback);
  97. app->submenu = submenu_alloc();
  98. // Settings
  99. app->haptic = FlipBipHapticOn;
  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 start screen
  163. //scene_manager_next_scene(app->scene_manager, FlipBipSceneMenu); //if you want to directly start with Menu
  164. furi_hal_power_suppress_charge_enter();
  165. view_dispatcher_run(app->view_dispatcher);
  166. furi_hal_power_suppress_charge_exit();
  167. flipbip_app_free(app);
  168. return 0;
  169. }