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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#![doc = include_str!("../README.md")]

#[cfg(feature = "dht")]
use mainline::{Dht, DhtSettings, GetMutableResponse, MutableItem, Response, StoreQueryMetdata};
#[cfg(feature = "relay")]
use url::Url;

// Rexports
pub use bytes;
pub use simple_dns as dns;
pub use url;

// Modules

mod error;
mod keys;
mod signed_packet;

// Exports
pub use crate::error::Error;
pub use crate::keys::{Keypair, PublicKey};
pub use crate::signed_packet::SignedPacket;

// Alias Result to be the crate Result.
pub type Result<T, E = Error> = core::result::Result<T, E>;

pub const DEFAULT_PKARR_RELAY: &str = "https://relay.pkarr.org";

pub struct PkarrClientBuilder {
    #[cfg(feature = "dht")]
    settings: DhtSettings,
}

impl PkarrClientBuilder {
    pub fn new() -> Self {
        Self {
            #[cfg(feature = "dht")]
            settings: DhtSettings::default(),
        }
    }

    #[cfg(feature = "dht")]
    pub fn bootstrap(mut self, bootstrap: &[String]) -> Self {
        self.settings.bootstrap = Some(bootstrap.to_owned());
        self
    }

    pub fn build(self) -> PkarrClient {
        PkarrClient {
            #[cfg(all(feature = "relay", not(feature = "async")))]
            http_client: reqwest::blocking::Client::new(),
            #[cfg(all(feature = "relay", feature = "async"))]
            http_client: reqwest::Client::new(),
            #[cfg(feature = "dht")]
            dht: Dht::new(self.settings),
        }
    }
}

impl Default for PkarrClientBuilder {
    fn default() -> Self {
        Self::new()
    }
}

#[derive(Debug, Clone)]
/// Main client for publishing and resolving [SginedPacket](crate::SignedPacket)s.
pub struct PkarrClient {
    #[cfg(all(feature = "relay", not(feature = "async")))]
    http_client: reqwest::blocking::Client,
    #[cfg(all(feature = "relay", feature = "async"))]
    http_client: reqwest::Client,
    #[cfg(feature = "dht")]
    dht: Dht,
}

impl PkarrClient {
    pub fn new() -> Self {
        Self {
            #[cfg(all(feature = "relay", not(feature = "async")))]
            http_client: reqwest::blocking::Client::new(),
            #[cfg(all(feature = "relay", feature = "async"))]
            http_client: reqwest::Client::new(),
            #[cfg(feature = "dht")]
            dht: Dht::default(),
        }
    }

    pub fn builder() -> PkarrClientBuilder {
        PkarrClientBuilder::default()
    }

    #[cfg(all(feature = "relay", not(feature = "async")))]
    /// Add your own Reqwest blocking client with custom config.
    pub fn with_http_client(mut self, client: reqwest::blocking::Client) -> Self {
        self.http_client = client;
        self
    }

    #[cfg(all(feature = "relay", feature = "async"))]
    /// Add your own Reqwest client with custom config.
    pub fn with_http_client(mut self, client: reqwest::Client) -> Self {
        self.http_client = client;
        self
    }

    #[cfg(all(feature = "relay", not(feature = "async")))]
    /// Resolves a [SignedPacket](crate::SignedPacket) from a [relay](https://github.com/Nuhvi/pkarr/blob/main/design/relays.md).
    pub fn relay_get(&self, url: &Url, public_key: PublicKey) -> Result<Option<SignedPacket>> {
        let url = format_relay_url(url, &public_key)?;

        let response = self.http_client.get(url).send()?;

        if response.status().is_success() {
            let bytes = response.bytes()?;
            return Ok(Some(SignedPacket::from_relay_response(public_key, bytes)?));
        } else if response.status() == reqwest::StatusCode::NOT_FOUND {
            return Ok(None);
        } else {
            return Err(Error::RelayResponse(
                response.url().clone(),
                response.status(),
                response.text()?,
            ));
        }
    }

