From 1810515ad210a75baa90818ffa673bf51b1d1271 Mon Sep 17 00:00:00 2001 From: William Casarin Date: Mon, 1 Jul 2024 06:28:48 -0700 Subject: [PATCH] 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 --- src/account_manager.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/account_manager.rs b/src/account_manager.rs index 91077c1c..67a11717 100644 --- a/src/account_manager.rs +++ b/src/account_manager.rs @@ -4,6 +4,7 @@ use enostr::Keypair; use crate::key_storage::KeyStorage; pub use crate::user_account::UserAccount; +use tracing::info; /// The interface for managing the user's accounts. /// 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); - self.accounts.push(account) + self.accounts.push(account); + true } pub fn num_accounts(&self) -> usize {