From e026a532dd2c9f8a05c478c2dc804a3d56f48abe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=20H=C3=A4ggblad?= Date: Wed, 4 May 2022 16:50:01 +0200 Subject: [PATCH] wallet: store unlocked accounts in Vec to preserve order --- .../src-tauri/src/operations/mixnet/account.rs | 15 ++++++--------- nym-wallet/src-tauri/src/state.rs | 11 +++++------ 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/nym-wallet/src-tauri/src/operations/mixnet/account.rs b/nym-wallet/src-tauri/src/operations/mixnet/account.rs index e146983df7..70cda15f9d 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 as WalletNetwork; use crate::nymd_client; use crate::state::State; -use crate::wallet_storage::account_data::{StoredLogin, WalletAccount}; +use crate::wallet_storage::account_data::StoredLogin; use crate::wallet_storage::{self, DEFAULT_WALLET_ACCOUNT_ID}; use bip39::{Language, Mnemonic}; @@ -444,10 +444,9 @@ pub async fn sign_in_with_password( { // Keep track of all accounts for that id - let all_accounts: HashMap = stored_account + let all_accounts: Vec<_> = stored_account .unwrap_into_multiple_accounts(id) .into_accounts() - .map(|a| (a.id.to_string(), a)) .collect(); let mut w_state = state.write().await; @@ -514,7 +513,6 @@ pub async fn list_accounts( let all_accounts = state .get_all_accounts() - .values() .map(|account| AccountEntry { id: account.id.to_string(), address: derive_address(account.account.mnemonic().clone(), prefix) @@ -558,13 +556,12 @@ pub async fn sign_in_decrypted_account( log::info!("Signing in to already decrypted account: {account_id}"); let mnemonic = { let state = state.read().await; - state + let account = &state .get_all_accounts() - .get(account_id) + .find(|a| a.id.as_ref() == account_id) .ok_or(BackendError::NoSuchIdInWalletLoginEntry)? - .account - .mnemonic() - .clone() + .account; + account.mnemonic().clone() }; _connect_with_mnemonic(mnemonic, state).await } diff --git a/nym-wallet/src-tauri/src/state.rs b/nym-wallet/src-tauri/src/state.rs index ae9f2c07e0..b66eb09515 100644 --- a/nym-wallet/src-tauri/src/state.rs +++ b/nym-wallet/src-tauri/src/state.rs @@ -21,7 +21,7 @@ pub struct State { // All the accounts the we get from decrypting the wallet. We hold on to these for being able to // switch accounts on-the-fly - all_accounts: HashMap, + all_accounts: Vec, /// Validators that have been fetched dynamically, probably during startup. fetched_validators: OptionalValidators, @@ -68,13 +68,12 @@ impl State { self.current_network } - pub(crate) fn set_all_accounts(&mut self, all_accounts: HashMap) { - self.all_accounts.clear(); - self.all_accounts.extend(all_accounts) + pub(crate) fn set_all_accounts(&mut self, all_accounts: Vec) { + self.all_accounts = all_accounts } - pub(crate) fn get_all_accounts(&self) -> &HashMap { - &self.all_accounts + pub(crate) fn get_all_accounts(&self) -> impl Iterator { + self.all_accounts.iter() } pub fn logout(&mut self) {