From bc830bb8715170d1ec33cc339a3e54d7dcb661ee Mon Sep 17 00:00:00 2001 From: Struan Clark Date: Sun, 12 Mar 2023 22:02:37 -0600 Subject: [PATCH] clean up labels --- flipbip.c | 2 +- helpers/flipbip_file.c | 42 ++++++++++++++------------ helpers/flipbip_file.h | 10 +++---- scenes/flipbip_scene_menu.c | 3 +- views/flipbip_scene_1.c | 59 +++++++++++++++++++++---------------- 5 files changed, 65 insertions(+), 51 deletions(-) diff --git a/flipbip.c b/flipbip.c index 2682ac2e10b..a14305eec16 100644 --- a/flipbip.c +++ b/flipbip.c @@ -50,7 +50,7 @@ static void text_input_callback(void* context) { if(mnemonic_check(app->import_mnemonic_text) == 0) status = FlipBipStatusMnemonicCheckError; // 13 = mnemonic check error // Save the mnemonic to persistent storage - else if(!flipbip_save_settings_secure(app->import_mnemonic_text)) + else if(!flipbip_save_file_secure(app->import_mnemonic_text)) status = FlipBipStatusSaveError; // 12 = save error if(status == FlipBipStatusSuccess) { diff --git a/helpers/flipbip_file.c b/helpers/flipbip_file.c index e4b01d187e8..4bac1709ada 100644 --- a/helpers/flipbip_file.c +++ b/helpers/flipbip_file.c @@ -1,5 +1,7 @@ #include "flipbip_file.h" #include +#include +#include #include "../helpers/flipbip_string.h" // From: lib/crypto #include @@ -22,15 +24,15 @@ const char* TEXT_QRFILE = "Filetype: QRCode\n" "Version: 0\n" "Message: "; // 37 chars + 1 null - -const size_t FILE_HLEN = 4; -const size_t FILE_KLEN = 256; -const size_t FILE_SLEN = 512; +#define FILE_HLEN 4 +#define FILE_KLEN 256 +#define FILE_SLEN 512 +#define FILE_MAX_PATH_LEN 48 const char* FILE_HSTR = "fb01"; const char* FILE_K1 = "fb0131d5cf688221c109163908ebe51debb46227c6cc8b37641910833222772a" "baefe6d9ceb651842260e0d1e05e3b90d15e7d5ffaaabc0207bf200a117793a2"; -bool flipbip_load_settings(char* settings, const FlipBipFile file_type, const char* file_name) { +bool flipbip_load_file(char* settings, const FlipBipFile file_type, const char* file_name) { bool ret = false; const char* path; if(file_type == FlipBipFileKey) { @@ -85,7 +87,7 @@ bool flipbip_load_settings(char* settings, const FlipBipFile file_type, const ch return ret; } -bool flipbip_has_settings(const FlipBipFile file_type, const char* file_name) { +bool flipbip_has_file(const FlipBipFile file_type, const char* file_name, const bool remove) { bool ret = false; const char* path; if(file_type == FlipBipFileKey) { @@ -101,15 +103,17 @@ bool flipbip_has_settings(const FlipBipFile file_type, const char* file_name) { } Storage* fs_api = furi_record_open(RECORD_STORAGE); - if(storage_file_exists(fs_api, path)) { - ret = true; + if(remove) { + ret = storage_simply_remove(fs_api, path); + } else { + ret = storage_file_exists(fs_api, path); } furi_record_close(RECORD_STORAGE); return ret; } -bool flipbip_save_settings( +bool flipbip_save_file( const char* settings, const FlipBipFile file_type, const char* file_name, @@ -124,7 +128,7 @@ bool flipbip_save_settings( path = FLIPBIP_DAT_PATH; path_bak = FLIPBIP_DAT_PATH_BAK; } else { - char path_buf[48] = {0}; + char path_buf[FILE_MAX_PATH_LEN] = {0}; strcpy(path_buf, FLIPBIP_APP_BASE_FOLDER); // 22 strcpy(path_buf + strlen(path_buf), "/"); strcpy(path_buf + strlen(path_buf), file_name); @@ -144,7 +148,7 @@ bool flipbip_save_settings( // return ret; // } // try to create the folder - storage_common_mkdir(fs_api, FLIPBIP_APP_BASE_FOLDER); + storage_simply_mkdir(fs_api, FLIPBIP_APP_BASE_FOLDER); File* settings_file = storage_file_alloc(fs_api); if(storage_file_open(settings_file, path, FSAM_WRITE, open_mode)) { @@ -178,10 +182,10 @@ bool flipbip_save_qrfile( strcpy(qr_buf, TEXT_QRFILE); strcpy(qr_buf + strlen(qr_buf), qr_msg_prefix); strcpy(qr_buf + strlen(qr_buf), qr_msg_content); - return flipbip_save_settings(qr_buf, FlipBipFileOther, file_name, false); + return flipbip_save_file(qr_buf, FlipBipFileOther, file_name, false); } -bool flipbip_load_settings_secure(char* settings) { +bool flipbip_load_file_secure(char* settings) { const size_t dlen = FILE_HLEN + FILE_SLEN + 1; // allocate memory for key/data @@ -189,7 +193,7 @@ bool flipbip_load_settings_secure(char* settings) { memzero(data, dlen); // load k2 from file - if(!flipbip_load_settings(data, FlipBipFileKey, NULL)) return false; + if(!flipbip_load_file(data, FlipBipFileKey, NULL)) return false; // check header if(data[0] != FILE_HSTR[0] || data[1] != FILE_HSTR[1] || data[2] != FILE_HSTR[2] || @@ -215,7 +219,7 @@ bool flipbip_load_settings_secure(char* settings) { data -= FILE_HLEN; // load data from file - if(!flipbip_load_settings(data, FlipBipFileDat, NULL)) return false; + if(!flipbip_load_file(data, FlipBipFileDat, NULL)) return false; // check header if(data[0] != FILE_HSTR[0] || data[1] != FILE_HSTR[1] || data[2] != FILE_HSTR[2] || @@ -248,7 +252,7 @@ bool flipbip_load_settings_secure(char* settings) { return true; } -bool flipbip_save_settings_secure(const char* settings) { +bool flipbip_save_file_secure(const char* settings) { const size_t dlen = FILE_HLEN + FILE_SLEN + 1; // cap settings to 256 bytes @@ -279,7 +283,7 @@ bool flipbip_save_settings_secure(const char* settings) { // seek <-- header data -= FILE_HLEN; // save k2 to file - flipbip_save_settings(data, FlipBipFileKey, NULL, false); + flipbip_save_file(data, FlipBipFileKey, NULL, false); // seek --> header data += FILE_HLEN; // zero k2 memory @@ -292,7 +296,7 @@ bool flipbip_save_settings_secure(const char* settings) { // seek <-- header data -= FILE_HLEN; // save data to file - flipbip_save_settings(data, FlipBipFileDat, NULL, false); + flipbip_save_file(data, FlipBipFileDat, NULL, false); // clear memory memzero(data, dlen); @@ -301,4 +305,4 @@ bool flipbip_save_settings_secure(const char* settings) { memzero(k2, FILE_KLEN / 2); return true; -} \ No newline at end of file +} diff --git a/helpers/flipbip_file.h b/helpers/flipbip_file.h index 3c271224ff0..c19f70dc519 100644 --- a/helpers/flipbip_file.h +++ b/helpers/flipbip_file.h @@ -6,9 +6,9 @@ typedef enum { FlipBipFileOther, } FlipBipFile; -bool flipbip_has_settings(const FlipBipFile file_type, const char* file_name); -bool flipbip_load_settings(char* settings, const FlipBipFile file_type, const char* file_name); -bool flipbip_save_settings( +bool flipbip_has_file(const FlipBipFile file_type, const char* file_name, const bool remove); +bool flipbip_load_file(char* settings, const FlipBipFile file_type, const char* file_name); +bool flipbip_save_file( const char* settings, const FlipBipFile file_type, const char* file_name, @@ -19,5 +19,5 @@ bool flipbip_save_qrfile( const char* qr_msg_content, const char* file_name); -bool flipbip_load_settings_secure(char* settings); -bool flipbip_save_settings_secure(const char* settings); \ No newline at end of file +bool flipbip_load_file_secure(char* settings); +bool flipbip_save_file_secure(const char* settings); diff --git a/scenes/flipbip_scene_menu.c b/scenes/flipbip_scene_menu.c index d0ac5a7b8da..4784fcdc1af 100644 --- a/scenes/flipbip_scene_menu.c +++ b/scenes/flipbip_scene_menu.c @@ -18,7 +18,8 @@ void flipbip_scene_menu_submenu_callback(void* context, uint32_t index) { void flipbip_scene_menu_on_enter(void* context) { FlipBip* app = context; - if(flipbip_has_settings(FlipBipFileKey, NULL) && flipbip_has_settings(FlipBipFileDat, NULL)) { + if(flipbip_has_file(FlipBipFileKey, NULL, false) && + flipbip_has_file(FlipBipFileDat, NULL, false)) { submenu_add_item( app->submenu, "View BTC wallet", diff --git a/views/flipbip_scene_1.c b/views/flipbip_scene_1.c index f1ddec4f347..1c3689fa719 100644 --- a/views/flipbip_scene_1.c +++ b/views/flipbip_scene_1.c @@ -280,13 +280,12 @@ void flipbip_scene_1_draw(Canvas* canvas, FlipBipScene1Model* model) { flipbip_scene_1_draw_generic(model->xpub_extended, 20); } else if(model->page >= PAGE_ADDR_BEGIN && model->page <= PAGE_ADDR_END) { flipbip_scene_1_draw_generic(model->recv_addresses[model->page - PAGE_ADDR_BEGIN], 12); - elements_button_right(canvas, TEXT_SAVE_QR); } if(model->page == PAGE_LOADING) { canvas_set_font(canvas, FontPrimary); - canvas_draw_str(canvas, 1, 10, TEXT_LOADING); - canvas_draw_str(canvas, 6, 30, s_derivation_text); + canvas_draw_str(canvas, 2, 10, TEXT_LOADING); + canvas_draw_str(canvas, 7, 30, s_derivation_text); canvas_draw_icon(canvas, 86, 25, &I_Keychain_39x36); } else if(model->page >= PAGE_ADDR_BEGIN && model->page <= PAGE_ADDR_END) { // draw address header @@ -297,22 +296,31 @@ void flipbip_scene_1_draw(Canvas* canvas, FlipBipScene1Model* model) { receive_text = TEXT_DEFAULT_COIN; } const size_t receive_len = strlen(receive_text) * 7; - canvas_draw_str_aligned(canvas, 1, 2, AlignLeft, AlignTop, receive_text); - canvas_draw_str_aligned(canvas, receive_len, 2, AlignLeft, AlignTop, TEXT_RECEIVE_ADDRESS); + canvas_draw_str_aligned(canvas, 2, 2, AlignLeft, AlignTop, receive_text); + canvas_draw_str_aligned( + canvas, receive_len + 1, 2, AlignLeft, AlignTop, TEXT_RECEIVE_ADDRESS); // draw address number const unsigned char addr_num[1] = {(unsigned char)(model->page - PAGE_ADDR_BEGIN)}; - char addr_num_text[3]; + char addr_num_text[3] = {0}; flipbip_btox(addr_num, 1, addr_num_text); addr_num_text[0] = '/'; - canvas_draw_str_aligned(canvas, 110, 2, AlignLeft, AlignTop, addr_num_text); + canvas_draw_str_aligned(canvas, 125, 2, AlignRight, AlignTop, addr_num_text); + + // draw QR code file path + char addr_name_text[14] = {0}; + strcpy(addr_name_text, COIN_TEXT_ARRAY[model->coin][0]); + flipbip_btox(addr_num, 1, addr_name_text + strlen(addr_name_text)); + strcpy(addr_name_text + strlen(addr_name_text), TEXT_QRFILE_EXT); + //elements_button_right(canvas, addr_name_text); + canvas_draw_str_aligned(canvas, 125, 53, AlignRight, AlignTop, addr_name_text); // draw address canvas_set_font(canvas, FontPrimary); - canvas_draw_str(canvas, 6, 22, s_disp_text1); - canvas_draw_str(canvas, 6, 34, s_disp_text2); - canvas_draw_str(canvas, 6, 46, s_disp_text3); - canvas_draw_str(canvas, 6, 58, s_disp_text4); + canvas_draw_str(canvas, 7, 22, s_disp_text1); + canvas_draw_str(canvas, 7, 34, s_disp_text2); + canvas_draw_str(canvas, 7, 46, s_disp_text3); + canvas_draw_str(canvas, 7, 58, s_disp_text4); } else { canvas_set_font(canvas, FontSecondary); canvas_draw_str_aligned(canvas, 1, 2, AlignLeft, AlignTop, s_disp_text1); @@ -341,8 +349,8 @@ static int flipbip_scene_1_model_init( memzero(mnemonic, TEXT_BUFFER_SIZE); // Check if the mnemonic key & data is already saved in persistent storage, or overwrite is true - if(overwrite || (!flipbip_has_settings(FlipBipFileKey, NULL) && - !flipbip_has_settings(FlipBipFileDat, NULL))) { + if(overwrite || (!flipbip_has_file(FlipBipFileKey, NULL, false) && + !flipbip_has_file(FlipBipFileDat, NULL, false))) { // Set mnemonic only mode model->mnemonic_only = true; // Generate a random mnemonic using trezor-crypto @@ -351,14 +359,14 @@ static int flipbip_scene_1_model_init( if(mnemonic_check(mnemonic_gen) == 0) return FlipBipStatusMnemonicCheckError; // 13 = mnemonic check error // Save the mnemonic to persistent storage - else if(!flipbip_save_settings_secure(mnemonic_gen)) + else if(!flipbip_save_file_secure(mnemonic_gen)) return FlipBipStatusSaveError; // 12 = save error // Clear the generated mnemonic from memory mnemonic_clear(); } // Load the mnemonic from persistent storage - if(!flipbip_load_settings_secure(mnemonic)) { + if(!flipbip_load_file_secure(mnemonic)) { // Set mnemonic only mode for this error for memory cleanup purposes model->mnemonic_only = true; return FlipBipStatusLoadError; // 11 = load error @@ -507,7 +515,6 @@ bool flipbip_scene_1_input(InputEvent* event, void* context) { }, true); break; - case InputKeyLeft: case InputKeyUp: with_view_model( instance->view, @@ -524,15 +531,17 @@ bool flipbip_scene_1_input(InputEvent* event, void* context) { break; case InputKeyRight: case InputKeyOk: - with_view_model( - instance->view, - FlipBipScene1Model * model, - { - if(model->page >= PAGE_ADDR_BEGIN && model->page <= PAGE_ADDR_END) { - } - }, - true); - break; + // with_view_model( + // instance->view, + // FlipBipScene1Model * model, + // { + // if(model->page >= PAGE_ADDR_BEGIN && model->page <= PAGE_ADDR_END) { + + // } + // }, + // true); + // break; + case InputKeyLeft: case InputKeyMAX: break; }