flipbip.c 7.0 KB

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