1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//! Relay builder
use std::sync::Arc;
use nostr::types::RelayUrl;
use nostr_database::{IntoNostrDatabase, NostrDatabase};
use super::options::RelayOptions;
use super::{Relay, RelayCapabilities};
use crate::authenticator::Authenticator;
use crate::events_tracker::MemoryEventsTracker;
use crate::policy::AdmitPolicy;
use crate::transport::websocket::{DefaultWebsocketTransport, WebSocketTransport};
/// Relay builder
#[derive(Debug, Clone)]
pub struct RelayBuilder {
/// Relay URL
pub url: RelayUrl,
/// WebSocket transport
pub websocket_transport: Arc<dyn WebSocketTransport>,
/// Database
pub database: Arc<dyn NostrDatabase>,
/// Admission policy
pub admit_policy: Option<Arc<dyn AdmitPolicy>>,
/// Authenticator
pub authenticator: Option<Arc<dyn Authenticator>>,
/// Capabilities
pub capabilities: RelayCapabilities,
/// Relay pool options
pub opts: RelayOptions,
}
impl RelayBuilder {
/// New relay builder
#[inline]
pub fn new(url: RelayUrl) -> Self {
Self {
url,
websocket_transport: Arc::new(DefaultWebsocketTransport),
database: Arc::new(MemoryEventsTracker::default()),
admit_policy: None,
authenticator: None,
capabilities: RelayCapabilities::default(),
opts: RelayOptions::default(),
}
}
/// Set a WebSocket transport
#[inline]
pub fn websocket_transport<T>(mut self, transport: T) -> Self
where
T: WebSocketTransport + 'static,
{
self.websocket_transport = Arc::new(transport);
self
}
/// Set a database
#[inline]
pub fn database<T>(mut self, database: T) -> Self
where
T: IntoNostrDatabase,
{
self.database = database.into_nostr_database();
self
}
/// Admission policy
#[inline]
pub fn admit_policy<T>(mut self, policy: T) -> Self
where
T: AdmitPolicy + 'static,
{
self.admit_policy = Some(Arc::new(policy));
self
}
/// Set a NIP-42 authenticator.
///
/// The authenticator is used when a relay requires authentication and the
/// client needs to build an `AUTH` event.
///
/// If you already have a signer that implements
/// [`AsyncGetPublicKey`](nostr::key::AsyncGetPublicKey) and
/// [`AsyncSignEvent`](nostr::event::AsyncSignEvent), you can wrap it with
/// [`SignerAuthenticator`](crate::authenticator::SignerAuthenticator).
///
/// # Example
///
/// ```rust
/// # use nostr_sdk::prelude::*;
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let keys = Keys::generate();
/// let authenticator = SignerAuthenticator::new(keys);
/// let url = RelayUrl::parse("wss://relay.damus.io")?;
/// let client = Relay::builder(url).authenticator(authenticator).build();
/// # Ok(())
/// # }
/// ```
#[inline]
pub fn authenticator<T>(mut self, authenticator: T) -> Self
where
T: Authenticator + 'static,
{
self.authenticator = Some(Arc::new(authenticator));
self
}
/// Set capabilities
#[inline]
pub fn capabilities(mut self, capabilities: RelayCapabilities) -> Self {
self.capabilities = capabilities;
self
}
/// Set options
#[inline]
pub fn opts(mut self, opts: RelayOptions) -> Self {
self.opts = opts;
self
}
/// Build relay
#[inline]
pub fn build(self) -> Relay {
Relay::from_builder(self)
}
}