nostr 0.45.0-alpha.1

Rust implementation of the Nostr protocol.
Documentation
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
// Copyright (c) 2026 Rust Nostr Developers
// Distributed under the MIT software license

//! NIP-66: Relay Discovery and Liveness Monitoring
//!
//! <https://github.com/nostr-protocol/nips/blob/master/66.md>

use alloc::borrow::ToOwned;
use alloc::string::{String, ToString};
use core::convert::Infallible;
use core::fmt;
use core::num::ParseIntError;
use core::str::FromStr;
use core::time::Duration;

use super::util::take_string;
use crate::Kind;
use crate::event::tag::{Tag, TagCodec, TagCodecError, impl_tag_codec_conversions};
use crate::util::UnwrapInfallible;

const RTT_OPEN: &str = "rtt-open";
const RTT_READ: &str = "rtt-read";
const RTT_WRITE: &str = "rtt-write";
const NETWORK_TYPE: &str = "n";
const RELAY_TYPE: &str = "T";
const NIP: &str = "N";
const REQUIREMENT: &str = "R";
const TOPIC: &str = "t";
const KIND: &str = "k";
const GEOHASH: &str = "g";

/// NIP-66 error
#[derive(Debug, PartialEq)]
pub enum Error {
    /// Parse int error
    ParseInt(ParseIntError),
    /// Codec error
    Codec(TagCodecError),
}

impl core::error::Error for Error {}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::ParseInt(e) => e.fmt(f),
            Self::Codec(e) => e.fmt(f),
        }
    }
}

impl From<ParseIntError> for Error {
    fn from(e: ParseIntError) -> Self {
        Self::ParseInt(e)
    }
}

impl From<TagCodecError> for Error {
    fn from(e: TagCodecError) -> Self {
        Self::Codec(e)
    }
}

/// Standardized NIP-66 tags
///
/// <https://github.com/nostr-protocol/nips/blob/master/66.md>
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Nip66Tag {
    /// Relay's open round-trip time
    RttOpen(Duration),
    /// Relay's read round-trip time
    RttRead(Duration),
    /// Relay's write round-trip time
    RttWrite(Duration),
    /// Relay's network type
    NetworkType(NetworkType),
    /// Relay type
    RelayType(RelayType),
    /// NIP supported by relay
    Nip(String),
    /// Relay requirement per NIP-11's limitations
    Requirement {
        /// Relay requirement
        requirement: Requirement,
        /// Required or not
        is_required: bool,
    },
    /// Topic associated with relay
    Topic(String),
    /// Accepted or unaccepted kind
    Kind {
        /// Event kind
        kind: Kind,
        /// Accepted by relay
        is_accepted: bool,
    },
    /// NIP-52 geohash
    Geohash(String),
}

impl TagCodec for Nip66Tag {
    type Error = Error;

    fn parse<I, S>(tag: I) -> Result<Self, Self::Error>
    where
        I: IntoIterator<Item = S>,
        S: AsRef<str>,
    {
        let mut iter = tag.into_iter();
        let kind: S = iter.next().ok_or(TagCodecError::missing_tag_kind())?;
        match kind.as_ref() {
            RTT_OPEN => Ok(Self::RttOpen(parse_time(iter, RTT_OPEN)?)),
            RTT_READ => Ok(Self::RttRead(parse_time(iter, RTT_READ)?)),
            RTT_WRITE => Ok(Self::RttWrite(parse_time(iter, RTT_WRITE)?)),
            NETWORK_TYPE => {
                let network_type = take_string(&mut iter, "network type")?
                    .parse()
                    .unwrap_infallible();
                Ok(Self::NetworkType(network_type))
            }
            RELAY_TYPE => {
                let relay_type = take_string(&mut iter, "relay type")?
                    .parse()
                    .unwrap_infallible();
                Ok(Self::RelayType(relay_type))
            }
            NIP => Ok(Self::Nip(take_string(&mut iter, "NIP")?)),
            REQUIREMENT => {
                let value = take_string(&mut iter, "requirement")?;
                let BoolTag { value, yes } = BoolTag::parse(&value);
                Ok(Self::Requirement {
                    requirement: value.parse().unwrap_infallible(),
                    is_required: yes,
                })
            }
            TOPIC => Ok(Self::Topic(take_string(&mut iter, "topic")?)),
            KIND => {
                let value = take_string(&mut iter, "kind")?;
                let BoolTag { value, yes } = BoolTag::parse(&value);
                Ok(Self::Kind {
                    kind: value.parse().map_err(Error::ParseInt)?,
                    is_accepted: yes,
                })
            }
            GEOHASH => Ok(Self::Geohash(take_string(&mut iter, "geohash")?)),
            _ => Err(TagCodecError::Unknown.into()),
        }
    }

