nula-core 0.2.2

Nostr protocol core: events, filters, keys, messages, NIP primitives.
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
//! [NIP-56] Reporting.
//!
//! A `kind: 1984` event signals that some referenced content is
//! objectionable. The spec requires:
//!
//! - one `p` tag identifying the user being reported, and
//! - optionally an `e` tag pointing at a specific note when the
//!   report concerns that note, plus an `x` tag for blob hashes,
//! - a *report type* token as the 3rd column of the `p` / `e` / `x`
//!   target tag (one of the documented strings: `nudity`, `malware`,
//!   `profanity`, `illegal`, `spam`, `impersonation`, `other`).
//!
//! NIP-32 `L`/`l` tags MAY co-exist; we expose them through
//! [`crate::nips::nip32::labels_from_tags`].
//!
//! # Forward compatibility
//!
//! - [`ReportType`] keeps a [`ReportType::Custom`] variant so future
//!   tokens decode cleanly.
//! - Unknown tags survive a round-trip through [`Report::extra_tags`].
//! - `x` blob reports may pin both an `e` host event and one or more
//!   `server` URLs, matching the spec ยง"Tags".
//!
//! [NIP-56]: https://github.com/nostr-protocol/nips/blob/master/56.md

use thiserror::Error;

use crate::event::{
    Alphabet, Event, EventBuilder, EventId, EventIdError, Kind, SingleLetterTag, Tag, TagKind,
};
use crate::key::{PublicKey, PublicKeyError};
use crate::types::{Url, UrlError};

/// `kind: 1984` โ€” reporting event.
pub const KIND_REPORT: Kind = Kind::REPORTING;

/// Wire-defined report types (spec ยง"Tags").
///
/// Unknown tokens decode as [`Self::Custom`] so the parser tolerates
/// new categories introduced by future spec revisions.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ReportType {
    /// `nudity` โ€” depictions of nudity, porn, etc.
    Nudity,
    /// `malware` โ€” virus, worm, ransomware, spyware, etc.
    Malware,
    /// `profanity` โ€” hateful speech.
    Profanity,
    /// `illegal` โ€” content that may be illegal in some jurisdiction.
    Illegal,
    /// `spam`.
    Spam,
    /// `impersonation` โ€” pretending to be someone else (profile-only).
    Impersonation,
    /// `other` โ€” generic catch-all defined by the spec.
    Other,
    /// Forward-compatible passthrough for unrecognised tokens.
    Custom(String),
}

impl ReportType {
    /// Wire token.
    ///
    /// Returns the spec-defined lowercase string or, for
    /// [`Self::Custom`], the inner string slice. The borrow on the
    /// inner string prevents this from being `const`.
    #[must_use]
    #[expect(
        clippy::missing_const_for_fn,
        reason = "`Self::Custom` borrows from a heap `String`"
    )]
    pub fn as_str(&self) -> &str {
        match self {
            Self::Nudity => "nudity",
            Self::Malware => "malware",
            Self::Profanity => "profanity",
            Self::Illegal => "illegal",
            Self::Spam => "spam",
            Self::Impersonation => "impersonation",
            Self::Other => "other",
            Self::Custom(s) => s.as_str(),
        }
    }

    /// Parse a wire token. Always succeeds: unknown tokens decode as
    /// [`Self::Custom`].
    #[must_use]
    pub fn parse(token: &str) -> Self {
        match token {
            "nudity" => Self::Nudity,
            "malware" => Self::Malware,
            "profanity" => Self::Profanity,
            "illegal" => Self::Illegal,
            "spam" => Self::Spam,
            "impersonation" => Self::Impersonation,
            "other" => Self::Other,
            _ => Self::Custom(token.to_owned()),
        }
    }
}

/// What is being reported.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ReportTarget {
    /// `e` tag โ€” reports a specific event. The `p` tag still
    /// identifies the event's author per spec.
    Event {
        /// Note id being reported.
        id: EventId,
        /// Note author. The `p` tag is required by spec even when an
        /// `e` tag is present.
        author: PublicKey,
        /// Optional `ReportType` carried on the `e` tag.
        report_type: Option<ReportType>,
    },
    /// `p`-only tag โ€” reports a profile.
    Profile {
        /// Pubkey being reported.
        pubkey: PublicKey,
        /// `ReportType` token. `impersonation` is only meaningful on
        /// profile reports per spec.
        report_type: Option<ReportType>,
    },
    /// `x` tag โ€” reports a blob by hash. Per spec, an `e` tag with
    /// the host event id MUST accompany the blob report; `servers`
    /// are optional URL hints pointing at media stores.
    Blob {
        /// Blob hash (typically SHA-256 hex).
        hash: String,
        /// Type token associated with the blob.
        report_type: Option<ReportType>,
        /// Host event id (`e` tag), required by spec.
        host_event: EventId,
        /// Host event's report type token (may differ from
        /// [`Self::Blob::report_type`]).
        host_report_type: Option<ReportType>,
        /// `server` URL hints pointing at media stores.
        servers: Vec<Url>,
    },
}

