//! The person key's publish and keep, over a real attached copy and a //! scripted object door. Synthetic keys only. use std::sync::Arc; use serde_json::{json, Value}; use cotton_client::transport::scripted::Reply; use cotton_core::keys::PersonKey; use crate::phone::unlock::tests::{door, fixture_vmk}; use crate::shell::client::phone_tests::attached_on_a_clock; fn local(client: &crate::shell::client::ClientController) -> Option<(String, String, String)> { client .with_writer(|replica| { Ok(replica.driver.get("SELECT id, public_key, sealed_private FROM cotton_person_keys", &[]).map_err(|e| e.to_string())?.map(|row| { (row.text("id").unwrap().to_string(), row.text("public_key").unwrap().to_string(), row.text("sealed_private").unwrap().to_string()) })) }) .unwrap() } /// RULE: a signed-in copy with no person key mints one the first time its vault opens, keeps it sealed under the vault key, publishes it to the object by fingerprint, and publishes once per launch after that. #[test] fn a_signed_in_copy_mints_its_person_key_with_the_vault_open_and_publishes_it_once() { let (_dir, client, _clock) = attached_on_a_clock(); assert!(client.signed_in(), "the fixture is a signed-in copy"); // No line to the object: nothing is made. super::ensure(&client, &fixture_vmk(), 1).expect("nothing to do"); assert!(local(&client).is_none(), "a copy that cannot ask the object makes no person key"); let (object, exchange) = door([Reply::ok(r#"{"rows":[]}"#), Reply::ok(r#"{"made":{"table":"person_key"}}"#)]); client.set_object_door(Some(Arc::new(object))); super::ensure(&client, &fixture_vmk(), 1).expect("kept"); assert_eq!(exchange.nth(0).method, "GET", "the object is asked whether it holds one first"); let (id, public, sealed) = local(&client).expect("a person key"); let opened = PersonKey::open_private(&fixture_vmk(), &public, 1, &sealed).expect("sealed under the vault key"); assert_eq!(opened.fingerprint(), id); let posted: Value = serde_json::from_slice(&exchange.nth(1).body).unwrap(); assert_eq!(posted["act"], "create"); assert_eq!(posted["args"]["table"], "person_key"); assert_eq!(posted["args"]["id"], json!(id)); assert_eq!(posted["args"]["data"]["public_key"], json!(public)); assert_eq!(posted["args"]["data"]["sealed_private_enc"], json!(sealed)); assert_eq!(posted["args"]["data"]["vmk_version"], 1); assert!(super::publish_owed(&client).is_none(), "published this launch already"); super::ensure(&client, &fixture_vmk(), 1).expect("kept"); assert_eq!(exchange.calls(), 2, "a copy holding its key asks, makes and posts nothing more"); client.set_object_door(None); } /// RULE: a copy with no person key takes the object's when the object holds one (a second vault device, or a reinstall), only when it opens with this vault. #[test] fn a_copy_takes_the_objects_person_key_when_it_opens_with_the_vault() { let (_dir, client, _clock) = attached_on_a_clock(); let key = PersonKey::from_seed(zeroize::Zeroizing::new([6; 32])); let other = cotton_core::vault::Vmk::from_bytes([1; 32]); let served = |sealed: &str| Reply::ok(&json!({"rows": [{"table": "person_key", "id": key.fingerprint(), "data": {"public_key": key.public_key(), "sealed_private_enc": sealed, "vmk_version": 1, "created_at": "2026-11-11T08:00:00.000Z"}}]}).to_string()); let (object, _exchange) = door([served(&key.seal_private(&other, 1).unwrap())]); client.set_object_door(Some(Arc::new(object))); assert_eq!(super::ensure(&client, &fixture_vmk(), 1).unwrap_err().code, "person_key_does_not_open"); assert!(local(&client).is_none()); let (object, exchange) = door([served(&key.seal_private(&fixture_vmk(), 1).unwrap()), Reply::ok("{}")]); client.set_object_door(Some(Arc::new(object))); super::ensure(&client, &fixture_vmk(), 1).expect("taken"); assert_eq!(local(&client).map(|(id, public, _)| (id, public)), Some((key.fingerprint(), key.public_key()))); assert_eq!(exchange.calls(), 2, "asked, then published as it stands"); client.set_object_door(None); }