//! A SHARED LIST (ruling R5, 16 Nov 2026): a list two or more //! people see and edit, every item sealed to the list's own set key //! ([`super::set`]), which is wrapped to each member's sharing key. //! //! ```text //! list = Membership (wraps, public keys) + items: item id → "g1:" e ":" … //! item body = JSON the kit lays ({"text", "done", …}), sealed with aad naming list, epoch and item //! ``` //! //! - **Any member edits.** A member opens the current epoch's key with their //! sharing secret and seals what they write under it; an edit replaces the //! item, sealed at the current epoch. //! - **Adding a member** wraps the current key for them: they see the whole //! list as it stands. //! - **Removing a member** mints the next epoch's key, wrapped only to those //! who remain, and the remover seals every item again under it: what the //! object holds from then on opens for nobody removed. What the removed //! member already read stays with them (the honest limit `set` says). //! - Only a member changes the list: a person with no wrap for the current //! epoch can neither seal an item nor wrap a key for anyone. //! //! The object keeps the list as this serialized state: wraps, public keys and //! sealed text, never a key or a word of an item. use std::collections::BTreeMap; use serde::{Deserialize, Serialize}; use serde_json::Value; use super::set::{epoch_of, open_in_set, seal_in_set, Membership, SetKey}; use super::SealError; /// A shared list as the object keeps it. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct SharedList { pub membership: Membership, /// Every item, sealed in the set: item id → `g1:` text. pub items: BTreeMap, } /// What a member reads of a list: each item opened, and each one they hold no /// key for, by id (a removed member reading what was sealed after). #[derive(Debug, Default, PartialEq)] pub struct Read { pub items: BTreeMap, pub unopened: Vec, } impl SharedList { /// A new list founded by its first members (the founder among them); the key is answered so the founder writes at once. pub fn found(list_id: &str, members: &BTreeMap) -> Result<(SharedList, SetKey), SealError> { let (membership, key) = Membership::found(list_id, members)?; Ok((SharedList { membership, items: BTreeMap::new() }, key)) } pub fn id(&self) -> &str { &self.membership.set_id } /// The current epoch's key, opened by member `who`; refused to anyone who is not a member now. pub fn current_key(&self, who: &str, secret: &[u8; 32]) -> Result { if !self.membership.members.contains_key(who) { return Err(SealError::KeyMissing(format!("{who} is not a member of {}", self.id()))); } self.membership.key_for(who, secret, self.membership.epoch) } /// An item written (new, or an edit replacing it) by member `who`, sealed at the current epoch. pub fn put(&mut self, who: &str, secret: &[u8; 32], item_id: &str, body: &Value) -> Result<(), SealError> { let key = self.current_key(who, secret)?; let plain = serde_json::to_vec(body).map_err(|_| SealError::Failed)?; let sealed = seal_in_set(&key, self.id(), self.membership.epoch, item_id, &plain)?; self.items.insert(item_id.into(), sealed); Ok(()) } /// An item taken off the list by member `who`. pub fn take(&mut self, who: &str, secret: &[u8; 32], item_id: &str) -> Result { self.current_key(who, secret)?; Ok(self.items.remove(item_id).is_some()) } /// The list as member `who` reads it: each item with the key of the epoch it names. pub fn read(&self, who: &str, secret: &[u8; 32]) -> Read { let mut keys: BTreeMap> = BTreeMap::new(); let mut out = Read::default(); for (id, sealed) in &self.items { let opened = epoch_of(sealed).ok().and_then(|epoch| { let key = keys.entry(epoch).or_insert_with(|| self.membership.key_for(who, secret, epoch).ok()); key.as_ref().and_then(|key| open_in_set(key, self.id(), id, sealed).ok()) }); match opened.and_then(|plain| serde_json::from_slice::(&plain).ok()) { Some(body) => { out.items.insert(id.clone(), body); } None => out.unopened.push(id.clone()), } } out } /// A member added by member `by`: the current key is wrapped for them. pub fn add_member(&mut self, by: &str, by_secret: &[u8; 32], id: &str, public: &str) -> Result<(), SealError> { let key = self.current_key(by, by_secret)?; self.membership.add(&key, id, public) } /// A member removed by member `by`: the next epoch's key, wrapped to those who remain, and every item sealed /// again under it, so nothing the object holds after opens for the one removed. pub fn remove_member(&mut self, by: &str, by_secret: &[u8; 32], id: &str) -> Result<(), SealError> { if by == id { return Err(SealError::BadInput("a member leaves a list another member keeps by being removed by them".into())); } let read = self.read(by, by_secret); if !read.unopened.is_empty() { return Err(SealError::KeyMissing(format!("{by} cannot open every item of {}", self.id()))); } self.current_key(by, by_secret)?; let key = self.membership.remove(id)?; let epoch = self.membership.epoch; let mut items = BTreeMap::new(); for (item, body) in &read.items { let plain = serde_json::to_vec(body).map_err(|_| SealError::Failed)?; items.insert(item.clone(), seal_in_set(&key, self.id(), epoch, item, &plain)?); } self.items = items; Ok(()) } } #[cfg(test)] #[path = "shared_list_tests.rs"] mod tests;