Skip to content

Commit

Permalink
Register nwc strings
Browse files Browse the repository at this point in the history
  • Loading branch information
benthecarman committed Oct 26, 2023
1 parent 9081d44 commit be2c5eb
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions mutiny-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ impl<S: MutinyStorage> MutinyWallet<S> {
let nostr = Arc::new(NostrManager::from_mnemonic(
node_manager.xprivkey,
storage.clone(),
notification_client.clone(),
node_manager.logger.clone(),
)?);

Expand Down
18 changes: 17 additions & 1 deletion mutiny-core/src/nostr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -69,6 +70,8 @@ pub struct NostrManager<S: MutinyStorage> {
pub storage: S,
/// Lock for pending nwc invoices
pending_nwc_lock: Arc<Mutex<()>>,
/// Notification Client
pub notifications: Option<Arc<MutinyNotificationClient>>,
/// Logger
pub logger: Arc<MutinyLogger>,
}
Expand Down Expand Up @@ -350,6 +353,17 @@ impl<S: MutinyStorage> NostrManager<S> {
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)
}

Expand Down Expand Up @@ -840,6 +854,7 @@ impl<S: MutinyStorage> NostrManager<S> {
pub fn from_mnemonic(
xprivkey: ExtendedPrivKey,
storage: S,
notifications: Option<Arc<MutinyNotificationClient>>,
logger: Arc<MutinyLogger>,
) -> Result<Self, MutinyError> {
let context = Secp256k1::new();
Expand All @@ -862,6 +877,7 @@ impl<S: MutinyStorage> NostrManager<S> {
nwc: Arc::new(RwLock::new(nwc)),
storage,
pending_nwc_lock: Arc::new(Mutex::new(())),
notifications,
logger,
})
}
Expand Down Expand Up @@ -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]
Expand Down
20 changes: 20 additions & 0 deletions mutiny-core/src/notifications.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(())
}
}

0 comments on commit be2c5eb

Please sign in to comment.