use ciborium::value::Value;
use ed25519_dalek::VerifyingKey;
use writerslogic_warm::core::provenance::{cose, did};
const MEMORY: &[u8] = include_bytes!("fixtures/cogmem.memory.provenance.cose");
const REASONING: &[u8] = include_bytes!("fixtures/crosstalk.orchestration.audit.cose");
fn field<'a>(claim: &'a Value, key: &str) -> Option<&'a Value> {
match claim {
Value::Map(entries) => entries
.iter()
.find(|(k, _)| matches!(k, Value::Text(t) if t == key))
.map(|(_, v)| v),
_ => None,
}
}
fn text(claim: &Value, key: &str) -> String {
match field(claim, key) {
Some(Value::Text(t)) => t.clone(),
_ => "?".to_string(),
}
}
fn verify(label: &str, cose_bytes: &[u8]) -> anyhow::Result<Value> {
let kid = cose::extract_key_id(cose_bytes)?;
let key_bytes: [u8; 32] = kid
.as_slice()
.try_into()
.map_err(|_| anyhow::anyhow!("kid must be a 32-byte ed25519 key"))?;
let verifying_key = VerifyingKey::from_bytes(&key_bytes)?;
let payload = cose::verify_and_extract(&verifying_key, cose_bytes)?;
let claim: Value = ciborium::from_reader(payload.as_slice())?;
let iss = text(&claim, "iss");
let derived = did::did_key_from_ed25519(&key_bytes);
if iss != derived {
anyhow::bail!("issuer binding mismatch: claim iss {iss} != signing key {derived}");
}
println!(" VERIFIED {label}");
println!(" issuer {iss} (matches the signing key)");
Ok(claim)
}
fn main() -> anyhow::Result<()> {
println!(
"writerslogic-warm independently verifying the cogmem C2PA sample's \
cognition statements:\n"
);
let mem = verify("memory cogmem.memory.provenance", MEMORY)?;
println!(
" attests: memory '{}' ({}, {})",
text(&mem, "memoryId"),
text(&mem, "memoryType"),
text(&mem, "event")
);
let rsn = verify("reasoning crosstalk.orchestration.audit", REASONING)?;
let turns = match field(&rsn, "turn_count") {
Some(Value::Integer(i)) => {
let n: i128 = (*i).into();
n.to_string()
}
_ => "?".to_string(),
};
println!(
" attests: session '{}', {} turns",
text(&rsn, "session_id"),
turns
);
println!(
"\nPASS: both cognition statements verify under writerslogic-warm — \
identical bytes,\n independent implementation. \
Cross-implementation conformance confirmed."
);
Ok(())
}