nostr 0.44.3

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
// Copyright (c) 2022-2023 Yuki Kishimoto
// Copyright (c) 2023-2025 Rust Nostr Developers
// Distributed under the MIT software license

//! Tag kind

use alloc::borrow::Cow;
use core::cmp::Ordering;
use core::fmt;
use core::hash::{Hash, Hasher};
use core::str::FromStr;

use crate::{Alphabet, SingleLetterTag};

/// Tag kind
#[derive(Debug, Clone)]
pub enum TagKind<'a> {
    /// AES 256 GCM
    Aes256Gcm,
    /// Human-readable plaintext summary of what that event is about
    ///
    /// <https://github.com/nostr-protocol/nips/blob/master/31.md>
    Alt,
    /// Amount
    Amount,
    /// Anonymous
    Anon,
    /// Blurhash
    Blurhash,
    /// Bolt11 invoice
    Bolt11,
    /// Challenge
    Challenge,
    /// Client
    ///
    /// <https://github.com/nostr-protocol/nips/blob/master/89.md>
    Client,
    /// Clone
    Clone,
    /// Commit
    ///
    /// <https://github.com/nostr-protocol/nips/blob/master/34.md>
    Commit,
    /// HEAD
    ///
    /// <https://github.com/nostr-protocol/nips/blob/master/34.md>
    Head,
    /// Content warning
    ContentWarning,
    /// Current participants
    CurrentParticipants,
    /// Required dependency
    ///
    /// <https://github.com/nostr-protocol/nips/blob/master/C0.md>
    Dependency,
    /// Description
    Description,
    /// Size of the file in pixels
    Dim,
    /// Emoji
    Emoji,
    /// Encrypted
    Encrypted,
    /// Ends
    Ends,
    /// Expiration
    ///
    /// <https://github.com/nostr-protocol/nips/blob/master/40.md>
    Expiration,
    /// File extension
    ///
    /// <https://github.com/nostr-protocol/nips/blob/master/C0.md>
    Extension,
    /// File
    File,
    /// Image
    Image,
    /// License of the shared content
    ///
    /// <https://github.com/nostr-protocol/nips/blob/master/C0.md>
    License,
    /// Lnurl
    Lnurl,
    /// Magnet
    Magnet,
    /// Maintainers
    Maintainers,
    /// HTTP Method Request
    Method,
    /// MLS Protocol Version
    MlsProtocolVersion,
    /// MLS Cipher Suite
    MlsCiphersuite,
    /// MLS Extensions
    MlsExtensions,
    /// Name
    Name,
    /// Nonce
    ///
    /// <https://github.com/nostr-protocol/nips/blob/master/13.md>
    Nonce,
    /// Option
    Option,
    /// Payload
    Payload,
    /// Poll type
    ///
    /// <https://github.com/nostr-protocol/nips/blob/master/88.md>
    PollType,
    /// Preimage
    Preimage,
    /// Protected event
    ///
    /// <https://github.com/nostr-protocol/nips/blob/master/70.md>
    Protected,
    /// Proxy
    Proxy,
    /// PublishedAt
    PublishedAt,
    /// Recording
    Recording,
    /// Relay
    Relay,
    /// Relays
    Relays,
    /// Reference to the origin repository
    ///
    /// <https://github.com/nostr-protocol/nips/blob/master/C0.md>
    Repository,
    /// Request
    Request,
    /// Response
    Response,
    /// Runtime or environment specification
    ///
    /// <https://github.com/nostr-protocol/nips/blob/master/C0.md>
    Runtime,
    /// Server
    Server,
    /// Size of the file in bytes
    Size,
    /// Starts
    Starts,
    /// Status
    Status,
    /// Streaming
    Streaming,
    /// Subject
    Subject,
    /// Summary
    Summary,
    /// Title
    Title,
    /// Thumbnail
    Thumb,
    /// Total participants
    TotalParticipants,
    /// Tracker
    Tracker,
    /// Url
    Url,
    /// Web
    Web,
    /// Word
    Word,
    /// Single letter
    SingleLetter(SingleLetterTag),
    /// Custom
    Custom(Cow<'a, str>),
}

impl PartialEq for TagKind<'_> {
    fn eq(&self, other: &TagKind<'_>) -> bool {
        self.as_str() == other.as_str()
    }
}

impl Eq for TagKind<'_> {}

impl PartialOrd for TagKind<'_> {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for TagKind<'_> {
    fn cmp(&self, other: &Self) -> Ordering {
        self.as_str().cmp(other.as_str())
    }
}

