فهرست منبع

adding BIP39 passphrase

Struan Clark 2 سال پیش
والد
کامیت
a376ba9faf
4فایلهای تغییر یافته به همراه94 افزوده شده و 38 حذف شده
  1. 36 4
      flipbip.c
  2. 13 1
      flipbip.h
  3. 28 24
      scenes/flipbip_scene_settings.c
  4. 17 9
      views/flipbip_scene_1.c

+ 36 - 4
flipbip.c

@@ -1,4 +1,5 @@
 #include "flipbip.h"
+#include "crypto/memzero.h"
 
 bool flipbip_custom_event_callback(void* context, uint32_t event) {
     furi_assert(context);
@@ -19,6 +20,20 @@ bool flipbip_navigation_event_callback(void* context) {
     return scene_manager_handle_back_event(app->scene_manager);
 }
 
+static void text_input_callback(void* context) {
+    furi_assert(context);
+    FlipBip* app = context;
+
+    if(app->passphrase == FlipBipPassphraseOn && strlen(app->input) > 0) {
+        strcpy(app->passphrase_text, app->input);
+    } else {
+        memzero(app->passphrase_text, TEXT_BUFFER_SIZE);
+    }
+    memzero(app->input, TEXT_BUFFER_SIZE);
+
+    view_dispatcher_switch_to_view(app->view_dispatcher, FlipBipViewIdSettings);
+}
+
 FlipBip* flipbip_app_alloc() {
     FlipBip* app = malloc(sizeof(FlipBip));
     app->gui = furi_record_open(RECORD_GUI);
@@ -41,9 +56,10 @@ FlipBip* flipbip_app_alloc() {
     app->submenu = submenu_alloc();
 
     // Settings
-    app->haptic = 1;
-    app->led = 1;
-    app->bip39_strength = 2; // 256 bits (24 words)
+    app->haptic = FlipBipHapticOn;
+    app->led = FlipBipLedOn;
+    app->passphrase = FlipBipPassphraseOff;
+    app->bip39_strength = FlipBipStrength256; // 256 bits (24 words)
     app->bip44_coin = FlipBipCoinBTC0; // 0 (BTC)
     app->overwrite_saved_seed = 0;
 
@@ -63,6 +79,19 @@ FlipBip* flipbip_app_alloc() {
         FlipBipViewIdSettings,
         variable_item_list_get_view(app->variable_item_list));
 
+    app->text_input = text_input_alloc();
+    text_input_set_result_callback(
+        app->text_input,
+        text_input_callback,
+        (void*)app,
+        app->input,
+        TEXT_BUFFER_SIZE,
+        //clear default text
+        true);
+    text_input_set_header_text(app->text_input, "Input");
+    view_dispatcher_add_view(
+        app->view_dispatcher, FlipBipViewIdTextInput, text_input_get_view(app->text_input));
+
     //End Scene Additions
 
     return app;
@@ -74,11 +103,13 @@ void flipbip_app_free(FlipBip* app) {
     // Scene manager
     scene_manager_free(app->scene_manager);
 
+    text_input_free(app->text_input);
+
     // View Dispatcher
     view_dispatcher_remove_view(app->view_dispatcher, FlipBipViewIdMenu);
     view_dispatcher_remove_view(app->view_dispatcher, FlipBipViewIdScene1);
-    // view_dispatcher_remove_view(app->view_dispatcher, FlipBipViewIdScene2);
     view_dispatcher_remove_view(app->view_dispatcher, FlipBipViewIdSettings);
+    view_dispatcher_remove_view(app->view_dispatcher, FlipBipViewIdTextInput);
     submenu_free(app->submenu);
 
     view_dispatcher_free(app->view_dispatcher);
@@ -88,6 +119,7 @@ void flipbip_app_free(FlipBip* app) {
     app->notification = NULL;
 
     //Remove whatever is left
+    memzero(app, sizeof(FlipBip));
     free(app);
 }
 

+ 13 - 1
flipbip.h

@@ -10,6 +10,7 @@
 #include <gui/modules/submenu.h>
 #include <gui/scene_manager.h>
 #include <gui/modules/variable_item_list.h>
+#include <gui/modules/text_input.h>
 #include "scenes/flipbip_scene.h"
 #include "views/flipbip_startscreen.h"
 #include "views/flipbip_scene_1.h"
@@ -20,6 +21,8 @@
 #define COIN_DOGE 3
 #define COIN_ETH 60
 
+#define TEXT_BUFFER_SIZE 256
+
 typedef struct {
     Gui* gui;
     NotificationApp* notification;
@@ -27,10 +30,14 @@ typedef struct {
     Submenu* submenu;
     SceneManager* scene_manager;
     VariableItemList* variable_item_list;
+    TextInput* text_input;
+    char input[TEXT_BUFFER_SIZE];
     FlipBipStartscreen* flipbip_startscreen;
     FlipBipScene1* flipbip_scene_1;
     int haptic;
     int led;
+    int passphrase;
+    char passphrase_text[TEXT_BUFFER_SIZE];
     int bip39_strength;
     int bip44_coin;
     int overwrite_saved_seed;
@@ -40,8 +47,8 @@ typedef enum {
     FlipBipViewIdStartscreen,
     FlipBipViewIdMenu,
     FlipBipViewIdScene1,
-    // FlipBipViewIdScene2,
     FlipBipViewIdSettings,
+    FlipBipViewIdTextInput,
 } FlipBipViewId;
 
 typedef enum {
@@ -60,6 +67,11 @@ typedef enum {
     FlipBipStrength256,
 } FlipBipStrengthState;
 
+typedef enum {
+    FlipBipPassphraseOff,
+    FlipBipPassphraseOn,
+} FlipBipPassphraseState;
+
 typedef enum {
     FlipBipCoinBTC0,
     FlipBipCoinETH60,

+ 28 - 24
scenes/flipbip_scene_settings.c

@@ -37,14 +37,14 @@ const uint32_t bip39_strength_value[3] = {
     FlipBipStrength256,
 };
 
-// const char* const bip44_coin_text[2] = {
-//     "BTC",
-//     "ETH",
-// };
-// const uint32_t bip44_coin_value[2] = {
-//     FlipBipCoinBTC0,
-//     FlipBipCoinETH60,
-// };
+const char* const passphrase_text[2] = {
+    "OFF",
+    "ON",
+};
+const uint32_t passphrase_value[2] = {
+    FlipBipPassphraseOff,
+    FlipBipPassphraseOn,
+};
 
 static void flipbip_scene_settings_set_haptic(VariableItem* item) {
     FlipBip* app = variable_item_get_context(item);
@@ -67,12 +67,16 @@ static void flipbip_scene_settings_set_bip39_strength(VariableItem* item) {
     app->bip39_strength = bip39_strength_value[index];
 }
 
-// static void flipbip_scene_settings_set_bip44_coin(VariableItem* item) {
-//     FlipBip* app = variable_item_get_context(item);
-//     uint8_t index = variable_item_get_current_value_index(item);
-//     variable_item_set_current_value_text(item, bip44_coin_text[index]);
-//     app->bip44_coin = bip44_coin_value[index];
-// }
+static void flipbip_scene_settings_set_passphrase(VariableItem* item) {
+    FlipBip* app = variable_item_get_context(item);
+    uint8_t index = variable_item_get_current_value_index(item);
+    variable_item_set_current_value_text(item, passphrase_text[index]);
+    app->passphrase = passphrase_value[index];
+
+    if(app->passphrase == FlipBipPassphraseOn) {
+        view_dispatcher_switch_to_view(app->view_dispatcher, FlipBipViewIdTextInput);
+    }
+}
 
 void flipbip_scene_settings_submenu_callback(void* context, uint32_t index) {
     FlipBip* app = context;
@@ -91,16 +95,16 @@ void flipbip_scene_settings_on_enter(void* context) {
     variable_item_set_current_value_index(item, value_index);
     variable_item_set_current_value_text(item, bip39_strength_text[value_index]);
 
-    // // BIP44 Coin
-    // item = variable_item_list_add(
-    //     app->variable_item_list,
-    //     "BIP44 Coin:",
-    //     2,
-    //     flipbip_scene_settings_set_bip44_coin,
-    //     app);
-    // value_index = value_index_uint32(app->bip44_coin, bip44_coin_value, 2);
-    // variable_item_set_current_value_index(item, value_index);
-    // variable_item_set_current_value_text(item, bip44_coin_text[value_index]);
+    // Passphrase
+    item = variable_item_list_add(
+        app->variable_item_list,
+        "BIP39 Passphrase:",
+        2,
+        flipbip_scene_settings_set_passphrase,
+        app);
+    value_index = value_index_uint32(app->passphrase, passphrase_value, 2);
+    variable_item_set_current_value_index(item, value_index);
+    variable_item_set_current_value_text(item, passphrase_text[value_index]);
 
     // Vibro on/off
     item = variable_item_list_add(

+ 17 - 9
views/flipbip_scene_1.c

@@ -298,7 +298,8 @@ static int flipbip_scene_1_model_init(
     FlipBipScene1Model* const model,
     const int strength,
     const uint32_t coin,
-    const bool overwrite) {
+    const bool overwrite,
+    const char* passphrase_text) {
     model->page = 0;
     model->mnemonic_only = false;
     model->strength = strength;
@@ -306,8 +307,8 @@ static int flipbip_scene_1_model_init(
     model->overwrite = overwrite;
 
     // Allocate memory for mnemonic
-    char* mnemonic = malloc(256);
-    memzero(mnemonic, 256);
+    char* mnemonic = malloc(TEXT_BUFFER_SIZE);
+    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(true) && !flipbip_has_settings(false))) {
@@ -335,7 +336,7 @@ static int flipbip_scene_1_model_init(
     //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";
 
     // Generate a BIP39 seed from the mnemonic
-    mnemonic_to_seed(model->mnemonic, "", model->seed, 0);
+    mnemonic_to_seed(model->mnemonic, passphrase_text, model->seed, 0);
 
     // Generate a BIP32 root HD node from the mnemonic
     HDNode* root = malloc(sizeof(HDNode));
@@ -509,12 +510,18 @@ void flipbip_scene_1_enter(void* context) {
     FlipBip* app = instance->context;
 
     // BIP39 Strength setting
-    int strength_setting = app->bip39_strength;
-    int strength = 256; // FlipBipStrength256                        // 24 words (256 bit)
-    if(strength_setting == FlipBipStrength128)
+    int strength = 256; // FlipBipStrength256 // 24 words (256 bit)
+    if(app->bip39_strength == FlipBipStrength128) {
         strength = 128; // 12 words (128 bit)
-    else if(strength_setting == FlipBipStrength192)
+    } else if(app->bip39_strength == FlipBipStrength192) {
         strength = 192; // 18 words (192 bit)
+    }
+
+    // BIP39 Passphrase setting
+    const char* passphrase_text = "";
+    if(app->passphrase == FlipBipPassphraseOn && strlen(app->passphrase_text) > 0) {
+        passphrase_text = app->passphrase_text;
+    }
 
     // BIP44 Coin setting
     uint32_t coin = app->bip44_coin;
@@ -536,7 +543,8 @@ void flipbip_scene_1_enter(void* context) {
         {
             // s_busy = true;
 
-            const int status = flipbip_scene_1_model_init(model, strength, coin, overwrite);
+            const int status =
+                flipbip_scene_1_model_init(model, strength, coin, overwrite, passphrase_text);
 
             // nonzero status, free the mnemonic
             if(status != 0) {