pub struct GroupManager<K: NostrKeypair> { /* private fields */ }Expand description
Pure, in-memory group transport state machine.
Generic over the in-process keypair K, matching SenderKeyState and
crate::nip_104::SessionManager.
Implementations§
Source§impl<K: NostrKeypair> GroupManager<K>
impl<K: NostrKeypair> GroupManager<K>
Sourcepub fn new(our_pubkey: impl Into<String>) -> Self
pub fn new(our_pubkey: impl Into<String>) -> Self
Create a manager owned by our_pubkey (our owner/device identity hex).
Sourcepub fn our_pubkey(&self) -> &str
pub fn our_pubkey(&self) -> &str
Our identity pubkey.
Sourcepub fn has_sending_chain(&self, group_id: &str) -> bool
pub fn has_sending_chain(&self, group_id: &str) -> bool
Whether we hold a sending chain for group_id.
Sourcepub fn known_senders(&self, group_id: &str) -> Vec<String>
pub fn known_senders(&self, group_id: &str) -> Vec<String>
The sender-event pubkeys whose chains we can currently decrypt for
group_id, sorted.
Sourcepub fn rotate_sending_chain(
&mut self,
group_id: &str,
key_id: u32,
created_at: i64,
) -> Result<SenderKeyDistribution, Nip104Error>
pub fn rotate_sending_chain( &mut self, group_id: &str, key_id: u32, created_at: i64, ) -> Result<SenderKeyDistribution, Nip104Error>
Mint our sending chain for group_id. Generates a fresh sender-event
keypair and a random chain key, then returns the
SenderKeyDistribution to hand to every member over their 1:1
session. Replaces any prior sending chain (a key rotation).
§Errors
Propagates key/derivation failures.
Sourcepub fn current_distribution(
&self,
group_id: &str,
created_at: i64,
) -> Result<SenderKeyDistribution, Nip104Error>
pub fn current_distribution( &self, group_id: &str, created_at: i64, ) -> Result<SenderKeyDistribution, Nip104Error>
Re-derive the current SenderKeyDistribution for our sending chain —
e.g. to hand the chain to a member who joined after we minted it. Note
the iteration reflects the chain’s current position, so a late
joiner only decrypts messages from here forward.
§Errors
Nip104Error::SessionNotReady if we have no sending chain yet.
Sourcepub fn apply_distribution(
&mut self,
dist: &SenderKeyDistribution,
) -> Result<(), Nip104Error>
pub fn apply_distribution( &mut self, dist: &SenderKeyDistribution, ) -> Result<(), Nip104Error>
Install a SenderKeyDistribution received from a member — the
receiving chain we use to decrypt that sender’s group messages. A newer
distribution for the same sender (same sender_event_pubkey) replaces
the old chain (handles rotation).
§Errors
Nip104Error if the chain key is malformed hex.
Sourcepub fn distribution_to_rumor(
&self,
dist: &SenderKeyDistribution,
created_at: i64,
now_ms: i64,
) -> Result<NostrNote, Nip104Error>
pub fn distribution_to_rumor( &self, dist: &SenderKeyDistribution, created_at: i64, now_ms: i64, ) -> Result<NostrNote, Nip104Error>
Frame a distribution as the unsigned kind-10446 session rumor to hand to
the SessionManager for every member; its serialized JSON becomes the
inner ratchet plaintext. Tags match the reference: l/key/ms; pubkey is
our device identity; id is the rumor hash.
§Errors
JSON serialization failure of the distribution payload.
Sourcepub fn apply_distribution_rumor(
&mut self,
rumor: &NostrNote,
) -> Result<Option<SenderKeyDistribution>, Nip104Error>
pub fn apply_distribution_rumor( &mut self, rumor: &NostrNote, ) -> Result<Option<SenderKeyDistribution>, Nip104Error>
Consume an inbound session rumor. If it is a kind-10446 distribution, parse and install it, returning the applied distribution. Returns Ok(None) for any other rumor kind, so it composes with a 1:1 loop.
§Errors
Malformed payload or bad chain-key hex.
Sourcepub fn encrypt(
&mut self,
group_id: &str,
plaintext: &[u8],
created_at: i64,
) -> Result<GroupSenderKeyMessage, Nip104Error>
pub fn encrypt( &mut self, group_id: &str, plaintext: &[u8], created_at: i64, ) -> Result<GroupSenderKeyMessage, Nip104Error>
Encrypt plaintext as the next message on our sending chain for
group_id, returning the GroupSenderKeyMessage to publish once.
§Errors
Nip104Error::SessionNotReady if we have no sending chain; plus
cipher failures.
Sourcepub fn decrypt(
&mut self,
msg: &GroupSenderKeyMessage,
) -> Result<GroupReceivedMessage, Nip104Error>
pub fn decrypt( &mut self, msg: &GroupSenderKeyMessage, ) -> Result<GroupReceivedMessage, Nip104Error>
Decrypt an inbound GroupSenderKeyMessage, routing it to the
receiving chain for its sender_event_pubkey.
§Errors
Nip104Error::SessionNotReady if we hold no chain for that sender
(we are missing their distribution); plus key-id/ordering/cipher
failures from the chain.
Sourcepub fn encrypt_to_event(
&mut self,
group_id: &str,
plaintext: &[u8],
created_at: i64,
) -> Result<NostrNote, Nip104Error>
pub fn encrypt_to_event( &mut self, group_id: &str, plaintext: &[u8], created_at: i64, ) -> Result<NostrNote, Nip104Error>
Encrypt and build the publishable outer event for group_id. The
returned nostro2::NostrNote is a signed kind-GROUP_MESSAGE_KIND
event authored by our per-group sender-event key, with
content = base64(key_id_be32 || message_number_be32 || nip44_bytes)
and empty tags — byte-compatible with the reference OneToManyChannel.
Publish it once; every member decrypts it.
§Errors
Nip104Error::SessionNotReady if we have no sending chain; plus
cipher/signing failures.
Sourcepub fn decrypt_event(
&mut self,
event: &NostrNote,
) -> Result<Option<GroupReceivedMessage>, Nip104Error>
pub fn decrypt_event( &mut self, event: &NostrNote, ) -> Result<Option<GroupReceivedMessage>, Nip104Error>
Decrypt an inbound outer event. Verifies the event, routes by its
pubkey (the sender-event key) to the matching receiving chain, parses
the compact payload, and decrypts. Returns Ok(None) if the author is
not a sender-key chain we know (e.g. a 1:1 message, or a member whose
distribution we have not yet received).
§Errors
Nip104Error on a bad signature, malformed payload, or chain
decrypt failure.
Sourcepub fn snapshot(&self) -> Vec<GroupSnapshot>
pub fn snapshot(&self) -> Vec<GroupSnapshot>
Snapshot every group’s transport state for persistence — the inverse of
Self::restore_group. Returns one GroupSnapshot per group in
sorted group_id order, each carrying our sending chain (if any) and
all receiving chains. Combined with our_pubkey(), this is everything
needed to revive the manager across a restart.
Sourcepub fn restore_group(&mut self, snap: GroupSnapshot)
pub fn restore_group(&mut self, snap: GroupSnapshot)
Restore one group’s transport state from a GroupSnapshot, replacing
any existing record for that group and rebuilding the sender-routing
index for both the sending and receiving chains. The inverse of
Self::snapshot.
Trait Implementations§
Source§impl<K: Clone + NostrKeypair> Clone for GroupManager<K>
impl<K: Clone + NostrKeypair> Clone for GroupManager<K>
Source§fn clone(&self) -> GroupManager<K>
fn clone(&self) -> GroupManager<K>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more