impl Hash for TagKind<'_> {
    fn hash<H>(&self, state: &mut H)
    where
        H: Hasher,
    {
        self.as_str().hash(state);
    }
}

impl<'a> TagKind<'a> {
    /// Construct a single letter tag
    #[inline]
    pub const fn single_letter(character: Alphabet, uppercase: bool) -> Self {
        Self::SingleLetter(SingleLetterTag {
            character,
            uppercase,
        })
    }

    /// Construct `a` kind
    ///
    /// Shorthand for `TagKind::SingleLetter(SingleLetterTag::lowercase(Alphabet::A))`.
    #[inline]
    pub const fn a() -> Self {
        Self::single_letter(Alphabet::A, false)
    }

    /// Construct `d` kind
    ///
    /// Shorthand for `TagKind::SingleLetter(SingleLetterTag::lowercase(Alphabet::D))`.
    #[inline]
    pub const fn d() -> Self {
        Self::single_letter(Alphabet::D, false)
    }

    /// Construct `e` kind
    ///
    /// Shorthand for `TagKind::SingleLetter(SingleLetterTag::lowercase(Alphabet::E))`.
    #[inline]
    pub const fn e() -> Self {
        Self::single_letter(Alphabet::E, false)
    }

    /// Construct `h` kind
    ///
    /// Shorthand for `TagKind::SingleLetter(SingleLetterTag::lowercase(Alphabet::H))`.
    #[inline]
    pub const fn h() -> Self {
        Self::single_letter(Alphabet::H, false)
    }

    /// Construct `i` kind
    ///
    /// Shorthand for `TagKind::SingleLetter(SingleLetterTag::lowercase(Alphabet::I))`.
    #[inline]
    pub const fn i() -> Self {
        Self::single_letter(Alphabet::I, false)
    }

    /// Construct `k` kind
    ///
    /// Shorthand for `TagKind::SingleLetter(SingleLetterTag::lowercase(Alphabet::K))`.
    #[inline]
    pub const fn k() -> Self {
        Self::single_letter(Alphabet::K, false)
    }

    /// Construct `p` kind
    ///
    /// Shorthand for `TagKind::SingleLetter(SingleLetterTag::lowercase(Alphabet::P))`.
    #[inline]
    pub const fn p() -> Self {
        Self::single_letter(Alphabet::P, false)
    }

    /// Construct `t` kind
    ///
    /// Shorthand for `TagKind::SingleLetter(SingleLetterTag::lowercase(Alphabet::T))`.
    #[inline]
    pub const fn t() -> Self {
        Self::single_letter(Alphabet::T, false)
    }

    /// Construct `u` kind
    ///
    /// Shorthand for `TagKind::SingleLetter(SingleLetterTag::lowercase(Alphabet::U))`.
    #[inline]
    pub const fn u() -> Self {
        Self::single_letter(Alphabet::U, false)
    }

    /// Construct `q` kind
    ///
    /// Shorthand for `TagKind::SingleLetter(SingleLetterTag::lowercase(Alphabet::Q))`.
    #[inline]
    pub const fn q() -> Self {
        Self::single_letter(Alphabet::Q, false)
    }

    /// Construct `x` kind
    ///
    /// Shorthand for `TagKind::SingleLetter(SingleLetterTag::lowercase(Alphabet::X))`.
    #[inline]
    pub const fn x() -> Self {
        Self::single_letter(Alphabet::X, false)
    }

    /// Construct [`TagKind::Custom`]
    ///
    /// Shorthand for `TagKind::Custom(Cow::from(...))`.
    #[inline]
    pub fn custom<T>(kind: T) -> Self
    where
        T: Into<Cow<'a, str>>,
    {
        Self::Custom(kind.into())
    }

