11
11
#![ allow( unknown_lints) ] // TODO: remove when MSRV >= 1.72.0, required for `clippy::arc_with_non_send_sync`
12
12
#![ allow( clippy:: arc_with_non_send_sync) ]
13
13
14
+ use std:: collections:: HashMap ;
14
15
use std:: sync:: atomic:: { AtomicBool , Ordering } ;
15
16
use std:: sync:: Arc ;
16
17
@@ -35,7 +36,7 @@ const ID: &str = "nwc";
35
36
#[ derive( Debug , Clone ) ]
36
37
pub struct NWC {
37
38
uri : NostrWalletConnectURI ,
38
- relay : Relay ,
39
+ pool : RelayPool ,
39
40
opts : NostrWalletConnectOptions ,
40
41
bootstrapped : Arc < AtomicBool > ,
41
42
}
@@ -50,17 +51,17 @@ impl NWC {
50
51
/// New `NWC` client with custom [`NostrWalletConnectOptions`].
51
52
pub fn with_opts ( uri : NostrWalletConnectURI , opts : NostrWalletConnectOptions ) -> Self {
52
53
Self {
53
- relay : Relay :: with_opts ( uri. relays . first ( ) . cloned ( ) . unwrap ( ) , opts. relay . clone ( ) ) ,
54
54
uri,
55
+ pool : RelayPool :: default ( ) ,
55
56
opts,
56
57
bootstrapped : Arc :: new ( AtomicBool :: new ( false ) ) ,
57
58
}
58
59
}
59
60
60
- /// Get relay status
61
- # [ inline ]
62
- pub fn status ( & self ) -> RelayStatus {
63
- self . relay . status ( )
61
+ /// Get relays status
62
+ pub async fn status ( & self ) -> HashMap < RelayUrl , RelayStatus > {
63
+ let relays = self . pool . relays ( ) . await ;
64
+ relays . into_iter ( ) . map ( | ( u , r ) | ( u , r . status ( ) ) ) . collect ( )
64
65
}
65
66
66
67
/// Connect and subscribe
@@ -70,16 +71,21 @@ impl NWC {
70
71
return Ok ( ( ) ) ;
71
72
}
72
73
73
- // Connect
74
- self . relay . connect ( ) ;
74
+ // Add relays
75
+ for url in self . uri . relays . iter ( ) {
76
+ self . pool . add_relay ( url, self . opts . relay . clone ( ) ) . await ?;
77
+ }
78
+
79
+ // Connect to relays
80
+ self . pool . connect ( ) . await ;
75
81
76
82
let filter = Filter :: new ( )
77
83
. author ( self . uri . public_key )
78
84
. kind ( Kind :: WalletConnectResponse )
79
85
. limit ( 0 ) ; // Limit to 0 means give me 0 events until EOSE
80
86
81
87
// Subscribe
82
- self . relay
88
+ self . pool
83
89
. subscribe_with_id ( SubscriptionId :: new ( ID ) , filter, SubscribeOptions :: default ( ) )
84
90
. await ?;
85
91
@@ -96,16 +102,16 @@ impl NWC {
96
102
// Convert request to event
97
103
let event: Event = req. to_event ( & self . uri ) ?;
98
104
99
- let mut notifications = self . relay . notifications ( ) ;
105
+ let mut notifications = self . pool . notifications ( ) ;
100
106
101
107
// Send request
102
- let id : EventId = self . relay . send_event ( & event) . await ?;
108
+ let output : Output < EventId > = self . pool . send_event ( & event) . await ?;
103
109
104
110
time:: timeout ( Some ( self . opts . timeout ) , async {
105
111
while let Ok ( notification) = notifications. recv ( ) . await {
106
- if let RelayNotification :: Event { event, .. } = notification {
112
+ if let RelayPoolNotification :: Event { event, .. } = notification {
107
113
if event. kind == Kind :: WalletConnectResponse
108
- && event. tags . event_ids ( ) . next ( ) . copied ( ) == Some ( id )
114
+ && event. tags . event_ids ( ) . next ( ) == Some ( output . id ( ) )
109
115
{
110
116
return Ok ( Response :: from_event ( & self . uri , & event) ?) ;
111
117
}
@@ -185,7 +191,7 @@ impl NWC {
185
191
186
192
/// Completely shutdown [NWC] client
187
193
#[ inline]
188
- pub fn shutdown ( self ) {
189
- self . relay . disconnect ( )
194
+ pub async fn shutdown ( self ) {
195
+ self . pool . disconnect ( ) . await
190
196
}
191
197
}
0 commit comments