nk-core 0.16.0

Nostr protocol core: events, keys, filters, messages and NIP codecs; no_std, sans-IO.
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
//! NIP-10 thread references: parsing marked and legacy positional `e` tags
//! plus `q` quotes and `p` profiles out of an event's tag list, and building
//! reply tags in the preferred marked style.
//!
//! [`ThreadReferences`] parses the `e`/`q`/`p` thread tags;
//! `reply_tags`/`text_reply` mirror `buildReplyTags`/`replyTo` (marked
//! `root`/`reply` style, `q` tags appended last). Relay strings read from
//! tags stay raw [`String`]s and are written back verbatim; the optional
//! caller-supplied relay hint is a normalized [`RelayUrl`] (NK3-00 N4).
//!
//! @see <https://github.com/nostr-protocol/nips/blob/master/10.md>

use alloc::string::{String, ToString};
use alloc::vec::Vec;

use crate::{Coordinate, Event, EventBuilder, EventId, Kind, PublicKey, RelayUrl, Tag, Tags};

use crate::nips::nip19::{AddressPointer, EventPointer, ProfilePointer};

/// The error type returned by NIP-10 operations.
#[non_exhaustive]
#[derive(Debug, thiserror::Error)]
pub enum Error {
    /// A NIP-10 operation was attempted on a non-kind-1 event.
    #[error("{0}")]
    Validation(&'static str),
}

/// Result type for NIP-10 operations.
pub type Result<T, E = Error> = core::result::Result<T, E>;

/// A quoted event (`q` tag): an event id or an addressable coordinate.
/// Discriminate by variant — event quotes carry an id, address quotes an address.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum Quote {
    /// An `EventPointer` (`id` plus relay/author hints).
    Event(EventPointer),
    /// An `AddressPointer` (`kind:pubkey:identifier`).
    Address(AddressPointer),
}

/// Parsed NIP-10 thread references from an event's `e`/`q`/`p` tags.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct ThreadReferences {
    /// Pointer to the root of the thread.
    pub root: Option<EventPointer>,
    /// Pointer to the parent event the note replies to.
    pub reply: Option<EventPointer>,
    /// Other e-tagged events (not root/reply).
    pub mentions: Vec<EventPointer>,
    /// Quoted events (`q` tags), in scan order.
    pub quotes: Vec<Quote>,
    /// P-tagged profiles involved in the thread.
    pub profiles: Vec<ProfilePointer>,
}

/// 64-hex of any case is accepted and normalized to lowercase.
fn is_hex32(value: &str) -> bool {
    EventId::from_wire_hex(value).is_ok()
}

/// `tag[i]` as a one-element relay list, or empty when absent/`""` — the TS
/// `tag[2] !== undefined && tag[2] !== "" ? [tag[2]] : []` pattern. The string
/// is kept verbatim (hints are opaque, never normalized).
fn relay_slot(items: &[String], index: usize) -> Vec<String> {
    match items.get(index) {
        Some(value) if !value.is_empty() => alloc::vec![value.clone()],
        _ => Vec::new(),
    }
}

/// The NIP-10 5-tuple pubkey is index 4; the NIP-01 4-tuple pubkey is index 3.
fn e_tag_author(items: &[String]) -> Option<PublicKey> {
    for index in [4usize, 3] {
        if let Some(author) = items.get(index)
            && let Ok(pubkey) = PublicKey::from_wire_hex(author)
        {
            return Some(pubkey);
        }
    }
    None
}

/// `["e", id, relay?, …]` with a mixed-case
/// hex32 id; relay hint kept verbatim, author from index 4 or 3.
fn event_pointer(items: &[String]) -> Option<EventPointer> {
    let id = EventId::from_wire_hex(items.get(1)?).ok()?;
    let mut pointer = EventPointer {
        id,
        relays: relay_slot(items, 2),
        author: None,
        kind: None,
    };
    pointer.author = e_tag_author(items);
    Some(pointer)
}

