Skip to content

Commit 67f446c

Browse files
committed
nostr: add Nip11GetOptions
Update `RelayInformationDocument::get` signature. Pull-Request: #913 Signed-off-by: Yuki Kishimoto <yukikishimoto@protonmail.com>
1 parent ff456bf commit 67f446c

File tree

4 files changed

+74
-20
lines changed

4 files changed

+74
-20
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
- nostr: remove `Market::Mention` (NIP-10) ([Yuki Kishimoto] at https://github.com/rust-nostr/nostr/pull/895)
3232
- nostr: remove `parser` feature ([Yuki Kishimoto] at https://github.com/rust-nostr/nostr/pull/899)
3333
- nostr: update `Nip19Profile::new` and `Nip19Coordinate::new` signature ([Yuki Kishimoto] at https://github.com/rust-nostr/nostr/pull/910)
34+
- nostr: update `RelayInformationDocument::get` signature ([Yuki Kishimoto] at https://github.com/rust-nostr/nostr/pull/913)
3435
- connect: remove `NostrConnect::get_relays` ([Yuki Kishimoto] at https://github.com/rust-nostr/nostr/pull/894)
3536

3637
### Changed
@@ -41,6 +42,7 @@
4142
### Added
4243

4344
- nostr: add NIP-88 support ([Yuki Kishimoto] at https://github.com/rust-nostr/nostr/pull/892)
45+
- nostr: add `Nip11GetOptions` ([Yuki Kishimoto] at https://github.com/rust-nostr/nostr/pull/913)
4446

4547
### Fixed
4648

crates/nostr-relay-pool/src/relay/inner.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -271,12 +271,20 @@ impl InnerRelay {
271271

272272
#[cfg(feature = "nip11")]
273273
fn request_nip11_document(&self) {
274-
let (allowed, proxy) = match self.opts.connection_mode {
275-
ConnectionMode::Direct => (true, None),
274+
#[cfg_attr(target_arch = "wasm32", allow(unused_mut))]
275+
let mut opts: Nip11GetOptions =
276+
Nip11GetOptions::default().timeout(DEFAULT_CONNECTION_TIMEOUT);
277+
278+
let allowed: bool = match self.opts.connection_mode {
279+
ConnectionMode::Direct => true,
276280
#[cfg(not(target_arch = "wasm32"))]
277-
ConnectionMode::Proxy(proxy) => (true, Some(proxy)),
281+
ConnectionMode::Proxy(proxy) => {
282+
// Update proxy
283+
opts.proxy = Some(proxy);
284+
true
285+
}
278286
#[cfg(all(feature = "tor", not(target_arch = "wasm32")))]
279-
ConnectionMode::Tor { .. } => (false, None),
287+
ConnectionMode::Tor { .. } => false,
280288
};
281289

282290
if allowed {
@@ -291,7 +299,7 @@ impl InnerRelay {
291299
let url = self.url.clone();
292300
let atomic = self.atomic.clone();
293301
task::spawn(async move {
294-
match RelayInformationDocument::get(url.clone().into(), proxy).await {
302+
match RelayInformationDocument::get(url.clone().into(), opts).await {
295303
Ok(document) => {
296304
let mut d = atomic.document.write().await;
297305
*d = document

crates/nostr/examples/nip11.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use nostr::prelude::*;
88
async fn main() -> Result<()> {
99
let relay_url = Url::parse("wss://relay.damus.io")?;
1010

11-
let info = RelayInformationDocument::get(relay_url, None).await?;
11+
let info = RelayInformationDocument::get(relay_url, Nip11GetOptions::default()).await?;
1212

1313
println!("{:#?}", info);
1414

crates/nostr/src/nips/nip11.rs

Lines changed: 58 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use alloc::string::String;
1111
use alloc::vec::Vec;
1212
use core::fmt;
13+
use core::time::Duration;
14+
#[cfg(not(target_arch = "wasm32"))]
1315
use std::net::SocketAddr;
1416

1517
use reqwest::Client;
@@ -54,6 +56,48 @@ impl From<reqwest::Error> for Error {
5456
}
5557
}
5658

59+
/// NIP11 get options
60+
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
61+
pub struct Nip11GetOptions {
62+
/// Proxy
63+
#[cfg(not(target_arch = "wasm32"))]
64+
pub proxy: Option<SocketAddr>,
65+
/// Timeout
66+
pub timeout: Duration,
67+
}
68+
69+
impl Default for Nip11GetOptions {
70+
fn default() -> Self {
71+
Self {
72+
proxy: None,
73+
timeout: Duration::from_secs(60),
74+
}
75+
}
76+
}
77+
78+
impl Nip11GetOptions {
79+
/// New default options
80+
#[inline]
81+
pub fn new() -> Self {
82+
Self::default()
83+
}
84+
85+
/// Set proxy
86+
#[inline]
87+
#[cfg(not(target_arch = "wasm32"))]
88+
pub fn proxy(mut self, proxy: SocketAddr) -> Self {
89+
self.proxy = Some(proxy);
90+
self
91+
}
92+
93+
/// Set timeout
94+
#[inline]
95+
pub fn timeout(mut self, timeout: Duration) -> Self {
96+
self.timeout = timeout;
97+
self
98+
}
99+
}
100+
57101
/// Relay information document
58102
#[derive(Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
59103
pub struct RelayInformationDocument {
@@ -181,27 +225,27 @@ pub struct FeeSchedule {
181225
}
182226

183227
impl RelayInformationDocument {
184-
/// Create new empty [`RelayInformationDocument`]
228+
/// Create a new empty [`RelayInformationDocument`].
185229
pub fn new() -> Self {
186230
Self::default()
187231
}
188232

189233
/// Get Relay Information Document
190-
///
191-
/// **Proxy is ignored for WASM targets!**
192-
pub async fn get(mut url: Url, _proxy: Option<SocketAddr>) -> Result<Self, Error> {
234+
pub async fn get(mut url: Url, opts: Nip11GetOptions) -> Result<Self, Error> {
235+
let mut builder = Client::builder();
236+
237+
// Set proxy
193238
#[cfg(not(target_arch = "wasm32"))]
194-
let client: Client = {
195-
let mut builder = Client::builder();
196-
if let Some(proxy) = _proxy {
197-
let proxy = format!("socks5h://{proxy}");
198-
builder = builder.proxy(Proxy::all(proxy)?);
199-
}
200-
builder.build()?
201-
};
239+
if let Some(proxy) = opts.proxy {
240+
let proxy = format!("socks5h://{proxy}");
241+
builder = builder.proxy(Proxy::all(proxy)?);
242+
}
243+
244+
// Set timeout
245+
builder = builder.timeout(opts.timeout);
202246

203-
#[cfg(target_arch = "wasm32")]
204-
let client: Client = Client::new();
247+
// Build client
248+
let client: Client = builder.build()?;
205249

206250
let url: &str = Self::with_http_scheme(&mut url)?;
207251
let req = client.get(url).header("Accept", "application/nostr+json");

0 commit comments

Comments
 (0)