wallet: general wallet_storage tidy

This commit is contained in:
Jon Häggblad
2022-03-21 11:07:21 +01:00
parent 07101a9dc5
commit cf7315f680
6 changed files with 26 additions and 22 deletions
@@ -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<bool, BackendError> {
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<RwLock<State>>>,
) -> Result<Account, BackendError> {
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
}
@@ -1,6 +1,9 @@
// Copyright 2022 - Nym Technologies SA <contact@nymtech.net>
// 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";
}
@@ -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
@@ -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)?;
@@ -1,10 +1,10 @@
// Copyright 2022 - Nym Technologies SA <contact@nymtech.net>
// 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<PathBuf, BackendError> {
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;
@@ -1,15 +1,13 @@
// Copyright 2022 - Nym Technologies SA <contact@nymtech.net>
// 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)
}