/// A `q` tag whose value is hex32 is an [`EventPointer`] (index 3 is the
/// author slot); anything else is parsed as an address and invalid values are
/// ignored. Address-form `q` tags never use index 3.
fn quote_from_tag(items: &[String]) -> Option<Quote> {
    let value = items.get(1)?;
    if value.is_empty() {
        return None;
    }
    let relays = relay_slot(items, 2);
    if let Ok(id) = EventId::from_wire_hex(value) {
        let author = items.get(3).and_then(|a| PublicKey::from_wire_hex(a).ok());
        return Some(Quote::Event(EventPointer {
            id,
            relays,
            author,
            kind: None,
        }));
    }
    let address = Coordinate::from_wire_str(value).ok()?;
    Some(Quote::Address(AddressPointer {
        identifier: String::from(address.identifier()),
        pubkey: address.pubkey(),
        kind: address.kind(),
        relays,
    }))
}

/// Merges a matching `p` tag's relay hints into `ptr` (dedup, order kept) —
/// the hint-inheritance step.
fn inherit_hints(mut ptr: EventPointer, profiles: &[ProfilePointer]) -> EventPointer {
    let Some(author) = ptr.author else {
        return ptr;
    };
    let Some(profile) = profiles.iter().find(|p| p.pubkey == author) else {
        return ptr;
    };
    for url in &profile.relays {
        if !ptr.relays.contains(url) {
            ptr.relays.push(url.clone());
        }
    }
    ptr
}

/// Parses NIP-10 thread markers and legacy positional e-tags from `tags`.
///
/// The scan runs from the last
/// tag backwards; `root`/`reply` markers win; a 64-hex index 3 is a NIP-01
/// pubkey, not a marker; without markers the last unmarked `e` is the parent
/// and the second-to-last the root; `root`/`reply` back-fill each other;
/// mentions drop whichever ids became root or reply; event pointers inherit
/// relay hints from a `p` tag on the same author (dedup, order kept).
/// `q` and `p` entries collect in the same scan order.
impl From<&Tags> for ThreadReferences {
    /// Extracts the NIP-10 thread references from `e`/`q`/`p` tags (TS
    /// `parseThreadTags`).
    fn from(tags: &Tags) -> Self {
        parse_thread_tags(tags)
    }
}

fn parse_thread_tags(tags: &Tags) -> ThreadReferences {
    let mut mentions = Vec::new();
    let mut quotes = Vec::new();
    let mut profiles = Vec::new();
    let mut root: Option<EventPointer> = None;
    let mut reply: Option<EventPointer> = None;
    let mut maybe_parent: Option<EventPointer> = None;
    let mut maybe_root: Option<EventPointer> = None;

    for tag in tags.iter().rev() {
        let items = tag.as_slice();
        let value = items.get(1);
        if tag.name() == "e" && value.is_some_and(|v| is_hex32(v)) {
            let Some(pointer) = event_pointer(items) else {
                continue;
            };
            let marker = items.get(3).map(String::as_str);

            if marker == Some("root") {
                root = Some(pointer);
                continue;
            }
            if marker == Some("reply") {
                reply = Some(pointer);
                continue;
            }
            // Preferred markers are root/reply only. A hex32 at index 3 is a
            // NIP-01 pubkey, not a marker.
            if let Some(marker) = marker
                && !marker.is_empty()
                && !is_hex32(marker)
            {
                mentions.push(pointer);
                continue;
            }

            // Legacy positional: last unmarked is parent, second-to-last root.
            if maybe_parent.is_some() {
                maybe_root = Some(pointer.clone());
            } else {
                maybe_parent = Some(pointer.clone());
            }
            mentions.push(pointer);
            continue;
        }

        if tag.name() == "q" {
            if let Some(quote) = quote_from_tag(items) {
                quotes.push(quote);
            }
            continue;
        }

        if tag.name() == "p"
            && let Some(p_value) = value
            && let Ok(pubkey) = PublicKey::from_wire_hex(p_value)
        {
            profiles.push(ProfilePointer {
                pubkey,
                relays: relay_slot(items, 2),
            });
        }
    }

    let root = root
        .or(maybe_root)
        .or_else(|| maybe_parent.clone())
        .or_else(|| reply.clone());
    let reply = reply.or(maybe_parent).or_else(|| root.clone());

    // Drop root/reply from mentions (by id).
    let drop: Vec<EventId> = [root.as_ref(), reply.as_ref()]
        .into_iter()
        .flatten()
        .map(|p| p.id)
        .collect();
    mentions.retain(|m| !drop.contains(&m.id));

    ThreadReferences {
        root: root.map(|r| inherit_hints(r, &profiles)),
        reply: reply.map(|r| inherit_hints(r, &profiles)),
        mentions: mentions
            .into_iter()
            .map(|m| inherit_hints(m, &profiles))
            .collect(),
        quotes,
        profiles,
    }
}

