//! The standing grant to the person's own Claude (owner's//! decision 20 Nov 2026: connecting Claude is the grant; no timer, no card per//! token). The keys are wrapped to a MEMBER key, an X25519 pair whose private//! half the object keeps only inside the connection's tokens (a carrier per//! token, sealed under a key derived from the token string, which the object//! never stores); so at rest the object holds nothing that opens a granted//! table.//!//! ```text//! bundle = the unlock bundle (grant.rs) with request_id = token_id = member_id//! wrapped = w1(member_pub, bundle)//! statement = "cotton-agent-grant-v1\n" database_id "\n" member_id "\n" b64url(member_pub) "\n"//! sorted(withheld) joined "," "\n" hex(sha256(wrapped))//! sig = b64url Ed25519(person key, statement)//! ```//!//! The statement signs the POLICY ("every sealed table except `withheld`") and//! the member key it is granted to, never a table list: a table the kit adds//! later, or a table's next key version, is inside the policy, and the phone//! adds its keys on its own as an unsigned [`addition`] (a second wrap to the//! same member key). Widening the policy (a withheld table turned back on) is//! signed again. [`NEVER`] is never inside it: saved secrets are not served to//! assistants.use rand::RngCore;use zeroize::Zeroizing;usesuper::grant::{bundle, bundle_json, parse_bundle, GrantBundle};usesuper::keys::DataKey;usesuper::{b64, plain_field, sha256_hex, unb64, SealError, NONCE_LEN};usecrate::keys::person::{self, PersonKey};usecrate::vault::wrap::{unwrap, wrap_with};pubconst AGENT_CONTEXT: &str = "cotton-agent-grant-v1";/// The tables no standing grant covers: saved passwords and passkeys and the/// code runner's credentials. A read of one still files today's timed card.pubconst NEVER: [&str; 3] = ["code_credential", "passkey", "secret"];/// The tables no assistant is ever given, not in a standing grant and not in a timed window, whatever the person/// picks: Cotton Sites' list (`site`: it says where he banks; assets/design/sites/DECISIONS.md question 14,/// "Assistants never see Sites"). Unlike [`NEVER`], no card offers them. The phone and the Mac hand no key for one,/// so the object never holds a key an assistant could open one with.pubconst CLOSED: [&str; 1] = ["site"];/// Whether `table` is one no assistant is ever given (`CLOSED`).pubfn closed(table: &str) -> bool { CLOSED.contains(&table)}/// What the phone posts for a member: the policy, the wrap and the signature.#[derive(Debug, Clone, PartialEq, Eq)]pubstructAgentGrant {/// Sorted, de-duplicated; every [`NEVER`] table among them.pub withheld: Vec<String>,pub wrapped: String,pub signer: String,pub sig: String,}/// `withheld` with [`NEVER`] added, sorted, de-duplicated, each checked.pubfn policy(withheld: implIntoIterator<Item = implAsRef<str>>) -> Result<Vec<String>, SealError> {letmut out: Vec<String> = NEVER.iter().map(|t| t.to_string()).collect();for table in withheld { plain_field("table", table.as_ref())?; out.push(table.as_ref().to_string()); } out.sort(); out.dedup();Ok(out)}/// The bytes the person key signs. `withheld` must already be a [`policy`].pubfn agent_statement(database_id: &str, member_id: &str, member_pub: &[u8; 32], withheld: &[String], wrapped: &str) -> Result<Vec<u8>, SealError> { plain_field("database", database_id)?; plain_field("member", member_id)?;let said = policy(withheld)?;if said != withheld {returnErr(SealError::BadInput("withheld is not sorted, once each, with every never-granted table".into())); }Ok(format!("{AGENT_CONTEXT}\n{database_id}\n{member_id}\n{}\n{}\n{}", b64(member_pub), said.join(","), sha256_hex(wrapped.as_bytes())).into_bytes())}fn check_inside(withheld: &[String], tables: &[(String, u32)]) -> Result<(), SealError> {if tables.is_empty() {returnErr(SealError::BadInput("a grant names at least one table".into())); }for (table, _) in tables { plain_field("table", table)?;if withheld.contains(table) {returnErr(SealError::BadInput(format!("{table} is withheld from the grant"))); } }Ok(())}fn randomness() -> (Zeroizing<[u8; 32]>, [u8; NONCE_LEN]) {letmut ephemeral = Zeroizing::new([0u8; 32]);letmut nonce = [0u8; NONCE_LEN]; rand::rngs::OsRng.fill_bytes(ephemeral.as_mut_slice()); rand::rngs::OsRng.fill_bytes(&mut nonce); (ephemeral, nonce)}/// The grant on the phone: the tables' keys (every version up to each current/// one) wrapped to the member key, and the policy signed.pubfn build_agent_grant(dk: &DataKey, database_id: &str, member_id: &str, member_pub: &[u8; 32], tables: &[(String, u32)], withheld: &[String], person: &PersonKey) -> Result<AgentGrant, SealError> {let (ephemeral, nonce) = randomness(); build_agent_grant_with(&ephemeral, &nonce, dk, database_id, member_id, member_pub, tables, withheld, person)}/// [`build_agent_grant`] with the randomness supplied.#[allow(clippy::too_many_arguments)]pubfn build_agent_grant_with(ephemeral: &[u8; 32], nonce: &[u8; NONCE_LEN], dk: &DataKey, database_id: &str, member_id: &str, member_pub: &[u8; 32], tables: &[(String, u32)], withheld: &[String], person: &PersonKey) -> Result<AgentGrant, SealError> {let withheld = policy(withheld)?; check_inside(&withheld, tables)?;let keys = bundle(dk, database_id, member_id, member_id, tables)?;let wrapped = wrap_with(ephemeral, nonce, member_pub, bundle_json(&keys).as_bytes()).ok_or(SealError::BadInput("member_pub is not a usable X25519 key".into()))?;let statement = agent_statement(database_id, member_id, member_pub, &withheld, &wrapped)?;Ok(AgentGrant { withheld, wrapped, signer: person.fingerprint(), sig: b64(&person.sign(&statement)) })}/// An unsigned addition inside a signed policy: more tables, or more versions,/// wrapped to the same member key.pubfn addition(dk: &DataKey, database_id: &str, member_id: &str, member_pub: &[u8; 32], tables: &[(String, u32)], withheld: &[String]) -> Result<String, SealError> {let withheld = policy(withheld)?; check_inside(&withheld, tables)?;let keys = bundle(dk, database_id, member_id, member_id, tables)?;let (ephemeral, nonce) = randomness(); wrap_with(&ephemeral, &nonce, member_pub, bundle_json(&keys).as_bytes()).ok_or(SealError::BadInput("member_pub is not a usable X25519 key".into()))}/// Does the person key sign this policy and wrap for this member key?pubfn verify_agent_grant(person_public_key: &str, database_id: &str, member_id: &str, member_pub: &[u8; 32], grant: &AgentGrant) -> bool {if person::fingerprint(person_public_key).as_deref() != Some(grant.signer.as_str()) {returnfalse; }letOk(statement) = agent_statement(database_id, member_id, member_pub, &grant.withheld, &grant.wrapped) else {returnfalse; };letSome(sig) = unb64(&grant.sig) else {returnfalse; }; person::verify(person_public_key, &statement, &sig)}/// Open one wrap with the member's private half (on the object, while it/// answers a call that presented a token carrying it). Refused unless it names/// this member and holds no withheld table.pubfn open_agent_wrap(member_secret: &[u8; 32], member_id: &str, withheld: &[String], wrapped: &str) -> Result<GrantBundle, SealError> {let text = Zeroizing::new(unwrap(member_secret, wrapped).ok_or(SealError::DoesNotOpen)?);let opened = parse_bundle(&text)?;if opened.request_id != member_id || opened.token_id != member_id {returnErr(SealError::DoesNotOpen); }if opened.keys.keys().any(|t| withheld.contains(t)) || opened.keys.keys().ne(opened.blind.keys()) {returnErr(SealError::DoesNotOpen); }Ok(opened)}#[cfg(test)]#[path = "agent_tests.rs"]mod tests;