flipbip.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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) status = FlipBipStatusMnemonicCheckError; // 13 = mnemonic check error
  44. // Save the mnemonic to persistent storage
  45. else if(!flipbip_save_settings_secure(app->import_mnemonic_text)) status = FlipBipStatusSaveError; // 12 = save error
  46. if (status == FlipBipStatusSuccess) {
  47. flipbip_play_happy_bump(app);
  48. } else {
  49. flipbip_play_long_bump(app);
  50. }
  51. memzero(app->import_mnemonic_text, TEXT_BUFFER_SIZE);
  52. }
  53. // clear input text
  54. memzero(app->input_text, TEXT_BUFFER_SIZE);
  55. // reset input state
  56. app->input_state = FlipBipTextInputDefault;
  57. handled = true;
  58. view_dispatcher_switch_to_view(app->view_dispatcher, FlipBipViewIdMenu);
  59. }
  60. }
  61. if(!handled) {
  62. // clear input text
  63. memzero(app->input_text, TEXT_BUFFER_SIZE);
  64. // reset input state
  65. app->input_state = FlipBipTextInputDefault;
  66. view_dispatcher_switch_to_view(app->view_dispatcher, FlipBipViewIdMenu);
  67. }
  68. }
  69. FlipBip* flipbip_app_alloc() {
  70. FlipBip* app = malloc(sizeof(FlipBip));
  71. app->gui = furi_record_open(RECORD_GUI);
  72. app->notification = furi_record_open(RECORD_NOTIFICATION);
  73. //Turn backlight on, believe me this makes testing your app easier
  74. notification_message(app->notification, &sequence_display_backlight_on);
  75. //Scene additions
  76. app->view_dispatcher = view_dispatcher_alloc();
  77. view_dispatcher_enable_queue(app->view_dispatcher);
  78. app->scene_manager = scene_manager_alloc(&flipbip_scene_handlers, app);
  79. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  80. view_dispatcher_set_navigation_event_callback(
  81. app->view_dispatcher, flipbip_navigation_event_callback);
  82. view_dispatcher_set_tick_event_callback(
  83. app->view_dispatcher, flipbip_tick_event_callback, 100);
  84. view_dispatcher_set_custom_event_callback(app->view_dispatcher, flipbip_custom_event_callback);
  85. app->submenu = submenu_alloc();
  86. // Settings
  87. app->haptic = FlipBipHapticOn;
  88. app->led = FlipBipLedOn;
  89. app->bip39_strength = FlipBipStrength256; // 256 bits (24 words)
  90. app->passphrase = FlipBipPassphraseOff;
  91. // Main menu
  92. app->bip44_coin = FlipBipCoinBTC0; // 0 (BTC)
  93. app->overwrite_saved_seed = 0;
  94. app->import_from_mnemonic = 0;
  95. // Text input
  96. app->input_state = FlipBipTextInputDefault;
  97. view_dispatcher_add_view(
  98. app->view_dispatcher, FlipBipViewIdMenu, submenu_get_view(app->submenu));
  99. app->flipbip_startscreen = flipbip_startscreen_alloc();
  100. view_dispatcher_add_view(
  101. app->view_dispatcher,
  102. FlipBipViewIdStartscreen,
  103. flipbip_startscreen_get_view(app->flipbip_startscreen));
  104. app->flipbip_scene_1 = flipbip_scene_1_alloc();
  105. view_dispatcher_add_view(
  106. app->view_dispatcher, FlipBipViewIdScene1, flipbip_scene_1_get_view(app->flipbip_scene_1));
  107. app->variable_item_list = variable_item_list_alloc();
  108. view_dispatcher_add_view(
  109. app->view_dispatcher,
  110. FlipBipViewIdSettings,
  111. variable_item_list_get_view(app->variable_item_list));
  112. app->text_input = text_input_alloc();
  113. text_input_set_result_callback(
  114. app->text_input,
  115. text_input_callback,
  116. (void*)app,
  117. app->input_text,
  118. TEXT_BUFFER_SIZE,
  119. //clear default text
  120. true);
  121. text_input_set_header_text(app->text_input, "Input");
  122. view_dispatcher_add_view(
  123. app->view_dispatcher, FlipBipViewIdTextInput, text_input_get_view(app->text_input));
  124. //End Scene Additions
  125. return app;
  126. }
  127. void flipbip_app_free(FlipBip* app) {
  128. furi_assert(app);
  129. // Scene manager
  130. scene_manager_free(app->scene_manager);
  131. text_input_free(app->text_input);
  132. // View Dispatcher
  133. view_dispatcher_remove_view(app->view_dispatcher, FlipBipViewIdMenu);
  134. view_dispatcher_remove_view(app->view_dispatcher, FlipBipViewIdScene1);
  135. view_dispatcher_remove_view(app->view_dispatcher, FlipBipViewIdSettings);
  136. view_dispatcher_remove_view(app->view_dispatcher, FlipBipViewIdTextInput);
  137. submenu_free(app->submenu);
  138. view_dispatcher_free(app->view_dispatcher);
  139. furi_record_close(RECORD_GUI);
  140. app->gui = NULL;
  141. app->notification = NULL;
  142. //Remove whatever is left
  143. memzero(app, sizeof(FlipBip));
  144. free(app);
  145. }
  146. int32_t flipbip_app(void* p) {
  147. UNUSED(p);
  148. FlipBip* app = flipbip_app_alloc();
  149. // Disabled because causes exit on customer firmwares such as RM
  150. /*if(!furi_hal_region_is_provisioned()) {
  151. flipbip_app_free(app);
  152. return 1;
  153. }*/
  154. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  155. scene_manager_next_scene(
  156. app->scene_manager, FlipBipSceneStartscreen); //Start with start screen
  157. //scene_manager_next_scene(app->scene_manager, FlipBipSceneMenu); //if you want to directly start with Menu
  158. furi_hal_power_suppress_charge_enter();
  159. view_dispatcher_run(app->view_dispatcher);
  160. furi_hal_power_suppress_charge_exit();
  161. flipbip_app_free(app);
  162. return 0;
  163. }