Skip to main content

Session

Struct Session 

Source
pub struct Session<K: NostrKeypair> {
    pub state: SessionState,
    /* private fields */
}
Expand description

A 1:1 double-ratchet session. Generic over the in-process keypair type K so it works with the production K256Keypair and any test signer alike.

Fields§

§state: SessionState

The serializable ratchet state.

Implementations§

Source§

impl<K: NostrKeypair> Session<K>

Source

pub fn from_state(state: SessionState) -> Self

Wrap an existing SessionState (e.g. loaded from storage).

Source

pub fn new_initiator( their_ephemeral_pubkey: &[u8; 32], our_secret: &[u8; 32], shared_secret: &[u8; 32], ) -> Result<Self, Nip104Error>

Initiator-side session bootstrap.

their_ephemeral_pubkey is the peer’s ephemeral x-only key, our_secret our ephemeral secret, and shared_secret the X3DH output.

§Errors

Propagates key-construction and NIP-44 derivation failures.

Source

pub fn new_responder( their_ephemeral_pubkey: &[u8; 32], our_secret: &[u8; 32], shared_secret: &[u8; 32], ) -> Result<Self, Nip104Error>

Responder-side session bootstrap. See Session::new_initiator.

§Errors

Propagates key-construction and NIP-44 derivation failures.

Source

pub const fn can_send(&self) -> bool

Whether the session currently holds enough state to encrypt a message.

Source

pub fn accepted_senders(&self) -> Vec<String>

The peer DH public keys (x-only hex) this session will currently accept as a message sender — exactly the set matches_sender tests: the peer’s current and next ratchet keys plus every banked skipped-chain key.

This set changes only when a message is received (the DH ratchet turns or a skipped key is banked); sending never alters it. A router can therefore index sessions by these keys and refresh the index after each plan_receive/apply, giving O(1) inbound routing instead of trial-decrypting every session.

Source

pub fn plan_send( &self, payload: &[u8], ) -> Result<(SessionState, MessageEnvelope), Nip104Error>

Encrypt payload, returning the envelope and the post-send state.

The session is not mutated; call Session::apply with the returned state to commit. This separation lets callers persist atomically.

§Errors

Nip104Error::CannotSendYet if no sending chain exists, plus any crypto failure.

Source

pub fn plan_receive( &self, envelope: &MessageEnvelope, ) -> Result<(SessionState, Vec<u8>), Nip104Error>

Decrypt envelope, returning the plaintext and the post-receive state. The session is not mutated; commit with Session::apply.

§Errors

Nip104Error::UnexpectedSender if the sender is unknown, plus any crypto or ratchet failure.

Source

pub fn apply(&mut self, next: SessionState)

Commit a planned state transition produced by Session::plan_send or Session::plan_receive.

Source

pub fn plan_send_event( &self, payload: &[u8], created_at: i64, ) -> Result<(SessionState, NostrNote), Nip104Error>

Like plan_send, but also renders a ready-to-publish, signed kind-MESSAGE_EVENT_KIND Nostr event.

The event is signed by the sender’s current ephemeral key (the one driving the ratchet), exactly as the reference implementation does — so the published event interoperates with Iris. The session is not mutated; commit the returned state with apply.

created_at is the Unix timestamp to stamp on the event.

§Errors

Propagates plan_send failures plus any signing error.

Source

pub fn plan_receive_event( &self, event: &NostrNote, ) -> Result<(SessionState, Vec<u8>), Nip104Error>

Like plan_receive, but takes a raw kind-1060 Nostr event, verifies it, and extracts the envelope before decrypting.

§Errors

Nip104Error::InvalidHeader if the event is malformed or fails signature verification, plus any plan_receive failure.

Trait Implementations§

Source§

impl<K: Clone + NostrKeypair> Clone for Session<K>

Source§

fn clone(&self) -> Session<K>

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<K: Debug + NostrKeypair> Debug for Session<K>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<K> Freeze for Session<K>

§

impl<K> RefUnwindSafe for Session<K>

§

impl<K> Send for Session<K>

§

impl<K> Sync for Session<K>

§

impl<K> Unpin for Session<K>

§

impl<K> UnsafeUnpin for Session<K>

§

impl<K> UnwindSafe for Session<K>

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> 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.