/// Typed bundle for a NIP-56 `kind: 1984` event.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Report {
    /// What is being reported.
    pub target: ReportTarget,
    /// `.content` โ€” free-form rationale from the reporter.
    pub content: String,
    /// Forward-compatible passthrough for unknown tags.
    pub extra_tags: Vec<Tag>,
}

impl Report {
    /// Construct a profile-only report.
    #[must_use]
    pub const fn profile(pubkey: PublicKey, report_type: Option<ReportType>) -> Self {
        Self {
            target: ReportTarget::Profile {
                pubkey,
                report_type,
            },
            content: String::new(),
            extra_tags: Vec::new(),
        }
    }

    /// Construct an event report. `author` must be the note's pubkey.
    #[must_use]
    pub const fn event(id: EventId, author: PublicKey, report_type: Option<ReportType>) -> Self {
        Self {
            target: ReportTarget::Event {
                id,
                author,
                report_type,
            },
            content: String::new(),
            extra_tags: Vec::new(),
        }
    }

    /// Construct a blob-hash report.
    #[must_use]
    pub fn blob(
        hash: impl Into<String>,
        host_event: EventId,
        report_type: Option<ReportType>,
    ) -> Self {
        Self {
            target: ReportTarget::Blob {
                hash: hash.into(),
                report_type: report_type.clone(),
                host_event,
                host_report_type: report_type,
                servers: Vec::new(),
            },
            content: String::new(),
            extra_tags: Vec::new(),
        }
    }

    /// Attach a free-form rationale.
    #[must_use]
    pub fn content(mut self, content: impl Into<String>) -> Self {
        self.content = content.into();
        self
    }

    /// Append a `server` URL hint to a blob report.
    ///
    /// Has no effect for non-blob targets.
    #[must_use]
    pub fn server(mut self, url: Url) -> Self {
        if let ReportTarget::Blob { servers, .. } = &mut self.target {
            servers.push(url);
        }
        self
    }

    /// Parse a `kind: 1984` event into a typed bundle.
    ///
    /// # Errors
    ///
    /// - [`ReportError::WrongKind`] when the event is not
    ///   `kind: 1984`.
    /// - [`ReportError::MissingTarget`] when no target tag is
    ///   present.
    /// - [`ReportError::MissingHostEvent`] when an `x` tag has no
    ///   matching `e` host tag.
    /// - [`ReportError::InvalidPublicKey`] /
    ///   [`ReportError::InvalidEventId`] /
    ///   [`ReportError::InvalidUrl`] when fields fail to parse.
    pub fn from_event(event: &Event) -> Result<Self, ReportError> {
        if event.kind != KIND_REPORT {
            return Err(ReportError::WrongKind(event.kind));
        }

        let mut p_tag: Option<(PublicKey, Option<ReportType>)> = None;
        let mut e_tag: Option<(EventId, Option<ReportType>)> = None;
        let mut x_tag: Option<(String, Option<ReportType>)> = None;
        let mut servers: Vec<Url> = Vec::new();
        let mut extra_tags: Vec<Tag> = Vec::new();

        for tag in &event.tags {
            match tag.kind() {
                TagKind::SingleLetter(s) if !s.uppercase && s.character == Alphabet::P => {
                    p_tag = Some(parse_p_tag(tag)?);
                }
                TagKind::SingleLetter(s) if !s.uppercase && s.character == Alphabet::E => {
                    e_tag = Some(parse_e_tag(tag)?);
                }
                TagKind::SingleLetter(s) if !s.uppercase && s.character == Alphabet::X => {
                    x_tag = Some(parse_x_tag(tag));
                }
                _ if tag.name() == "server" => {
                    let url_str = tag.get(1).ok_or(ReportError::MalformedServer)?;
                    servers.push(Url::parse(url_str)?);
                }
                _ => extra_tags.push(tag.clone()),
            }
        }

        let target = build_target(p_tag, e_tag, x_tag, servers)?;
        Ok(Self {
            target,
            content: event.content.clone(),
            extra_tags,
        })
    }
}

