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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
//! [NIP-01](https://github.com/nostr-protocol/nips/blob/master/01.md) —
//! the event model: event ids, signatures, unsigned and signed events,
//! kinds, tags, builders, and the canonical serialization used for hashing.

mod builder;
pub(crate) mod canonical;
mod id;
mod kind;
mod tag;
mod unsigned;

pub use builder::{DeletionTarget, EventBuilder, Metadata};
pub use id::EventId;
pub use kind::{Kind, KindClass};
pub use tag::{Coordinate, Tag, Tags};
pub use unsigned::UnsignedEvent;

use alloc::string::String;
use alloc::vec::Vec;
use core::cmp::Ordering;
use core::fmt;

use serde::de::{MapAccess, Visitor};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use sha2::{Digest, Sha256};

use crate::json::{self, JsonError, WireHex, WireInt, WireTags};
use crate::key::{PublicKey, Signature};
use crate::limits::MAX_SAFE_INTEGER;
use crate::time::Timestamp;

/// The result type for this module.
pub type Result<T, E = Error> = core::result::Result<T, E>;

/// Why an event operation failed.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
    /// A byte input was not the required length.
    #[error("invalid length: expected {expected} bytes, got {found}")]
    InvalidLength {
        /// The required byte count.
        expected: usize,
        /// The actual byte count.
        found: usize,
    },
    /// The input is not valid hexadecimal for the required width.
    #[error("invalid hex input")]
    InvalidHex,
    /// A tag must contain at least its name element.
    #[error("tag must not be empty")]
    EmptyTag,
    /// The text is not a `kind:pubkey:identifier` coordinate.
    #[error("invalid event address")]
    InvalidCoordinate,
    /// The stored id does not match the recomputed hash of the contents.
    #[error("event id does not match the event contents")]
    IdMismatch,
    /// Signature verification failed.
    #[error("invalid signature")]
    InvalidSignature {
        /// The verification failure.
        #[source]
        source: crate::key::Error,
    },
    /// An addressable target has no `d` tag, so no coordinate can be built.
    #[error("addressable event is missing d tag")]
    MissingIdentifier,
    /// The target kind is not valid for this builder.
    #[error("invalid repost target kind {found}")]
    InvalidTargetKind {
        /// The target's kind.
        found: Kind,
    },
}

/// A fully formed NIP-01 event with id and signature.
///
/// Obtained through deserialization or [`crate::key::Keys::sign_event`];
/// there is no public constructor.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Event {
    id: EventId,
    pubkey: PublicKey,
    created_at: Timestamp,
    kind: Kind,
    tags: Tags,
    content: String,
    sig: Signature,
}

/// A borrowed view of the fields the canonical writer needs; lets the
/// id hash and [`Event::verify`] run without cloning the event.
pub(crate) struct EventParts<'a> {
    /// The author public key.
    pub pubkey: &'a PublicKey,
    /// The creation timestamp.
    pub created_at: Timestamp,
    /// The event kind.
    pub kind: Kind,
    /// The tag list.
    pub tags: &'a Tags,
    /// The event content.
    pub content: &'a str,
}

impl Event {
    /// The event id.
    #[must_use]
    pub const fn id(&self) -> EventId {
        self.id
    }

    /// The author public key.
    #[must_use]
    pub const fn pubkey(&self) -> PublicKey {
        self.pubkey
    }

    /// The creation timestamp.
    #[must_use]
    pub const fn created_at(&self) -> Timestamp {
        self.created_at
    }

    /// The event kind.
    #[must_use]
    pub const fn kind(&self) -> Kind {
        self.kind
    }

    /// The tag list.
    #[must_use]
    pub const fn tags(&self) -> &Tags {
        &self.tags
    }

    /// The event content.
    #[must_use]
    pub fn content(&self) -> &str {
        &self.content
    }

    /// The BIP-340 signature over the id.
    #[must_use]
    pub const fn sig(&self) -> Signature {
        self.sig
    }

    /// The `kind:pubkey:d` coordinate for replaceable and addressable
    /// events; `None` for regular and ephemeral kinds. An addressable event
    /// without a `d` tag takes the empty identifier.
    #[must_use]
    pub fn address(&self) -> Option<Coordinate> {
        if self.kind.is_addressable() {
            Some(Coordinate::new(
                self.kind,
                self.pubkey,
                self.tags.identifier().unwrap_or_default(),
            ))
        } else if self.kind.is_replaceable() {
            Some(Coordinate::new(self.kind, self.pubkey, ""))
        } else {
            None
        }
    }

