//! Sealed at arrival: round trips, refusals by name, and the vector the //! Worker's twin answers byte for byte. Synthetic keys and rows only. use serde_json::{json, Map, Value}; use super::*; const DATABASE: &str = "00000000-0000-4000-8000-0000000000a1"; fn dk() -> DataKey { DataKey::from_bytes([21; 32]) } fn spec() -> SealSpec { SealSpec { clear: vec!["day".into()], ..SealSpec::default() } } fn row() -> Map { json!({"day": "2026-11-15", "title": "Lantern market", "notes": "Bring the copper kettle"}).as_object().unwrap().clone() } fn sealed() -> Map { let public = arrival_public(&dk(), DATABASE).unwrap(); seal_arrival_with(&Dek::from_bytes([3; 32]), &[4; 12], &[5; 32], &[6; 12], &public, DATABASE, "garden_event", "e-1", &spec(), &row()).unwrap() } /// RULE (e2e rulings R2): a row the object seals at arrival keeps only its /// clear columns in the clear, says it is an arrival row, and opens on a /// device holding the DK into the row the object was handed. #[test] fn an_arrival_row_opens_with_the_key_the_data_key_derives() { let data = sealed(); assert!(is_arrival(&data)); assert_eq!(data["day"], "2026-11-15"); assert_eq!(data[row::SEAL_V], ARRIVAL_V); assert!(!Value::Object(data.clone()).to_string().contains("Lantern"), "no typed word in the clear"); let opened = open_arrival(&dk(), DATABASE, "garden_event", "e-1", &data).unwrap(); assert_eq!(Value::Object(opened), Value::Object(row())); } /// RULE: another DK, another table, another row or another database opens /// nothing; each is refused by name, never answered blank. #[test] fn an_arrival_row_opens_for_no_other_key_table_row_or_database() { let data = sealed(); let other = DataKey::from_bytes([22; 32]); assert_eq!(open_arrival(&other, DATABASE, "garden_event", "e-1", &data).unwrap_err().code(), "seal_does_not_open"); assert_eq!(open_arrival(&dk(), DATABASE, "garden_note", "e-1", &data).unwrap_err().code(), "seal_does_not_open"); assert_eq!(open_arrival(&dk(), DATABASE, "garden_event", "e-2", &data).unwrap_err().code(), "seal_does_not_open"); assert_eq!(open_arrival(&dk(), "00000000-0000-4000-8000-0000000000a2", "garden_event", "e-1", &data).unwrap_err().code(), "seal_does_not_open"); } fn vector() -> Value { let path = cotton_store::manifest_dir().join("../cotton-conformance/vectors/seal/arrival.json"); serde_json::from_str(&std::fs::read_to_string(&path).unwrap_or_else(|error| panic!("{}: {error}", path.display()))).expect("the vector reads") } /// RULE: the vector the Worker's twin seals byte for byte: the arrival /// public key a DK derives, and the stored row for the randomness given. #[test] fn the_arrival_vector_holds() { let v = vector(); let public = arrival_public(&dk(), DATABASE).unwrap(); assert_eq!(json!(crate::seal::share::key_text(&public)), v["arrival_pub"], "the arrival public key"); assert_eq!(Value::Object(sealed()), v["stored"], "the stored row"); let opened = open_arrival(&dk(), DATABASE, "garden_event", "e-1", v["stored"].as_object().unwrap()).unwrap(); assert_eq!(Value::Object(opened), v["row"]); } /// A patch the object keeps for a change to a sealed row: the changed columns alone, sealed as an arrival row's sealed half is. fn patch(id: &str, columns: Value, dek: u8) -> Value { let public = arrival_public(&dk(), DATABASE).unwrap(); let sealed = seal_arrival_with(&Dek::from_bytes([dek; 32]), &[7; 12], &[8; 32], &[9; 12], &public, DATABASE, "garden_event", id, &SealSpec::default(), columns.as_object().unwrap()).unwrap(); json!({ "sealed_enc": sealed[row::SEALED_ENC], "dek_enc": sealed[row::DEK_ENC] }) } /// RULE (e2e rulings R2, the arrival patch): the object's changes to a row it /// holds sealed are folded over the row as a device opens it, in order, later /// over earlier; what no patch names stays as sealed; the patches themselves /// are gone from the opened row; and a patch made for another row opens /// nothing, refused by name. #[test] fn arrival_patches_fold_over_the_row_in_order() { let mut data = sealed(); data.insert(ARRIVAL_PATCHES.into(), json!([patch("e-1", json!({"title": "Lantern fair"}), 10), patch("e-1", json!({"title": "Lantern fair, moved", "place": "the old mill"}), 11)])); assert!(has_patches(&data)); assert!(!Value::Object(data.clone()).to_string().contains("mill"), "no typed word in the clear"); let opened = open_arrival(&dk(), DATABASE, "garden_event", "e-1", &data).unwrap(); let folded = fold_patches(&dk(), DATABASE, "garden_event", "e-1", &data, opened).unwrap(); assert_eq!(Value::Object(folded), json!({"day": "2026-11-15", "title": "Lantern fair, moved", "notes": "Bring the copper kettle", "place": "the old mill"})); let mut stray = sealed(); stray.insert(ARRIVAL_PATCHES.into(), json!([patch("e-2", json!({"title": "Someone else's"}), 12)])); let opened = open_arrival(&dk(), DATABASE, "garden_event", "e-1", &stray).unwrap(); assert_eq!(fold_patches(&dk(), DATABASE, "garden_event", "e-1", &stray, opened).unwrap_err().code(), "seal_does_not_open"); } /// RULE: a fill patch (a lookup's details) fills only what the row leaves /// empty: what the person wrote stays, an empty column takes the lookup's. #[test] fn a_fill_patch_fills_only_what_the_row_leaves_empty() { let mut data = sealed(); let mut fill = patch("e-1", json!({"title": "Lantern market (Open Library)", "author": "An invented author"}), 13); fill["fill"] = json!(true); data.insert(ARRIVAL_PATCHES.into(), json!([fill])); let opened = open_arrival(&dk(), DATABASE, "garden_event", "e-1", &data).unwrap(); let folded = fold_patches(&dk(), DATABASE, "garden_event", "e-1", &data, opened).unwrap(); assert_eq!(folded["title"], "Lantern market", "the person's title stays"); assert_eq!(folded["author"], "An invented author", "an empty column takes the lookup's"); } /// RULE (keys DECISIONS.md S3, 25 Nov 2026): a password's value saved over MCP is sealed by the object to the arrival /// key as `a2:|`, bound to the secret's id; a device holding the DK opens the payload, and the /// same box under another id, another DK or a broken shape does not open. #[test] fn a_value_sealed_at_arrival_opens_on_a_device_for_its_own_secret_alone() { let public = arrival_public(&dk(), DATABASE).unwrap(); let payload = r#"{"kind":"api_key","key":"invented-key-0000-1111"}"#; let data = json!({ "payload": payload }).as_object().unwrap().clone(); let row = seal_arrival_with(&Dek::from_bytes([7; 32]), &[8; 12], &[9; 32], &[10; 12], &public, DATABASE, VALUE_TABLE, "s-1", &SealSpec::default(), &data).unwrap(); let sealed = format!("{VALUE_PREFIX}{}|{}", row[row::SEALED_ENC].as_str().unwrap(), row[row::DEK_ENC].as_str().unwrap()); assert!(is_arrival_value(&sealed)); assert!(!sealed.contains("invented-key"), "no value in the clear"); assert_eq!(open_arrival_value(&dk(), DATABASE, "s-1", &sealed).unwrap(), payload); assert!(open_arrival_value(&dk(), DATABASE, "s-2", &sealed).is_err(), "bound to its own secret"); assert!(open_arrival_value(&DataKey::from_bytes([22; 32]), DATABASE, "s-1", &sealed).is_err(), "another person's key"); assert!(open_arrival_value(&dk(), DATABASE, "s-1", sealed.trim_start_matches(VALUE_PREFIX)).is_err(), "not an a2 value"); assert!(open_arrival_value(&dk(), DATABASE, "s-1", &sealed.replace('|', "")).is_err(), "a broken shape"); }