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: u32Identifies which chain this is; rotates on membership/key change.
Implementations§
Source§impl SenderKeyState
impl SenderKeyState
Sourcepub fn new(key_id: u32, chain_key: &[u8; 32], iteration: u32) -> Self
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.
Sourcepub fn chain_key_hex(&self) -> String
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).
Sourcepub fn skipped_len(&self) -> usize
pub fn skipped_len(&self) -> usize
Number of skipped message keys currently stored.
Sourcepub const fn skipped_keys(&self) -> &BTreeMap<u32, String>
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.
Sourcepub const fn from_parts(
key_id: u32,
chain_key_hex: String,
iteration: u32,
skipped_message_keys: BTreeMap<u32, String>,
) -> Self
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.
Sourcepub fn plan_encrypt<K: NostrKeypair>(
&self,
plaintext: &[u8],
) -> Result<SenderKeyEncryptPlan, Nip104Error>
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.
Sourcepub fn apply_encrypt(&mut self, plan: SenderKeyEncryptPlan)
pub fn apply_encrypt(&mut self, plan: SenderKeyEncryptPlan)
Commit an SenderKeyEncryptPlan.
Sourcepub fn encrypt<K: NostrKeypair>(
&mut self,
plaintext: &[u8],
) -> Result<(u32, String), Nip104Error>
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.
Sourcepub fn plan_decrypt<K: NostrKeypair>(
&self,
key_id: u32,
message_number: u32,
ciphertext_b64: &str,
) -> Result<SenderKeyDecryptPlan, Nip104Error>
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.
Sourcepub fn apply_decrypt(&mut self, plan: SenderKeyDecryptPlan) -> Vec<u8> ⓘ
pub fn apply_decrypt(&mut self, plan: SenderKeyDecryptPlan) -> Vec<u8> ⓘ
Commit a SenderKeyDecryptPlan.
Sourcepub fn decrypt<K: NostrKeypair>(
&mut self,
message_number: u32,
ciphertext_b64: &str,
) -> Result<Vec<u8>, Nip104Error>
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
impl Clone for SenderKeyState
Source§fn clone(&self) -> SenderKeyState
fn clone(&self) -> SenderKeyState
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for SenderKeyState
impl Debug for SenderKeyState
Source§impl Drop for SenderKeyState
Scrub the chain key and every banked skipped message key on drop.
impl Drop for SenderKeyState
Scrub the chain key and every banked skipped message key on drop.