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