@@ -18,7 +18,6 @@ use std::sync::Arc;
18
18
19
19
pub extern crate nostr;
20
20
21
- use async_utility:: time;
22
21
use nostr:: nips:: nip47:: { Notification , Request , Response } ;
23
22
use nostr_relay_pool:: prelude:: * ;
24
23
@@ -31,7 +30,6 @@ pub use self::error::Error;
31
30
#[ doc( hidden) ]
32
31
pub use self :: options:: NostrWalletConnectOptions ;
33
32
34
- const ID : & str = "nwc" ;
35
33
const NOTIFICATIONS_ID : & str = "nwc-notifications" ;
36
34
37
35
/// Nostr Wallet Connect client
@@ -94,16 +92,6 @@ impl NWC {
94
92
// Connect to relays
95
93
self . pool . connect ( ) . await ;
96
94
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
-
107
95
// Mark as bootstrapped
108
96
self . bootstrapped . store ( true , Ordering :: SeqCst ) ;
109
97
@@ -119,26 +107,29 @@ impl NWC {
119
107
// Convert request to event
120
108
let event: Event = req. to_event ( & self . uri ) ?;
121
109
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 ) ;
123
115
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 ?;
126
121
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) ?;
137
130
138
- Err ( Error :: PrematureExit )
139
- } )
140
- . await
141
- . ok_or ( Error :: Timeout ) ?
131
+ // Return response
132
+ Ok ( response)
142
133
}
143
134
144
135
/// Pay invoice
0 commit comments