    fn to_tag(&self) -> Tag {
        match self {
            Self::RttOpen(time) => {
                Tag::new(vec![RTT_OPEN.to_owned(), time.as_millis().to_string()])
            }
            Self::RttRead(time) => {
                Tag::new(vec![RTT_READ.to_owned(), time.as_millis().to_string()])
            }
            Self::RttWrite(time) => {
                Tag::new(vec![RTT_WRITE.to_owned(), time.as_millis().to_string()])
            }
            Self::NetworkType(network_type) => {
                Tag::new(vec![NETWORK_TYPE.to_owned(), network_type.to_string()])
            }
            Self::RelayType(relay_type) => {
                Tag::new(vec![RELAY_TYPE.to_owned(), relay_type.to_string()])
            }
            Self::Nip(nip) => Tag::new(vec![String::from(NIP), nip.to_owned()]),
            Self::Requirement {
                requirement,
                is_required,
            } => Tag::new(vec![
                REQUIREMENT.to_owned(),
                BoolTag::to_string(requirement.as_str(), *is_required),
            ]),
            Self::Topic(topic) => Tag::new(vec![TOPIC.to_owned(), topic.to_owned()]),
            Self::Kind { kind, is_accepted } => Tag::new(vec![
                KIND.to_owned(),
                BoolTag::to_string(kind.as_u16(), *is_accepted),
            ]),
            Self::Geohash(geohash) => Tag::new(vec![GEOHASH.to_owned(), geohash.to_owned()]),
        }
    }
}

/// Network type
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum NetworkType {
    /// Clearnet
    Clearnet,
    /// Tor
    Tor,
    /// I2P
    I2p,
    /// Loki
    Loki,
    /// Other
    Other(String),
}

impl FromStr for NetworkType {
    type Err = Infallible;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        Ok(match value {
            "clearnet" => Self::Clearnet,
            "tor" => Self::Tor,
            "i2p" => Self::I2p,
            "loki" => Self::Loki,
            _ => Self::Other(value.to_owned()),
        })
    }
}

impl fmt::Display for NetworkType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

impl NetworkType {
    /// Serialize as `&str`
    pub fn as_str(&self) -> &str {
        match self {
            Self::Clearnet => "clearnet",
            Self::Tor => "tor",
            Self::I2p => "i2p",
            Self::Loki => "loki",
            Self::Other(value) => value,
        }
    }
}

/// Relay type
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum RelayType {
    /// Relays that store all the public content of a user in a way that anyone can download
    PublicOutbox,
    /// Relays that accept any event from anyone as long as they p-tag
    /// one of the subscribers or the public in general
    PublicInbox,
    /// Relays that accept any event from anyone as long as they tag one of the
    /// subscribers or the public in general, however only the tagged individual can
    /// download their tagged events
    PrivateStorage,
    /// Relays that accept events from an author and make sure only the author can download them
    PrivateInbox,
    /// Relays that implement NIP-50 and can help find content
    Search,
    /// Relays that only track kind 0 and kind 10002 to help find people and distribute content
    Directory,
    /// Relays whose read or write access is closed to members of a NIP-29 group or NIP-71 community
    Community,
    /// Relays that return events in their own algorithm in any order they prefer
    Algo,
    /// Relays that serve as archival nodes for the network
    Archival,
    /// Private Storage relays that take priority given their closer proximity
    /// (in ping latency) to the Client
    LocalCache,
    /// Storage relays for NIP-95 content and other types of binary content
    BlobRelays,
    /// Re-broadcast content to other relays (Blastr)
    Broadcast,
    /// Aggregator proxy that connects to multiple relays while sustaining
    /// only one connection to the Client (bostr)
    Proxy,
    /// Relays that store events that are not verifiable (like Decrypted NIP-17 DMs)
    Trusted,
    /// Ephemeral relays that Push to the receiver any event received by them
    Push,
    /// Catch-all variant for relay types not covered by the standard categories
    Other(String),
}

impl FromStr for RelayType {
    type Err = Infallible;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        Ok(match value {
            "PublicOutbox" => Self::PublicOutbox,
            "PublicInbox" => Self::PublicInbox,
            "PrivateStorage" => Self::PrivateStorage,
            "PrivateInbox" => Self::PrivateInbox,
            "Search" => Self::Search,
            "Directory" => Self::Directory,
            "Community" => Self::Community,
            "Algo" => Self::Algo,
            "Archival" => Self::Archival,
            "LocalCache" => Self::LocalCache,
            "BlobRelays" => Self::BlobRelays,
            "Broadcast" => Self::Broadcast,
            "Proxy" => Self::Proxy,
            "Trusted" => Self::Trusted,
            "Push" => Self::Push,
            _ => Self::Other(value.to_owned()),
        })
    }
}

