wallet: revert back to old default account name for the time being

This commit is contained in:
Jon Häggblad
2022-05-11 14:42:26 +02:00
parent b83d4ca1a3
commit 54e95d795e
3 changed files with 50 additions and 23 deletions
@@ -387,9 +387,12 @@ pub async fn sign_in_with_password(
let id = wallet_storage::AccountId::new(DEFAULT_WALLET_ACCOUNT_ID.to_string());
let password = wallet_storage::UserPassword::new(password);
let stored_account = wallet_storage::load_existing_wallet_login_information(&id, &password)?;
let (mnemonic, all_accounts) = extract_mnemonic_and_all_accounts(stored_account)?;
let (mnemonic, all_accounts) = extract_first_mnemonic_and_all_accounts(stored_account, id)?;
{
for account in &all_accounts {
log::trace!("account: {:?}", account);
}
let mut w_state = state.write().await;
w_state.set_all_accounts(all_accounts);
}
@@ -397,8 +400,9 @@ pub async fn sign_in_with_password(
_connect_with_mnemonic(mnemonic, state).await
}
fn extract_mnemonic_and_all_accounts(
fn extract_first_mnemonic_and_all_accounts(
stored_account: StoredLogin,
login_id: wallet_storage::AccountId,
) -> Result<(Mnemonic, Vec<wallet_storage::WalletAccount>), BackendError> {
let mnemonic = match stored_account {
StoredLogin::Mnemonic(ref account) => account.mnemonic().clone(),
@@ -416,10 +420,9 @@ fn extract_mnemonic_and_all_accounts(
// Keep track of all accounts for that id
let all_accounts: Vec<_> = stored_account
.unwrap_into_multiple_accounts()
.unwrap_into_multiple_accounts(login_id)
.into_accounts()
.collect();
Ok((mnemonic, all_accounts))
}
@@ -477,7 +480,7 @@ async fn reset_state_with_all_accounts_from_file(
) -> Result<(), BackendError> {
let stored_account = wallet_storage::load_existing_wallet_login_information(id, password)?;
let all_accounts: Vec<_> = stored_account
.unwrap_into_multiple_accounts()
.unwrap_into_multiple_accounts(id.clone())
.into_accounts()
.collect();
@@ -518,6 +521,7 @@ fn derive_address(
pub async fn list_accounts(
state: tauri::State<'_, Arc<RwLock<State>>>,
) -> Result<Vec<AccountEntry>, BackendError> {
log::trace!("Listing accounts");
let state = state.read().await;
let network: Network = state.current_network().into();
let prefix = network.bech32_prefix();
@@ -530,6 +534,10 @@ pub async fn list_accounts(
.unwrap()
.to_string(),
})
.map(|account| {
log::trace!("{:?}", account);
account
})
.collect();
Ok(all_accounts)
@@ -547,13 +555,18 @@ pub fn show_mnemonic_for_account_in_password(
let stored_account = wallet_storage::load_existing_wallet_login_information(&id, &password)?;
let mnemonic = match stored_account {
StoredLogin::Mnemonic(_) => return Err(BackendError::WalletUnexpectedMnemonicAccount),
StoredLogin::Multiple(ref accounts) => accounts
.get_account(&account_id)
.ok_or(BackendError::NoSuchIdInWalletLoginEntry)?
.account
.mnemonic()
.clone(),
StoredLogin::Mnemonic(ref account) => account.mnemonic().clone(),
StoredLogin::Multiple(ref accounts) => {
for account in accounts.get_accounts() {
log::debug!("{:?}", account);
}
accounts
.get_account(&account_id)
.ok_or(BackendError::NoSuchIdInWalletLoginEntry)?
.account
.mnemonic()
.clone()
}
};
Ok(mnemonic.to_string())
@@ -599,7 +612,7 @@ mod tests {
wallet_storage::load_existing_wallet_login_information_at_file(wallet_file, &id, &password)
.unwrap();
let (mnemonic, all_accounts) =
extract_mnemonic_and_all_accounts(stored_account, id.clone()).unwrap();
extract_first_mnemonic_and_all_accounts(&stored_account, id.clone()).unwrap();
let expected_mnemonic = bip39::Mnemonic::from_str("country mean universe text phone begin deputy reject result good cram illness common cluster proud swamp digital patrol spread bar face december base kick").unwrap();
assert_eq!(mnemonic, expected_mnemonic);
@@ -1,6 +1,19 @@
// Copyright 2022 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: Apache-2.0
// The wallet storage is a single json file, containing multiple entries. These are referred to as
// Logins, and has a plaintext id tag attached.
//
// Each encrypted login contains either a single account, or a list of multiple accounts.
//
// NOTE: A not insignificant amount of complexity comes from being able to handle both these cases,
// instead of, for example, converting a single account to a list of multiple accounts with a single
// entry. This also avoids resaving the wallet file when opening a file created with an earlier
// version of the wallet.
//
// In the future we might want to simplify by dropping the support for a single account entry,
// instead treating as muliple accounts with one entry.
use cosmrs::bip32::DerivationPath;
use serde::{Deserialize, Serialize};
use zeroize::Zeroize;
@@ -10,7 +23,6 @@ use crate::error::BackendError;
use super::encryption::EncryptedData;
use super::password::AccountId;
use super::UserPassword;
use super::DEFAULT_NAME_FIRST_ACCOUNT;
const CURRENT_WALLET_FILE_VERSION: u32 = 1;
@@ -161,11 +173,9 @@ impl StoredLogin {
}
}
pub(crate) fn unwrap_into_multiple_accounts(self) -> MultipleAccounts {
pub(crate) fn unwrap_into_multiple_accounts(self, id: AccountId) -> MultipleAccounts {
match self {
StoredLogin::Mnemonic(ref account) => account
.clone()
.into_multiple(AccountId::new(DEFAULT_NAME_FIRST_ACCOUNT.to_string())),
StoredLogin::Mnemonic(ref account) => account.clone().into_multiple(id),
StoredLogin::Multiple(ref accounts) => accounts.clone(),
}
}
@@ -189,11 +199,15 @@ impl MnemonicAccount {
&self.hd_path
}
pub(crate) fn into_multiple(self, id: AccountId) -> MultipleAccounts {
MultipleAccounts::new(WalletAccount {
pub(crate) fn into_wallet_account(self, id: AccountId) -> WalletAccount {
WalletAccount {
id,
account: self.into(),
})
}
}
pub(crate) fn into_multiple(self, id: AccountId) -> MultipleAccounts {
MultipleAccounts::new(self.into_wallet_account(id))
}
}
@@ -22,7 +22,7 @@ pub(crate) const DEFAULT_WALLET_ACCOUNT_ID: &str = "default";
/// When converting a single account entry to one that contains many, the first account will use
/// this name.
pub(crate) const DEFAULT_NAME_FIRST_ACCOUNT: &str = "Account 1";
//pub(crate) const DEFAULT_NAME_FIRST_ACCOUNT: &str = "Account 1";
fn get_storage_directory() -> Result<PathBuf, BackendError> {
tauri::api::path::local_data_dir()
@@ -158,7 +158,7 @@ fn append_account_to_wallet_login_information_at_file(
let decrypted_login = stored_wallet.decrypt_login(&id, password)?;
// Add accounts to the inner structure
let mut accounts = decrypted_login.unwrap_into_multiple_accounts();
let mut accounts = decrypted_login.unwrap_into_multiple_accounts(id.clone());
accounts.add(inner_id, mnemonic, hd_path)?;
let encrypted_accounts = EncryptedLogin {