Struan Clark 2 лет назад
Родитель
Сommit
f133996899
3 измененных файлов с 62 добавлено и 23 удалено
  1. 43 13
      helpers/flipbip_file.c
  2. 1 0
      helpers/flipbip_file.h
  3. 18 10
      views/flipbip_scene_1.c

+ 43 - 13
helpers/flipbip_file.c

@@ -26,14 +26,17 @@ const char* FILE_K1 = "fb0131d5cf688221c109163908ebe51debb46227c6cc8b37641910833
                       "baefe6d9ceb651842260e0d1e05e3b90d15e7d5ffaaabc0207bf200a117793a2";
                       "baefe6d9ceb651842260e0d1e05e3b90d15e7d5ffaaabc0207bf200a117793a2";
 
 
 bool flipbip_load_settings(char* settings, bool key_file) {
 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;
     const char* path;
     if(key_file) {
     if(key_file) {
         path = FLIPBIP_KEY_PATH;
         path = FLIPBIP_KEY_PATH;
     } else {
     } else {
         path = FLIPBIP_DAT_PATH;
         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)) {
     if(storage_file_open(settings_file, path, FSAM_READ, FSOM_OPEN_EXISTING)) {
         char chr;
         char chr;
         int i = 0;
         int i = 0;
@@ -42,10 +45,11 @@ bool flipbip_load_settings(char* settings, bool key_file) {
             settings[i] = chr;
             settings[i] = chr;
             i++;
             i++;
         }
         }
+        ret = true;
     } else {
     } else {
         memzero(settings, strlen(settings));
         memzero(settings, strlen(settings));
         settings[0] = '\0';
         settings[0] = '\0';
-        return false;
+        ret = false;
     }
     }
     storage_file_close(settings_file);
     storage_file_close(settings_file);
     storage_file_free(settings_file);
     storage_file_free(settings_file);
@@ -60,7 +64,7 @@ bool flipbip_load_settings(char* settings, bool key_file) {
         if(file_check_err != FSE_OK) {
         if(file_check_err != FSE_OK) {
             memzero(settings, strlen(settings));
             memzero(settings, strlen(settings));
             settings[0] = '\0';
             settings[0] = '\0';
-            return false;
+            ret = false;
         }
         }
         // if(layout_file_info.size != 256) {
         // if(layout_file_info.size != 256) {
         //     memzero(settings, strlen(settings));
         //     memzero(settings, strlen(settings));
@@ -68,19 +72,29 @@ bool flipbip_load_settings(char* settings, bool key_file) {
         // }
         // }
     }
     }
 
 
-    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* 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;
     const char* path_bak;
     const char* path_bak;
     if(key_file) {
     if(key_file) {
@@ -90,13 +104,29 @@ bool flipbip_save_settings(const char* settings, bool key_file, bool append) {
         path = FLIPBIP_DAT_PATH;
         path = FLIPBIP_DAT_PATH;
         path_bak = FLIPBIP_DAT_PATH_BAK;
         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)) {
     if(storage_file_open(settings_file, path, FSAM_WRITE, open_mode)) {
         storage_file_write(
         storage_file_write(
             settings_file,
             settings_file,
             settings,
             settings,
             strlen(settings));
             strlen(settings));
         storage_file_write(settings_file, "\n", 1);
         storage_file_write(settings_file, "\n", 1);
+        ret = true;
     }
     }
     storage_file_close(settings_file);
     storage_file_close(settings_file);
     storage_file_free(settings_file);
     storage_file_free(settings_file);
@@ -114,7 +144,7 @@ bool flipbip_save_settings(const char* settings, bool key_file, bool append) {
 
 
     furi_record_close(RECORD_STORAGE);
     furi_record_close(RECORD_STORAGE);
 
 
-    return true;
+    return ret;
 }
 }
 
 
 bool flipbip_load_settings_secure(char* settings) {
 bool flipbip_load_settings_secure(char* settings) {

+ 1 - 0
helpers/flipbip_file.h

@@ -1,5 +1,6 @@
 #include <stdbool.h>
 #include <stdbool.h>
 
 
+bool flipbip_has_settings(bool key_file);
 bool flipbip_load_settings(char* 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);
 bool flipbip_save_settings(const char* settings, bool key_file , bool append);
 
 

+ 18 - 10
views/flipbip_scene_1.c

@@ -267,18 +267,25 @@ static void flipbip_scene_1_model_init(FlipBipScene1Model* const model, const in
     
     
     model->page = 0;
     model->page = 0;
     model->coin = coin;
     model->coin = coin;
-
-    // Generate a random mnemonic using trezor-crypto
     model->strength = strength;
     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
     // 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";
     //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";
@@ -431,7 +438,8 @@ void flipbip_scene_1_exit(void* context) {
             for (int i = 0; i < 64; i++) {
             for (int i = 0; i < 64; i++) {
                 model->seed[i] = 0;
                 model->seed[i] = 0;
             }
             }
-            mnemonic_clear();
+            memzero((void*)model->mnemonic, strlen(model->mnemonic));
+            free((void*)model->mnemonic);
             memzero((void*)model->node, sizeof(HDNode));
             memzero((void*)model->node, sizeof(HDNode));
             free((void*)model->node);
             free((void*)model->node);
             memzero((void*)model->xprv_root, strlen(model->xprv_root));
             memzero((void*)model->xprv_root, strlen(model->xprv_root));