flipbip.c 7.1 KB

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