    #[cfg(all(feature = "relay", feature = "async"))]
    /// Resolves a [SignedPacket] from a [relay](https://github.com/Nuhvi/pkarr/blob/main/design/relays.md).
    pub async fn relay_get(
        &self,
        url: &Url,
        public_key: PublicKey,
    ) -> Result<Option<SignedPacket>> {
        let url = format_relay_url(url, &public_key)?;

        let response = self.http_client.get(url).send().await?;

        if response.status().is_success() {
            let bytes = response.bytes().await?;
            return Ok(Some(SignedPacket::from_relay_response(public_key, bytes)?));
        } else if response.status() == reqwest::StatusCode::NOT_FOUND {
            return Ok(None);
        } else {
            return Err(Error::RelayResponse(
                response.url().clone(),
                response.status(),
                response.text().await?,
            ));
        }
    }

    #[cfg(all(feature = "relay", not(feature = "async")))]
    /// Publishes a [SignedPacket] through a [relay](https://github.com/Nuhvi/pkarr/blob/main/design/relays.md).
    pub fn relay_put(&self, url: &Url, signed_packet: &SignedPacket) -> Result<()> {
        let url = format_relay_url(url, signed_packet.public_key())?;

        let response = self
            .http_client
            .put(url)
            .body(signed_packet.as_relay_request())
            .send()?;

        if !response.status().is_success() {
            return Err(Error::RelayResponse(
                response.url().clone(),
                response.status(),
                response.text()?,
            ));
        }

        Ok(())
    }

    #[cfg(all(feature = "relay", feature = "async"))]
    /// Publishes a [SignedPacket] through a [relay](https://github.com/Nuhvi/pkarr/blob/main/design/relays.md).
    pub async fn relay_put(&self, url: &Url, signed_packet: &SignedPacket) -> Result<()> {
        let url = format_relay_url(url, signed_packet.public_key())?;

        let response = self
            .http_client
            .put(url)
            .body(signed_packet.as_relay_request())
            .send()
            .await?;

        if !response.status().is_success() {
            return Err(Error::RelayResponse(
                response.url().clone(),
                response.status(),
                response.text().await?,
            ));
        }

        Ok(())
    }

    #[cfg(all(feature = "dht", not(feature = "async")))]
    /// Publish a [SignedPacket] to the DHT.
    ///
    /// It performs a thorough lookup first to find the closest nodes,
    /// before storing the signed packet to them, so it may take few seconds.
    pub fn publish(&self, signed_packet: &SignedPacket) -> Result<StoreQueryMetdata> {
        let item: MutableItem = signed_packet.into();
        self.dht.put_mutable(item).map_err(Error::MainlineError)
    }

    #[cfg(all(feature = "dht", feature = "async"))]
    /// Publish a [SignedPacket] to the DHT.
    ///
    /// It performs a thorough lookup first to find the closest nodes,
    /// before storing the signed packet to them, so it may take few seconds.
    pub async fn publish(&self, signed_packet: &SignedPacket) -> Result<StoreQueryMetdata> {
        let item: MutableItem = signed_packet.into();
        self.dht.put_mutable(item).map_err(Error::MainlineError)
    }

    #[cfg(all(feature = "dht", not(feature = "async")))]
    /// Eagerly return the first resolved [SignedPacket] from the DHT.
    pub fn resolve(&self, public_key: PublicKey) -> Option<SignedPacket> {
        let mut response = self.dht.get_mutable(public_key.as_bytes(), None);

        for res in &mut response {
            let signed_packet: Result<SignedPacket> = res.item.try_into();
            if let Ok(signed_packet) = signed_packet {
                return Some(signed_packet);
            };
        }

        None
    }

