Cotton code
71 lines · 5.1 KB · RustRaw
//! What a phone does for a new object, done by hand for an INVENTED dev account (the token-scopes proof, 24 Nov//! 2026): the device seal-device signed in and published a vault key for gets the first wrap of a fresh vault//! master key (it founds the vault), and the database's data key is minted under it, as `object_e2e.rs`'s//! `found_and_mint` does for its walk. Never for a real person's account: an account with a vault already is//! refused.//!//!   DEV_FOUND_BASE=<the dev Worker's address (config/dev-server.json)> DEV_FOUND_DEVICE_ID=<id> \//!   DEV_FOUND_TOKEN_FILE=<device credential file> \//!     cargo test -p cotton-mac --test dev_found -- --ignored --nocapture//!//! Nothing secret is printed.use cotton_mac::object::ObjectClient;use cotton_mac::vault::KeyRows as _;use cotton_client::seal::DataKeyStore as _;use serde_json::{json, Map, Value};#[test]#[ignore = "founds the vault of an invented dev account: run by hand"]fn found_the_vault_of_an_invented_dev_account() {    use base64::Engine as _;    let env = |name: &str| std::env::var(name).unwrap_or_else(|_| panic!("{name} is needed"));    let base = env("DEV_FOUND_BASE");    assert!(base.contains("cottondb-dev.") || base.contains("127.0.0.1") || base.contains("localhost"), "the dev Worker only, never live: {base}");    let device_id = env("DEV_FOUND_DEVICE_ID");    let token = std::fs::read_to_string(env("DEV_FOUND_TOKEN_FILE")).expect("the device credential").trim().to_string();    assert!(!token.starts_with("ctn_"), "a device credential, never a ctn_ token");    let client = ObjectClient::new(base, Some(token)).expect("a client");    let held = client.get_json("/v1/sync", &[("since", "0".to_string()), ("tables", "seal_key,vault_key".to_string())]).expect("the key rows");    let rows = held["rows"].as_array().cloned().unwrap_or_default();    assert!(!rows.iter().any(|r| r["table"] == "seal_key" && r["id"] == cotton_client::seal::DK_ROW && r["deleted"] != json!(true)), "this account holds a data key already: founded, nothing done");    let (id, mut data) = client.key_row(&device_id).expect("the key rows").expect("seal-device published this device's vault key");    let public: [u8; 32] = base64::engine::general_purpose::URL_SAFE_NO_PAD.decode(data["public_key"].as_str().expect("a public key")).expect("base64").try_into().expect("32 bytes");    let vmk = cotton_core::vault::Vmk::mint();    data.insert("wrapped_vmk".into(), json!(cotton_core::vault::wrap::wrap(&public, vmk.expose()).expect("a wrap")));    data.insert("vmk_version".into(), json!(1));    client.put_key_row(&id, &Value::Object(data)).expect("the founding wrap");    let at = chrono::Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Millis, true);    let (_dk, row): (_, Map<String, Value>) = cotton_client::seal::mint_data_key(&vmk, 1, &at).expect("a data key");    client.upsert(cotton_client::seal::SEAL_KEY_TABLE, cotton_client::seal::DK_ROW, &Value::Object(row)).expect("the data key row");    println!("founded: the vault opens for device {device_id}, and the data key is minted");}/// What a phone holding the data key does once (e2e rulings R2): it hands the object the arrival key's public half,/// so the object can seal what it writes itself (a fetch's claim and progress). Done here for the invented account,/// by this device, which opens the vault.//////   DEV_FOUND_BASE=… DEV_FOUND_DEVICE_ID=<id> DEV_FOUND_TOKEN_FILE=<device credential file> DEV_FOUND_KEY_FILE=<vault key file> \///     cargo test -p cotton-mac --test dev_found hand_the_arrival_key -- --ignored --nocapture#[test]#[ignore = "hands an invented dev account's arrival key: run by hand"]fn hand_the_arrival_key_of_an_invented_dev_account() {    let env = |name: &str| std::env::var(name).unwrap_or_else(|_| panic!("{name} is needed"));    let base = env("DEV_FOUND_BASE");    assert!(base.contains("cottondb-dev.") || base.contains("127.0.0.1") || base.contains("localhost"), "the dev Worker only, never live: {base}");    let device_id = env("DEV_FOUND_DEVICE_ID");    let token = std::fs::read_to_string(env("DEV_FOUND_TOKEN_FILE")).expect("the device credential").trim().to_string();    let key_file = std::path::PathBuf::from(env("DEV_FOUND_KEY_FILE"));    let object = ObjectClient::new(base, Some(token)).expect("a client");    let keyring = cotton_mac::vault::tool_keyring(&device_id, Some(&key_file));    let device = cotton_mac::device::Device::open(object, &device_id, keyring.as_ref(), None).expect("this device opens the vault");    let dk = device.keys.data_key(&device.database_id).expect("the held key").expect("a data key");    let public = cotton_core::seal::arrival::arrival_public(&dk, &device.database_id).expect("the arrival key");    let row = json!({ "pub": cotton_core::seal::share::key_text(&public) });    let answer = device.object.post_act("create", &json!({ "table": "seal_key", "id": cotton_core::seal::arrival::ARRIVAL_ROW, "data": row }));    match answer {        Ok(_) => println!("handed: the object holds the arrival key"),        Err(e) if e.code() == Some("duplicate_id") => println!("the object holds an arrival key already"),        Err(e) => panic!("the arrival key was not taken: {e}"),    }}