From be2c5eb9ebc78b873a4720968d56719b73be2a71 Mon Sep 17 00:00:00 2001 From: benthecarman Date: Wed, 25 Oct 2023 18:12:07 -0500 Subject: [PATCH] Register nwc strings --- mutiny-core/src/lib.rs | 1 + mutiny-core/src/nostr/mod.rs | 18 +++++++++++++++++- mutiny-core/src/notifications.rs | 20 ++++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/mutiny-core/src/lib.rs b/mutiny-core/src/lib.rs index 7e81b191d..8f65063e1 100644 --- a/mutiny-core/src/lib.rs +++ b/mutiny-core/src/lib.rs @@ -200,6 +200,7 @@ impl MutinyWallet { let nostr = Arc::new(NostrManager::from_mnemonic( node_manager.xprivkey, storage.clone(), + notification_client.clone(), node_manager.logger.clone(), )?); diff --git a/mutiny-core/src/nostr/mod.rs b/mutiny-core/src/nostr/mod.rs index ddf1c0947..f7d7c9252 100644 --- a/mutiny-core/src/nostr/mod.rs +++ b/mutiny-core/src/nostr/mod.rs @@ -6,6 +6,7 @@ use crate::nostr::nwc::{ PendingNwcInvoice, Profile, SingleUseSpendingConditions, SpendingConditions, PENDING_NWC_EVENTS_KEY, }; +use crate::notifications::MutinyNotificationClient; use crate::storage::MutinyStorage; use crate::{error::MutinyError, utils::get_random_bip32_child_index}; use crate::{utils, HTLCStatus}; @@ -69,6 +70,8 @@ pub struct NostrManager { pub storage: S, /// Lock for pending nwc invoices pending_nwc_lock: Arc>, + /// Notification Client + pub notifications: Option>, /// Logger pub logger: Arc, } @@ -350,6 +353,17 @@ impl NostrManager { let _ = client.disconnect().await; } + // register for subscriptions + if let Some(notifications) = &self.notifications { + // just created, unwrap is safe + let uri = NostrWalletConnectURI::from_str(&profile.nwc_uri).expect("invalid uri"); + let author = uri.secret.x_only_public_key(nostr::SECP256K1).0; + + notifications + .register_nwc(author, uri.public_key, &profile.relay, &profile.name) + .await?; + } + Ok(profile) } @@ -840,6 +854,7 @@ impl NostrManager { pub fn from_mnemonic( xprivkey: ExtendedPrivKey, storage: S, + notifications: Option>, logger: Arc, ) -> Result { let context = Secp256k1::new(); @@ -862,6 +877,7 @@ impl NostrManager { nwc: Arc::new(RwLock::new(nwc)), storage, pending_nwc_lock: Arc::new(Mutex::new(())), + notifications, logger, }) } @@ -889,7 +905,7 @@ mod test { let logger = Arc::new(MutinyLogger::default()); - NostrManager::from_mnemonic(xprivkey, storage, logger).unwrap() + NostrManager::from_mnemonic(xprivkey, storage, None, logger).unwrap() } #[test] diff --git a/mutiny-core/src/notifications.rs b/mutiny-core/src/notifications.rs index 6ceba1ea8..27fb67062 100644 --- a/mutiny-core/src/notifications.rs +++ b/mutiny-core/src/notifications.rs @@ -3,6 +3,7 @@ use crate::{error::MutinyError, logging::MutinyLogger}; use anyhow::anyhow; use lightning::util::logger::*; use lightning::{log_error, log_info}; +use nostr::secp256k1::XOnlyPublicKey; use reqwest::{Method, Url}; use serde_json::{json, Value}; use std::sync::Arc; @@ -81,4 +82,23 @@ impl MutinyNotificationClient { Ok(()) } + + pub async fn register_nwc( + &self, + author: XOnlyPublicKey, + tagged: XOnlyPublicKey, + relay: &str, + name: &str, + ) -> Result<(), MutinyError> { + let url = Url::parse(&format!("{}/register-nwc", self.url)).map_err(|e| { + log_error!(self.logger, "Error parsing register url: {e}"); + MutinyError::InvalidArgumentsError + })?; + + let body = json!({"id": self.id, "author": author, "tagged": tagged, "relay": relay, "name": name}); + + self.make_request(Method::POST, url, Some(body)).await?; + + Ok(()) + } }