    /// Whether this is the signed form of `unsigned`: `kind`, `content`,
    /// `created_at`, `tags`, and `pubkey` all equal.
    #[must_use]
    pub fn matches_unsigned(&self, unsigned: &UnsignedEvent) -> bool {
        self.kind == unsigned.kind
            && self.content == unsigned.content
            && self.created_at == unsigned.created_at
            && self.tags == unsigned.tags
            && self.pubkey == unsigned.pubkey
    }

    /// NIP-01 replaceable/addressable winner: higher `created_at`, or equal
    /// timestamp and lexicographically lower id.
    #[must_use]
    pub fn supersedes(&self, other: &Self) -> bool {
        if self.created_at != other.created_at {
            return self.created_at > other.created_at;
        }
        self.id < other.id
    }

    /// Drops id and sig, returning the unsigned form.
    #[must_use]
    pub fn into_unsigned(self) -> UnsignedEvent {
        UnsignedEvent::new(
            self.pubkey,
            self.created_at,
            self.kind,
            self.tags,
            self.content,
        )
    }

    /// Assembles a signed event from its parts; only signing and
    /// deserialization can produce `Event`s.
    pub(crate) fn new_signed(unsigned: UnsignedEvent, id: EventId, sig: Signature) -> Self {
        Self {
            id,
            pubkey: unsigned.pubkey,
            created_at: unsigned.created_at,
            kind: unsigned.kind,
            tags: unsigned.tags,
            content: unsigned.content,
            sig,
        }
    }

    /// The borrowed view used by the canonical writer.
    pub(crate) fn parts(&self) -> EventParts<'_> {
        EventParts {
            pubkey: &self.pubkey,
            created_at: self.created_at,
            kind: self.kind,
            tags: &self.tags,
            content: &self.content,
        }
    }

    /// Full NIP-01 verification: recomputes the id from the canonical
    /// serialization, then BIP-340-verifies `sig` over it. Allocates
    /// nothing.
    ///
    /// # Errors
    ///
    /// [`Error::IdMismatch`] when the stored id does not match the
    /// recomputed id; [`Error::InvalidSignature`] when the public key is
    /// not on the curve or the signature is invalid.
    pub fn verify(&self) -> Result<()> {
        let computed = {
            let mut hasher = Sha256::new();
            canonical::write_event(&self.parts(), &mut hasher);
            EventId::from_bytes(hasher.finalize().into())
        };
        if computed != self.id {
            return Err(Error::IdMismatch);
        }
        self.sig
            .verify(&self.id, &self.pubkey)
            .map_err(|source| Error::InvalidSignature { source })
    }

    /// The wire JSON object: `id`, `pubkey`, `created_at`, `kind`, `tags`,
    /// `content`, `sig` in canonical field order and escaping.
    #[must_use]
    pub fn to_json(&self) -> String {
        let mut out = String::new();
        canonical::write_signed(self, &mut out);
        out
    }

    /// Constructs an event from its wire fields. Shape-valid by
    /// construction — like [`Self::from_json`], this does not verify the id
    /// or the signature; call [`Self::verify`] when that matters.
    #[must_use]
    pub const fn new(
        id: EventId,
        pubkey: PublicKey,
        created_at: Timestamp,
        kind: Kind,
        tags: Tags,
        content: String,
        sig: Signature,
    ) -> Self {
        Self {
            id,
            pubkey,
            created_at,
            kind,
            tags,
            content,
            sig,
        }
    }

    /// Parses the wire JSON object.
    ///
    /// # Errors
    ///
    /// [`JsonError`] on any malformed input; the message names the
    /// offending field.
    pub fn from_json(json: &str) -> json::Result<Self> {
        serde_json::from_str(json).map_err(JsonError::from)
    }

    /// NIP-01 newest-first order: `created_at` descending, `id` ascending
    /// as the tie-break. Usable as `sort_by(Event::cmp_newest_first)`.
    #[must_use]
    pub fn cmp_newest_first(a: &Self, b: &Self) -> Ordering {
        b.created_at
            .cmp(&a.created_at)
            .then_with(|| a.id.cmp(&b.id))
    }

    /// Negentropy item order: `created_at` ascending, `id` ascending as the
    /// tie-break — the same id direction as [`Self::cmp_newest_first`].
    #[must_use]
    pub fn cmp_oldest_first(a: &Self, b: &Self) -> Ordering {
        a.created_at
            .cmp(&b.created_at)
            .then_with(|| a.id.cmp(&b.id))
    }
}

