Skip to content

Commit 16b4e71

Browse files
committed
nwc: subscribe to the response using the request event ID
This fixes the compatibility issues with some NWC services, like Primal. Pull-Request: #1073 Signed-off-by: Yuki Kishimoto <[email protected]>
1 parent bead389 commit 16b4e71

File tree

4 files changed

+28
-31
lines changed

4 files changed

+28
-31
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/nwc/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,19 @@
2525

2626
## Unreleased
2727

28+
### Changed
29+
30+
- Subscribe to the response using the request event ID (https://github.com/rust-nostr/nostr/pull/1073)
31+
2832
### Added
2933

3034
- Add `NWC::reconnect_relay` (https://github.com/rust-nostr/nostr/pull/1020)
3135
- Add `NWC::uri`
3236

37+
### Fixed
38+
39+
- Fix the compatibility issues with some NWC services, like Primal (https://github.com/rust-nostr/nostr/pull/1073)
40+
3341
## v0.43.0 - 2025/07/28
3442

3543
### Added

crates/nwc/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ default = []
1616
tor = ["nostr-relay-pool/tor"]
1717

1818
[dependencies]
19-
async-utility.workspace = true
2019
nostr = { workspace = true, features = ["std", "nip47"] }
2120
nostr-relay-pool.workspace = true
2221
tracing = { workspace = true, features = ["std"] }

crates/nwc/src/lib.rs

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use std::sync::Arc;
1818

1919
pub extern crate nostr;
2020

21-
use async_utility::time;
2221
use nostr::nips::nip47::{Notification, Request, Response};
2322
use nostr_relay_pool::prelude::*;
2423

@@ -31,7 +30,6 @@ pub use self::error::Error;
3130
#[doc(hidden)]
3231
pub use self::options::NostrWalletConnectOptions;
3332

34-
const ID: &str = "nwc";
3533
const NOTIFICATIONS_ID: &str = "nwc-notifications";
3634

3735
/// Nostr Wallet Connect client
@@ -94,16 +92,6 @@ impl NWC {
9492
// Connect to relays
9593
self.pool.connect().await;
9694

97-
let filter = Filter::new()
98-
.author(self.uri.public_key)
99-
.kind(Kind::WalletConnectResponse)
100-
.limit(0); // Limit to 0 means give me 0 events until EOSE
101-
102-
// Subscribe
103-
self.pool
104-
.subscribe_with_id(SubscriptionId::new(ID), filter, SubscribeOptions::default())
105-
.await?;
106-
10795
// Mark as bootstrapped
10896
self.bootstrapped.store(true, Ordering::SeqCst);
10997

@@ -119,26 +107,29 @@ impl NWC {
119107
// Convert request to event
120108
let event: Event = req.to_event(&self.uri)?;
121109

122-
let mut notifications = self.pool.notifications();
110+
// Construct the filter to wait for the response
111+
let filter = Filter::new()
112+
.author(self.uri.public_key)
113+
.kind(Kind::WalletConnectResponse)
114+
.event(event.id);
123115

124-
// Send request
125-
let output: Output<EventId> = self.pool.send_event(&event).await?;
116+
// Subscribe to filter and create the stream
117+
let mut stream = self
118+
.pool
119+
.stream_events(filter, self.opts.timeout, ReqExitPolicy::WaitForEvents(1))
120+
.await?;
126121

127-
time::timeout(Some(self.opts.timeout), async {
128-
while let Ok(notification) = notifications.recv().await {
129-
if let RelayPoolNotification::Event { event, .. } = notification {
130-
if event.kind == Kind::WalletConnectResponse
131-
&& event.tags.event_ids().next() == Some(output.id())
132-
{
133-
return Ok(Response::from_event(&self.uri, &event)?);
134-
}
135-
}
136-
}
122+
// Send the request
123+
self.pool.send_event(&event).await?;
124+
125+
// Wait for the response event
126+
let received_event: Event = stream.next().await.ok_or(Error::PrematureExit)?;
127+
128+
// Parse response
129+
let response: Response = Response::from_event(&self.uri, &received_event)?;
137130

138-
Err(Error::PrematureExit)
139-
})
140-
.await
141-
.ok_or(Error::Timeout)?
131+
// Return response
132+
Ok(response)
142133
}
143134

144135
/// Pay invoice

0 commit comments

Comments
 (0)