|
| 1 | +// Copyright (c) 2022-2023 Yuki Kishimoto |
| 2 | +// Copyright (c) 2023-2024 Rust Nostr Developers |
| 3 | +// Distributed under the MIT software license |
| 4 | + |
| 5 | +//! NWC client and zapper backend for Nostr apps |
| 6 | +
|
| 7 | +#![forbid(unsafe_code)] |
| 8 | +#![warn(missing_docs)] |
| 9 | +#![warn(rustdoc::bare_urls)] |
| 10 | +#![allow(unknown_lints)] |
| 11 | +#![allow(clippy::arc_with_non_send_sync)] |
| 12 | + |
| 13 | +use std::time::Duration; |
| 14 | + |
| 15 | +pub extern crate nostr; |
| 16 | +pub extern crate nostr_zapper as zapper; |
| 17 | + |
| 18 | +use async_utility::time; |
| 19 | +use nostr::nips::nip47::{ |
| 20 | + MakeInvoiceRequestParams, MakeInvoiceResponseResult, Method, NostrWalletConnectURI, |
| 21 | + PayInvoiceRequestParams, PayInvoiceResponseResult, Request, RequestParams, Response, |
| 22 | +}; |
| 23 | +use nostr::{Filter, Kind, SubscriptionId}; |
| 24 | +use nostr_relay_pool::{FilterOptions, RelayPool, RelayPoolNotification, RelaySendOptions}; |
| 25 | +use nostr_zapper::{async_trait, NostrZapper, ZapperBackend}; |
| 26 | + |
| 27 | +pub mod error; |
| 28 | +pub mod options; |
| 29 | +pub mod prelude; |
| 30 | + |
| 31 | +pub use self::error::Error; |
| 32 | +pub use self::options::NostrWalletConnectOptions; |
| 33 | + |
| 34 | +/// Nostr Wallet Connect client |
| 35 | +#[derive(Debug, Clone)] |
| 36 | +pub struct NWC { |
| 37 | + uri: NostrWalletConnectURI, |
| 38 | + pool: RelayPool, |
| 39 | +} |
| 40 | + |
| 41 | +impl NWC { |
| 42 | + /// Compose new [NWC] client |
| 43 | + pub async fn new(uri: NostrWalletConnectURI) -> Result<Self, Error> { |
| 44 | + Self::with_opts(uri, NostrWalletConnectOptions::default()).await |
| 45 | + } |
| 46 | + |
| 47 | + /// Compose new [NWC] client with [NostrWalletConnectOptions] |
| 48 | + pub async fn with_opts( |
| 49 | + uri: NostrWalletConnectURI, |
| 50 | + opts: NostrWalletConnectOptions, |
| 51 | + ) -> Result<Self, Error> { |
| 52 | + // Compose pool |
| 53 | + let pool = RelayPool::new(opts.pool); |
| 54 | + pool.add_relay(&uri.relay_url, opts.relay).await?; |
| 55 | + pool.connect(Some(Duration::from_secs(10))).await; |
| 56 | + |
| 57 | + Ok(Self { uri, pool }) |
| 58 | + } |
| 59 | + |
| 60 | + /// Create invoice |
| 61 | + pub async fn make_invoice( |
| 62 | + &self, |
| 63 | + satoshi: u64, |
| 64 | + description: Option<String>, |
| 65 | + expiry: Option<u64>, |
| 66 | + ) -> Result<String, Error> { |
| 67 | + // Compose NWC request event |
| 68 | + let req = Request { |
| 69 | + method: Method::MakeInvoice, |
| 70 | + params: RequestParams::MakeInvoice(MakeInvoiceRequestParams { |
| 71 | + amount: satoshi * 1000, |
| 72 | + description, |
| 73 | + description_hash: None, |
| 74 | + expiry, |
| 75 | + }), |
| 76 | + }; |
| 77 | + let event = req.to_event(&self.uri)?; |
| 78 | + let event_id = event.id; |
| 79 | + |
| 80 | + // Subscribe |
| 81 | + let relay = self.pool.relay(&self.uri.relay_url).await?; |
| 82 | + let id = SubscriptionId::generate(); |
| 83 | + let filter = Filter::new() |
| 84 | + .author(self.uri.public_key) |
| 85 | + .kind(Kind::WalletConnectResponse) |
| 86 | + .event(event_id) |
| 87 | + .limit(1); |
| 88 | + |
| 89 | + // Subscribe |
| 90 | + relay |
| 91 | + .send_req( |
| 92 | + id, |
| 93 | + vec![filter], |
| 94 | + Some(FilterOptions::WaitForEventsAfterEOSE(1)), |
| 95 | + ) |
| 96 | + .await?; |
| 97 | + |
| 98 | + let mut notifications = self.pool.notifications(); |
| 99 | + |
| 100 | + // Send request |
| 101 | + self.pool |
| 102 | + .send_event_to([&self.uri.relay_url], event, RelaySendOptions::new()) |
| 103 | + .await?; |
| 104 | + |
| 105 | + time::timeout(Some(Duration::from_secs(10)), async { |
| 106 | + while let Ok(notification) = notifications.recv().await { |
| 107 | + if let RelayPoolNotification::Event { event, .. } = notification { |
| 108 | + if event.kind() == Kind::WalletConnectResponse |
| 109 | + && event.event_ids().next().copied() == Some(event_id) |
| 110 | + { |
| 111 | + let res = Response::from_event(&self.uri, &event)?; |
| 112 | + let MakeInvoiceResponseResult { invoice, .. } = res.to_make_invoice()?; |
| 113 | + return Ok(invoice); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + Err(Error::Timeout) |
| 119 | + }) |
| 120 | + .await |
| 121 | + .ok_or(Error::Timeout)? |
| 122 | + } |
| 123 | + |
| 124 | + /// Pay invoice |
| 125 | + pub async fn send_payment(&self, invoice: String) -> Result<(), Error> { |
| 126 | + // Compose NWC request event |
| 127 | + let req = Request { |
| 128 | + method: Method::PayInvoice, |
| 129 | + params: RequestParams::PayInvoice(PayInvoiceRequestParams { |
| 130 | + id: None, |
| 131 | + invoice, |
| 132 | + amount: None, |
| 133 | + }), |
| 134 | + }; |
| 135 | + let event = req.to_event(&self.uri)?; |
| 136 | + let event_id = event.id; |
| 137 | + |
| 138 | + // Subscribe |
| 139 | + let relay = self.pool.relay(&self.uri.relay_url).await?; |
| 140 | + let id = SubscriptionId::generate(); |
| 141 | + let filter = Filter::new() |
| 142 | + .author(self.uri.public_key) |
| 143 | + .kind(Kind::WalletConnectResponse) |
| 144 | + .event(event_id) |
| 145 | + .limit(1); |
| 146 | + |
| 147 | + // Subscribe |
| 148 | + relay |
| 149 | + .send_req( |
| 150 | + id, |
| 151 | + vec![filter], |
| 152 | + Some(FilterOptions::WaitForEventsAfterEOSE(1)), |
| 153 | + ) |
| 154 | + .await?; |
| 155 | + |
| 156 | + let mut notifications = self.pool.notifications(); |
| 157 | + |
| 158 | + // Send request |
| 159 | + self.pool |
| 160 | + .send_event_to([&self.uri.relay_url], event, RelaySendOptions::new()) |
| 161 | + .await?; |
| 162 | + |
| 163 | + time::timeout(Some(Duration::from_secs(10)), async { |
| 164 | + while let Ok(notification) = notifications.recv().await { |
| 165 | + if let RelayPoolNotification::Event { event, .. } = notification { |
| 166 | + if event.kind() == Kind::WalletConnectResponse |
| 167 | + && event.event_ids().next().copied() == Some(event_id) |
| 168 | + { |
| 169 | + let res = Response::from_event(&self.uri, &event)?; |
| 170 | + let PayInvoiceResponseResult { preimage } = res.to_pay_invoice()?; |
| 171 | + tracing::info!("Invoice paid! Preimage: {preimage}"); |
| 172 | + break; |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + Ok::<(), Error>(()) |
| 178 | + }) |
| 179 | + .await |
| 180 | + .ok_or(Error::Timeout)? |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] |
| 185 | +#[cfg_attr(not(target_arch = "wasm32"), async_trait)] |
| 186 | +impl NostrZapper for NWC { |
| 187 | + type Err = Error; |
| 188 | + |
| 189 | + fn backend(&self) -> ZapperBackend { |
| 190 | + ZapperBackend::NWC |
| 191 | + } |
| 192 | + |
| 193 | + async fn pay_invoice(&self, invoice: String) -> Result<(), Self::Err> { |
| 194 | + self.send_payment(invoice).await |
| 195 | + } |
| 196 | +} |
0 commit comments