Skip to main content

SignedEvent

Struct SignedEvent 

Source
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

Source

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.

Source

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.

Source

pub fn id(&self) -> EventId

Returns the event ID.

Source

pub fn author(&self) -> DidNostr

Returns the signer, from the event’s pubkey field.

Source

pub fn created_at(&self) -> UnixTime

Returns the claimed creation time, as signed. It is not checked against any clock here.

Source

pub fn kind(&self) -> u16

Returns the event kind.

Source

pub fn tags(&self) -> &[Vec<String>]

Returns the tags in order.

Source

pub fn content(&self) -> &str

Returns the content.

Source

pub fn signature(&self) -> &[u8; 64]

Returns the 64-byte BIP-340 signature.

Trait Implementations§

Source§

impl Clone for SignedEvent

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SignedEvent

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for SignedEvent

Source§

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.

Source§

impl Eq for SignedEvent

Source§

impl PartialEq for SignedEvent

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Serialize for SignedEvent

Source§

fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for SignedEvent

Auto Trait Implementations§

§

impl Forget for SignedEvent

§

impl Freeze for SignedEvent

§

impl Move for SignedEvent

§

impl RefUnwindSafe for SignedEvent

§

impl Send for SignedEvent
where RawVecInner: Send,

§

impl Sync for SignedEvent
where RawVecInner: Sync,

§

impl Unpin for SignedEvent
where *const u8 is !null: Unpin,

§

impl UnsafeUnpin for SignedEvent

§

impl UnwindSafe for SignedEvent
where RawVecInner: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.