/// Positional tag values, exactly like `Tag::event`/`Tag::public_key` but with
/// the raw relay string tags carry (a typed [`RelayUrl`] cannot express a
/// verbatim `""` or unnormalized hint): absent slots before a present one
/// emit `""`, trailing absent slots are omitted.
fn push_positions(items: &mut Vec<String>, positions: &[Option<String>]) {
    let end = positions
        .iter()
        .rposition(Option::is_some)
        .map_or(0, |i| i + 1);
    for position in positions.iter().take(end) {
        items.push(position.clone().unwrap_or_default());
    }
}

/// `["e", id, relay, marker, pubkey]` with `Tag.e` positional trimming.
fn e_tag(
    id: EventId,
    relay: Option<String>,
    marker: Option<&str>,
    pubkey: Option<PublicKey>,
) -> Tag {
    let mut items = alloc::vec![id.to_hex()];
    push_positions(
        &mut items,
        &[
            relay,
            marker.map(String::from),
            pubkey.map(PublicKey::to_hex),
        ],
    );
    Tag::custom("e", items)
}

/// `["p", pubkey, relay]` with `Tag.p` positional trimming.
fn p_tag(pubkey: PublicKey, relay: Option<String>) -> Tag {
    let mut items = alloc::vec![pubkey.to_hex()];
    push_positions(&mut items, &[relay]);
    Tag::custom("p", items)
}

/// Appends `["p", pubkey, relay?]`, once per pubkey (first occurrence kept).
fn add_p_tag(
    tags: &mut Vec<Tag>,
    seen: &mut Vec<PublicKey>,
    pubkey: PublicKey,
    relay: Option<String>,
) {
    if seen.contains(&pubkey) {
        return;
    }
    seen.push(pubkey);
    tags.push(p_tag(pubkey, relay));
}

/// A built `q` tag plus the author/relay hints TS uses to p-tag the author.
struct BuiltQuote {
    tag: Tag,
    author: Option<PublicKey>,
    relay: Option<String>,
}

/// A typed quote serializes as `["q", id]` / `["q", id, relay]` /
/// `["q", id, relay, author]` for events (author forces the relay slot, `""`
/// when absent) and `["q", coord]` / `["q", coord, relay]` for addresses.
fn quote_to_tag(quote: &Quote) -> BuiltQuote {
    match quote {
        Quote::Event(pointer) => {
            let relay = pointer.relays.first().cloned();
            match pointer.author {
                Some(author) => BuiltQuote {
                    tag: Tag::custom(
                        "q",
                        [
                            pointer.id.to_hex(),
                            relay.clone().unwrap_or_default(),
                            author.to_hex(),
                        ],
                    ),
                    author: Some(author),
                    relay,
                },
                None => BuiltQuote {
                    tag: match &relay {
                        Some(relay) if !relay.is_empty() => {
                            Tag::custom("q", [pointer.id.to_hex(), relay.clone()])
                        }
                        _ => Tag::custom("q", [pointer.id.to_hex()]),
                    },
                    author: None,
                    relay,
                },
            }
        }
        Quote::Address(pointer) => {
            let coordinate =
                Coordinate::new(pointer.kind, pointer.pubkey, &pointer.identifier).to_string();
            let relay = pointer.relays.first().cloned();
            let tag = match &relay {
                Some(relay) if !relay.is_empty() => Tag::custom("q", [coordinate, relay.clone()]),
                _ => Tag::custom("q", [coordinate]),
            };
            BuiltQuote {
                tag,
                author: Some(pointer.pubkey),
                relay,
            }
        }
    }
}