impl Serialize for Event {
    /// The wire object in canonical field order: `id`, `pubkey`,
    /// `created_at`, `kind`, `tags`, `content`, `sig`.
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        #[derive(Serialize)]
        struct Ref<'a> {
            id: &'a EventId,
            pubkey: &'a PublicKey,
            created_at: Timestamp,
            kind: Kind,
            tags: &'a Tags,
            content: &'a str,
            sig: &'a Signature,
        }
        Ref {
            id: &self.id,
            pubkey: &self.pubkey,
            created_at: self.created_at,
            kind: self.kind,
            tags: &self.tags,
            content: &self.content,
            sig: &self.sig,
        }
        .serialize(serializer)
    }
}

impl<'de> Deserialize<'de> for Event {
    /// Strict wire rules: every key must be unique; `id`/`pubkey`/`sig`
    /// are exact-length lowercase hex, `created_at` a plain integer below
    /// `2^53`, `kind` an integer in `0..=65535`, `content` a string, `tags`
    /// an array of non-empty string arrays. Unknown fields are ignored.
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        struct EventVisitor;

        impl<'de> Visitor<'de> for EventVisitor {
            type Value = Event;

            fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                f.write_str("a signed NIP-01 event object")
            }

            #[allow(
                clippy::excessive_nesting,
                reason = "a strict visitor is a loop over tagged fields"
            )]
            fn visit_map<M: MapAccess<'de>>(self, mut map: M) -> Result<Event, M::Error> {
                let mut seen = Vec::new();
                let mut id = None;
                let mut pubkey = None;
                let mut created_at = None;
                let mut kind = None;
                let mut tags = None;
                let mut content = None;
                let mut sig = None;
                while let Some(key) = json::next_key(&mut map, &mut seen)? {
                    match key.as_ref() {
                        "id" => id = Some(map.next_value::<WireHex<32, true>>()?.0),
                        "pubkey" => pubkey = Some(map.next_value::<WireHex<32, true>>()?.0),
                        "created_at" => {
                            created_at = Some(map.next_value::<WireInt<MAX_SAFE_INTEGER>>()?.0);
                        }
                        "kind" => {
                            kind = Some(map.next_value::<WireInt<{ u16::MAX as u64 }>>()?.0);
                        }
                        "tags" => tags = Some(map.next_value::<WireTags>()?.0),
                        "content" => content = Some(map.next_value::<String>()?),
                        "sig" => sig = Some(map.next_value::<WireHex<64, true>>()?.0),
                        _ => {
                            map.next_value::<serde::de::IgnoredAny>()?;
                        }
                    }
                }
                Ok(Event {
                    id: EventId::from_bytes(json::required(id, "id")?),
                    pubkey: PublicKey::from_bytes(json::required(pubkey, "pubkey")?),
                    created_at: Timestamp::from_secs(json::required(created_at, "created_at")?),
                    kind: Kind::new(
                        u16::try_from(json::required(kind, "kind")?)
                            .map_err(serde::de::Error::custom)?,
                    ),
                    tags: json::required(tags, "tags")?,
                    content: json::required(content, "content")?,
                    sig: Signature::from_bytes(json::required(sig, "sig")?),
                })
            }
        }

        deserializer.deserialize_map(EventVisitor)
    }
}

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

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

    use super::*;
    use crate::event::tag::Tag;

    const PK: &str = "90a80db6eb294b9eab0b4e8ddfa3efe7263458ce2d07566df4e6c58868feef23";

    fn unsigned() -> UnsignedEvent {
        let mut tags = Tags::new();
        tags.push(Tag::hashtag("smoke"));
        UnsignedEvent::new(
            PublicKey::from_hex(PK).unwrap(),
            Timestamp::from_secs(1_700_000_000),
            Kind::TEXT_NOTE,
            tags,
            "hello",
        )
    }

    /// The id/serialized values frozen in `vectors/core/event-serialize.json`.
    fn signed(json: &str) -> Event {
        serde_json::from_str(json).unwrap()
    }

    #[test]
    fn canonical_json_and_id_are_stable() {
        let event = unsigned();
        assert_eq!(
            event.id_preimage(),
            alloc::format!("[0,\"{PK}\",1700000000,1,[[\"t\",\"smoke\"]],\"hello\"]")
        );
        assert_eq!(
            event.id().to_hex(),
            "659f3c96e7c424216353386f3720992d8d7be0717a9a0c23422094f7f9bb591f"
        );
    }

    #[test]
    fn id_matches_hash_of_canonical_bytes() {
        let event = unsigned();
        assert_eq!(
            event.id(),
            EventId::from_bytes(Sha256::digest(event.id_preimage().as_bytes()).into())
        );
    }

    #[test]
    fn canonical_json_flushes_long_runs_and_sparse_escapes() {
        let mut content = String::with_capacity(10_000);
        let mut escaped = String::with_capacity(10_000);
        for i in 0..10_000 {
            if i % 1024 == 512 {
                content.push('\n');
                escaped.push_str("\\n");
            } else {
                content.push('x');
                escaped.push('x');
            }
        }
        let event = UnsignedEvent::new(
            PublicKey::from_hex(PK).unwrap(),
            Timestamp::from_secs(0),
            Kind::METADATA,
            Tags::new(),
            content,
        );
        assert_eq!(
            event.id_preimage(),
            alloc::format!("[0,\"{PK}\",0,0,[],\"{escaped}\"]")
        );
        assert_eq!(
            event.id(),
            EventId::from_bytes(Sha256::digest(event.id_preimage().as_bytes()).into())
        );
    }

    #[test]
    fn new_round_trips_wire_json() {
        let sig = "ab".repeat(64);
        let json = alloc::format!(
            r#"{{"id":"{}","pubkey":"{}","created_at":1700000000,"kind":1,"tags":[["t","smoke"]],"content":"hello","sig":"{sig}"}}"#,
            "cd".repeat(32),
            PK,
        );
        let original = signed(&json);
        let rebuilt = Event::new(
            original.id(),
            original.pubkey(),
            original.created_at(),
            original.kind(),
            original.tags().clone(),
            alloc::string::String::from(original.content()),
            original.sig(),
        );
        assert_eq!(rebuilt, original);
        assert_eq!(rebuilt.to_json(), original.to_json());
    }

    #[test]
    fn unsigned_wire_roundtrip() {
        let event = unsigned();
        let json = serde_json::to_string(&event).unwrap();
        assert_eq!(
            json,
            alloc::format!(
                "{{\"pubkey\":\"{PK}\",\"created_at\":1700000000,\"kind\":1,\"tags\":[[\"t\",\"smoke\"]],\"content\":\"hello\"}}"
            )
        );
        assert_eq!(serde_json::from_str::<UnsignedEvent>(&json).unwrap(), event);
    }

    #[test]
    fn event_wire_roundtrip_and_accessors() {
        let raw = alloc::format!(
            "{{\"id\":\"{}\",\"pubkey\":\"{PK}\",\"created_at\":1700000000,\"kind\":1,\"tags\":[[\"t\",\"smoke\"]],\"content\":\"hello\",\"sig\":\"{}\"}}",
            "ab".repeat(32),
            "cd".repeat(64),
        );
        let event = signed(&raw);
        assert_eq!(serde_json::to_string(&event).unwrap(), raw);
        assert_eq!(event.id().to_hex(), "ab".repeat(32));
        assert_eq!(event.pubkey().to_hex(), PK);
        assert_eq!(event.created_at(), Timestamp::from_secs(1_700_000_000));
        assert_eq!(event.kind(), Kind::TEXT_NOTE);
        assert_eq!(event.content(), "hello");
        assert_eq!(event.sig().to_hex(), "cd".repeat(64));
        assert!(event.matches_unsigned(&unsigned()));
        assert_eq!(event.into_unsigned(), unsigned());
    }

    #[test]
    fn address_only_for_replaceable_and_addressable() {
        let regular = signed(&alloc::format!(
            "{{\"id\":\"{}\",\"pubkey\":\"{PK}\",\"created_at\":1,\"kind\":1,\"tags\":[],\"content\":\"\",\"sig\":\"{}\"}}",
            "ab".repeat(32),
            "cd".repeat(64),
        ));
        assert!(regular.address().is_none());
        let ephemeral = signed(&alloc::format!(
            "{{\"id\":\"{}\",\"pubkey\":\"{PK}\",\"created_at\":1,\"kind\":20000,\"tags\":[],\"content\":\"\",\"sig\":\"{}\"}}",
            "ab".repeat(32),
            "cd".repeat(64),
        ));
        assert!(ephemeral.address().is_none());
        let addressable = signed(&alloc::format!(
            "{{\"id\":\"{}\",\"pubkey\":\"{PK}\",\"created_at\":1,\"kind\":30023,\"tags\":[[\"d\",\"post\"]],\"content\":\"\",\"sig\":\"{}\"}}",
            "ab".repeat(32),
            "cd".repeat(64),
        ));
        assert_eq!(
            addressable.address().unwrap().to_string(),
            alloc::format!("30023:{PK}:post")
        );
        let replaceable = signed(&alloc::format!(
            "{{\"id\":\"{}\",\"pubkey\":\"{PK}\",\"created_at\":1,\"kind\":10000,\"tags\":[],\"content\":\"\",\"sig\":\"{}\"}}",
            "ab".repeat(32),
            "cd".repeat(64),
        ));
        assert_eq!(
            replaceable.address().unwrap().to_string(),
            alloc::format!("10000:{PK}:")
        );
    }

    #[test]
    fn ordering_and_supersedes() {
        let a = signed(&alloc::format!(
            "{{\"id\":\"{}\",\"pubkey\":\"{PK}\",\"created_at\":2,\"kind\":1,\"tags\":[],\"content\":\"\",\"sig\":\"{}\"}}",
            "aa".repeat(32),
            "cd".repeat(64),
        ));
        let b = signed(&alloc::format!(
            "{{\"id\":\"{}\",\"pubkey\":\"{PK}\",\"created_at\":1,\"kind\":1,\"tags\":[],\"content\":\"\",\"sig\":\"{}\"}}",
            "bb".repeat(32),
            "cd".repeat(64),
        ));
        assert_eq!(Event::cmp_newest_first(&a, &b), Ordering::Less);
        assert_eq!(Event::cmp_oldest_first(&a, &b), Ordering::Greater);
        assert!(a.supersedes(&b));
        assert!(!b.supersedes(&a));
        // Equal timestamps: lexicographically lower id wins.
        let c = signed(&alloc::format!(
            "{{\"id\":\"{}\",\"pubkey\":\"{PK}\",\"created_at\":2,\"kind\":1,\"tags\":[],\"content\":\"\",\"sig\":\"{}\"}}",
            "cc".repeat(32),
            "cd".repeat(64),
        ));
        assert!(a.supersedes(&c));
        assert!(!c.supersedes(&a));
    }

    #[test]
    fn strict_wire_rules() {
        let base = |patch: &str| {
            alloc::format!(
                "{{\"id\":\"{}\",\"pubkey\":\"{PK}\",\"created_at\":1700000000,\"kind\":1,\"tags\":[[\"t\",\"smoke\"]],\"content\":\"hello\",\"sig\":\"{}\"{patch}}}",
                "ab".repeat(32),
                "cd".repeat(64),
            )
        };
        assert!(serde_json::from_str::<Event>(&base("")).is_ok());
        // Unknown fields are ignored.
        assert!(serde_json::from_str::<Event>(&base(",\"extra\":1")).is_ok());
        // Duplicate keys are rejected — even of unknown fields.
        let dup = alloc::format!(
            "{{\"id\":\"{}\",\"id\":\"{}\",\"pubkey\":\"{PK}\",\"created_at\":1700000000,\"kind\":1,\"tags\":[],\"content\":\"\",\"sig\":\"{}\"}}",
            "ab".repeat(32),
            "cd".repeat(32),
            "cd".repeat(64),
        );
        assert!(serde_json::from_str::<Event>(&dup).is_err());
        assert!(serde_json::from_str::<Event>(&base(",\"extra\":1,\"extra\":2")).is_err());
        // Non-canonical integer spellings are rejected.
        for (field, spelling) in [("created_at", "1e3"), ("created_at", "-0"), ("kind", "1.0")] {
            let raw = base("").replace(
                &alloc::format!("\"{field}\":1700000000"),
                &alloc::format!("\"{field}\":{spelling}"),
            );
            let raw = if field == "created_at" {
                raw
            } else {
                base("").replace("\"kind\":1,", &alloc::format!("\"kind\":{spelling},"))
            };
            assert!(serde_json::from_str::<Event>(&raw).is_err(), "{raw}");
        }
        // Visitor `expecting` text reaches the deserialization error message.
        let error = serde_json::from_str::<UnsignedEvent>("[]").unwrap_err();
        assert!(
            error.to_string().contains("unsigned NIP-01 event"),
            "{error}"
        );
    }

    /// `vectors/core/event-sign.json` case 0 — sk=3, aux=1.
    fn valid_event() -> Event {
        signed(
            "{\"id\":\"791e76a4715f1514309947f51c1d20a6f80698833ecc97b7b3a9c56cc3063113\",\"pubkey\":\"f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9\",\"created_at\":1700000000,\"kind\":1,\"tags\":[[\"t\",\"hi\"]],\"content\":\"gm\",\"sig\":\"35931bdb21989c9049272418de0df733b0ebede3bcb45c54161511d8633cab516d6b25cd808ddb8cbba7600940678e3ba3fe655a830abbe631e640e9fc460ff1\"}",
        )
    }

    #[test]
    fn verify_accepts_a_correctly_signed_event() {
        valid_event().verify().unwrap();
    }

    #[test]
    fn verify_rejects_an_id_mismatch() {
        let mut value = serde_json::to_value(valid_event()).unwrap();
        value
            .as_object_mut()
            .unwrap()
            .insert("content".into(), "tampered".into());
        let event: Event = serde_json::from_value(value).unwrap();
        assert!(matches!(event.verify().unwrap_err(), Error::IdMismatch));
    }

    #[test]
    fn verify_rejects_a_bad_signature() {
        let mut value = serde_json::to_value(valid_event()).unwrap();
        value
            .as_object_mut()
            .unwrap()
            .insert("sig".into(), "ff".repeat(64).into());
        let event: Event = serde_json::from_value(value).unwrap();
        assert!(matches!(
            event.verify().unwrap_err(),
            Error::InvalidSignature { .. }
        ));
    }

    #[test]
    fn signature_verify_rejects_an_off_curve_pubkey() {
        let event = valid_event();
        let off_curve = PublicKey::from_bytes([0xff; 32]);
        assert!(matches!(
            event.sig().verify(&event.id(), &off_curve).unwrap_err(),
            crate::key::Error::InvalidPublicKey
        ));
    }

    #[test]
    fn signature_verify_rejects_a_wrong_message() {
        let event = valid_event();
        let other_id = EventId::from_bytes([0u8; 32]);
        assert!(matches!(
            event.sig().verify(&other_id, &event.pubkey()).unwrap_err(),
            crate::key::Error::InvalidSignature
        ));
    }

    #[test]
    fn event_id_glue() {
        let id = EventId::from_bytes([0xab; 32]);
        assert_eq!(EventId::from_slice(id.as_bytes()).unwrap(), id);
        assert!(matches!(
            EventId::from_slice(&[0u8; 31]).unwrap_err(),
            Error::InvalidLength {
                expected: 32,
                found: 31
            }
        ));
        assert_eq!(id.to_string(), "ab".repeat(32));
        assert_eq!(
            alloc::format!("{id:?}"),
            alloc::format!("EventId({})", "ab".repeat(32))
        );
        // `FromStr` accepts any case (caller input); wire decoding stays strict.
        assert_eq!("AB".repeat(32).parse::<EventId>().unwrap(), id);
        assert!("ab".parse::<EventId>().is_err());
        let wire: EventId = serde_json::from_str(&serde_json::to_string(&id).unwrap()).unwrap();
        assert_eq!(wire, id);
        assert!(
            serde_json::from_str::<EventId>(&alloc::format!("\"{}\"", "AB".repeat(32))).is_err()
        );
    }

    #[test]
    fn signature_glue_and_wire_rejects() {
        let sig = Signature::from_bytes([0xcd; 64]);
        assert_eq!(Signature::from_slice(sig.as_bytes()).unwrap(), sig);
        assert!(matches!(
            Signature::from_slice(&[0u8; 63]).unwrap_err(),
            crate::key::Error::InvalidLength {
                expected: 64,
                found: 63
            }
        ));
        assert_eq!(sig.to_string(), "cd".repeat(64));
        assert_eq!(
            alloc::format!("{sig:?}"),
            alloc::format!("Signature({})", "cd".repeat(64))
        );
        assert_eq!("CD".repeat(64).parse::<Signature>().unwrap(), sig);
        assert!("cd".parse::<Signature>().is_err());
        let wire: Signature = serde_json::from_str(&serde_json::to_string(&sig).unwrap()).unwrap();
        assert_eq!(wire, sig);
        // Strict wire decoding: uppercase, short, and non-strings fail.
        assert!(
            serde_json::from_str::<Signature>(&alloc::format!("\"{}\"", "CD".repeat(64))).is_err()
        );
        assert!(serde_json::from_str::<Signature>("\"cd\"").is_err());
        assert!(serde_json::from_str::<Signature>("5").is_err());
        assert!(serde_json::from_str::<Signature>("null").is_err());
    }
}