notedeck

One damus client to rule them all
git clone git://jb55.com/notedeck
Log | Files | Refs | README | LICENSE

commit 1810515ad210a75baa90818ffa673bf51b1d1271
parent 307b8af8f19498db514fee3e0effc4fbb7090cef
Author: William Casarin <jb55@jb55.com>
Date:   Mon,  1 Jul 2024 06:28:48 -0700

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>

Diffstat:
Msrc/account_manager.rs | 20++++++++++++++++++--
1 file changed, 18 insertions(+), 2 deletions(-)

diff --git 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 {