Skip to content

Commit

Permalink
cleanup flow
Browse files Browse the repository at this point in the history
  • Loading branch information
xtruan committed Mar 7, 2023
1 parent 2de9326 commit f133996
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 23 deletions.
56 changes: 43 additions & 13 deletions helpers/flipbip_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,17 @@ const char* FILE_K1 = "fb0131d5cf688221c109163908ebe51debb46227c6cc8b37641910833
"baefe6d9ceb651842260e0d1e05e3b90d15e7d5ffaaabc0207bf200a117793a2";

bool flipbip_load_settings(char* settings, bool key_file) {
Storage *fs_api = furi_record_open(RECORD_STORAGE);
File* settings_file = storage_file_alloc(fs_api);
bool ret = false;
const char* path;
if(key_file) {
path = FLIPBIP_KEY_PATH;
} else {
path = FLIPBIP_DAT_PATH;
}

Storage *fs_api = furi_record_open(RECORD_STORAGE);

File* settings_file = storage_file_alloc(fs_api);
if(storage_file_open(settings_file, path, FSAM_READ, FSOM_OPEN_EXISTING)) {
char chr;
int i = 0;
Expand All @@ -42,10 +45,11 @@ bool flipbip_load_settings(char* settings, bool key_file) {
settings[i] = chr;
i++;
}
ret = true;
} else {
memzero(settings, strlen(settings));
settings[0] = '\0';
return false;
ret = false;
}
storage_file_close(settings_file);
storage_file_free(settings_file);
Expand All @@ -60,27 +64,37 @@ bool flipbip_load_settings(char* settings, bool key_file) {
if(file_check_err != FSE_OK) {
memzero(settings, strlen(settings));
settings[0] = '\0';
return false;
ret = false;
}
// if(layout_file_info.size != 256) {
// memzero(settings, strlen(settings));
// settings[0] = '\0';
// }
}

return true;
return ret;
}

bool flipbip_save_settings(const char* settings, bool key_file, bool append) {
bool flipbip_has_settings(bool key_file) {
bool ret = false;
const char* path;
if(key_file) {
path = FLIPBIP_KEY_PATH;
} else {
path = FLIPBIP_DAT_PATH;
}

Storage* fs_api = furi_record_open(RECORD_STORAGE);

storage_common_mkdir(fs_api, FLIPBIP_APP_BASE_FOLDER);
int open_mode = FSOM_OPEN_ALWAYS;
if(append) {
open_mode = FSOM_OPEN_APPEND;
if(storage_file_exists(fs_api, path)) {
ret = true;
}
furi_record_close(RECORD_STORAGE);

File* settings_file = storage_file_alloc(fs_api);
return ret;
}

bool flipbip_save_settings(const char* settings, bool key_file, bool append) {
bool ret = false;
const char* path;
const char* path_bak;
if(key_file) {
Expand All @@ -90,13 +104,29 @@ bool flipbip_save_settings(const char* settings, bool key_file, bool append) {
path = FLIPBIP_DAT_PATH;
path_bak = FLIPBIP_DAT_PATH_BAK;
}
int open_mode = FSOM_OPEN_ALWAYS;
if(append) {
open_mode = FSOM_OPEN_APPEND;
}

Storage* fs_api = furi_record_open(RECORD_STORAGE);
// // if the key file exists, we don't want to overwrite it
// if (key_file && storage_file_exists(fs_api, path)) {
// furi_record_close(RECORD_STORAGE);
// ret = true;
// return ret;
// }
// try to create the folder
storage_common_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)) {
storage_file_write(
settings_file,
settings,
strlen(settings));
storage_file_write(settings_file, "\n", 1);
ret = true;
}
storage_file_close(settings_file);
storage_file_free(settings_file);
Expand All @@ -114,7 +144,7 @@ bool flipbip_save_settings(const char* settings, bool key_file, bool append) {

furi_record_close(RECORD_STORAGE);

return true;
return ret;
}

bool flipbip_load_settings_secure(char* settings) {
Expand Down
1 change: 1 addition & 0 deletions helpers/flipbip_file.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <stdbool.h>

bool flipbip_has_settings(bool key_file);
bool flipbip_load_settings(char* settings, bool key_file);
bool flipbip_save_settings(const char* settings, bool key_file , bool append);

Expand Down
28 changes: 18 additions & 10 deletions views/flipbip_scene_1.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,18 +267,25 @@ static void flipbip_scene_1_model_init(FlipBipScene1Model* const model, const in

model->page = 0;
model->coin = coin;

// Generate a random mnemonic using trezor-crypto
model->strength = strength;

const char* mnemonic = mnemonic_generate(strength);
if (!flipbip_save_settings_secure(mnemonic)) return;

char* mnemonic2 = malloc(256);
memzero((void*)mnemonic2, 256);
if (!flipbip_load_settings_secure(mnemonic2)) return;
// Allocate memory for mnemonic
char* mnemonic = malloc(256);
memzero(mnemonic, 256);

// Check if the mnemonic key & data is already saved in persistent storage
if (!flipbip_has_settings(true) && !flipbip_has_settings(false)) {
// Generate a random mnemonic using trezor-crypto
const char* mnemonic_gen = mnemonic_generate(strength);
// Save the mnemonic to persistent storage
if (!flipbip_save_settings_secure(mnemonic_gen)) return;
// Clear the mnemonic from memory
mnemonic_clear();
}

model->mnemonic = mnemonic2;
// Load the mnemonic from persistent storage
if (!flipbip_load_settings_secure(mnemonic)) return;
model->mnemonic = mnemonic;

// test mnemonic
//model->mnemonic = "wealth budget salt video delay obey neutral tail sure soda hold rubber joy movie boat raccoon tornado noise off inmate payment patch group topple";
Expand Down Expand Up @@ -431,7 +438,8 @@ void flipbip_scene_1_exit(void* context) {
for (int i = 0; i < 64; i++) {
model->seed[i] = 0;
}
mnemonic_clear();
memzero((void*)model->mnemonic, strlen(model->mnemonic));
free((void*)model->mnemonic);
memzero((void*)model->node, sizeof(HDNode));
free((void*)model->node);
memzero((void*)model->xprv_root, strlen(model->xprv_root));
Expand Down

0 comments on commit f133996

Please sign in to comment.