fn build_target(
    p_tag: Option<(PublicKey, Option<ReportType>)>,
    e_tag: Option<(EventId, Option<ReportType>)>,
    x_tag: Option<(String, Option<ReportType>)>,
    servers: Vec<Url>,
) -> Result<ReportTarget, ReportError> {
    match (x_tag, e_tag, p_tag) {
        (Some((hash, x_type)), Some((host_event, e_type)), _) => Ok(ReportTarget::Blob {
            hash,
            report_type: x_type,
            host_event,
            host_report_type: e_type,
            servers,
        }),
        (Some(_), None, _) => Err(ReportError::MissingHostEvent),
        (None, Some((id, e_type)), Some((author, _))) => Ok(ReportTarget::Event {
            id,
            author,
            report_type: e_type,
        }),
        (None, None, Some((pubkey, p_type))) => Ok(ReportTarget::Profile {
            pubkey,
            report_type: p_type,
        }),
        _ => Err(ReportError::MissingTarget),
    }
}

fn parse_p_tag(tag: &Tag) -> Result<(PublicKey, Option<ReportType>), ReportError> {
    let pk_hex = tag.get(1).ok_or(ReportError::MalformedPubkey)?;
    let pubkey = PublicKey::parse(pk_hex)?;
    let report_type = tag.get(2).filter(|s| !s.is_empty()).map(ReportType::parse);
    Ok((pubkey, report_type))
}

fn parse_e_tag(tag: &Tag) -> Result<(EventId, Option<ReportType>), ReportError> {
    let id_hex = tag.get(1).ok_or(ReportError::MalformedEvent)?;
    let id = EventId::parse(id_hex)?;
    let report_type = tag.get(2).filter(|s| !s.is_empty()).map(ReportType::parse);
    Ok((id, report_type))
}

fn parse_x_tag(tag: &Tag) -> (String, Option<ReportType>) {
    let hash = tag.get(1).unwrap_or_default().to_owned();
    let report_type = tag.get(2).filter(|s| !s.is_empty()).map(ReportType::parse);
    (hash, report_type)
}

