account_manager: don't add the same pubkey more than once

If we are passing keys on the command line, let's make sure
we aren't adding duplicates when we integrate the keystore.

Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
William Casarin 2024-07-01 06:28:48 -07:00
parent 307b8af8f1
commit 1810515ad2

View File

@ -4,6 +4,7 @@ use enostr::Keypair;
use crate::key_storage::KeyStorage; use crate::key_storage::KeyStorage;
pub use crate::user_account::UserAccount; pub use crate::user_account::UserAccount;
use tracing::info;
/// The interface for managing the user's accounts. /// The interface for managing the user's accounts.
/// Represents all user-facing operations related to account management. /// Represents all user-facing operations related to account management.
@ -55,9 +56,24 @@ impl AccountManager {
} }
} }
pub fn add_account(&mut self, account: Keypair) { pub fn has_account_pubkey(&self, pubkey: &[u8; 32]) -> bool {
for account in &self.accounts {
if account.pubkey.bytes() == pubkey {
return true;
}
}
false
}
pub fn add_account(&mut self, account: Keypair) -> bool {
if self.has_account_pubkey(account.pubkey.bytes()) {
info!("already have account, not adding {}", account.pubkey);
return false;
}
let _ = self.key_store.add_key(&account); let _ = self.key_store.add_key(&account);
self.accounts.push(account) self.accounts.push(account);
true
} }
pub fn num_accounts(&self) -> usize { pub fn num_accounts(&self) -> usize {