Skip to content

Commit

Permalink
clean up labels
Browse files Browse the repository at this point in the history
  • Loading branch information
xtruan committed Mar 13, 2023
1 parent 2aa66d1 commit bc830bb
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 51 deletions.
2 changes: 1 addition & 1 deletion flipbip.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
42 changes: 23 additions & 19 deletions helpers/flipbip_file.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "flipbip_file.h"
#include <storage/storage.h>
#include <applications.h>
#include <loader/loader.h>
#include "../helpers/flipbip_string.h"
// From: lib/crypto
#include <memzero.h>
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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,
Expand All @@ -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);
Expand All @@ -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)) {
Expand Down Expand Up @@ -178,18 +182,18 @@ 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
char* data = malloc(dlen);
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] ||
Expand All @@ -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] ||
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -301,4 +305,4 @@ bool flipbip_save_settings_secure(const char* settings) {
memzero(k2, FILE_KLEN / 2);

return true;
}
}
10 changes: 5 additions & 5 deletions helpers/flipbip_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
bool flipbip_load_file_secure(char* settings);
bool flipbip_save_file_secure(const char* settings);
3 changes: 2 additions & 1 deletion scenes/flipbip_scene_menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
59 changes: 34 additions & 25 deletions views/flipbip_scene_1.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -507,7 +515,6 @@ bool flipbip_scene_1_input(InputEvent* event, void* context) {
},
true);
break;
case InputKeyLeft:
case InputKeyUp:
with_view_model(
instance->view,
Expand All @@ -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;
}
Expand Down

0 comments on commit bc830bb

Please sign in to comment.