Skip to main content

SenderKeyState

Struct SenderKeyState 

Source
pub struct SenderKeyState {
    pub key_id: u32,
    /* private fields */
}
Expand description

One sender-key chain: a symmetric KDF ratchet identified by key_id.

Both the sender and every receiver hold a copy seeded from the same distribution; the sender advances it on encrypt, receivers on decrypt. Chain keys are stored as 64-char hex to match the crate’s SessionState convention (the reference stores raw bytes; the wire SenderKeyDistribution is where the hex/byte boundary lives).

Fields§

§key_id: u32

Identifies which chain this is; rotates on membership/key change.

Implementations§

Source§

impl SenderKeyState

Source

pub fn new(key_id: u32, chain_key: &[u8; 32], iteration: u32) -> Self

Create a fresh chain from a 32-byte chain_key, starting at iteration.

Source

pub const fn key_id(&self) -> u32

This chain’s id.

Source

pub const fn iteration(&self) -> u32

The next message index this chain will produce/expect.

Source

pub fn chain_key_hex(&self) -> String

The current chain key as 64-char hex (used to re-derive a crate::nip_104::SenderKeyDistribution for late joiners).

Source

pub fn skipped_len(&self) -> usize

Number of skipped message keys currently stored.

Source

pub const fn skipped_keys(&self) -> &BTreeMap<u32, String>

The skipped message keys currently banked for out-of-order delivery, as message_index → message_key_hex. Exposed for persistence: a chain with banked skipped keys cannot be faithfully restored from Self::new alone (which starts empty), so snapshots must carry these and rebuild with Self::from_parts.

Source

pub const fn from_parts( key_id: u32, chain_key_hex: String, iteration: u32, skipped_message_keys: BTreeMap<u32, String>, ) -> Self

Reconstruct a chain from a full persisted snapshot — the inverse of the getters (Self::key_id, Self::chain_key_hex, Self::iteration, Self::skipped_keys). Unlike Self::new, this preserves banked skipped message keys, so an out-of-order-delivery chain round-trips losslessly through storage.

Source

pub fn plan_encrypt<K: NostrKeypair>( &self, plaintext: &[u8], ) -> Result<SenderKeyEncryptPlan, Nip104Error>

Plan encryption of plaintext as the next message on the chain. Pure: the returned plan carries the advanced next_state; self is unchanged until apply_encrypt.

§Errors

Nip104Error on KDF/cipher failure or iteration overflow.

Source

pub fn apply_encrypt(&mut self, plan: SenderKeyEncryptPlan)

Source

pub fn encrypt<K: NostrKeypair>( &mut self, plaintext: &[u8], ) -> Result<(u32, String), Nip104Error>

Encrypt plaintext, returning (message_number, base64 ciphertext) and advancing the chain. Convenience over plan/apply.

§Errors

As plan_encrypt.

Source

pub fn plan_decrypt<K: NostrKeypair>( &self, key_id: u32, message_number: u32, ciphertext_b64: &str, ) -> Result<SenderKeyDecryptPlan, Nip104Error>

Plan decryption of a message at message_number carrying key_id. Handles in-order, future (skip-ahead) and past (skipped-key) messages. Pure: self is unchanged until apply_decrypt.

§Errors

Nip104Error on key-id mismatch, too many skipped messages, a duplicate/missing past message, or cipher failure.

Source

pub fn apply_decrypt(&mut self, plan: SenderKeyDecryptPlan) -> Vec<u8>

Source

pub fn decrypt<K: NostrKeypair>( &mut self, message_number: u32, ciphertext_b64: &str, ) -> Result<Vec<u8>, Nip104Error>

Decrypt a message at message_number, advancing the chain. Convenience over plan/apply.

§Errors

As plan_decrypt.

Trait Implementations§

Source§

impl Clone for SenderKeyState

Source§

fn clone(&self) -> SenderKeyState

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 SenderKeyState

Source§

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

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

impl Drop for SenderKeyState

Scrub the chain key and every banked skipped message key on drop.

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl Eq for SenderKeyState

Source§

impl PartialEq for SenderKeyState

Source§

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

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

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

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for SenderKeyState

Auto Trait Implementations§

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.