    /// Convert to `&str`
    pub fn as_str(&self) -> &str {
        match self {
            Self::Aes256Gcm => "aes-256-gcm",
            Self::Alt => "alt",
            Self::Amount => "amount",
            Self::Anon => "anon",
            Self::Blurhash => "blurhash",
            Self::Bolt11 => "bolt11",
            Self::Challenge => "challenge",
            Self::Client => "client",
            Self::Clone => "clone",
            Self::Commit => "commit",
            Self::ContentWarning => "content-warning",
            Self::CurrentParticipants => "current_participants",
            Self::Dependency => "dep",
            Self::Description => "description",
            Self::Dim => "dim",
            Self::Emoji => "emoji",
            Self::Encrypted => "encrypted",
            Self::Ends => "ends",
            Self::Expiration => "expiration",
            Self::Extension => "extension",
            Self::File => "file",
            Self::Head => "HEAD",
            Self::Image => "image",
            Self::License => "license",
            Self::Lnurl => "lnurl",
            Self::Magnet => "magnet",
            Self::Maintainers => "maintainers",
            Self::Method => "method",
            Self::MlsProtocolVersion => "mls_protocol_version",
            Self::MlsCiphersuite => "mls_ciphersuite",
            Self::MlsExtensions => "mls_extensions",
            Self::Name => "name",
            Self::Nonce => "nonce",
            Self::Option => "option",
            Self::Payload => "payload",
            Self::PollType => "polltype",
            Self::Preimage => "preimage",
            Self::Protected => "-",
            Self::Proxy => "proxy",
            Self::PublishedAt => "published_at",
            Self::Recording => "recording",
            Self::Relay => "relay",
            Self::Relays => "relays",
            Self::Repository => "repo",
            Self::Request => "request",
            Self::Response => "response",
            Self::Runtime => "runtime",
            Self::Server => "server",
            Self::Size => "size",
            Self::Starts => "starts",
            Self::Status => "status",
            Self::Streaming => "streaming",
            Self::Subject => "subject",
            Self::Summary => "summary",
            Self::Title => "title",
            Self::Thumb => "thumb",
            Self::TotalParticipants => "total_participants",
            Self::Tracker => "tracker",
            Self::Url => "url",
            Self::Web => "web",
            Self::Word => "word",
            Self::SingleLetter(s) => s.as_str(),
            Self::Custom(tag) => tag.as_ref(),
        }
    }
}

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

impl<'a> From<&'a str> for TagKind<'a> {
    fn from(kind: &'a str) -> Self {
        match kind {
            "-" => Self::Protected,
            "aes-256-gcm" => Self::Aes256Gcm,
            "alt" => Self::Alt,
            "amount" => Self::Amount,
            "anon" => Self::Anon,
            "blurhash" => Self::Blurhash,
            "bolt11" => Self::Bolt11,
            "challenge" => Self::Challenge,
            "client" => Self::Client,
            "clone" => Self::Clone,
            "commit" => Self::Commit,
            "content-warning" => Self::ContentWarning,
            "current_participants" => Self::CurrentParticipants,
            "dep" => Self::Dependency,
            "description" => Self::Description,
            "dim" => Self::Dim,
            "emoji" => Self::Emoji,
            "encrypted" => Self::Encrypted,
            "ends" => Self::Ends,
            "expiration" => Self::Expiration,
            "extension" => Self::Extension,
            "file" => Self::File,
            "image" => Self::Image,
            "license" => Self::License,
            "lnurl" => Self::Lnurl,
            "magnet" => Self::Magnet,
            "maintainers" => Self::Maintainers,
            "method" => Self::Method,
            "mls_protocol_version" => Self::MlsProtocolVersion,
            "mls_ciphersuite" => Self::MlsCiphersuite,
            "mls_extensions" => Self::MlsExtensions,
            "name" => Self::Name,
            "nonce" => Self::Nonce,
            "option" => Self::Option,
            "payload" => Self::Payload,
            "polltype" => Self::PollType,
            "preimage" => Self::Preimage,
            "proxy" => Self::Proxy,
            "published_at" => Self::PublishedAt,
            "recording" => Self::Recording,
            "relay" => Self::Relay,
            "relays" => Self::Relays,
            "repo" => Self::Repository,
            "request" => Self::Request,
            "response" => Self::Response,
            "runtime" => Self::Runtime,
            "HEAD" => Self::Head,
            "server" => Self::Server,
            "size" => Self::Size,
            "starts" => Self::Starts,
            "status" => Self::Status,
            "streaming" => Self::Streaming,
            "subject" => Self::Subject,
            "summary" => Self::Summary,
            "title" => Self::Title,
            "thumb" => Self::Thumb,
            "total_participants" => Self::TotalParticipants,
            "tracker" => Self::Tracker,
            "url" => Self::Url,
            "web" => Self::Web,
            "word" => Self::Word,
            k => match SingleLetterTag::from_str(k) {
                Ok(s) => Self::SingleLetter(s),
                Err(..) => Self::Custom(Cow::Borrowed(k)),
            },
        }
    }
}

#[cfg(test)]
mod tests {
    use alloc::string::String;