    #[cfg(all(feature = "dht", feature = "async"))]
    /// Eagerly return the first resolved [SignedPacket] from the DHT.
    pub async fn resolve(&self, public_key: PublicKey) -> Option<SignedPacket> {
        let mut response = self.dht.get_mutable(public_key.as_bytes(), None);

        for res in &mut response {
            let signed_packet: Result<SignedPacket> = res.item.try_into();
            if let Ok(signed_packet) = signed_packet {
                return Some(signed_packet);
            };
        }

        None
    }

    #[cfg(all(feature = "dht", not(feature = "async")))]
    /// Fully traverse the DHT and the return the most recent resolved [SignedPacket].
    pub fn resolve_most_recent(&self, public_key: PublicKey) -> Option<SignedPacket> {
        let mut response = self.dht.get_mutable(public_key.as_bytes(), None);

        let mut most_recent: Option<SignedPacket> = None;

        for next in &mut response {
            let next_packet: Result<SignedPacket> = next.item.try_into();
            if let Ok(next_packet) = next_packet {
                if let Some(most_recent) = &most_recent {
                    if most_recent.more_recent_than(&next_packet) {
                        continue;
                    }
                }

                most_recent = Some(next_packet)
            };
        }

        most_recent
    }

    #[cfg(all(feature = "dht", feature = "async"))]
    /// Fully traverse the DHT and the return the most recent resolved [SignedPacket].
    pub async fn resolve_most_recent(&self, public_key: PublicKey) -> Option<SignedPacket> {
        let mut response = self.dht.get_mutable(public_key.as_bytes(), None);

        let mut most_recent: Option<SignedPacket> = None;

        for next in &mut response {
            let next_packet: Result<SignedPacket> = next.item.try_into();
            if let Ok(next_packet) = next_packet {
                if let Some(most_recent) = &most_recent {
                    if most_recent.more_recent_than(&next_packet) {
                        continue;
                    }
                }

                most_recent = Some(next_packet)
            };
        }

        most_recent
    }

    #[cfg(all(feature = "dht", not(feature = "async")))]
    /// Return `mainline's` [Response] to have the most control over the response and access its metadata.
    ///
    /// Mostly useful to terminate as soon as you find a [SignedPacket] that satisfies a specific
    /// condition, for example:
    /// - it is [more_recent_than](SignedPacket::more_recent_than) a cached one.
    /// - or it has a [timestamp](SignedPacket::timestamp) higher than a specific value.
    /// - or it contains specific [fresh_resource_records](SignedPacket::fresh_resource_records).
    ///
    /// Most likely you want to use [resolve](PkarrClient::resolve) or
    /// [resolve_most_recent](PkarrClient::resolve_most_recent) instead.
    pub fn resolve_raw(&self, public_key: PublicKey) -> Response<GetMutableResponse> {
        self.dht.get_mutable(public_key.as_bytes(), None)
    }

    #[cfg(all(feature = "dht", feature = "async"))]
    /// Return `mainline's` [Response] to have the most control over the response and access its metadata.
    ///
    /// Mostly useful to terminate as soon as you find a [SignedPacket] that satisfies a specific
    /// condition, for example:
    /// - it is [more_recent_than](SignedPacket::more_recent_than) a cached one.
    /// - or it has a [timestamp](SignedPacket::timestamp) higher than a specific value.
    /// - or it contains specific [fresh_resource_records](SignedPacket::fresh_resource_records).
    ///
    /// Most likely you want to use [resolve](PkarrClient::resolve) or
    /// [resolve_most_recent](PkarrClient::resolve_most_recent) instead.
    pub async fn resolve_raw(&self, public_key: PublicKey) -> Response<GetMutableResponse> {
        self.dht.get_mutable(public_key.as_bytes(), None)
    }
}

impl Default for PkarrClient {
    fn default() -> Self {
        Self::builder().build()
    }
}

#[cfg(feature = "relay")]
fn format_relay_url(url: &Url, public_key: &PublicKey) -> Result<Url> {
    let mut url = url.to_owned();
    url.path_segments_mut()
        .map_err(|_| Error::Static("invalid url"))?
        .push(&public_key.to_z32());
    Ok(url)
}