Skip to main content

GroupManager

Struct GroupManager 

Source
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>

Source

pub fn new(our_pubkey: impl Into<String>) -> Self

Create a manager owned by our_pubkey (our owner/device identity hex).

Source

pub fn our_pubkey(&self) -> &str

Our identity pubkey.

Source

pub fn has_sending_chain(&self, group_id: &str) -> bool

Whether we hold a sending chain for group_id.

Source

pub fn known_senders(&self, group_id: &str) -> Vec<String>

The sender-event pubkeys whose chains we can currently decrypt for group_id, sorted.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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>

Source§

fn clone(&self) -> GroupManager<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 GroupManager<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 GroupManager<K>

§

impl<K> RefUnwindSafe for GroupManager<K>

§

impl<K> Send for GroupManager<K>

§

impl<K> Sync for GroupManager<K>

§

impl<K> Unpin for GroupManager<K>

§

impl<K> UnsafeUnpin for GroupManager<K>

§

impl<K> UnwindSafe for GroupManager<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.