-
Notifications
You must be signed in to change notification settings - Fork 12
/
flipbip.c
238 lines (198 loc) · 8.44 KB
/
flipbip.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include "flipbip.h"
#include "helpers/flipbip_file.h"
// From: lib/crypto
#include <memzero.h>
#include <bip39.h>
#define MNEMONIC_MENU_DEFAULT "Import mnemonic seed"
#define MNEMONIC_MENU_SUCCESS "Import seed (success)"
#define MNEMONIC_MENU_FAILURE "Import seed (failed!)"
bool flipbip_custom_event_callback(void* context, uint32_t event) {
furi_assert(context);
FlipBip* app = context;
return scene_manager_handle_custom_event(app->scene_manager, event);
}
void flipbip_tick_event_callback(void* context) {
furi_assert(context);
FlipBip* app = context;
scene_manager_handle_tick_event(app->scene_manager);
}
//leave app if back button pressed
bool flipbip_navigation_event_callback(void* context) {
furi_assert(context);
FlipBip* app = context;
return scene_manager_handle_back_event(app->scene_manager);
}
static void text_input_callback(void* context) {
furi_assert(context);
FlipBip* app = context;
bool handled = false;
// check that there is text in the input
if(strlen(app->input_text) > 0) {
if(app->input_state == FlipBipTextInputPassphrase) {
if(app->passphrase == FlipBipPassphraseOn) {
strcpy(app->passphrase_text, app->input_text);
}
// clear input text
memzero(app->input_text, TEXT_BUFFER_SIZE);
// reset input state
app->input_state = FlipBipTextInputDefault;
handled = true;
// switch back to settings view
view_dispatcher_switch_to_view(app->view_dispatcher, FlipBipViewIdSettings);
} else if(app->input_state == FlipBipTextInputMnemonic) {
if(app->import_from_mnemonic == 1) {
strcpy(app->import_mnemonic_text, app->input_text);
int status = FlipBipStatusSuccess;
// Check if the mnemonic is valid
if(mnemonic_check(app->import_mnemonic_text) == 0)
status = FlipBipStatusMnemonicCheckError; // 13 = mnemonic check error
// Save the mnemonic to persistent storage
else if(!flipbip_save_file_secure(app->import_mnemonic_text))
status = FlipBipStatusSaveError; // 12 = save error
if(status == FlipBipStatusSuccess) {
app->mnemonic_menu_text = MNEMONIC_MENU_SUCCESS;
//notification_message(app->notification, &sequence_blink_cyan_100);
//flipbip_play_happy_bump(app);
} else {
app->mnemonic_menu_text = MNEMONIC_MENU_FAILURE;
//notification_message(app->notification, &sequence_blink_red_100);
//flipbip_play_long_bump(app);
}
memzero(app->import_mnemonic_text, TEXT_BUFFER_SIZE);
}
// clear input text
memzero(app->input_text, TEXT_BUFFER_SIZE);
// reset input state
app->input_state = FlipBipTextInputDefault;
handled = true;
// exit scene 1 instance that's being used for text input and go back to menu
scene_manager_previous_scene(app->scene_manager);
//view_dispatcher_switch_to_view(app->view_dispatcher, FlipBipViewIdMenu);
}
}
if(!handled) {
// clear input text
memzero(app->input_text, TEXT_BUFFER_SIZE);
// reset input state
app->input_state = FlipBipTextInputDefault;
// something went wrong, switch to menu view
view_dispatcher_switch_to_view(app->view_dispatcher, FlipBipViewIdMenu);
}
}
static void flipbip_scene_renew_dialog_callback(DialogExResult result, void* context) {
FlipBip* app = context;
if(result == DialogExResultRight) {
app->wallet_create(app);
} else {
view_dispatcher_switch_to_view(app->view_dispatcher, FlipBipViewIdMenu);
}
}
static void flipbip_wallet_create(void* context) {
FlipBip* app = context;
furi_assert(app);
scene_manager_set_scene_state(app->scene_manager, FlipBipSceneMenu, SubmenuIndexScene1New);
scene_manager_next_scene(app->scene_manager, FlipBipSceneScene_1);
}
FlipBip* flipbip_app_alloc() {
FlipBip* app = malloc(sizeof(FlipBip));
app->gui = furi_record_open(RECORD_GUI);
//app->notification = furi_record_open(RECORD_NOTIFICATION);
// Turn backlight on, believe me this makes testing your app easier
//notification_message(app->notification, &sequence_display_backlight_on);
// Scene additions
app->view_dispatcher = view_dispatcher_alloc();
app->scene_manager = scene_manager_alloc(&flipbip_scene_handlers, app);
view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
view_dispatcher_set_navigation_event_callback(
app->view_dispatcher, flipbip_navigation_event_callback);
view_dispatcher_set_tick_event_callback(
app->view_dispatcher, flipbip_tick_event_callback, 100);
view_dispatcher_set_custom_event_callback(app->view_dispatcher, flipbip_custom_event_callback);
app->submenu = submenu_alloc();
// Settings
app->bip39_strength = FlipBipStrength256; // 256 bits (24 words)
app->passphrase = FlipBipPassphraseOff;
// Main menu
app->coin_type = CoinTypeBTC0; // 0 (BTC)
app->overwrite_saved_seed = 0;
app->import_from_mnemonic = 0;
app->mnemonic_menu_text = MNEMONIC_MENU_DEFAULT;
// Text input
app->input_state = FlipBipTextInputDefault;
view_dispatcher_add_view(
app->view_dispatcher, FlipBipViewIdMenu, submenu_get_view(app->submenu));
app->flipbip_scene_1 = flipbip_scene_1_alloc();
view_dispatcher_add_view(
app->view_dispatcher, FlipBipViewIdScene1, flipbip_scene_1_get_view(app->flipbip_scene_1));
app->variable_item_list = variable_item_list_alloc();
view_dispatcher_add_view(
app->view_dispatcher,
FlipBipViewIdSettings,
variable_item_list_get_view(app->variable_item_list));
app->text_input = text_input_alloc();
text_input_set_result_callback(
app->text_input,
text_input_callback,
(void*)app,
app->input_text,
TEXT_BUFFER_SIZE,
// clear default text
true);
//text_input_set_header_text(app->text_input, "Input");
view_dispatcher_add_view(
app->view_dispatcher, FlipBipViewIdTextInput, text_input_get_view(app->text_input));
app->wallet_create = flipbip_wallet_create;
app->renew_dialog = dialog_ex_alloc();
dialog_ex_set_result_callback(app->renew_dialog, flipbip_scene_renew_dialog_callback);
dialog_ex_set_context(app->renew_dialog, app);
dialog_ex_set_left_button_text(app->renew_dialog, "No");
dialog_ex_set_right_button_text(app->renew_dialog, "Yes");
dialog_ex_set_header(
app->renew_dialog,
"Current wallet\nwill be deleted!\nProceed?",
16,
12,
AlignLeft,
AlignTop);
view_dispatcher_add_view(
app->view_dispatcher, FlipBipViewRenewConfirm, dialog_ex_get_view(app->renew_dialog));
// End Scene Additions
return app;
}
void flipbip_app_free(FlipBip* app) {
furi_assert(app);
// Scene manager
scene_manager_free(app->scene_manager);
text_input_free(app->text_input);
// View Dispatcher
view_dispatcher_remove_view(app->view_dispatcher, FlipBipViewIdMenu);
view_dispatcher_remove_view(app->view_dispatcher, FlipBipViewIdScene1);
view_dispatcher_remove_view(app->view_dispatcher, FlipBipViewIdSettings);
view_dispatcher_remove_view(app->view_dispatcher, FlipBipViewIdTextInput);
submenu_free(app->submenu);
view_dispatcher_remove_view(app->view_dispatcher, FlipBipViewRenewConfirm);
dialog_ex_free(app->renew_dialog);
view_dispatcher_free(app->view_dispatcher);
furi_record_close(RECORD_GUI);
app->gui = NULL;
//app->notification = NULL;
//Remove whatever is left
memzero(app, sizeof(FlipBip));
free(app);
}
int32_t flipbip_app(void* p) {
UNUSED(p);
FlipBip* app = flipbip_app_alloc();
// Disabled because causes exit on custom firmwares such as RM
/*if(!furi_hal_region_is_provisioned()) {
flipbip_app_free(app);
return 1;
}*/
view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
scene_manager_next_scene(app->scene_manager, FlipBipSceneMenu); //Start with menu
furi_hal_power_suppress_charge_enter();
view_dispatcher_run(app->view_dispatcher);
furi_hal_power_suppress_charge_exit();
flipbip_app_free(app);
return 0;
}