/// Errors raised by [`Report::from_event`].
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum ReportError {
    /// The event was not `kind: 1984`.
    #[error("expected kind 1984 (report), got kind {}", .0.as_u16())]
    WrongKind(Kind),
    /// No target tag was present.
    #[error("report must include at least one of `p`, `e`, or `x` tags")]
    MissingTarget,
    /// An `x` tag was present without a host `e` tag.
    #[error("`x` blob report must accompany an `e` host event tag")]
    MissingHostEvent,
    /// `p` tag is missing the pubkey column.
    #[error("`p` tag missing pubkey")]
    MalformedPubkey,
    /// `e` tag is missing the event id column.
    #[error("`e` tag missing event id")]
    MalformedEvent,
    /// `server` tag is missing the URL column.
    #[error("`server` tag missing URL")]
    MalformedServer,
    /// `p` pubkey is malformed.
    #[error(transparent)]
    InvalidPublicKey(#[from] PublicKeyError),
    /// `e` event id is malformed.
    #[error(transparent)]
    InvalidEventId(#[from] EventIdError),
    /// `server` URL is malformed.
    #[error(transparent)]
    InvalidUrl(#[from] UrlError),
}

fn p_target(pubkey: PublicKey, report_type: Option<&ReportType>) -> Tag {
    let head = TagKind::single_letter(SingleLetterTag::lowercase(Alphabet::P));
    report_type.map_or_else(
        || Tag::with(&head, [pubkey.to_hex()]),
        |rt| Tag::with(&head, [pubkey.to_hex(), rt.as_str().to_owned()]),
    )
}

fn e_target(id: EventId, report_type: Option<&ReportType>) -> Tag {
    let head = TagKind::single_letter(SingleLetterTag::lowercase(Alphabet::E));
    report_type.map_or_else(
        || Tag::with(&head, [id.to_hex()]),
        |rt| Tag::with(&head, [id.to_hex(), rt.as_str().to_owned()]),
    )
}

fn x_target(hash: &str, report_type: Option<&ReportType>) -> Tag {
    let head = TagKind::single_letter(SingleLetterTag::lowercase(Alphabet::X));
    report_type.map_or_else(
        || Tag::with(&head, [hash.to_owned()]),
        |rt| Tag::with(&head, [hash.to_owned(), rt.as_str().to_owned()]),
    )
}

impl EventBuilder {
    /// Author a NIP-56 `kind: 1984` report event.
    ///
    /// Tag order matches the spec examples:
    ///
    /// 1. Primary target tag (`p`, `e`, or `x`).
    /// 2. Secondary tags (the `p` author for an `e` report or the
    ///    host `e` for an `x` report).
    /// 3. `server` URL hints, if any.
    /// 4. Caller-supplied [`Report::extra_tags`].
    #[must_use]
    pub fn report(report: &Report) -> Self {
        let mut builder = Self::new(KIND_REPORT, report.content.clone());
        match &report.target {
            ReportTarget::Profile {
                pubkey,
                report_type,
            } => {
                builder = builder.tag(p_target(*pubkey, report_type.as_ref()));
            }
            ReportTarget::Event {
                id,
                author,
                report_type,
            } => {
                builder = builder.tag(e_target(*id, report_type.as_ref()));
                builder = builder.tag(p_target(*author, None));
            }
            ReportTarget::Blob {
                hash,
                report_type,
                host_event,
                host_report_type,
                servers,
            } => {
                builder = builder.tag(x_target(hash, report_type.as_ref()));
                builder = builder.tag(e_target(*host_event, host_report_type.as_ref()));
                for server in servers {
                    builder = builder.tag(Tag::with(
                        &TagKind::from_wire("server"),
                        [server.as_str().to_owned()],
                    ));
                }
            }
        }
        for tag in &report.extra_tags {
            builder = builder.tag(tag.clone());
        }
        builder
    }
}

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

    fn keys() -> Keys {
        Keys::parse("0000000000000000000000000000000000000000000000000000000000000003").unwrap()
    }

    fn other_pubkey() -> PublicKey {
        *Keys::parse("0000000000000000000000000000000000000000000000000000000000000004")
            .unwrap()
            .public_key()
    }

    #[test]
    fn report_type_wire_tokens_round_trip() {
        for token in [
            "nudity",
            "malware",
            "profanity",
            "illegal",
            "spam",
            "impersonation",
            "other",
        ] {
            assert_eq!(ReportType::parse(token).as_str(), token);
        }
        assert_eq!(ReportType::parse("new-category").as_str(), "new-category",);
    }

    #[test]
    fn profile_report_round_trip() {
        let report = Report::profile(other_pubkey(), Some(ReportType::Impersonation))
            .content("not the king");
        let event = EventBuilder::report(&report)
            .sign_with_keys(&keys())
            .unwrap();
        let parsed = Report::from_event(&event).unwrap();
        assert_eq!(parsed, report);
    }

    #[test]
    fn event_report_round_trip() {
        let id = EventId::from_byte_array([0x07; 32]);
        let report = Report::event(id, other_pubkey(), Some(ReportType::Illegal))
            .content("contains illegal speech");
        let event = EventBuilder::report(&report)
            .sign_with_keys(&keys())
            .unwrap();
        let parsed = Report::from_event(&event).unwrap();
        assert_eq!(parsed, report);
    }

    #[test]
    fn blob_report_round_trip_with_server_hints() {
        let host = EventId::from_byte_array([0x09; 32]);
        let report = Report::blob("abc123def", host, Some(ReportType::Malware))
            .server(Url::parse("https://media.example.com/blob.bin").unwrap())
            .content("contains malware");
        let event = EventBuilder::report(&report)
            .sign_with_keys(&keys())
            .unwrap();
        let parsed = Report::from_event(&event).unwrap();
        assert_eq!(parsed, report);
    }

    #[test]
    fn wrong_kind_is_rejected() {
        let event = EventBuilder::text_note("nope")
            .sign_with_keys(&keys())
            .unwrap();
        assert!(matches!(
            Report::from_event(&event),
            Err(ReportError::WrongKind(_)),
        ));
    }

    #[test]
    fn missing_target_is_rejected() {
        let event = EventBuilder::new(KIND_REPORT, "")
            .sign_with_keys(&keys())
            .unwrap();
        assert!(matches!(
            Report::from_event(&event),
            Err(ReportError::MissingTarget),
        ));
    }

    #[test]
    fn blob_without_host_event_is_rejected() {
        let event = EventBuilder::new(KIND_REPORT, "")
            .tag(Tag::with(
                &TagKind::single_letter(SingleLetterTag::lowercase(Alphabet::X)),
                ["abc", "malware"],
            ))
            .sign_with_keys(&keys())
            .unwrap();
        assert!(matches!(
            Report::from_event(&event),
            Err(ReportError::MissingHostEvent),
        ));
    }

    #[test]
    fn unknown_report_type_decodes_as_custom() {
        let report = Report::profile(other_pubkey(), Some(ReportType::Custom("doxx".into())));
        let event = EventBuilder::report(&report)
            .sign_with_keys(&keys())
            .unwrap();
        let parsed = Report::from_event(&event).unwrap();
        assert_eq!(parsed, report);
    }

    #[test]
    fn extra_tags_are_preserved_on_round_trip() {
        let custom = Tag::with(&TagKind::Custom("note".to_owned()), ["context"]);
        let report = Report::profile(other_pubkey(), None);
        let mut builder = EventBuilder::report(&report);
        builder = builder.tag(custom.clone());
        let event = builder.sign_with_keys(&keys()).unwrap();
        let parsed = Report::from_event(&event).unwrap();
        assert_eq!(parsed.extra_tags, vec![custom]);
    }
}