wallet: simplify account handling

This commit is contained in:
Jon Häggblad
2022-04-29 16:49:57 +02:00
parent f7be9e7e6f
commit 7e356ea3b3
3 changed files with 8 additions and 25 deletions
@@ -445,7 +445,7 @@ pub async fn sign_in_with_password(
{
// Keep track of all accounts for that id
let all_accounts: HashMap<String, WalletAccount> = stored_account
.into_multiple_accounts(id)
.unwrap_into_multiple_accounts(id)
.into_accounts()
.map(|a| (a.id.to_string(), a))
.collect();
@@ -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
}
+5 -16
View File
@@ -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)?;