flipbip.c 7.0 KB

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