diff --git a/nym-wallet/src-tauri/src/main.rs b/nym-wallet/src-tauri/src/main.rs index 61346a19cf..5de1cd2aa7 100644 --- a/nym-wallet/src-tauri/src/main.rs +++ b/nym-wallet/src-tauri/src/main.rs @@ -38,14 +38,18 @@ fn main() { mixnet::account::validate_mnemonic, mixnet::account::create_new_account, mixnet::account::create_new_mnemonic, + mixnet::account::does_account_id_exist, mixnet::account::create_password, + mixnet::account::create_password_for_id, mixnet::account::does_password_file_exist, mixnet::account::get_balance, mixnet::account::get_validator_nymd_urls, mixnet::account::get_validator_api_urls, mixnet::account::logout, mixnet::account::remove_password, + mixnet::account::remove_password_for_id, mixnet::account::sign_in_with_password, + mixnet::account::sign_in_with_password_and_id, mixnet::account::switch_network, mixnet::account::update_validator_urls, mixnet::account::validate_mnemonic, diff --git a/nym-wallet/src-tauri/src/operations/mixnet/account.rs b/nym-wallet/src-tauri/src/operations/mixnet/account.rs index 12901cc046..c57e6588db 100644 --- a/nym-wallet/src-tauri/src/operations/mixnet/account.rs +++ b/nym-wallet/src-tauri/src/operations/mixnet/account.rs @@ -393,6 +393,14 @@ pub fn does_password_file_exist() -> Result { } } +#[tauri::command] +pub fn does_account_id_exist(id: String) -> Result { + let wallet = wallet_storage::load_existing_wallet()?; + let id = wallet_storage::WalletAccountId::new(id); + Ok(wallet.account_id_exists(&id)) +} + +// Will be deprecated once we support multiple accounts. #[tauri::command] pub fn create_password(mnemonic: String, password: String) -> Result<(), BackendError> { if does_password_file_exist()? { @@ -408,6 +416,21 @@ pub fn create_password(mnemonic: String, password: String) -> Result<(), Backend wallet_storage::store_wallet_login_information(mnemonic, hd_path, id, &password) } +#[tauri::command] +pub fn create_password_for_id( + id: String, + mnemonic: String, + password: String, +) -> Result<(), BackendError> { + log::info!("Creating password for: {id}"); + let mnemonic = Mnemonic::from_str(&mnemonic)?; + let hd_path: DerivationPath = COSMOS_DERIVATION_PATH.parse().unwrap(); + let id = wallet_storage::WalletAccountId::new(id); + let password = wallet_storage::UserPassword::new(password); + wallet_storage::store_wallet_login_information(mnemonic, hd_path, id, &password) +} + +// Deprecated as soon as we support multiple accounts #[tauri::command] pub async fn sign_in_with_password( password: String, @@ -422,9 +445,29 @@ pub async fn sign_in_with_password( _connect_with_mnemonic(stored_account.mnemonic().clone(), state).await } +#[tauri::command] +pub async fn sign_in_with_password_and_id( + id: String, + password: String, + state: tauri::State<'_, Arc>>, +) -> Result { + log::info!("Signing in with: {id}"); + let id = wallet_storage::WalletAccountId::new(id); + let password = wallet_storage::UserPassword::new(password); + let stored_account = wallet_storage::load_existing_wallet_login_information(&id, &password)?; + _connect_with_mnemonic(stored_account.mnemonic().clone(), state).await +} + #[tauri::command] pub fn remove_password() -> Result<(), BackendError> { log::info!("Removing password"); let id = wallet_storage::WalletAccountId::new(DEFAULT_WALLET_ACCOUNT_ID.to_string()); wallet_storage::remove_wallet_login_information(&id) } + +#[tauri::command] +pub fn remove_password_for_id(id: String) -> Result<(), BackendError> { + log::info!("Removing password"); + let id = wallet_storage::WalletAccountId::new(id); + wallet_storage::remove_wallet_login_information(&id) +} 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 760208b6b8..79daa7e111 100644 --- a/nym-wallet/src-tauri/src/wallet_storage/account_data.rs +++ b/nym-wallet/src-tauri/src/wallet_storage/account_data.rs @@ -44,6 +44,14 @@ impl StoredWallet { } } + pub fn account_id_exists(&self, id: &WalletAccountId) -> bool { + self + .accounts + .iter() + .find(|account| &account.id == id) + .is_some() + } + #[allow(unused)] pub fn encrypted_account_by_index(&self, index: usize) -> Option<&EncryptedAccount> { self.accounts.get(index) diff --git a/nym-wallet/src-tauri/src/wallet_storage/mod.rs b/nym-wallet/src-tauri/src/wallet_storage/mod.rs index 862e23e682..2f8a5cc2a4 100644 --- a/nym-wallet/src-tauri/src/wallet_storage/mod.rs +++ b/nym-wallet/src-tauri/src/wallet_storage/mod.rs @@ -30,8 +30,7 @@ pub(crate) fn wallet_login_filepath() -> Result { get_storage_directory().map(|dir| dir.join(WALLET_INFO_FILENAME)) } -#[allow(unused)] -pub(crate) fn load_existing_wallet(password: &UserPassword) -> Result { +pub(crate) fn load_existing_wallet() -> Result { let store_dir = get_storage_directory()?; let filepath = store_dir.join(WALLET_INFO_FILENAME); load_existing_wallet_at_file(filepath)