/// NIP-10 e/p/q tags for a reply to `parent`.
///
/// Preferred marked style: an `e` tag for the thread root (`root` marker,
/// the root's first relay hint falling back to `relay_hint`), an `e` tag for
/// `parent` (`reply` marker, skipped when the parent *is* the root), `p`
/// tags for the root author, the parent author, every thread profile, and
/// quoted-event authors (deduped, first-seen order), then the `q` tags last.
///
/// # Errors
///
/// [`Error::Validation`] `NIP-10 replyTo is for kind 1` — NIP-10 is
/// kind 1 only (comments are NIP-22).
pub(crate) fn reply_tags(
    parent: &Event,
    relay_hint: Option<&RelayUrl>,
    quotes: &[Quote],
) -> Result<Tags> {
    if parent.kind() != Kind::TEXT_NOTE {
        return Err(Error::Validation("NIP-10 replyTo is for kind 1"));
    }
    let thread = parse_thread_tags(parent.tags());
    let root = thread.root.unwrap_or_else(|| EventPointer {
        id: parent.id(),
        relays: Vec::new(),
        author: Some(parent.pubkey()),
        kind: None,
    });
    let parent_is_root = root.id == parent.id();

    let mut tags = Vec::new();
    let root_relay = root
        .relays
        .first()
        .cloned()
        .or_else(|| relay_hint.map(|url| String::from(url.as_str())))
        .unwrap_or_default();
    tags.push(e_tag(root.id, Some(root_relay), Some("root"), root.author));

    if !parent_is_root {
        tags.push(e_tag(
            parent.id(),
            Some(relay_hint.map_or_else(String::new, |url| String::from(url.as_str()))),
            Some("reply"),
            Some(parent.pubkey()),
        ));
    }

    // Ensure root + parent authors are p-tagged.
    let mut seen: Vec<PublicKey> = Vec::new();
    if let Some(author) = root.author {
        add_p_tag(&mut tags, &mut seen, author, root.relays.first().cloned());
    }
    add_p_tag(
        &mut tags,
        &mut seen,
        parent.pubkey(),
        relay_hint.map(|url| String::from(url.as_str())),
    );
    for profile in &thread.profiles {
        add_p_tag(
            &mut tags,
            &mut seen,
            profile.pubkey,
            profile.relays.first().cloned(),
        );
    }

    let mut q_tags = Vec::new();
    for quote in quotes {
        let built = quote_to_tag(quote);
        if let Some(author) = built.author {
            add_p_tag(&mut tags, &mut seen, author, built.relay.clone());
        }
        q_tags.push(built.tag);
    }
    tags.extend(q_tags);

    Ok(tags.into_iter().collect())
}

impl EventBuilder {
    /// A kind-1 reply with NIP-10 tags.
    ///
    /// # Errors
    ///
    /// [`Error::Validation`] `NIP-10 replyTo is for kind 1` — same as
    /// `reply_tags`.
    pub fn text_reply(
        parent: &Event,
        content: &str,
        relay_hint: Option<&RelayUrl>,
        quotes: &[Quote],
    ) -> Result<Self> {
        Ok(Self::text_note(content).tags(reply_tags(parent, relay_hint, quotes)?))
    }
}

#[cfg(test)]
mod tests {
    #![allow(clippy::unwrap_used, reason = "tests fail by panicking")]

    use alloc::borrow::ToOwned;
    use alloc::string::ToString;

    use crate::{SecretKey, Timestamp};

    use super::*;

    const PK1: &str = "166bf3765ebd1fc55decfe395beff2ea3b2a4e0a8946e7eb578512b555737c99";
    const PK2: &str = "3550510ffb21f7663873ebf343d701d871b958743cf78ff6c151fe03125e5858";
    const ID1: &str = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
    const ID2: &str = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";

