Skip to content
This repository was archived by the owner on Feb 3, 2025. It is now read-only.

Commit be2c5eb

Browse files
committed
Register nwc strings
1 parent 9081d44 commit be2c5eb

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

mutiny-core/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ impl<S: MutinyStorage> MutinyWallet<S> {
200200
let nostr = Arc::new(NostrManager::from_mnemonic(
201201
node_manager.xprivkey,
202202
storage.clone(),
203+
notification_client.clone(),
203204
node_manager.logger.clone(),
204205
)?);
205206

mutiny-core/src/nostr/mod.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::nostr::nwc::{
66
PendingNwcInvoice, Profile, SingleUseSpendingConditions, SpendingConditions,
77
PENDING_NWC_EVENTS_KEY,
88
};
9+
use crate::notifications::MutinyNotificationClient;
910
use crate::storage::MutinyStorage;
1011
use crate::{error::MutinyError, utils::get_random_bip32_child_index};
1112
use crate::{utils, HTLCStatus};
@@ -69,6 +70,8 @@ pub struct NostrManager<S: MutinyStorage> {
6970
pub storage: S,
7071
/// Lock for pending nwc invoices
7172
pending_nwc_lock: Arc<Mutex<()>>,
73+
/// Notification Client
74+
pub notifications: Option<Arc<MutinyNotificationClient>>,
7275
/// Logger
7376
pub logger: Arc<MutinyLogger>,
7477
}
@@ -350,6 +353,17 @@ impl<S: MutinyStorage> NostrManager<S> {
350353
let _ = client.disconnect().await;
351354
}
352355

356+
// register for subscriptions
357+
if let Some(notifications) = &self.notifications {
358+
// just created, unwrap is safe
359+
let uri = NostrWalletConnectURI::from_str(&profile.nwc_uri).expect("invalid uri");
360+
let author = uri.secret.x_only_public_key(nostr::SECP256K1).0;
361+
362+
notifications
363+
.register_nwc(author, uri.public_key, &profile.relay, &profile.name)
364+
.await?;
365+
}
366+
353367
Ok(profile)
354368
}
355369

@@ -840,6 +854,7 @@ impl<S: MutinyStorage> NostrManager<S> {
840854
pub fn from_mnemonic(
841855
xprivkey: ExtendedPrivKey,
842856
storage: S,
857+
notifications: Option<Arc<MutinyNotificationClient>>,
843858
logger: Arc<MutinyLogger>,
844859
) -> Result<Self, MutinyError> {
845860
let context = Secp256k1::new();
@@ -862,6 +877,7 @@ impl<S: MutinyStorage> NostrManager<S> {
862877
nwc: Arc::new(RwLock::new(nwc)),
863878
storage,
864879
pending_nwc_lock: Arc::new(Mutex::new(())),
880+
notifications,
865881
logger,
866882
})
867883
}
@@ -889,7 +905,7 @@ mod test {
889905

890906
let logger = Arc::new(MutinyLogger::default());
891907

892-
NostrManager::from_mnemonic(xprivkey, storage, logger).unwrap()
908+
NostrManager::from_mnemonic(xprivkey, storage, None, logger).unwrap()
893909
}
894910

895911
#[test]

mutiny-core/src/notifications.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::{error::MutinyError, logging::MutinyLogger};
33
use anyhow::anyhow;
44
use lightning::util::logger::*;
55
use lightning::{log_error, log_info};
6+
use nostr::secp256k1::XOnlyPublicKey;
67
use reqwest::{Method, Url};
78
use serde_json::{json, Value};
89
use std::sync::Arc;
@@ -81,4 +82,23 @@ impl MutinyNotificationClient {
8182

8283
Ok(())
8384
}
85+
86+
pub async fn register_nwc(
87+
&self,
88+
author: XOnlyPublicKey,
89+
tagged: XOnlyPublicKey,
90+
relay: &str,
91+
name: &str,
92+
) -> Result<(), MutinyError> {
93+
let url = Url::parse(&format!("{}/register-nwc", self.url)).map_err(|e| {
94+
log_error!(self.logger, "Error parsing register url: {e}");
95+
MutinyError::InvalidArgumentsError
96+
})?;
97+
98+
let body = json!({"id": self.id, "author": author, "tagged": tagged, "relay": relay, "name": name});
99+
100+
self.make_request(Method::POST, url, Some(body)).await?;
101+
102+
Ok(())
103+
}
84104
}

0 commit comments

Comments
 (0)