pub struct SignedEvent { /* private fields */ }Expand description
A Nostr event whose event ID and BIP-340 signature have been verified.
The only ways to obtain one are SignedEvent::from_json, serde
deserialisation (which runs the same checks) and signing with
NostrSecretKey::sign_event. Holding
a SignedEvent therefore means its author signed exactly
these fields. Only the JSON object form is accepted: a positional array,
unknown fields, duplicate fields and empty tags are rejected.
The serde implementations target self-describing formats such as JSON: deserialisation accepts only a map, so formats that write structs as bare sequences (bincode, postcard and similar) cannot read an event back.
Verification says nothing about freshness or single use; see
verify_request for the HTTP profile.
§Character restriction
NIP-01 defines the event ID over a JSON serialisation that escapes only
", \, line feed, carriage return, tab, backspace and form feed, and
writes every other character verbatim. This crate serialises the ID
preimage with serde_json, which agrees with NIP-01 on all of those but
writes the remaining control characters (U+0000 to U+001F other than the
five named) as \u00XX. Rather than hand-write a second JSON encoder,
this crate refuses to parse, or sign, any event whose content or tags
contain one of those characters (ParseEventError::UnsupportedCharacter,
SignError::UnsupportedCharacter).
Every other Unicode text, including U+007F and U+2028, is supported.
§Examples
use stratum_node_core::{NostrSecretKey, SignedEvent, UnixTime, UnsignedEvent};
let key = NostrSecretKey::from_hex(&"02".repeat(32))?;
let event = key.sign_event(&UnsignedEvent::new(1, UnixTime::from_secs(1)), &[0u8; 32])?;
let parsed = SignedEvent::from_json(event.to_json().as_bytes())?;
assert_eq!(parsed, event);Implementations§
Source§impl SignedEvent
impl SignedEvent
Sourcepub fn from_json(json: &[u8]) -> Result<Self, ParseEventError>
pub fn from_json(json: &[u8]) -> Result<Self, ParseEventError>
Parses NIP-01 event JSON and verifies its ID and signature.
§Errors
Returns a ParseEventError naming the first check that failed.
Sourcepub fn to_json(&self) -> String
pub fn to_json(&self) -> String
Serialises the event as NIP-01 JSON with fields in the order id,
pubkey, created_at, kind, tags, content, sig.
§Panics
Does not panic: serde_json has no failure path for a struct of
strings, integers and string arrays.
Returns the signer, from the event’s pubkey field.
Sourcepub fn created_at(&self) -> UnixTime
pub fn created_at(&self) -> UnixTime
Returns the claimed creation time, as signed. It is not checked against any clock here.
Returns the tags in order.
Trait Implementations§
Source§impl Clone for SignedEvent
impl Clone for SignedEvent
Source§impl Debug for SignedEvent
impl Debug for SignedEvent
Source§impl<'de> Deserialize<'de> for SignedEvent
impl<'de> Deserialize<'de> for SignedEvent
Source§fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
Deserialises and verifies, with the same checks as
SignedEvent::from_json. Only a map (a JSON object) is accepted.