Parcourir la source

Merge pull request #5 from xtruan/develop

Memory management, minor cleanup
Struan Clark il y a 2 ans
Parent
commit
298aebe6e3
5 fichiers modifiés avec 105 ajouts et 61 suppressions
  1. 2 0
      crypto/bip32.c
  2. 1 1
      flipbip.c
  3. 1 1
      flipbip.h
  4. 1 1
      helpers/flipbip_file.c
  5. 100 58
      views/flipbip_scene_1.c

+ 2 - 0
crypto/bip32.c

@@ -526,6 +526,8 @@ int hdnode_get_ethereum_pubkeyhash(const HDNode* node, uint8_t* pubkeyhash) {
 
 
     /* get uncompressed public key */
     /* get uncompressed public key */
     if(ecdsa_get_public_key65(node->curve->params, node->private_key, buf) != 0) {
     if(ecdsa_get_public_key65(node->curve->params, node->private_key, buf) != 0) {
+        memzero(ctx, sizeof(SHA3_CTX));
+        free(ctx);
         return 0;
         return 0;
     }
     }
 
 

+ 1 - 1
flipbip.c

@@ -179,7 +179,7 @@ int32_t flipbip_app(void* p) {
     UNUSED(p);
     UNUSED(p);
     FlipBip* app = flipbip_app_alloc();
     FlipBip* app = flipbip_app_alloc();
 
 
-    // Disabled because causes exit on customer firmwares such as RM
+    // Disabled because causes exit on custom firmwares such as RM
     /*if(!furi_hal_region_is_provisioned()) {
     /*if(!furi_hal_region_is_provisioned()) {
         flipbip_app_free(app);
         flipbip_app_free(app);
         return 1;
         return 1;

+ 1 - 1
flipbip.h

@@ -15,7 +15,7 @@
 #include "views/flipbip_startscreen.h"
 #include "views/flipbip_startscreen.h"
 #include "views/flipbip_scene_1.h"
 #include "views/flipbip_scene_1.h"
 
 
-#define FLIPBIP_VERSION "v0.0.5"
+#define FLIPBIP_VERSION "v0.0.6"
 
 
 #define COIN_BTC 0
 #define COIN_BTC 0
 #define COIN_DOGE 3
 #define COIN_DOGE 3

+ 1 - 1
helpers/flipbip_file.c

@@ -55,7 +55,7 @@ bool flipbip_load_settings(char* settings, bool key_file) {
     storage_file_free(settings_file);
     storage_file_free(settings_file);
     furi_record_close(RECORD_STORAGE);
     furi_record_close(RECORD_STORAGE);
 
 
-    if(!strlen(settings) == 0) {
+    if(strlen(settings) > 0) {
         Storage* fs_api = furi_record_open(RECORD_STORAGE);
         Storage* fs_api = furi_record_open(RECORD_STORAGE);
         FileInfo layout_file_info;
         FileInfo layout_file_info;
         FS_Error file_check_err = storage_common_stat(fs_api, path, &layout_file_info);
         FS_Error file_check_err = storage_common_stat(fs_api, path, &layout_file_info);

+ 100 - 58
views/flipbip_scene_1.c

@@ -22,6 +22,18 @@
 #define DERIV_ACCOUNT 0
 #define DERIV_ACCOUNT 0
 #define DERIV_CHANGE 0
 #define DERIV_CHANGE 0
 
 
+#define PAGE_LOADING 0
+#define PAGE_INFO 1
+#define PAGE_MNEMONIC 2
+#define PAGE_SEED 3
+#define PAGE_XPRV_ROOT 4
+#define PAGE_XPRV_ACCT 5
+#define PAGE_XPUB_ACCT 6
+#define PAGE_XPRV_EXTD 7
+#define PAGE_XPUB_EXTD 8
+#define PAGE_ADDR_BEGIN 9
+#define PAGE_ADDR_END 14 //18
+
 #define TEXT_LOADING "Loading..."
 #define TEXT_LOADING "Loading..."
 #define TEXT_NEW_WALLET "New wallet"
 #define TEXT_NEW_WALLET "New wallet"
 #define TEXT_DEFAULT_COIN "Coin"
 #define TEXT_DEFAULT_COIN "Coin"
@@ -68,12 +80,15 @@ typedef struct {
     CONFIDENTIAL const char* xpub_extended;
     CONFIDENTIAL const char* xpub_extended;
 } FlipBipScene1Model;
 } FlipBipScene1Model;
 
 
-static CONFIDENTIAL char s_disp_text1[30 + 1];
-static CONFIDENTIAL char s_disp_text2[30 + 1];
-static CONFIDENTIAL char s_disp_text3[30 + 1];
-static CONFIDENTIAL char s_disp_text4[30 + 1];
-static CONFIDENTIAL char s_disp_text5[30 + 1];
-static CONFIDENTIAL char s_disp_text6[30 + 1];
+static CONFIDENTIAL HDNode* s_addr_node = NULL;
+
+static CONFIDENTIAL char* s_disp_text1 = NULL;
+static CONFIDENTIAL char* s_disp_text2 = NULL;
+static CONFIDENTIAL char* s_disp_text3 = NULL;
+static CONFIDENTIAL char* s_disp_text4 = NULL;
+static CONFIDENTIAL char* s_disp_text5 = NULL;
+static CONFIDENTIAL char* s_disp_text6 = NULL;
+
 static const char* s_derivation_text = TEXT_DEFAULT_DERIV;
 static const char* s_derivation_text = TEXT_DEFAULT_DERIV;
 //static bool s_busy = false;
 //static bool s_busy = false;
 
 
@@ -116,7 +131,8 @@ static void flipbip_scene_1_draw_generic(const char* text, size_t line_len) {
 
 
 static void flipbip_scene_1_draw_mnemonic(const char* mnemonic) {
 static void flipbip_scene_1_draw_mnemonic(const char* mnemonic) {
     // Delineate sections of the mnemonic every 4 words
     // Delineate sections of the mnemonic every 4 words
-    char* mnemonic_working = malloc(strlen(mnemonic) + 1);
+    const size_t mnemonic_working_len = strlen(mnemonic) + 1;
+    char* mnemonic_working = malloc(mnemonic_working_len);
     strcpy(mnemonic_working, mnemonic);
     strcpy(mnemonic_working, mnemonic);
     int word = 0;
     int word = 0;
     for(size_t i = 0; i < strlen(mnemonic_working); i++) {
     for(size_t i = 0; i < strlen(mnemonic_working); i++) {
@@ -159,19 +175,20 @@ static void flipbip_scene_1_draw_mnemonic(const char* mnemonic) {
     }
     }
 
 
     // Free the working mnemonic memory
     // Free the working mnemonic memory
-    memzero(mnemonic_working, strlen(mnemonic_working));
+    memzero(mnemonic_working, mnemonic_working_len);
     free(mnemonic_working);
     free(mnemonic_working);
 }
 }
 
 
 static void flipbip_scene_1_draw_seed(FlipBipScene1Model* const model) {
 static void flipbip_scene_1_draw_seed(FlipBipScene1Model* const model) {
-    char* seed_working = malloc(64 * 2 + 1);
+    const size_t seed_working_len = 64 * 2 + 1;
+    char* seed_working = malloc(seed_working_len);
     // Convert the seed to a hex string
     // Convert the seed to a hex string
     flipbip_btox(model->seed, 64, seed_working);
     flipbip_btox(model->seed, 64, seed_working);
 
 
     flipbip_scene_1_draw_generic(seed_working, 22);
     flipbip_scene_1_draw_generic(seed_working, 22);
 
 
     // Free the working seed memory
     // Free the working seed memory
-    memzero(seed_working, sizeof(seed_working));
+    memzero(seed_working, seed_working_len);
     free(seed_working);
     free(seed_working);
 }
 }
 
 
@@ -179,15 +196,15 @@ static void
     flipbip_scene_1_draw_address(const HDNode* node, uint32_t addr_type, uint32_t addr_index) {
     flipbip_scene_1_draw_address(const HDNode* node, uint32_t addr_type, uint32_t addr_index) {
     //s_busy = true;
     //s_busy = true;
 
 
-    // buffer for key serialization
-    const size_t buflen = 128;
-    char buf[128 + 1];
+    // buffer for address serialization
+    const size_t buflen = 40;
+    char buf[40 + 1] = {0};
 
 
-    HDNode* addr_node = malloc(sizeof(HDNode));
-    memcpy(addr_node, node, sizeof(HDNode));
+    // use static node for address generation
+    memcpy(s_addr_node, node, sizeof(HDNode));
 
 
-    hdnode_private_ckd(addr_node, addr_index);
-    hdnode_fill_public_key(addr_node);
+    hdnode_private_ckd(s_addr_node, addr_index);
+    hdnode_fill_public_key(s_addr_node);
 
 
     // coin info
     // coin info
     // bip44_coin, xprv_version, xpub_version, addr_version, wif_version, addr_format
     // bip44_coin, xprv_version, xpub_version, addr_version, wif_version, addr_format
@@ -199,32 +216,23 @@ static void
     if(coin_info[5] == FlipBipCoinBTC0) { // BTC / DOGE style address
     if(coin_info[5] == FlipBipCoinBTC0) { // BTC / DOGE style address
         // BTC / DOGE style address
         // BTC / DOGE style address
         ecdsa_get_address(
         ecdsa_get_address(
-            addr_node->public_key, coin_info[3], HASHER_SHA2_RIPEMD, HASHER_SHA2D, buf, buflen);
+            s_addr_node->public_key, coin_info[3], HASHER_SHA2_RIPEMD, HASHER_SHA2D, buf, buflen);
 
 
-        char* address = malloc(buflen + 1);
-        strncpy(address, buf, buflen);
-        flipbip_scene_1_draw_generic(address, 12);
-        memzero(address, buflen + 1);
-        free(address);
+        flipbip_scene_1_draw_generic(buf, 12);
 
 
         //ecdsa_get_wif(addr_node->private_key, WIF_VERSION, HASHER_SHA2D, buf, buflen);
         //ecdsa_get_wif(addr_node->private_key, WIF_VERSION, HASHER_SHA2D, buf, buflen);
-        //char *wif = malloc(buflen + 1);
-        //strncpy(wif, buf, buflen);
+
     } else if(coin_info[5] == FlipBipCoinETH60) { // ETH
     } else if(coin_info[5] == FlipBipCoinETH60) { // ETH
         // ETH style address
         // ETH style address
-        hdnode_get_ethereum_pubkeyhash(addr_node, (uint8_t*)buf);
-        char* address = malloc(42 + 1);
-        memcpy(address, "0x", 2);
+        hdnode_get_ethereum_pubkeyhash(s_addr_node, (uint8_t*)buf);
+        char address[42 + 1] = {0};
+        address[0] = '0';
+        address[1] = 'x';
         // Convert the hash to a hex string
         // Convert the hash to a hex string
         flipbip_btox((uint8_t*)buf, 20, address + 2);
         flipbip_btox((uint8_t*)buf, 20, address + 2);
         flipbip_scene_1_draw_generic(address, 12);
         flipbip_scene_1_draw_generic(address, 12);
-        memzero(address, 42 + 1);
-        free(address);
     }
     }
 
 
-    memzero(addr_node, sizeof(HDNode));
-    free(addr_node);
-
     //s_busy = false;
     //s_busy = false;
 }
 }
 
 
@@ -243,32 +251,33 @@ void flipbip_scene_1_draw(Canvas* canvas, FlipBipScene1Model* model) {
     canvas_set_color(canvas, ColorBlack);
     canvas_set_color(canvas, ColorBlack);
 
 
     flipbip_scene_1_clear_text();
     flipbip_scene_1_clear_text();
-    if(model->page == 1) {
+    if(model->page == PAGE_INFO) {
         flipbip_scene_1_draw_generic(TEXT_INFO, 27);
         flipbip_scene_1_draw_generic(TEXT_INFO, 27);
-    } else if(model->page == 2) {
+    } else if(model->page == PAGE_MNEMONIC) {
         flipbip_scene_1_draw_mnemonic(model->mnemonic);
         flipbip_scene_1_draw_mnemonic(model->mnemonic);
-    } else if(model->page == 3) {
+    } else if(model->page == PAGE_SEED) {
         flipbip_scene_1_draw_seed(model);
         flipbip_scene_1_draw_seed(model);
-    } else if(model->page == 4) {
+    } else if(model->page == PAGE_XPRV_ROOT) {
         flipbip_scene_1_draw_generic(model->xprv_root, 20);
         flipbip_scene_1_draw_generic(model->xprv_root, 20);
-    } else if(model->page == 5) {
+    } else if(model->page == PAGE_XPRV_ACCT) {
         flipbip_scene_1_draw_generic(model->xprv_account, 20);
         flipbip_scene_1_draw_generic(model->xprv_account, 20);
-    } else if(model->page == 6) {
+    } else if(model->page == PAGE_XPUB_ACCT) {
         flipbip_scene_1_draw_generic(model->xpub_account, 20);
         flipbip_scene_1_draw_generic(model->xpub_account, 20);
-    } else if(model->page == 7) {
+    } else if(model->page == PAGE_XPRV_EXTD) {
         flipbip_scene_1_draw_generic(model->xprv_extended, 20);
         flipbip_scene_1_draw_generic(model->xprv_extended, 20);
-    } else if(model->page == 8) {
+    } else if(model->page == PAGE_XPUB_EXTD) {
         flipbip_scene_1_draw_generic(model->xpub_extended, 20);
         flipbip_scene_1_draw_generic(model->xpub_extended, 20);
-    } else if(model->page >= 9 && model->page <= 13) {
-        flipbip_scene_1_draw_address(model->node, model->coin, model->page - 9);
+    } else if(model->page >= PAGE_ADDR_BEGIN && model->page <= PAGE_ADDR_END) {
+        flipbip_scene_1_draw_address(model->node, model->coin, model->page - PAGE_ADDR_BEGIN);
     }
     }
 
 
-    if(model->page == 0) {
+    if(model->page == PAGE_LOADING) {
         canvas_set_font(canvas, FontPrimary);
         canvas_set_font(canvas, FontPrimary);
         canvas_draw_str(canvas, 1, 10, TEXT_LOADING);
         canvas_draw_str(canvas, 1, 10, TEXT_LOADING);
         canvas_draw_str(canvas, 6, 30, s_derivation_text);
         canvas_draw_str(canvas, 6, 30, s_derivation_text);
         canvas_draw_icon(canvas, 86, 25, &I_Keychain_39x36);
         canvas_draw_icon(canvas, 86, 25, &I_Keychain_39x36);
-    } else if(model->page >= 9 && model->page <= 13) {
+    } else if(model->page >= PAGE_ADDR_BEGIN && model->page <= PAGE_ADDR_END) {
+        // draw address header
         canvas_set_font(canvas, FontSecondary);
         canvas_set_font(canvas, FontSecondary);
         // coin_name, derivation_path
         // coin_name, derivation_path
         const char* receive_text = COIN_TEXT_ARRAY[model->coin][0];
         const char* receive_text = COIN_TEXT_ARRAY[model->coin][0];
@@ -278,6 +287,15 @@ void flipbip_scene_1_draw(Canvas* canvas, FlipBipScene1Model* model) {
         const size_t receive_len = strlen(receive_text) * 7;
         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, 1, 2, AlignLeft, AlignTop, receive_text);
         canvas_draw_str_aligned(canvas, receive_len, 2, AlignLeft, AlignTop, TEXT_RECEIVE_ADDRESS);
         canvas_draw_str_aligned(canvas, receive_len, 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];
+        flipbip_btox(addr_num, 1, addr_num_text);
+        addr_num_text[0] = '/';
+        canvas_draw_str_aligned(canvas, 110, 2, AlignLeft, AlignTop, addr_num_text);
+
+        // draw address
         canvas_set_font(canvas, FontPrimary);
         canvas_set_font(canvas, FontPrimary);
         canvas_draw_str(canvas, 6, 22, s_disp_text1);
         canvas_draw_str(canvas, 6, 22, s_disp_text1);
         canvas_draw_str(canvas, 6, 34, s_disp_text2);
         canvas_draw_str(canvas, 6, 34, s_disp_text2);
@@ -300,7 +318,7 @@ static int flipbip_scene_1_model_init(
     const uint32_t coin,
     const uint32_t coin,
     const bool overwrite,
     const bool overwrite,
     const char* passphrase_text) {
     const char* passphrase_text) {
-    model->page = 0;
+    model->page = PAGE_LOADING;
     model->mnemonic_only = false;
     model->mnemonic_only = false;
     model->strength = strength;
     model->strength = strength;
     model->coin = coin;
     model->coin = coin;
@@ -414,7 +432,7 @@ static int flipbip_scene_1_model_init(
 
 
     model->node = node;
     model->node = node;
 
 
-    model->page = 1;
+    model->page = PAGE_INFO;
 
 
 #if USE_BIP39_CACHE
 #if USE_BIP39_CACHE
     // Clear the BIP39 cache
     // Clear the BIP39 cache
@@ -454,10 +472,11 @@ bool flipbip_scene_1_input(InputEvent* event, void* context) {
                 FlipBipScene1Model * model,
                 FlipBipScene1Model * model,
                 {
                 {
                     //UNUSED(model);
                     //UNUSED(model);
-                    model->page = (model->page + 1) % 14;
-                    if(model->page == 0) {
-                        model->page = 1;
+                    int page = (model->page + 1) % (PAGE_ADDR_END + 1);
+                    if(page == 0) {
+                        page = PAGE_INFO;
                     }
                     }
+                    model->page = page;
                 },
                 },
                 true);
                 true);
             break;
             break;
@@ -468,10 +487,11 @@ bool flipbip_scene_1_input(InputEvent* event, void* context) {
                 FlipBipScene1Model * model,
                 FlipBipScene1Model * model,
                 {
                 {
                     //UNUSED(model);
                     //UNUSED(model);
-                    model->page = (model->page - 1) % 14;
-                    if(model->page == 0) {
-                        model->page = 13;
+                    int page = (model->page - 1) % (PAGE_ADDR_END + 1);
+                    if(page == 0) {
+                        page = PAGE_ADDR_END;
                     }
                     }
+                    model->page = page;
                 },
                 },
                 true);
                 true);
             break;
             break;
@@ -490,8 +510,8 @@ void flipbip_scene_1_exit(void* context) {
         instance->view,
         instance->view,
         FlipBipScene1Model * model,
         FlipBipScene1Model * model,
         {
         {
-            model->page = 0;
-            model->strength = 0;
+            model->page = PAGE_LOADING;
+            model->strength = FlipBipStrength256;
             model->coin = FlipBipCoinBTC0;
             model->coin = FlipBipCoinBTC0;
             memzero(model->seed, 64);
             memzero(model->seed, 64);
             // if mnemonic_only is true, then we don't need to free the data here
             // if mnemonic_only is true, then we don't need to free the data here
@@ -570,15 +590,15 @@ void flipbip_scene_1_enter(void* context) {
             // if error, set the error message
             // if error, set the error message
             if(status == FlipBipStatusSaveError) {
             if(status == FlipBipStatusSaveError) {
                 model->mnemonic = "ERROR:,Save error";
                 model->mnemonic = "ERROR:,Save error";
-                model->page = 2;
+                model->page = PAGE_MNEMONIC;
                 flipbip_play_long_bump(app);
                 flipbip_play_long_bump(app);
             } else if(status == FlipBipStatusLoadError) {
             } else if(status == FlipBipStatusLoadError) {
                 model->mnemonic = "ERROR:,Load error";
                 model->mnemonic = "ERROR:,Load error";
-                model->page = 2;
+                model->page = PAGE_MNEMONIC;
                 flipbip_play_long_bump(app);
                 flipbip_play_long_bump(app);
             } else if(status == FlipBipStatusMnemonicCheckError) {
             } else if(status == FlipBipStatusMnemonicCheckError) {
                 model->mnemonic = "ERROR:,Mnemonic check error";
                 model->mnemonic = "ERROR:,Mnemonic check error";
-                model->page = 2;
+                model->page = PAGE_MNEMONIC;
                 flipbip_play_long_bump(app);
                 flipbip_play_long_bump(app);
             }
             }
 
 
@@ -602,6 +622,17 @@ FlipBipScene1* flipbip_scene_1_alloc() {
     view_set_enter_callback(instance->view, flipbip_scene_1_enter);
     view_set_enter_callback(instance->view, flipbip_scene_1_enter);
     view_set_exit_callback(instance->view, flipbip_scene_1_exit);
     view_set_exit_callback(instance->view, flipbip_scene_1_exit);
 
 
+    // allocate the address node
+    s_addr_node = (HDNode*)malloc(sizeof(HDNode));
+
+    // allocate the display text
+    s_disp_text1 = (char*)malloc(30 + 1);
+    s_disp_text2 = (char*)malloc(30 + 1);
+    s_disp_text3 = (char*)malloc(30 + 1);
+    s_disp_text4 = (char*)malloc(30 + 1);
+    s_disp_text5 = (char*)malloc(30 + 1);
+    s_disp_text6 = (char*)malloc(30 + 1);
+
     return instance;
     return instance;
 }
 }
 
 
@@ -611,7 +642,18 @@ void flipbip_scene_1_free(FlipBipScene1* instance) {
     with_view_model(
     with_view_model(
         instance->view, FlipBipScene1Model * model, { UNUSED(model); }, true);
         instance->view, FlipBipScene1Model * model, { UNUSED(model); }, true);
 
 
+    // free the address node
+    memzero(s_addr_node, sizeof(HDNode));
+    free(s_addr_node);
+
+    // free the display text
     flipbip_scene_1_clear_text();
     flipbip_scene_1_clear_text();
+    free(s_disp_text1);
+    free(s_disp_text2);
+    free(s_disp_text3);
+    free(s_disp_text4);
+    free(s_disp_text5);
+    free(s_disp_text6);
 
 
     view_free(instance->view);
     view_free(instance->view);
     free(instance);
     free(instance);