From cf7315f680db02f9caff957b46f0bb6cd151bdb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=20H=C3=A4ggblad?= Date: Mon, 21 Mar 2022 11:07:21 +0100 Subject: [PATCH] wallet: general wallet_storage tidy --- .../src/operations/mixnet/account.rs | 21 ++++++++----------- .../src-tauri/src/platform_constants.rs | 4 ++++ .../src/wallet_storage/account_data.rs | 11 +++++++--- .../src/wallet_storage/encryption.rs | 1 - .../src-tauri/src/wallet_storage/mod.rs | 7 ++++--- .../src-tauri/src/wallet_storage/password.rs | 4 +--- 6 files changed, 26 insertions(+), 22 deletions(-) diff --git a/nym-wallet/src-tauri/src/operations/mixnet/account.rs b/nym-wallet/src-tauri/src/operations/mixnet/account.rs index 63ada03ad7..f448fb4e8e 100644 --- a/nym-wallet/src-tauri/src/operations/mixnet/account.rs +++ b/nym-wallet/src-tauri/src/operations/mixnet/account.rs @@ -4,7 +4,7 @@ use crate::error::BackendError; use crate::network::Network; use crate::nymd_client; use crate::state::State; -use crate::wallet_storage; +use crate::wallet_storage::{self, DEFAULT_WALLET_ID}; use bip39::{Language, Mnemonic}; use config::defaults::COSMOS_DERIVATION_PATH; @@ -328,10 +328,11 @@ pub fn does_password_file_exist() -> Result { let file = wallet_storage::wallet_login_filepath()?; if file.exists() { log::info!("Exists: {}", file.to_string_lossy()); + Ok(true) } else { log::info!("Does not exist: {}", file.to_string_lossy()); + Ok(false) } - Ok(file.exists()) } #[tauri::command] @@ -343,10 +344,9 @@ pub fn create_password(mnemonic: String, password: String) -> Result<(), Backend let mnemonic = Mnemonic::from_str(&mnemonic)?; let hd_path: DerivationPath = COSMOS_DERIVATION_PATH.parse().unwrap(); + // Currently we only support a single, default, id in the wallet + let id = wallet_storage::UserId::new(DEFAULT_WALLET_ID.to_string()); let password = wallet_storage::UserPassword::new(password); - // WIP(JON): extract this one out - let id = wallet_storage::UserId::new("default".to_string()); - wallet_storage::store_wallet_login_information(mnemonic, hd_path, id, &password) } @@ -356,13 +356,10 @@ pub async fn sign_in_with_password( state: tauri::State<'_, Arc>>, ) -> Result { log::info!("Signing in with password"); + + // Currently we only support a single, default, id in the wallet + let id = wallet_storage::UserId::new(DEFAULT_WALLET_ID.to_string()); let password = wallet_storage::UserPassword::new(password); - let id = wallet_storage::UserId::new("default".to_string()); let stored_account = wallet_storage::load_existing_wallet_login_information(&id, &password)?; - - let mnemonic = match stored_account { - wallet_storage::account_data::StoredAccount::Mnemonic(ref mn) => mn.mnemonic().clone(), - }; - - _connect_with_mnemonic(mnemonic, state).await + _connect_with_mnemonic(stored_account.mnemonic().clone(), state).await } diff --git a/nym-wallet/src-tauri/src/platform_constants.rs b/nym-wallet/src-tauri/src/platform_constants.rs index 5565a8ac96..309a5fee7c 100644 --- a/nym-wallet/src-tauri/src/platform_constants.rs +++ b/nym-wallet/src-tauri/src/platform_constants.rs @@ -1,6 +1,9 @@ // Copyright 2022 - Nym Technologies SA // SPDX-License-Identifier: Apache-2.0 +// Specify filenames and other platform specific constants to respect platform conventions, or at +// least, something popular on each respective platform. + cfg_if::cfg_if! { if #[cfg(target_os = "linux")] { pub const STORAGE_DIR_NAME: &str = "nym-wallet"; @@ -12,6 +15,7 @@ cfg_if::cfg_if! { pub const STORAGE_DIR_NAME: &str = "NymWallet"; pub const WALLET_INFO_FILENAME: &str = "saved_wallet.json"; } else { + // This case is likely to be a unix-y system pub const STORAGE_DIR_NAME: &str = "nym-wallet"; pub const WALLET_INFO_FILENAME: &str = "saved-wallet.json"; } diff --git a/nym-wallet/src-tauri/src/wallet_storage/account_data.rs b/nym-wallet/src-tauri/src/wallet_storage/account_data.rs index dc6a4e5835..ef641f311d 100644 --- a/nym-wallet/src-tauri/src/wallet_storage/account_data.rs +++ b/nym-wallet/src-tauri/src/wallet_storage/account_data.rs @@ -92,6 +92,14 @@ impl StoredAccount { ) -> StoredAccount { StoredAccount::Mnemonic(MnemonicAccount { mnemonic, hd_path }) } + + // If we add accounts backed by something that is not a mnemonic, this should probably be changed + // to return `Option<..>`. + pub(crate) fn mnemonic(&self) -> &bip39::Mnemonic { + match self { + StoredAccount::Mnemonic(account) => account.mnemonic(), + } + } } #[derive(Serialize, Deserialize, Debug)] @@ -101,9 +109,6 @@ pub(crate) struct MnemonicAccount { hd_path: DerivationPath, } -// we only ever want to expose those getters in the test code -// WIP(JON): temporarily comment out -//#[cfg(test)] impl MnemonicAccount { pub(crate) fn mnemonic(&self) -> &bip39::Mnemonic { &self.mnemonic diff --git a/nym-wallet/src-tauri/src/wallet_storage/encryption.rs b/nym-wallet/src-tauri/src/wallet_storage/encryption.rs index fbac118f89..a8aa27fe84 100644 --- a/nym-wallet/src-tauri/src/wallet_storage/encryption.rs +++ b/nym-wallet/src-tauri/src/wallet_storage/encryption.rs @@ -182,7 +182,6 @@ where T: Serialize, { let bytes = serde_json::to_vec(data).map_err(|_| BackendError::EncryptionError)?; - //dbg!(&bytes); let (salt, iv) = random_salt_and_iv(); let ciphertext = encrypt(&bytes, password, &salt, &iv)?; diff --git a/nym-wallet/src-tauri/src/wallet_storage/mod.rs b/nym-wallet/src-tauri/src/wallet_storage/mod.rs index a4720e13e3..e899a4b1e3 100644 --- a/nym-wallet/src-tauri/src/wallet_storage/mod.rs +++ b/nym-wallet/src-tauri/src/wallet_storage/mod.rs @@ -1,10 +1,10 @@ // Copyright 2022 - Nym Technologies SA // SPDX-License-Identifier: Apache-2.0 -use crate::operations::mixnet::account::create_new_account; pub(crate) use crate::wallet_storage::password::{UserId, UserPassword}; use crate::error::BackendError; +use crate::operations::mixnet::account::create_new_account; use crate::platform_constants::{STORAGE_DIR_NAME, WALLET_INFO_FILENAME}; use crate::wallet_storage::account_data::StoredAccount; use crate::wallet_storage::encryption::{encrypt_struct, EncryptedData}; @@ -20,6 +20,8 @@ pub(crate) mod encryption; mod password; +pub(crate) const DEFAULT_WALLET_ID: &str = "default"; + fn get_storage_directory() -> Result { tauri::api::path::local_data_dir() .map(|dir| dir.join(STORAGE_DIR_NAME)) @@ -117,9 +119,8 @@ fn store_wallet_login_information_at_file( #[cfg(test)] mod tests { - use crate::wallet_storage::encryption::encrypt_data; - use super::*; + use crate::wallet_storage::encryption::encrypt_data; use config::defaults::COSMOS_DERIVATION_PATH; use std::path::Path; use tempfile::tempdir; diff --git a/nym-wallet/src-tauri/src/wallet_storage/password.rs b/nym-wallet/src-tauri/src/wallet_storage/password.rs index eb4025cdf8..8e1f9face9 100644 --- a/nym-wallet/src-tauri/src/wallet_storage/password.rs +++ b/nym-wallet/src-tauri/src/wallet_storage/password.rs @@ -1,15 +1,13 @@ // Copyright 2022 - Nym Technologies SA // SPDX-License-Identifier: Apache-2.0 -use zeroize::Zeroize; - use serde::{Deserialize, Deserializer, Serialize, Serializer}; +use zeroize::Zeroize; #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] pub(crate) struct UserId(String); impl UserId { - // WIP(JON): consider making inner String pub pub(crate) fn new(id: String) -> UserId { UserId(id) }