flipbip.c 8.5 KB

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