impl fmt::Display for RelayType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

impl RelayType {
    /// Serialize as `&str`
    pub fn as_str(&self) -> &str {
        match self {
            Self::PublicOutbox => "PublicOutbox",
            Self::PublicInbox => "PublicInbox",
            Self::PrivateStorage => "PrivateStorage",
            Self::PrivateInbox => "PrivateInbox",
            Self::Search => "Search",
            Self::Directory => "Directory",
            Self::Community => "Community",
            Self::Algo => "Algo",
            Self::Archival => "Archival",
            Self::LocalCache => "LocalCache",
            Self::BlobRelays => "BlobRelays",
            Self::Broadcast => "Broadcast",
            Self::Proxy => "Proxy",
            Self::Trusted => "Trusted",
            Self::Push => "Push",
            Self::Other(value) => value,
        }
    }
}

/// Relay requirement
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Requirement {
    /// NIP-42 authentication
    Auth,
    /// Writes
    Writes,
    /// NIP-13 PoW
    Pow,
    /// Payment
    Payment,
    /// Other relay requirement
    Other(String),
}

impl FromStr for Requirement {
    type Err = Infallible;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        Ok(match value {
            "auth" => Self::Auth,
            "writes" => Self::Writes,
            "pow" => Self::Pow,
            "payment" => Self::Payment,
            _ => Self::Other(value.to_owned()),
        })
    }
}

impl fmt::Display for Requirement {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

impl Requirement {
    /// Serialize as `&str`
    pub fn as_str(&self) -> &str {
        match self {
            Self::Auth => "auth",
            Self::Writes => "writes",
            Self::Pow => "pow",
            Self::Payment => "payment",
            Self::Other(value) => value,
        }
    }
}

fn parse_time<I, S>(mut iter: I, tag: &'static str) -> Result<Duration, Error>
where
    I: Iterator<Item = S>,
    S: AsRef<str>,
{
    let time = iter
        .next()
        .ok_or(TagCodecError::Missing(tag))?
        .as_ref()
        .parse::<u64>()
        .map_err(Error::ParseInt)?;
    Ok(Duration::from_millis(time))
}

struct BoolTag<'a> {
    value: &'a str,
    yes: bool,
}

impl<'a> BoolTag<'a> {
    const NEGATION: &'static str = "!";

    fn parse(raw_value: &'a str) -> Self {
        let (value, yes) = raw_value
            .split_once(Self::NEGATION)
            .map(|(_, r)| (r, false))
            .unwrap_or_else(|| (raw_value, true));
        Self { value, yes }
    }

    fn to_string<T: fmt::Display>(value: T, yes: bool) -> String {
        format!("{}{value}", if yes { "" } else { Self::NEGATION })
    }
}

impl_tag_codec_conversions!(Nip66Tag);

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_standardized_rtt_open_tag() {
        let tag = ["rtt-open", "234"];
        let parsed = Nip66Tag::parse(tag).unwrap();

        assert_eq!(parsed, Nip66Tag::RttOpen(Duration::from_millis(234)));
        assert_eq!(parsed.to_tag(), Tag::parse(tag).unwrap());

        let err = Nip66Tag::parse(["rtt-open"]).unwrap_err();
        assert_eq!(err, Error::Codec(TagCodecError::Missing("rtt-open")));
    }

    #[test]
    fn test_standardized_rtt_read_tag() {
        let tag = ["rtt-read", "234"];
        let parsed = Nip66Tag::parse(tag).unwrap();

        assert_eq!(parsed, Nip66Tag::RttRead(Duration::from_millis(234)));
        assert_eq!(parsed.to_tag(), Tag::parse(tag).unwrap());

        let err = Nip66Tag::parse(["rtt-read"]).unwrap_err();
        assert_eq!(err, Error::Codec(TagCodecError::Missing("rtt-read")));
    }

    #[test]
    fn test_standardized_rtt_write_tag() {
        let tag = ["rtt-write", "234"];
        let parsed = Nip66Tag::parse(tag).unwrap();

        assert_eq!(parsed, Nip66Tag::RttWrite(Duration::from_millis(234)));
        assert_eq!(parsed.to_tag(), Tag::parse(tag).unwrap());

        let err = Nip66Tag::parse(["rtt-write"]).unwrap_err();
        assert_eq!(err, Error::Codec(TagCodecError::Missing("rtt-write")));
    }

