From 7e356ea3b3627b666c7c140a7bfa03e255abfbe4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=20H=C3=A4ggblad?= Date: Fri, 29 Apr 2022 16:49:57 +0200 Subject: [PATCH] wallet: simplify account handling --- .../src/operations/mixnet/account.rs | 2 +- .../src/wallet_storage/account_data.rs | 10 ++------- .../src-tauri/src/wallet_storage/mod.rs | 21 +++++-------------- 3 files changed, 8 insertions(+), 25 deletions(-) diff --git a/nym-wallet/src-tauri/src/operations/mixnet/account.rs b/nym-wallet/src-tauri/src/operations/mixnet/account.rs index 55b0ac30d0..5709dcdf58 100644 --- a/nym-wallet/src-tauri/src/operations/mixnet/account.rs +++ b/nym-wallet/src-tauri/src/operations/mixnet/account.rs @@ -445,7 +445,7 @@ pub async fn sign_in_with_password( { // Keep track of all accounts for that id let all_accounts: HashMap = stored_account - .into_multiple_accounts(id) + .unwrap_into_multiple_accounts(id) .into_accounts() .map(|a| (a.id.to_string(), a)) .collect(); 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 dca15df57e..b6b334e814 100644 --- a/nym-wallet/src-tauri/src/wallet_storage/account_data.rs +++ b/nym-wallet/src-tauri/src/wallet_storage/account_data.rs @@ -160,7 +160,7 @@ impl StoredLogin { } } - pub(crate) fn into_multiple_accounts(self, id: AccountId) -> MultipleAccounts { + pub(crate) fn unwrap_into_multiple_accounts(self, id: AccountId) -> MultipleAccounts { match self { StoredLogin::Mnemonic(ref account) => account.clone().into_multiple(id), StoredLogin::Multiple(ref accounts) => accounts.clone(), @@ -177,17 +177,11 @@ pub(crate) struct MnemonicAccount { } impl MnemonicAccount { - pub(crate) fn generate_new(&self) -> MnemonicAccount { - MnemonicAccount { - mnemonic: bip39::Mnemonic::generate(self.mnemonic().word_count()).unwrap(), - hd_path: self.hd_path().clone(), - } - } - pub(crate) fn mnemonic(&self) -> &bip39::Mnemonic { &self.mnemonic } + #[cfg(test)] pub(crate) fn hd_path(&self) -> &DerivationPath { &self.hd_path } diff --git a/nym-wallet/src-tauri/src/wallet_storage/mod.rs b/nym-wallet/src-tauri/src/wallet_storage/mod.rs index 82c7585d35..9dfaa74fa4 100644 --- a/nym-wallet/src-tauri/src/wallet_storage/mod.rs +++ b/nym-wallet/src-tauri/src/wallet_storage/mod.rs @@ -151,26 +151,15 @@ fn append_account_to_wallet_login_information_at_file( result => result?, }; - let mut decrypted_login = stored_wallet.decrypt_login(&id, password)?; + let decrypted_login = stored_wallet.decrypt_login(&id, password)?; - // WIP(JON): redo this since we now can clone the mnemonic - // Since we can't clone the mnemonic, we have to perform a little dance were we add the mnemonic - // to the inner enum payload, while also converting by swapping if necessary. - if let StoredLogin::Multiple(ref mut accounts) = decrypted_login { - accounts.add(inner_id, mnemonic, hd_path)?; - } else if let StoredLogin::Mnemonic(ref mut account) = decrypted_login { - // Move out the account by swapping, since we can't clone. - let account = std::mem::replace(account, account.generate_new()); - // Convert the enum variant - let mut accounts = account.into_multiple(id.clone()); - accounts.add(inner_id, mnemonic, hd_path)?; - // Overwrite the stored login with the new enum variant - decrypted_login = StoredLogin::Multiple(accounts); - } + // Add accounts to the inner structure + let mut accounts = decrypted_login.unwrap_into_multiple_accounts(id.clone()); + accounts.add(inner_id, mnemonic, hd_path)?; let encrypted_accounts = EncryptedLogin { id, - account: encrypt_struct(&decrypted_login, password)?, + account: encrypt_struct(&StoredLogin::Multiple(accounts), password)?, }; stored_wallet.replace_encrypted_login(encrypted_accounts)?;