#![forbid(unsafe_code)]
#![warn(missing_docs)]
#![warn(rustdoc::bare_urls)]
#![allow(unknown_lints)]
#![allow(clippy::arc_with_non_send_sync)]
use std::time::Duration;
pub extern crate nostr;
pub extern crate nostr_zapper as zapper;
use async_utility::time;
use nostr::nips::nip47::{
GetBalanceResponseResult, GetInfoResponseResult, ListTransactionsRequestParams,
LookupInvoiceRequestParams, LookupInvoiceResponseResult, MakeInvoiceRequestParams,
MakeInvoiceResponseResult, NostrWalletConnectURI, PayInvoiceRequestParams,
PayInvoiceResponseResult, PayKeysendRequestParams, PayKeysendResponseResult, Request, Response,
};
use nostr::{Filter, Kind, SubscriptionId};
use nostr_relay_pool::{FilterOptions, RelayPool, RelayPoolNotification, RelaySendOptions};
use nostr_zapper::{async_trait, NostrZapper, ZapperBackend};
pub mod error;
pub mod options;
pub mod prelude;
pub use self::error::Error;
pub use self::options::NostrWalletConnectOptions;
#[derive(Debug, Clone)]
pub struct NWC {
uri: NostrWalletConnectURI,
pool: RelayPool,
}
impl NWC {
pub async fn new(uri: NostrWalletConnectURI) -> Result<Self, Error> {
Self::with_opts(uri, NostrWalletConnectOptions::default()).await
}
pub async fn with_opts(
uri: NostrWalletConnectURI,
opts: NostrWalletConnectOptions,
) -> Result<Self, Error> {
let pool = RelayPool::new(opts.pool);
pool.add_relay(&uri.relay_url, opts.relay).await?;
pool.connect(Some(Duration::from_secs(10))).await;
Ok(Self { uri, pool })
}
async fn send_request(&self, req: Request) -> Result<Response, Error> {
let event = req.to_event(&self.uri)?;
let event_id = event.id;
let relay = self.pool.relay(&self.uri.relay_url).await?;
let id = SubscriptionId::generate();
let filter = Filter::new()
.author(self.uri.public_key)
.kind(Kind::WalletConnectResponse)
.event(event_id)
.limit(1);
relay
.send_req(
id,
vec![filter],
Some(FilterOptions::WaitForEventsAfterEOSE(1)),
)
.await?;
let mut notifications = self.pool.notifications();
self.pool
.send_event_to([&self.uri.relay_url], event, RelaySendOptions::new())
.await?;
time::timeout(Some(Duration::from_secs(10)), async {
while let Ok(notification) = notifications.recv().await {
if let RelayPoolNotification::Event { event, .. } = notification {
if event.kind() == Kind::WalletConnectResponse
&& event.event_ids().next().copied() == Some(event_id)
{
return Ok(Response::from_event(&self.uri, &event)?);
}
}
}
Err(Error::Timeout)
})
.await
.ok_or(Error::Timeout)?
}
pub async fn pay_invoice<S>(&self, invoice: S) -> Result<String, Error>
where
S: Into<String>,
{
let req = Request::pay_invoice(PayInvoiceRequestParams {
id: None,
invoice: invoice.into(),
amount: None,
});
let res: Response = self.send_request(req).await?;
let PayInvoiceResponseResult { preimage } = res.to_pay_invoice()?;
Ok(preimage)
}
pub async fn pay_keysend(
&self,
params: PayKeysendRequestParams,
) -> Result<PayKeysendResponseResult, Error> {
let req = Request::pay_keysend(params);
let res: Response = self.send_request(req).await?;
Ok(res.to_pay_keysend()?)
}
pub async fn make_invoice(
&self,
params: MakeInvoiceRequestParams,
) -> Result<MakeInvoiceResponseResult, Error> {
let req: Request = Request::make_invoice(params);
let res: Response = self.send_request(req).await?;
Ok(res.to_make_invoice()?)
}
pub async fn lookup_invoice(
&self,
params: LookupInvoiceRequestParams,
) -> Result<LookupInvoiceResponseResult, Error> {
let req = Request::lookup_invoice(params);
let res: Response = self.send_request(req).await?;
Ok(res.to_lookup_invoice()?)
}
pub async fn list_transactions(
&self,
params: ListTransactionsRequestParams,
) -> Result<Vec<LookupInvoiceResponseResult>, Error> {
let req = Request::list_transactions(params);
let res: Response = self.send_request(req).await?;
Ok(res.to_list_transactions()?)
}
pub async fn get_balance(&self) -> Result<u64, Error> {
let req = Request::get_balance();
let res: Response = self.send_request(req).await?;
let GetBalanceResponseResult { balance } = res.to_get_balance()?;
Ok(balance)
}
pub async fn get_info(&self) -> Result<GetInfoResponseResult, Error> {
let req = Request::get_info();
let res: Response = self.send_request(req).await?;
Ok(res.to_get_info()?)
}
}
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl NostrZapper for NWC {
type Err = Error;
fn backend(&self) -> ZapperBackend {
ZapperBackend::NWC
}
async fn pay(&self, invoice: String) -> Result<(), Self::Err> {
self.pay_invoice(invoice).await?;
Ok(())
}
}