Skip to content

Commit 2039c90

Browse files
authored
Nostr connect on the fly if needed (#708)
1 parent 81358df commit 2039c90

File tree

1 file changed

+34
-19
lines changed

1 file changed

+34
-19
lines changed

crates/bcr-ebill-transport/src/nostr.rs

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ impl NostrClient {
132132

133133
/// Subscribe to some nostr events with a filter
134134
pub async fn subscribe(&self, subscription: Filter) -> Result<()> {
135-
self.client()?
135+
self.client()
136+
.await?
136137
.subscribe(subscription, None)
137138
.await
138139
.map_err(|e| {
@@ -146,7 +147,8 @@ impl NostrClient {
146147
/// from this clients relays.
147148
pub async fn fetch_metadata(&self, npub: PublicKey) -> Result<Option<Metadata>> {
148149
let result = self
149-
.client()?
150+
.client()
151+
.await?
150152
.fetch_metadata(npub, self.config.default_timeout.to_owned())
151153
.await
152154
.map_err(|e| {
@@ -190,7 +192,8 @@ impl NostrClient {
190192
relays: Vec<(RelayUrl, Option<RelayMetadata>)>,
191193
) -> Result<()> {
192194
let event = EventBuilder::relay_list(relays);
193-
self.client()?
195+
self.client()
196+
.await?
194197
.send_event_builder(event)
195198
.await
196199
.map_err(|e| {
@@ -210,7 +213,8 @@ impl NostrClient {
210213
relays: Option<Vec<url::Url>>,
211214
) -> Result<Vec<Event>> {
212215
let events = self
213-
.client()?
216+
.client()
217+
.await?
214218
.fetch_events_from(
215219
relays.unwrap_or(self.config.relays.clone()),
216220
filter,
@@ -238,10 +242,15 @@ impl NostrClient {
238242
let event = create_nip04_event(&self.get_signer().await, &public_key, &message).await?;
239243
let relays = recipient.nostr_relays();
240244
if !relays.is_empty() {
241-
if let Err(e) = self.client()?.send_event_builder_to(&relays, event).await {
245+
if let Err(e) = self
246+
.client()
247+
.await?
248+
.send_event_builder_to(&relays, event)
249+
.await
250+
{
242251
error!("Error sending Nostr message: {e}")
243252
};
244-
} else if let Err(e) = self.client()?.send_event_builder(event).await {
253+
} else if let Err(e) = self.client().await?.send_event_builder(event).await {
245254
error!("Error sending Nostr message: {e}")
246255
}
247256
Ok(())
@@ -257,14 +266,16 @@ impl NostrClient {
257266
let relays = recipient.nostr_relays();
258267
if !relays.is_empty() {
259268
if let Err(e) = self
260-
.client()?
269+
.client()
270+
.await?
261271
.send_private_msg_to(&relays, public_key, message, None)
262272
.await
263273
{
264274
error!("Error sending Nostr message: {e}")
265275
};
266276
} else if let Err(e) = self
267-
.client()?
277+
.client()
278+
.await?
268279
.send_private_msg(public_key, message, None)
269280
.await
270281
{
@@ -277,12 +288,11 @@ impl NostrClient {
277288
self.connected.load(Ordering::Relaxed)
278289
}
279290

280-
pub fn client(&self) -> Result<&Client> {
281-
if self.is_connected() {
282-
Ok(&self.client)
283-
} else {
284-
Err(Error::Network("Nostr client not connected".to_string()))
291+
pub async fn client(&self) -> Result<&Client> {
292+
if !self.is_connected() {
293+
self.connect().await?;
285294
}
295+
Ok(&self.client)
286296
}
287297
}
288298

@@ -332,17 +342,22 @@ impl NotificationJsonTransportApi for NostrClient {
332342
root_event,
333343
)?;
334344
let send_event = self
335-
.client()?
345+
.client()
346+
.await?
336347
.sign_event_builder(event)
337348
.await
338349
.map_err(|e| {
339350
error!("Failed to sign Nostr event: {e}");
340351
Error::Crypto("Failed to sign Nostr event".to_string())
341352
})?;
342-
self.client()?.send_event(&send_event).await.map_err(|e| {
343-
error!("Failed to send Nostr event: {e}");
344-
Error::Network("Failed to send Nostr event".to_string())
345-
})?;
353+
self.client()
354+
.await?
355+
.send_event(&send_event)
356+
.await
357+
.map_err(|e| {
358+
error!("Failed to send Nostr event: {e}");
359+
Error::Network("Failed to send Nostr event".to_string())
360+
})?;
346361
Ok(send_event)
347362
}
348363

@@ -393,7 +408,7 @@ impl NotificationJsonTransportApi for NostrClient {
393408
}
394409

395410
async fn publish_metadata(&self, data: &Metadata) -> Result<()> {
396-
self.client()?.set_metadata(data).await.map_err(|e| {
411+
self.client().await?.set_metadata(data).await.map_err(|e| {
397412
error!("Failed to send user metadata with Nostr client: {e}");
398413
Error::Network("Failed to send user metadata with Nostr client".to_string())
399414
})?;

0 commit comments

Comments
 (0)