    #[test]
    fn test_standardized_network_type_tag() {
        let tag = ["n", "clearnet"];
        let parsed = Nip66Tag::parse(tag).unwrap();
        assert_eq!(parsed, Nip66Tag::NetworkType(NetworkType::Clearnet));
        assert_eq!(parsed.to_tag(), Tag::parse(tag).unwrap());

        let tag = ["n", "fips"];
        let parsed = Nip66Tag::parse(tag).unwrap();
        assert_eq!(
            parsed,
            Nip66Tag::NetworkType(NetworkType::Other("fips".to_owned()))
        );
        assert_eq!(parsed.to_tag(), Tag::parse(tag).unwrap());

        let err = Nip66Tag::parse(["n"]).unwrap_err();
        assert_eq!(err, Error::Codec(TagCodecError::Missing("network type")));
    }

    #[test]
    fn test_standardized_relay_type_tag() {
        let tag = ["T", "PrivateInbox"];
        let parsed = Nip66Tag::parse(tag).unwrap();

        assert_eq!(
            parsed,
            Nip66Tag::RelayType("PrivateInbox".parse::<RelayType>().unwrap())
        );
        assert_eq!(parsed.to_tag(), Tag::parse(tag).unwrap());

        let err = Nip66Tag::parse(["T"]).unwrap_err();
        assert_eq!(err, Error::Codec(TagCodecError::Missing("relay type")));
    }

    #[test]
    fn test_standardized_n_tag() {
        let tag = ["N", "66"];
        let parsed = Nip66Tag::parse(tag).unwrap();

        assert_eq!(parsed, Nip66Tag::Nip("66".to_owned()));
        assert_eq!(parsed.to_tag(), Tag::parse(tag).unwrap());

        let err = Nip66Tag::parse(["N"]).unwrap_err();
        assert_eq!(err, Error::Codec(TagCodecError::Missing("NIP")));
    }

    #[test]
    fn test_standardized_r_tag() {
        let tag = ["R", "!payment"];
        let parsed = Nip66Tag::parse(tag).unwrap();
        assert_eq!(
            parsed,
            Nip66Tag::Requirement {
                requirement: Requirement::Payment,
                is_required: false
            }
        );
        assert_eq!(parsed.to_tag(), Tag::parse(tag).unwrap());

        let tag = ["R", "auth"];
        let parsed = Nip66Tag::parse(tag).unwrap();
        assert_eq!(
            parsed,
            Nip66Tag::Requirement {
                requirement: Requirement::Auth,
                is_required: true
            }
        );
        assert_eq!(parsed.to_tag(), Tag::parse(tag).unwrap());

        let tag = ["R", "!unknown"];
        let parsed = Nip66Tag::parse(tag).unwrap();
        assert_eq!(
            parsed,
            Nip66Tag::Requirement {
                requirement: Requirement::Other("unknown".to_owned()),
                is_required: false
            }
        );
        assert_eq!(parsed.to_tag(), Tag::parse(tag).unwrap());

        let err = Nip66Tag::parse(["R"]).unwrap_err();
        assert_eq!(err, Error::Codec(TagCodecError::Missing("requirement")));
    }

    #[test]
    fn test_standardized_t_tag() {
        let tag = ["t", "nsfw"];
        let parsed = Nip66Tag::parse(tag).unwrap();

        assert_eq!(parsed, Nip66Tag::Topic("nsfw".to_owned()));
        assert_eq!(parsed.to_tag(), Tag::parse(tag).unwrap());

        let err = Nip66Tag::parse(["t"]).unwrap_err();
        assert_eq!(err, Error::Codec(TagCodecError::Missing("topic")));
    }

    #[test]
    fn test_standardized_k_tag() {
        let tag = ["k", "!1"];
        let parsed = Nip66Tag::parse(tag).unwrap();
        assert_eq!(
            parsed,
            Nip66Tag::Kind {
                kind: Kind::TextNote,
                is_accepted: false
            }
        );
        assert_eq!(parsed.to_tag(), Tag::parse(tag).unwrap());

        let tag = ["k", "1"];
        let parsed = Nip66Tag::parse(tag).unwrap();
        assert_eq!(
            parsed,
            Nip66Tag::Kind {
                kind: Kind::TextNote,
                is_accepted: true
            }
        );
        assert_eq!(parsed.to_tag(), Tag::parse(tag).unwrap());

        let err = Nip66Tag::parse(["k"]).unwrap_err();
        assert_eq!(err, Error::Codec(TagCodecError::Missing("kind")));
    }

    #[test]
    fn test_standardized_g_tag() {
        let tag = ["g", "ww8p1r4t8"];
        let parsed = Nip66Tag::parse(tag).unwrap();

        assert_eq!(parsed, Nip66Tag::Geohash("ww8p1r4t8".to_owned()));
        assert_eq!(parsed.to_tag(), Tag::parse(tag).unwrap());

        let err = Nip66Tag::parse(["g"]).unwrap_err();
        assert_eq!(err, Error::Codec(TagCodecError::Missing("geohash")));
    }
}