    fn id(hex: &str) -> EventId {
        EventId::from_hex(hex).unwrap()
    }

    fn pk(hex: &str) -> PublicKey {
        PublicKey::from_hex(hex).unwrap()
    }

    fn tags(raw: &[&[&str]]) -> Tags {
        raw.iter()
            .map(|items| Tag::new(items.iter().map(|s| String::from(*s))).unwrap())
            .collect()
    }

    fn note(raw: &[&[&str]], kind: u16) -> Event {
        let keys = crate::Keys::new(
            SecretKey::from_hex("e108399bd8424357a710b606ae0c13166d853d327e47a6e5e038197346bdbf45")
                .unwrap(),
        );
        let unsigned = crate::UnsignedEvent::new(
            keys.public_key(),
            Timestamp::from_secs(1_700_000_000),
            Kind::new(kind),
            tags(raw),
            "n",
        );
        crate::testutil::sign_with_aux(&keys, unsigned, [7u8; 32])
    }

    #[test]
    fn positional_parent_and_root() {
        let thread = parse_thread_tags(&tags(&[&["e", ID1], &["e", ID2]]));
        assert_eq!(thread.root.map(|r| r.id), Some(id(ID1)));
        assert_eq!(thread.reply.map(|r| r.id), Some(id(ID2)));
        assert_eq!(thread.mentions.len(), 0);
    }

    #[test]
    fn hex_at_marker_slot_is_a_nip01_author() {
        let thread = parse_thread_tags(&tags(&[&["e", ID1, "wss://r", PK1]]));
        let root = thread.root.unwrap();
        assert_eq!(root.author, Some(pk(PK1)));
        assert_eq!(thread.mentions.len(), 0);
    }

    #[test]
    fn marked_reply_backfills_root() {
        let thread = parse_thread_tags(&tags(&[&["e", ID1, "", "reply"]]));
        assert_eq!(thread.root.map(|r| r.id), Some(id(ID1)));
        assert_eq!(thread.reply.map(|r| r.id), Some(id(ID1)));
    }

    #[test]
    fn reply_tags_rejects_non_kind1() {
        let parent = note(&[], 6);
        let error = reply_tags(&parent, None, &[]).unwrap_err();
        assert!(matches!(error, Error::Validation(_)));
        assert_eq!(error.to_string(), "NIP-10 replyTo is for kind 1");
    }

    #[test]
    fn reply_tags_root_parent_emits_one_e_tag() {
        let parent = note(&[], 1);
        let out = reply_tags(&parent, None, &[]).unwrap();
        let slices: Vec<Vec<String>> = out.iter().map(|t| t.as_slice().to_vec()).collect();
        let parent_id = parent.id().to_hex();
        let parent_pk = parent.pubkey().to_hex();
        assert_eq!(
            slices,
            alloc::vec![
                alloc::vec![
                    "e".to_owned(),
                    parent_id,
                    String::new(),
                    "root".to_owned(),
                    parent_pk.clone()
                ],
                alloc::vec!["p".to_owned(), parent_pk],
            ]
        );
    }

    #[test]
    fn quote_address_writes_coordinate_and_author_p() {
        let parent = note(&[], 1);
        let quote = Quote::Address(AddressPointer {
            identifier: "post".into(),
            pubkey: pk(PK2),
            kind: Kind::new(30023),
            relays: alloc::vec!["wss://a.example".to_owned()],
        });
        let out = reply_tags(&parent, None, &[quote]).unwrap();
        let slices: Vec<Vec<String>> = out.iter().map(|t| t.as_slice().to_vec()).collect();
        assert!(slices.contains(&alloc::vec![
            "q".to_owned(),
            alloc::format!("30023:{PK2}:post"),
            "wss://a.example".to_owned()
        ]));
        assert!(slices.contains(&alloc::vec![
            "p".to_owned(),
            PK2.to_owned(),
            "wss://a.example".to_owned()
        ]));
    }
}