//! A member's own signed People claim, read without changing any fact.usecrate::{membership, Driver, Result};use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine};use ed25519_dalek::{Signature, VerifyingKey};use rusqlite::types::ValueasSql;pubconst TABLE: &str = "cotton_member_person_claims";#[derive(Debug, Clone, PartialEq, Eq)]pubstructBoundPerson {pub person_id: String,pub photo_key: Option<String>,}/// Shared by the device signer, serving writer and every reader. Including/// the admission prevents an old claim attaching to a later admission.pubfn claim_bytes( database: &str, admission: &str, member: &str, person: &str, at: &str,) -> Vec<u8> {letmut bytes = Vec::new();for field in ["cotton-member-person-v1", database, admission, member, person, at, ] { bytes.extend_from_slice(&(field.len() as u32).to_be_bytes()); bytes.extend_from_slice(field.as_bytes()); } bytes}fn text(value: &str) -> Sql {Sql::Text(value.into())}pubfn latest_admission(db: &Driver, database: &str, member: &str) -> Result<Option<String>> {if membership::of(db, member)?.is_none() {returnOk(None); }Ok(db.get("SELECT id FROM cotton_admissions WHERE workspace_id=? AND person_key=? ORDER BY _hlc DESC,id DESC LIMIT 1", &[text(database),text(member)])?.and_then(|row| row.text("id").map(str::to_owned)))}/// The owner's person key as his PERSONAL database holds it — the public half,/// and nothing else of the row: the key his claim in a shared database is/// signed by, so the member to ask [`bound_person`] about. `None` before the/// first vault pass has written one.////// It is here, and answers one column, because the same row holds the key's/// sealed private half: no tool's own code names that table (the guard is/// `person_key_private_half_is_never_listed_by_any_mcp_tool`), so a tool that/// needs to know WHO the owner is asks this and can be handed nothing more.pubfn owner_public_key(personal: &Driver) -> Result<Option<String>> {Ok(personal .get("SELECT public_key FROM cotton_person_keys ORDER BY created_at, id LIMIT 1", &[])? .and_then(|row| row.text("public_key").map(str::to_owned)))}/// No pre-link, name join, owner fallback, unsigned overlay or read-time birth.pubfn bound_person(db: &Driver, database: &str, member: &str) -> Result<Option<BoundPerson>> {letSome(admission) = latest_admission(db, database, member)? else {returnOk(None); };letSome(row) = db.get("SELECT person_key,person_id,p_sig,claimed_at FROM cotton_member_person_claims WHERE workspace_id=? AND admission_id=? ORDER BY claimed_at DESC,_hlc DESC,id DESC LIMIT 1", &[text(database),text(&admission)])? else { returnOk(None); };let (Some(key), Some(person), Some(sig), Some(at)) = ( row.text("person_key"), row.text("person_id"), row.text("p_sig"), row.text("claimed_at"), ) else {returnOk(None); };if key != member || !verifies( member, &claim_bytes(database, &admission, member, person, at), sig, ) {returnOk(None); }letSome(person_row) = db.get("SELECT avatar_key FROM cotton_people WHERE workspace_id=? AND id=?", &[text(database), text(person)], )?else {returnOk(None); };let prefix = format!("{database}/");Ok(Some(BoundPerson { person_id: person.into(), photo_key: person_row .text("avatar_key") .filter(|key| key.starts_with(&prefix)) .map(str::to_owned), }))}fn verifies(member: &str, bytes: &[u8], signature: &str) -> bool {letSome(key) = URL_SAFE_NO_PAD .decode(member) .ok() .and_then(|raw| raw.try_into().ok()) .and_then(|raw| VerifyingKey::from_bytes(&raw).ok())else {returnfalse; };letSome(sig) = URL_SAFE_NO_PAD .decode(signature) .ok() .and_then(|raw| Signature::from_slice(&raw).ok())else {returnfalse; }; key.verify_strict(bytes, &sig).is_ok()}