From 427b9a21da12f6afa9c05cf974ddcf9bd01fc8aa Mon Sep 17 00:00:00 2001 From: David Caseria Date: Mon, 23 Sep 2024 07:15:38 -0400 Subject: [PATCH] Fixup NWC --- crates/cdk-nostr/Cargo.toml | 3 ++ crates/cdk-nostr/examples/app_pay_invoice.rs | 44 ++++++++++---------- crates/cdk-nostr/src/nwc.rs | 1 + 3 files changed, 26 insertions(+), 22 deletions(-) diff --git a/crates/cdk-nostr/Cargo.toml b/crates/cdk-nostr/Cargo.toml index 585125133..d9e88380e 100644 --- a/crates/cdk-nostr/Cargo.toml +++ b/crates/cdk-nostr/Cargo.toml @@ -30,3 +30,6 @@ thiserror = "1" tokio = "1" tracing = "0.1" url = "2.3" + +[dev-dependencies] +tokio-stream = "0.1" diff --git a/crates/cdk-nostr/examples/app_pay_invoice.rs b/crates/cdk-nostr/examples/app_pay_invoice.rs index f0d87da15..75713f04e 100644 --- a/crates/cdk-nostr/examples/app_pay_invoice.rs +++ b/crates/cdk-nostr/examples/app_pay_invoice.rs @@ -4,8 +4,9 @@ use lightning_invoice::Bolt11Invoice; use nostr_database::{MemoryDatabase, MemoryDatabaseOptions}; use nostr_sdk::{ nips::nip47::{self, NostrWalletConnectURI, PayInvoiceRequestParams}, - Alphabet, EventSource, Filter, Keys, Kind, SingleLetterTag, + Alphabet, Filter, FilterOptions, Keys, Kind, SingleLetterTag, }; +use tokio_stream::StreamExt; #[tokio::main] async fn main() -> Result<(), Box> { @@ -17,7 +18,7 @@ async fn main() -> Result<(), Box> { let connect_uri = NostrWalletConnectURI::from_str(&args[1])?; let invoice = Bolt11Invoice::from_str(&args[2])?; - let keys = Keys::generate(); + let keys = Keys::new(connect_uri.secret.clone()); let client = nostr_sdk::Client::builder() .database(MemoryDatabase::with_opts(MemoryDatabaseOptions { events: true, @@ -35,30 +36,29 @@ async fn main() -> Result<(), Box> { }); let event = request.to_event(&connect_uri)?; let event_id = event.id; + + let mut stream = client + .pool() + .stream_events_of( + vec![Filter::new() + .kind(Kind::WalletConnectResponse) + .custom_tag(SingleLetterTag::lowercase(Alphabet::E), vec![event_id])], + Duration::from_secs(60), + FilterOptions::WaitForEventsAfterEOSE(1), + ) + .await?; client.send_event(event).await?; - loop { - let events = client - .get_events_of( - vec![Filter::new() - .kind(Kind::WalletConnectResponse) - .custom_tag(SingleLetterTag::lowercase(Alphabet::E), vec![event_id])], - EventSource::relays(None), - ) - .await?; - if let Some(event) = events.first() { - match nip47::Response::from_event(&connect_uri, event) { - Ok(response) => { - println!("{:?}", response); - break; - } - Err(e) => { - eprintln!("Error: {}", e); - std::process::exit(1); - } + if let Some(event) = stream.next().await { + match nip47::Response::from_event(&connect_uri, &event) { + Ok(response) => { + println!("{:?}", response); + } + Err(e) => { + eprintln!("Error: {}", e); + std::process::exit(1); } } - tokio::time::sleep(Duration::from_secs(1)).await; } Ok(()) diff --git a/crates/cdk-nostr/src/nwc.rs b/crates/cdk-nostr/src/nwc.rs index c1184dcc6..3638a1561 100644 --- a/crates/cdk-nostr/src/nwc.rs +++ b/crates/cdk-nostr/src/nwc.rs @@ -490,6 +490,7 @@ impl WalletConnection { fn filter(&self, service_pubkey: PublicKey, since: Timestamp) -> Filter { Filter::new() .kind(Kind::WalletConnectRequest) + .author(self.keys.public_key()) .since(since) .custom_tag( SingleLetterTag::lowercase(Alphabet::P),