wallet: change default name of first account

This commit is contained in:
Jon Häggblad
2022-05-10 10:11:33 +02:00
parent ec19de6fa3
commit beeb67e9c2
2 changed files with 70 additions and 63 deletions
@@ -269,7 +269,7 @@ impl MultipleAccounts {
pub(crate) fn remove(&mut self, id: &AccountId) -> Result<(), BackendError> {
if self.get_account(id).is_none() {
return Err(BackendError::NoSuchIdInWalletLoginEntry);
return Err(BackendError::NoSuchIdInWalletLoginEntry);
}
self.accounts.retain(|accounts| &accounts.id != id);
Ok(())
+69 -62
View File
@@ -20,6 +20,10 @@ mod password;
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.
const DEFAULT_NAME_FIRST_ACCOUNT: &str = "Account 1";
fn get_storage_directory() -> Result<PathBuf, BackendError> {
tauri::api::path::local_data_dir()
.map(|dir| dir.join(STORAGE_DIR_NAME))
@@ -154,7 +158,8 @@ 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(id.clone());
let mut accounts = decrypted_login
.unwrap_into_multiple_accounts(AccountId::new(DEFAULT_NAME_FIRST_ACCOUNT.to_string()));
accounts.add(inner_id, mnemonic, hd_path)?;
let encrypted_accounts = EncryptedLogin {
@@ -711,17 +716,18 @@ mod tests {
let dummy_account1 = bip39::Mnemonic::generate(24).unwrap();
let dummy_account2 = bip39::Mnemonic::generate(24).unwrap();
let cosmos_hd_path: DerivationPath = COSMOS_DERIVATION_PATH.parse().unwrap();
let hd_path: DerivationPath = COSMOS_DERIVATION_PATH.parse().unwrap();
let password = UserPassword::new("password".to_string());
let id1 = AccountId::new("first".to_string());
let id2 = AccountId::new("second".to_string());
let default_id = AccountId::new(DEFAULT_NAME_FIRST_ACCOUNT.to_string());
store_wallet_login_information_at_file(
wallet_file.clone(),
dummy_account1.clone(),
cosmos_hd_path.clone(),
hd_path.clone(),
id1.clone(),
&password,
)
@@ -732,12 +738,12 @@ mod tests {
load_existing_wallet_login_information_at_file(wallet_file.clone(), &id1, &password).unwrap();
let acc1 = loaded_account.as_mnemonic_account().unwrap();
assert_eq!(acc1.mnemonic(), &dummy_account1);
assert_eq!(acc1.hd_path(), &cosmos_hd_path);
assert_eq!(acc1.hd_path(), &hd_path);
append_account_to_wallet_login_information_at_file(
wallet_file.clone(),
dummy_account2.clone(),
cosmos_hd_path.clone(),
hd_path.clone(),
id1.clone(),
id2.clone(),
&password,
@@ -749,8 +755,8 @@ mod tests {
load_existing_wallet_login_information_at_file(wallet_file, &id1, &password).unwrap();
let accounts = loaded_accounts.as_multiple_accounts().unwrap();
let expected = vec![
WalletAccount::new_mnemonic_backed_account(id1, dummy_account1, cosmos_hd_path.clone()),
WalletAccount::new_mnemonic_backed_account(id2, dummy_account2, cosmos_hd_path),
WalletAccount::new_mnemonic_backed_account(default_id, dummy_account1, hd_path.clone()),
WalletAccount::new_mnemonic_backed_account(id2, dummy_account2, hd_path),
]
.into();
assert_eq!(accounts, &expected);
@@ -765,7 +771,7 @@ mod tests {
let dummy_account2 = bip39::Mnemonic::generate(24).unwrap();
let dummy_account3 = bip39::Mnemonic::generate(24).unwrap();
let dummy_account4 = bip39::Mnemonic::generate(24).unwrap();
let cosmos_hd_path: DerivationPath = COSMOS_DERIVATION_PATH.parse().unwrap();
let hd_path: DerivationPath = COSMOS_DERIVATION_PATH.parse().unwrap();
let password = UserPassword::new("password".to_string());
@@ -773,11 +779,12 @@ mod tests {
let id2 = AccountId::new("second".to_string());
let id3 = AccountId::new("third".to_string());
let id4 = AccountId::new("fourth".to_string());
let default_id = AccountId::new(DEFAULT_NAME_FIRST_ACCOUNT.to_string());
store_wallet_login_information_at_file(
wallet_file.clone(),
dummy_account1.clone(),
cosmos_hd_path.clone(),
hd_path.clone(),
id1.clone(),
&password,
)
@@ -786,7 +793,7 @@ mod tests {
store_wallet_login_information_at_file(
wallet_file.clone(),
dummy_account2.clone(),
cosmos_hd_path.clone(),
hd_path.clone(),
id2.clone(),
&password,
)
@@ -797,13 +804,13 @@ mod tests {
load_existing_wallet_login_information_at_file(wallet_file.clone(), &id2, &password).unwrap();
let acc2 = loaded_account.as_mnemonic_account().unwrap();
assert_eq!(acc2.mnemonic(), &dummy_account2);
assert_eq!(acc2.hd_path(), &cosmos_hd_path);
assert_eq!(acc2.hd_path(), &hd_path);
// Add a third and fourth mnenonic grouped together with the second one
append_account_to_wallet_login_information_at_file(
wallet_file.clone(),
dummy_account3.clone(),
cosmos_hd_path.clone(),
hd_path.clone(),
id2.clone(),
id3.clone(),
&password,
@@ -812,7 +819,7 @@ mod tests {
append_account_to_wallet_login_information_at_file(
wallet_file.clone(),
dummy_account4.clone(),
cosmos_hd_path.clone(),
hd_path.clone(),
id2.clone(),
id4.clone(),
&password,
@@ -824,15 +831,15 @@ mod tests {
load_existing_wallet_login_information_at_file(wallet_file.clone(), &id1, &password).unwrap();
let acc1 = loaded_account.as_mnemonic_account().unwrap();
assert_eq!(acc1.mnemonic(), &dummy_account1);
assert_eq!(acc1.hd_path(), &cosmos_hd_path);
assert_eq!(acc1.hd_path(), &hd_path);
let loaded_accounts =
load_existing_wallet_login_information_at_file(wallet_file, &id2, &password).unwrap();
let accounts = loaded_accounts.as_multiple_accounts().unwrap();
let expected = vec![
WalletAccount::new_mnemonic_backed_account(id2, dummy_account2, cosmos_hd_path.clone()),
WalletAccount::new_mnemonic_backed_account(id3, dummy_account3, cosmos_hd_path.clone()),
WalletAccount::new_mnemonic_backed_account(id4, dummy_account4, cosmos_hd_path),
WalletAccount::new_mnemonic_backed_account(default_id, dummy_account2, hd_path.clone()),
WalletAccount::new_mnemonic_backed_account(id3, dummy_account3, hd_path.clone()),
WalletAccount::new_mnemonic_backed_account(id4, dummy_account4, hd_path),
]
.into();
assert_eq!(accounts, &expected);
@@ -887,18 +894,19 @@ mod tests {
let dummy_account1 = bip39::Mnemonic::generate(24).unwrap();
let dummy_account2 = bip39::Mnemonic::generate(24).unwrap();
let dummy_account3 = bip39::Mnemonic::generate(24).unwrap();
let cosmos_hd_path: DerivationPath = COSMOS_DERIVATION_PATH.parse().unwrap();
let hd_path: DerivationPath = COSMOS_DERIVATION_PATH.parse().unwrap();
let password = UserPassword::new("password".to_string());
let id1 = AccountId::new("first".to_string());
let id2 = AccountId::new("second".to_string());
let id3 = AccountId::new("third".to_string());
let default_id = AccountId::new(DEFAULT_NAME_FIRST_ACCOUNT.to_string());
store_wallet_login_information_at_file(
wallet_file.clone(),
dummy_account1,
cosmos_hd_path.clone(),
hd_path.clone(),
id1.clone(),
&password,
)
@@ -907,7 +915,7 @@ mod tests {
store_wallet_login_information_at_file(
wallet_file.clone(),
dummy_account2.clone(),
cosmos_hd_path.clone(),
hd_path.clone(),
id2.clone(),
&password,
)
@@ -916,7 +924,7 @@ mod tests {
append_account_to_wallet_login_information_at_file(
wallet_file.clone(),
dummy_account3.clone(),
cosmos_hd_path.clone(),
hd_path.clone(),
id2.clone(),
id3.clone(),
&password,
@@ -930,8 +938,8 @@ mod tests {
load_existing_wallet_login_information_at_file(wallet_file, &id2, &password).unwrap();
let accounts = loaded_accounts.as_multiple_accounts().unwrap();
let expected = vec![
WalletAccount::new_mnemonic_backed_account(id2, dummy_account2, cosmos_hd_path.clone()),
WalletAccount::new_mnemonic_backed_account(id3, dummy_account3, cosmos_hd_path),
WalletAccount::new_mnemonic_backed_account(default_id, dummy_account2, hd_path.clone()),
WalletAccount::new_mnemonic_backed_account(id3, dummy_account3, hd_path),
]
.into();
assert_eq!(accounts, &expected);
@@ -940,7 +948,7 @@ mod tests {
#[test]
fn remove_all_accounts_for_a_login_removes_the_file_when_empty() {
let store_dir = tempdir().unwrap();
let wallet_file = store_dir.path().join(WALLET_INFO_FILENAME);
let wallet = store_dir.path().join(WALLET_INFO_FILENAME);
let dummy_account1 = bip39::Mnemonic::generate(24).unwrap();
let dummy_account2 = bip39::Mnemonic::generate(24).unwrap();
@@ -950,9 +958,10 @@ mod tests {
let id1 = AccountId::new("first".to_string());
let id2 = AccountId::new("second".to_string());
let default_id = AccountId::new(DEFAULT_NAME_FIRST_ACCOUNT.to_string());
store_wallet_login_information_at_file(
wallet_file.clone(),
wallet.clone(),
dummy_account1,
cosmos_hd_path.clone(),
id1.clone(),
@@ -961,7 +970,7 @@ mod tests {
.unwrap();
append_account_to_wallet_login_information_at_file(
wallet_file.clone(),
wallet.clone(),
dummy_account2,
cosmos_hd_path,
id1.clone(),
@@ -970,15 +979,15 @@ mod tests {
)
.unwrap();
remove_account_from_wallet_login_at_file(wallet_file.clone(), &id1, &id1, &password).unwrap();
remove_account_from_wallet_login_at_file(wallet_file.clone(), &id1, &id2, &password).unwrap();
remove_account_from_wallet_login_at_file(wallet.clone(), &id1, &default_id, &password).unwrap();
remove_account_from_wallet_login_at_file(wallet.clone(), &id1, &id2, &password).unwrap();
// The file should now be removed
assert!(!wallet_file.exists());
assert!(!wallet.exists());
// And trying to load when the file is gone fails
assert!(matches!(
load_existing_wallet_login_information_at_file(wallet_file, &id1, &password),
load_existing_wallet_login_information_at_file(wallet, &id1, &password),
Err(BackendError::WalletFileNotFound),
));
}
@@ -986,7 +995,7 @@ mod tests {
#[test]
fn remove_all_accounts_for_a_login_removes_that_login() {
let store_dir = tempdir().unwrap();
let wallet_file = store_dir.path().join(WALLET_INFO_FILENAME);
let wallet = store_dir.path().join(WALLET_INFO_FILENAME);
let dummy_account1 = bip39::Mnemonic::generate(24).unwrap();
let dummy_account2 = bip39::Mnemonic::generate(24).unwrap();
@@ -998,9 +1007,10 @@ mod tests {
let id1 = AccountId::new("first".to_string());
let id2 = AccountId::new("second".to_string());
let id3 = AccountId::new("third".to_string());
let default_id = AccountId::new(DEFAULT_NAME_FIRST_ACCOUNT.to_string());
store_wallet_login_information_at_file(
wallet_file.clone(),
wallet.clone(),
dummy_account1,
cosmos_hd_path.clone(),
id1.clone(),
@@ -1009,7 +1019,7 @@ mod tests {
.unwrap();
append_account_to_wallet_login_information_at_file(
wallet_file.clone(),
wallet.clone(),
dummy_account2,
cosmos_hd_path.clone(),
id1.clone(),
@@ -1019,7 +1029,7 @@ mod tests {
.unwrap();
store_wallet_login_information_at_file(
wallet_file.clone(),
wallet.clone(),
dummy_account3.clone(),
cosmos_hd_path.clone(),
id3.clone(),
@@ -1027,18 +1037,18 @@ mod tests {
)
.unwrap();
remove_account_from_wallet_login_at_file(wallet_file.clone(), &id1, &id1, &password).unwrap();
remove_account_from_wallet_login_at_file(wallet_file.clone(), &id1, &id2, &password).unwrap();
remove_account_from_wallet_login_at_file(wallet.clone(), &id1, &default_id, &password).unwrap();
remove_account_from_wallet_login_at_file(wallet.clone(), &id1, &id2, &password).unwrap();
// And trying to load when the file is gone fails
assert!(matches!(
load_existing_wallet_login_information_at_file(wallet_file.clone(), &id1, &password),
load_existing_wallet_login_information_at_file(wallet.clone(), &id1, &password),
Err(BackendError::NoSuchIdInWallet),
));
// The other login is still there
let loaded_account =
load_existing_wallet_login_information_at_file(wallet_file, &id3, &password).unwrap();
load_existing_wallet_login_information_at_file(wallet, &id3, &password).unwrap();
let acc3 = loaded_account.as_mnemonic_account().unwrap();
assert_eq!(acc3.mnemonic(), &dummy_account3);
assert_eq!(acc3.hd_path(), &cosmos_hd_path);
@@ -1047,13 +1057,13 @@ mod tests {
#[test]
fn append_accounts_and_remove_appended_accounts() {
let store_dir = tempdir().unwrap();
let wallet_file = store_dir.path().join(WALLET_INFO_FILENAME);
let wallet = store_dir.path().join(WALLET_INFO_FILENAME);
let dummy_account1 = bip39::Mnemonic::generate(24).unwrap();
let dummy_account2 = bip39::Mnemonic::generate(24).unwrap();
let dummy_account3 = bip39::Mnemonic::generate(24).unwrap();
let dummy_account4 = bip39::Mnemonic::generate(24).unwrap();
let cosmos_hd_path: DerivationPath = COSMOS_DERIVATION_PATH.parse().unwrap();
let hd_path: DerivationPath = COSMOS_DERIVATION_PATH.parse().unwrap();
let password = UserPassword::new("password".to_string());
@@ -1061,20 +1071,21 @@ mod tests {
let id2 = AccountId::new("second".to_string());
let id3 = AccountId::new("third".to_string());
let id4 = AccountId::new("fourth".to_string());
let default_id = AccountId::new(DEFAULT_NAME_FIRST_ACCOUNT.to_string());
store_wallet_login_information_at_file(
wallet_file.clone(),
wallet.clone(),
dummy_account1.clone(),
cosmos_hd_path.clone(),
hd_path.clone(),
id1.clone(),
&password,
)
.unwrap();
store_wallet_login_information_at_file(
wallet_file.clone(),
wallet.clone(),
dummy_account2.clone(),
cosmos_hd_path.clone(),
hd_path.clone(),
id2.clone(),
&password,
)
@@ -1082,18 +1093,18 @@ mod tests {
// Add a third and fourth mnenonic grouped together with the second one
append_account_to_wallet_login_information_at_file(
wallet_file.clone(),
wallet.clone(),
dummy_account3,
cosmos_hd_path.clone(),
hd_path.clone(),
id2.clone(),
id3.clone(),
&password,
)
.unwrap();
append_account_to_wallet_login_information_at_file(
wallet_file.clone(),
wallet.clone(),
dummy_account4.clone(),
cosmos_hd_path.clone(),
hd_path.clone(),
id2.clone(),
id4.clone(),
&password,
@@ -1101,41 +1112,37 @@ mod tests {
.unwrap();
// Delete the third mnemonic, from the second login entry
remove_account_from_wallet_login_at_file(wallet_file.clone(), &id2, &id3, &password).unwrap();
remove_account_from_wallet_login_at_file(wallet.clone(), &id2, &id3, &password).unwrap();
// Check that we can still load the other accounts
let loaded_accounts =
load_existing_wallet_login_information_at_file(wallet_file.clone(), &id2, &password).unwrap();
load_existing_wallet_login_information_at_file(wallet.clone(), &id2, &password).unwrap();
let accounts = loaded_accounts.as_multiple_accounts().unwrap();
let expected = vec![
WalletAccount::new_mnemonic_backed_account(
id2.clone(),
default_id.clone(),
dummy_account2,
cosmos_hd_path.clone(),
),
WalletAccount::new_mnemonic_backed_account(
id4.clone(),
dummy_account4,
cosmos_hd_path.clone(),
hd_path.clone(),
),
WalletAccount::new_mnemonic_backed_account(id4.clone(), dummy_account4, hd_path.clone()),
]
.into();
assert_eq!(accounts, &expected);
// Delete the second and fourth mnemonic from the second login entry removes the login entry
remove_account_from_wallet_login_at_file(wallet_file.clone(), &id2, &id2, &password).unwrap();
remove_account_from_wallet_login_at_file(wallet_file.clone(), &id2, &id4, &password).unwrap();
remove_account_from_wallet_login_at_file(wallet.clone(), &id2, &default_id, &password).unwrap();
remove_account_from_wallet_login_at_file(wallet.clone(), &id2, &id4, &password).unwrap();
assert!(matches!(
load_existing_wallet_login_information_at_file(wallet_file.clone(), &id2, &password),
load_existing_wallet_login_information_at_file(wallet.clone(), &id2, &password),
Err(BackendError::NoSuchIdInWallet),
));
// The first login is still available
let loaded_account =
load_existing_wallet_login_information_at_file(wallet_file, &id1, &password).unwrap();
load_existing_wallet_login_information_at_file(wallet, &id1, &password).unwrap();
let acc1 = loaded_account.as_mnemonic_account().unwrap();
assert_eq!(acc1.mnemonic(), &dummy_account1);
assert_eq!(acc1.hd_path(), &cosmos_hd_path);
assert_eq!(acc1.hd_path(), &hd_path);
}
// Test to that decrypts a stored file from the repo, to make sure we are able to decrypt stored