lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README

canonical.rs (1553B)


      1 //! Canonical NIP-01 event representations.
      2 //!
      3 //! Canonicalization computes bytes and identifiers; it does not assert that an
      4 //! envelope's declared identifier or signature is valid. Use [`crate::verify`]
      5 //! for those explicit state transitions.
      6 
      7 #[cfg(not(feature = "std"))]
      8 use alloc::string::String;
      9 #[cfg(feature = "std")]
     10 use std::string::String;
     11 
     12 use radroots_event::{
     13     envelope::EventEnvelope,
     14     id::EventId,
     15     wire::{
     16         CanonicalEventIdError, canonical_nip01_event_id_preimage, compute_canonical_nip01_event_id,
     17     },
     18 };
     19 
     20 /// A failure while producing a canonical NIP-01 representation.
     21 pub type CanonicalError = CanonicalEventIdError;
     22 
     23 /// Produces the canonical NIP-01 identifier preimage for an event.
     24 ///
     25 /// This operation is deterministic and does not compare the computed
     26 /// identifier with the envelope's declared identifier.
     27 pub fn id_preimage(event: &EventEnvelope) -> Result<String, CanonicalError> {
     28     canonical_nip01_event_id_preimage(
     29         &event.author().to_hex(),
     30         event.created_at_u64(),
     31         event.kind_u32(),
     32         &event.tags_as_vec(),
     33         event.content(),
     34     )
     35 }
     36 
     37 /// Computes the canonical NIP-01 identifier for an event.
     38 ///
     39 /// This operation is deterministic and does not advance the event's
     40 /// verification state.
     41 pub fn id(event: &EventEnvelope) -> Result<EventId, CanonicalError> {
     42     compute_canonical_nip01_event_id(
     43         &event.author().to_hex(),
     44         event.created_at_u64(),
     45         event.kind_u32(),
     46         &event.tags_as_vec(),
     47         event.content(),
     48     )
     49 }