//! An id two producers can agree on without talking to each other: sha256 //! over the table name and the key parts joined by `|`, rendered uuid-shaped. //! The named, `Namespace`-typed wrappers stay with the store //! (`cotton_store::protocol::derived_id`); the Worker asks for this one //! through the boundary (`ids.derive`). use sha2::{Digest, Sha256}; /// The id every producer of this logical row will independently arrive at. /// /// `parts` are the natural key's values as they are STORED — pass the encoded /// form, never a domain value, or two producers holding the same key in /// different representations derive two different ids. The separator cannot /// appear in a uuid or an ISO timestamp, so no two part-lists can join to the /// same string. pub fn derived_id(table: &str, parts: &[&str]) -> String { let mut material = String::from(table); for part in parts { material.push('|'); material.push_str(part); } let hex = hex::encode(Sha256::digest(material.as_bytes())); format!("{}-{}-{}-{}-{}", &hex[0..8], &hex[8..12], &hex[12..16], &hex[16..20], &hex[20..32]) } /// A message contact's id — `(namespace, phone_e164)`, digits only. The /// store's `Namespace`-typed wrapper asks this one. pub fn msg_contact_id(namespace: &str, phone_e164: &str) -> String { derived_id("cotton_msg_contacts", &[namespace, phone_e164]) } /// A message contact reached only by a social handle — `(namespace, kind, value)`. pub fn msg_contact_handle_id(namespace: &str, kind: &str, value: &str) -> String { derived_id("cotton_msg_contacts", &[namespace, kind, value]) } /// A contact's original record (`person_source`, PEOPLE BUILD 1): `(namespace, /// kind, device, id there)`, so every device and every importer (the phone, /// the Mac) writing the same source writes one row. pub fn person_source_id(namespace: &str, kind: &str, device: &str, source_id: &str) -> String { derived_id("person_source", &[namespace, kind, device, source_id]) } /// A contact handle row's id — `(namespace, kind, value)`, value normalised. pub fn contact_handle_id(namespace: &str, kind: &str, value: &str) -> String { derived_id("cotton_msg_contact_handles", &[namespace, kind, value]) } /// A one-to-one thread's id — `(namespace, contact)`. pub fn dm_thread_id(namespace: &str, contact_id: &str) -> String { derived_id("cotton_msg_threads:dm", &[namespace, contact_id]) } /// A group thread's id — `(namespace, external key)`. pub fn group_thread_id(namespace: &str, external_key: &str) -> String { derived_id("cotton_msg_threads:group", &[namespace, external_key]) } /// A relay name as every file spells it: bare, lower-case, never all digits — /// 2 to 31 characters, opening with a letter or a digit. /// /// This is `isRelayName` from `packages/store/src/protocol/relay.ts`, whose /// "not all digits" rule is `/[^0-9]/` (any non-digit) rather than the relay /// module's `/[a-z-]/`. Over this grammar the two agree — the only characters /// the shape admits are lowercase letters, digits and hyphens — but the store's /// is the one the claim invariant is stated against, so it is the one ported. pub fn is_relay_name(value: &str) -> bool { let bytes = value.as_bytes(); if bytes.len() < 2 || bytes.len() > 31 { return false; } let Some((first, rest)) = bytes.split_first() else { return false; }; if !first.is_ascii_lowercase() && !first.is_ascii_digit() { return false; } if !rest .iter() .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || *b == b'-') { return false; } bytes.iter().any(|b| !b.is_ascii_digit()) } /// `REACTION_ACTOR_SELF` — the reaction actor that means "this file's owner". /// /// Reactions are keyed by PHONE across files, but the owner's own phone is not /// something a file reliably knows (the server holds it), and a mirror arriving /// from another file must be able to say "that one is yours". So the owner is /// always the literal `self` in their OWN file, and the relay translates in /// both directions. pub const REACTION_ACTOR_SELF: &str = "self";