    use super::*;

    #[test]
    fn test_custom_tag_kind_constructor() {
        let owned = TagKind::custom(String::from("owned"));
        match owned {
            TagKind::Custom(Cow::Owned(val)) => assert_eq!(val, "owned"),
            _ => panic!("Unexpected tag kind"),
        };

        let borrowed = TagKind::custom("borrowed");
        match borrowed {
            TagKind::Custom(Cow::Borrowed(val)) => assert_eq!(val, "borrowed"),
            _ => panic!("Unexpected tag kind"),
        };
    }

    #[test]
    fn test_de_serialization() {
        assert_eq!(TagKind::from("-"), TagKind::Protected);
        assert_eq!(TagKind::Protected.as_str(), "-");

        assert_eq!(TagKind::from("aes-256-gcm"), TagKind::Aes256Gcm);
        assert_eq!(TagKind::Aes256Gcm.as_str(), "aes-256-gcm");

        assert_eq!(TagKind::from("alt"), TagKind::Alt);
        assert_eq!(TagKind::Alt.as_str(), "alt");

        assert_eq!(TagKind::from("amount"), TagKind::Amount);
        assert_eq!(TagKind::Amount.as_str(), "amount");

        assert_eq!(TagKind::from("anon"), TagKind::Anon);
        assert_eq!(TagKind::Anon.as_str(), "anon");

        assert_eq!(TagKind::from("clone"), TagKind::Clone);
        assert_eq!(TagKind::Clone.as_str(), "clone");

        assert_eq!(TagKind::from("dep"), TagKind::Dependency);
        assert_eq!(TagKind::Dependency.as_str(), "dep");

        assert_eq!(TagKind::from("expiration"), TagKind::Expiration);
        assert_eq!(TagKind::Expiration.as_str(), "expiration");

        assert_eq!(TagKind::from("extension"), TagKind::Extension);
        assert_eq!(TagKind::Extension.as_str(), "extension");

        assert_eq!(TagKind::from("file"), TagKind::File);
        assert_eq!(TagKind::File.as_str(), "file");

        assert_eq!(TagKind::from("HEAD"), TagKind::Head);
        assert_eq!(TagKind::Head.as_str(), "HEAD");

        assert_eq!(TagKind::from("license"), TagKind::License);
        assert_eq!(TagKind::License.as_str(), "license");

        assert_eq!(TagKind::from("maintainers"), TagKind::Maintainers);
        assert_eq!(TagKind::Maintainers.as_str(), "maintainers");

        assert_eq!(TagKind::from("option"), TagKind::Option);
        assert_eq!(TagKind::Option.as_str(), "option");

        assert_eq!(TagKind::from("polltype"), TagKind::PollType);
        assert_eq!(TagKind::PollType.as_str(), "polltype");

        assert_eq!(TagKind::from("repo"), TagKind::Repository);
        assert_eq!(TagKind::Repository.as_str(), "repo");

        assert_eq!(TagKind::from("response"), TagKind::Response);
        assert_eq!(TagKind::Response.as_str(), "response");

        assert_eq!(TagKind::from("runtime"), TagKind::Runtime);
        assert_eq!(TagKind::Runtime.as_str(), "runtime");

        assert_eq!(TagKind::from("tracker"), TagKind::Tracker);
        assert_eq!(TagKind::Tracker.as_str(), "tracker");

        assert_eq!(TagKind::from("web"), TagKind::Web);
        assert_eq!(TagKind::Web.as_str(), "web");

        assert_eq!(TagKind::from("a"), TagKind::a());
        assert_eq!(TagKind::a().as_str(), "a");

        assert_eq!(TagKind::from("e"), TagKind::e());
        assert_eq!(TagKind::e().as_str(), "e");

        assert_eq!(TagKind::from("p"), TagKind::p());
        assert_eq!(TagKind::p().as_str(), "p");
    }

    #[test]
    fn test_eq() {
        assert_eq!(TagKind::Custom(Cow::from("-")), TagKind::Protected);
        assert_eq!(TagKind::Custom(Cow::from("p")), TagKind::p());
        assert_eq!(
            TagKind::SingleLetter(SingleLetterTag::lowercase(Alphabet::P)),
            TagKind::p()
        );
        assert_eq!(TagKind::Custom(Cow::from("e")), TagKind::e());
        assert_eq!(
            TagKind::SingleLetter(SingleLetterTag::lowercase(Alphabet::E)),
            TagKind::e()
        );
    }
}