From d01bd30f3ef722f9d96078838f07e4cb3f7d63f8 Mon Sep 17 00:00:00 2001 From: benthecarman Date: Tue, 6 Feb 2024 16:35:53 +0000 Subject: [PATCH] Get contact for npub --- mutiny-core/src/labels.rs | 14 ++++++++++++++ mutiny-wasm/src/lib.rs | 13 +++++++++++++ 2 files changed, 27 insertions(+) diff --git a/mutiny-core/src/labels.rs b/mutiny-core/src/labels.rs index 54b81431f..9fd0a0adb 100644 --- a/mutiny-core/src/labels.rs +++ b/mutiny-core/src/labels.rs @@ -155,6 +155,20 @@ pub trait LabelStorage { } Ok(None) } + /// Finds a contact that has the given npub + fn get_contact_for_npub( + &self, + npub: XOnlyPublicKey, + ) -> Result, MutinyError> { + // todo this is not efficient, we should have a map of npub to contact + let contacts = self.get_contacts()?; + for (id, contact) in contacts { + if contact.npub == Some(npub) { + return Ok(Some((id, contact))); + } + } + Ok(None) + } } impl LabelStorage for S { diff --git a/mutiny-wasm/src/lib.rs b/mutiny-wasm/src/lib.rs index 22bad5527..e38cbb934 100644 --- a/mutiny-wasm/src/lib.rs +++ b/mutiny-wasm/src/lib.rs @@ -1312,6 +1312,19 @@ impl MutinyWallet { Ok(self.inner.node_manager.edit_contact(id, contact)?) } + pub async fn get_contact_for_npub( + &self, + npub: String, + ) -> Result, MutinyJsError> { + let npub = parse_npub(&npub)?; + let contact = self.inner.node_manager.get_contact_for_npub(npub)?; + + match contact { + Some((id, c)) => Ok(Some((id, c).into())), + None => Ok(None), + } + } + pub fn get_tag_items(&self) -> Result, MutinyJsError> { let mut tags: Vec = self .inner