use serde_json::{json, Map, Value};
use crate::data_integrity::{self, BuildProofOptions};
use crate::error::{CoreError, Result};
use crate::keys;
use crate::pq::{MlDsa44KeyPair, MLDSA44_PUBLIC_LEN};
use crate::{hybrid, jcs, multikey};
use aes_gcm::aead::Aead;
use aes_gcm::{Aes256Gcm, KeyInit, Nonce};
use base64::engine::general_purpose::URL_SAFE_NO_PAD;
use base64::Engine;
use sha2::{Digest, Sha256};
use std::collections::HashSet;
pub const VC_CONTEXT_V2: &str = "https://www.w3.org/ns/credentials/v2";
pub const VOUCH_CONTEXT_V1: &str = "https://vouch-protocol.com/contexts/v1";
pub const ROBOT_IDENTITY_TYPE: &str = "RobotIdentityCredential";
pub(crate) fn mb64(bytes: &[u8]) -> String {
format!("u{}", URL_SAFE_NO_PAD.encode(bytes))
}
pub(crate) fn unmb64(s: &str) -> Result<Vec<u8>> {
let body = s
.strip_prefix('u')
.ok_or_else(|| CoreError::Json("expected multibase 'u' prefix".into()))?;
URL_SAFE_NO_PAD
.decode(body)
.map_err(|e| CoreError::Json(format!("bad base64url: {e}")))
}
pub(crate) fn has_type(type_field: Option<&Value>, want: &str) -> bool {
match type_field {
Some(Value::String(s)) => s == want,
Some(Value::Array(items)) => items.iter().any(|v| v.as_str() == Some(want)),
_ => false,
}
}
pub fn robot_identity_binding(robot_did: &str, robot_key_multibase: &str) -> Vec<u8> {
jcs::canonicalize(&json!({
"key": robot_key_multibase,
"robotDid": robot_did,
}))
}
#[derive(Debug, Clone)]
pub struct MintRobotIdentity {
pub robot_did: String,
pub make: String,
pub model: String,
pub serial: String,
pub owner: Option<String>,
pub root_kind: String,
pub root_public_multibase: String,
pub attestation: Vec<u8>,
pub lifecycle: Option<Value>,
pub valid_from: String,
pub valid_until: Option<String>,
}
pub fn mint_robot_identity(robot_seed: &[u8], params: &MintRobotIdentity) -> Result<Value> {
let lifecycle = params
.lifecycle
.clone()
.unwrap_or_else(|| json!([{ "event": "commissioned", "timestamp": params.valid_from }]));
let mut subject = Map::new();
subject.insert("id".into(), json!(params.robot_did));
subject.insert("make".into(), json!(params.make));
subject.insert("model".into(), json!(params.model));
subject.insert("serial".into(), json!(params.serial));
subject.insert(
"hardwareRoot".into(),
json!({
"kind": params.root_kind,
"publicKeyMultibase": params.root_public_multibase,
"attestation": mb64(¶ms.attestation),
}),
);
subject.insert("lifecycle".into(), lifecycle);
if let Some(owner) = ¶ms.owner {
subject.insert("owner".into(), json!(owner));
}
let mut cred = Map::new();
cred.insert("@context".into(), json!([VC_CONTEXT_V2, VOUCH_CONTEXT_V1]));
cred.insert(
"type".into(),
json!(["VerifiableCredential", ROBOT_IDENTITY_TYPE]),
);
cred.insert("issuer".into(), json!(params.robot_did));
cred.insert("validFrom".into(), json!(params.valid_from));
if let Some(vu) = ¶ms.valid_until {
cred.insert("validUntil".into(), json!(vu));
}
cred.insert("credentialSubject".into(), Value::Object(subject));
let opts = BuildProofOptions::new(
format!("{}#key-1", params.robot_did),
params.valid_from.clone(),
);
data_integrity::sign(&Value::Object(cred), robot_seed, &opts)
}
pub fn verify_robot_identity(credential: &Value, robot_public_key: &[u8]) -> Result<Option<Value>> {
let obj = credential
.as_object()
.ok_or_else(|| CoreError::Json("credential must be a JSON object".into()))?;
if !has_type(obj.get("type"), ROBOT_IDENTITY_TYPE) {
return Ok(None);
}
if !data_integrity::verify_proof(credential, robot_public_key)? {
return Ok(None);
}
let subject = match obj.get("credentialSubject").and_then(|s| s.as_object()) {
Some(s) => s,
None => return Ok(None),
};
let hw = match subject.get("hardwareRoot").and_then(|h| h.as_object()) {
Some(h) => h,
None => return Ok(None),
};
let hw_mb = match hw.get("publicKeyMultibase").and_then(|v| v.as_str()) {
Some(s) => s,
None => return Ok(None),
};
let attestation_mb = match hw.get("attestation").and_then(|v| v.as_str()) {
Some(s) => s,
None => return Ok(None),
};
let decoded = match multikey::decode(hw_mb) {
Ok(d) if d.algorithm == "Ed25519" && d.raw_key.len() == 32 => d,
_ => return Ok(None),
};
let attestation = match unmb64(attestation_mb) {
Ok(a) => a,
Err(_) => return Ok(None),
};
let robot_key_mb = multikey::encode_ed25519_public(robot_public_key)?;
let robot_did = subject.get("id").and_then(|v| v.as_str()).unwrap_or("");
let binding = robot_identity_binding(robot_did, &robot_key_mb);
if !keys::verify(&decoded.raw_key, &binding, &attestation)? {
return Ok(None);
}
Ok(Some(Value::Object(subject.clone())))
}
pub const MODEL_PROVENANCE_TYPE: &str = "ModelProvenanceAttestation";
pub fn config_hash(config: &Value) -> String {
let digest = Sha256::digest(jcs::canonicalize(config));
mb64(&digest)
}
#[derive(Debug, Clone)]
pub struct BuildProvenance {
pub issuer_did: String,
pub robot_did: String,
pub model_name: String,
pub weights_hash: String,
pub safety_policy: String,
pub config: Option<Value>,
pub version: Option<String>,
pub supersedes: Option<String>,
pub valid_from: String,
pub valid_until: Option<String>,
}
pub fn build_provenance_attestation(signer_seed: &[u8], params: &BuildProvenance) -> Result<Value> {
let mut vla = Map::new();
vla.insert("modelName".into(), json!(params.model_name));
vla.insert("weightsHash".into(), json!(params.weights_hash));
vla.insert("safetyPolicy".into(), json!(params.safety_policy));
if let Some(v) = ¶ms.version {
vla.insert("version".into(), json!(v));
}
if let Some(cfg) = ¶ms.config {
vla.insert("configHash".into(), json!(config_hash(cfg)));
}
let mut subject = Map::new();
subject.insert("id".into(), json!(params.robot_did));
subject.insert("vla".into(), Value::Object(vla));
if let Some(s) = ¶ms.supersedes {
subject.insert("supersedes".into(), json!(s));
}
let mut cred = Map::new();
cred.insert("@context".into(), json!([VC_CONTEXT_V2, VOUCH_CONTEXT_V1]));
cred.insert(
"type".into(),
json!(["VerifiableCredential", MODEL_PROVENANCE_TYPE]),
);
cred.insert("issuer".into(), json!(params.issuer_did));
cred.insert("validFrom".into(), json!(params.valid_from));
if let Some(vu) = ¶ms.valid_until {
cred.insert("validUntil".into(), json!(vu));
}
cred.insert("credentialSubject".into(), Value::Object(subject));
let opts = BuildProofOptions::new(
format!("{}#key-1", params.issuer_did),
params.valid_from.clone(),
);
data_integrity::sign(&Value::Object(cred), signer_seed, &opts)
}
pub fn verify_provenance_attestation(
attestation: &Value,
public_key: &[u8],
config: Option<&Value>,
) -> Result<Option<Value>> {
let obj = attestation
.as_object()
.ok_or_else(|| CoreError::Json("attestation must be a JSON object".into()))?;
if !has_type(obj.get("type"), MODEL_PROVENANCE_TYPE) {
return Ok(None);
}
if !data_integrity::verify_proof(attestation, public_key)? {
return Ok(None);
}
let subject = match obj.get("credentialSubject").and_then(|s| s.as_object()) {
Some(s) => s,
None => return Ok(None),
};
if let Some(cfg) = config {
let want = config_hash(cfg);
let got = subject
.get("vla")
.and_then(|v| v.as_object())
.and_then(|v| v.get("configHash"))
.and_then(|v| v.as_str())
.unwrap_or("");
if got != want {
return Ok(None);
}
}
Ok(Some(Value::Object(subject.clone())))
}
pub const PHYSICAL_SCOPE_TYPE: &str = "PhysicalCapabilityScope";
#[derive(Debug, Clone)]
pub struct ShiftWindow {
pub start: String,
pub end: String,
}
#[derive(Debug, Clone, Default)]
pub struct PhysicalAction {
pub force_n: Option<f64>,
pub speed_mps: Option<f64>,
pub near_humans: bool,
pub zone: Option<String>,
pub time_hm: Option<String>,
}
#[derive(Debug, Clone)]
pub struct CheckResult {
pub ok: bool,
pub reasons: Vec<String>,
}
#[derive(Debug, Clone, Default)]
pub struct BuildPhysicalScope {
pub issuer_did: String,
pub subject_did: String,
pub max_force_n: Option<f64>,
pub max_speed_mps: Option<f64>,
pub max_speed_near_humans_mps: Option<f64>,
pub allowed_zones: Option<Vec<String>>,
pub shift_windows: Option<Vec<ShiftWindow>>,
pub valid_from: String,
pub valid_until: Option<String>,
}
pub fn build_physical_scope_credential(
signer_seed: &[u8],
params: &BuildPhysicalScope,
) -> Result<Value> {
let mut scope = Map::new();
if let Some(v) = params.max_force_n {
scope.insert("maxForceN".into(), json!(v));
}
if let Some(v) = params.max_speed_mps {
scope.insert("maxSpeedMps".into(), json!(v));
}
if let Some(v) = params.max_speed_near_humans_mps {
scope.insert("maxSpeedNearHumansMps".into(), json!(v));
}
if let Some(zones) = ¶ms.allowed_zones {
scope.insert("allowedZones".into(), json!(zones));
}
if let Some(ws) = ¶ms.shift_windows {
let arr: Vec<Value> = ws
.iter()
.map(|w| json!({"start": w.start, "end": w.end}))
.collect();
scope.insert("shiftWindows".into(), Value::Array(arr));
}
let mut subject = Map::new();
subject.insert("id".into(), json!(params.subject_did));
subject.insert("physicalScope".into(), Value::Object(scope));
let mut cred = Map::new();
cred.insert("@context".into(), json!([VC_CONTEXT_V2, VOUCH_CONTEXT_V1]));
cred.insert(
"type".into(),
json!(["VerifiableCredential", PHYSICAL_SCOPE_TYPE]),
);
cred.insert("issuer".into(), json!(params.issuer_did));
cred.insert("validFrom".into(), json!(params.valid_from));
if let Some(vu) = ¶ms.valid_until {
cred.insert("validUntil".into(), json!(vu));
}
cred.insert("credentialSubject".into(), Value::Object(subject));
let opts = BuildProofOptions::new(
format!("{}#key-1", params.issuer_did),
params.valid_from.clone(),
);
data_integrity::sign(&Value::Object(cred), signer_seed, &opts)
}
fn scope_num(scope: &Map<String, Value>, key: &str) -> Option<f64> {
scope.get(key).and_then(|v| v.as_f64())
}
fn window_bounds(w: &Value) -> (String, String) {
let start = w
.get("start")
.and_then(|v| v.as_str())
.filter(|s| !s.is_empty())
.unwrap_or("00:00")
.to_string();
let end = w
.get("end")
.and_then(|v| v.as_str())
.filter(|s| !s.is_empty())
.unwrap_or("23:59")
.to_string();
(start, end)
}
fn in_window(hm: &str, w: &Value) -> bool {
let (start, end) = window_bounds(w);
start.as_str() <= hm && hm <= end.as_str()
}
pub fn check_physical_action(scope: &Value, action: &PhysicalAction) -> CheckResult {
let empty = Map::new();
let scope = scope.as_object().unwrap_or(&empty);
let mut reasons = Vec::new();
if let Some(force) = action.force_n {
if let Some(cap) = scope_num(scope, "maxForceN") {
if force > cap {
reasons.push(format!("force_exceeded: {force}N > {cap}N"));
}
}
}
if let Some(speed) = action.speed_mps {
let mut cap = scope_num(scope, "maxSpeedMps");
if action.near_humans {
if let Some(nh) = scope_num(scope, "maxSpeedNearHumansMps") {
cap = Some(nh);
}
}
if let Some(c) = cap {
if speed > c {
let label = if action.near_humans {
"near_humans "
} else {
""
};
reasons.push(format!("{label}speed_exceeded: {speed} m/s > {c} m/s"));
}
}
}
if let Some(zone) = &action.zone {
if let Some(zones) = scope.get("allowedZones").and_then(|v| v.as_array()) {
if !zones.iter().any(|z| z.as_str() == Some(zone.as_str())) {
reasons.push(format!("zone_not_allowed: {zone}"));
}
}
}
if let Some(time_hm) = &action.time_hm {
if let Some(windows) = scope.get("shiftWindows").and_then(|v| v.as_array()) {
if !windows.is_empty() && !windows.iter().any(|w| in_window(time_hm, w)) {
reasons.push(format!("outside_shift_window: {time_hm}"));
}
}
}
CheckResult {
ok: reasons.is_empty(),
reasons,
}
}
pub fn attenuates(parent: &Value, child: &Value) -> bool {
let empty = Map::new();
let p = parent.as_object().unwrap_or(&empty);
let c = child.as_object().unwrap_or(&empty);
for key in ["maxForceN", "maxSpeedMps", "maxSpeedNearHumansMps"] {
if let Some(pv) = scope_num(p, key) {
match scope_num(c, key) {
Some(cv) if cv <= pv => {}
_ => return false,
}
}
}
if let Some(pz) = p.get("allowedZones").and_then(|v| v.as_array()) {
let pset: Vec<&str> = pz.iter().filter_map(|z| z.as_str()).collect();
let cz: Vec<&str> = c
.get("allowedZones")
.and_then(|v| v.as_array())
.map(|a| a.iter().filter_map(|z| z.as_str()).collect())
.unwrap_or_default();
if cz.is_empty() || !cz.iter().all(|z| pset.contains(z)) {
return false;
}
}
if let Some(pw) = p.get("shiftWindows").and_then(|v| v.as_array()) {
let empty_arr = Vec::new();
let cw = c
.get("shiftWindows")
.and_then(|v| v.as_array())
.unwrap_or(&empty_arr);
for cwin in cw {
let (cs, ce) = window_bounds(cwin);
let fits = pw.iter().any(|pwin| {
let (ps, pe) = window_bounds(pwin);
ps.as_str() <= cs.as_str() && ce.as_str() <= pe.as_str()
});
if !fits {
return false;
}
}
}
true
}
pub const HELLO: &str = "handshake_hello";
pub const ACCEPT: &str = "handshake_accept";
pub const CONFIRM: &str = "handshake_confirm";
#[derive(Debug, Clone, Default)]
pub struct TrustPolicy {
pub trusted_domains: HashSet<String>,
pub accept_unknown: bool,
}
impl TrustPolicy {
pub fn new(domains: impl IntoIterator<Item = String>, accept_unknown: bool) -> Self {
Self {
trusted_domains: domains.into_iter().collect(),
accept_unknown,
}
}
pub fn is_trusted(&self, did: &str) -> bool {
if self.accept_unknown {
return true;
}
match did_web_domain(did) {
Some(d) => self.trusted_domains.contains(d),
None => false,
}
}
}
#[derive(Debug, Clone)]
pub struct BoundedSession {
pub session_id: String,
pub initiator: String,
pub responder: String,
pub scope: Vec<String>,
pub nonce: String,
pub valid_until: Option<String>,
}
fn did_web_domain(did: &str) -> Option<&str> {
let rest = did.strip_prefix("did:web:")?;
let domain = rest.split(':').next().unwrap_or("");
if domain.is_empty() {
None
} else {
Some(domain)
}
}
#[derive(Debug, Clone, Default)]
pub struct BuildHello {
pub from_did: String,
pub proposed_scope: Vec<String>,
pub nonce: String,
pub peer_did: Option<String>,
pub issued_at: String,
}
pub fn build_hello(signer_seed: &[u8], params: &BuildHello) -> Result<Value> {
let mut hello = Map::new();
hello.insert("type".into(), json!(HELLO));
hello.insert("from".into(), json!(params.from_did));
hello.insert(
"to".into(),
match ¶ms.peer_did {
Some(p) => json!(p),
None => Value::Null,
},
);
hello.insert("nonce".into(), json!(params.nonce));
hello.insert("proposedScope".into(), json!(params.proposed_scope));
hello.insert("issuedAt".into(), json!(params.issued_at));
let opts = BuildProofOptions::new(format!("{}#key-1", params.from_did), ¶ms.issued_at);
data_integrity::sign(&Value::Object(hello), signer_seed, &opts)
}
#[derive(Debug, Clone, Default)]
pub struct BuildAccept {
pub from_did: String,
pub offered_scope: Vec<String>,
pub session_id: String,
pub valid_until: String,
pub created: String,
}
pub fn build_accept(
signer_seed: &[u8],
hello: &Value,
hello_public_key: &[u8],
policy: &TrustPolicy,
params: &BuildAccept,
) -> Result<Value> {
if hello.get("type").and_then(|v| v.as_str()) != Some(HELLO) {
return Err(CoreError::Json("not a HELLO message".into()));
}
if !data_integrity::verify_proof(hello, hello_public_key)? {
return Err(CoreError::Json("HELLO signature invalid".into()));
}
let initiator = hello.get("from").and_then(|v| v.as_str()).unwrap_or("");
if !policy.is_trusted(initiator) {
return Err(CoreError::Json(format!(
"peer {initiator} is not in this trust domain's policy"
)));
}
let offered: HashSet<&str> = params.offered_scope.iter().map(|s| s.as_str()).collect();
let empty = Vec::new();
let proposed = hello
.get("proposedScope")
.and_then(|v| v.as_array())
.unwrap_or(&empty);
let mut seen: HashSet<String> = HashSet::new();
let mut bounded: Vec<String> = Vec::new();
for item in proposed {
if let Some(s) = item.as_str() {
if offered.contains(s) && seen.insert(s.to_string()) {
bounded.push(s.to_string());
}
}
}
bounded.sort();
let mut accept = Map::new();
accept.insert("type".into(), json!(ACCEPT));
accept.insert("from".into(), json!(params.from_did));
accept.insert("to".into(), json!(initiator));
accept.insert("sessionId".into(), json!(params.session_id));
accept.insert(
"nonce".into(),
hello.get("nonce").cloned().unwrap_or(Value::Null),
);
accept.insert("boundedScope".into(), json!(bounded));
accept.insert("validUntil".into(), json!(params.valid_until));
let opts = BuildProofOptions::new(format!("{}#key-1", params.from_did), ¶ms.created);
data_integrity::sign(&Value::Object(accept), signer_seed, &opts)
}
pub fn verify_accept(
accept: &Value,
accept_public_key: &[u8],
expected_nonce: &str,
policy: Option<&TrustPolicy>,
) -> Result<Option<BoundedSession>> {
if accept.get("type").and_then(|v| v.as_str()) != Some(ACCEPT) {
return Ok(None);
}
if !data_integrity::verify_proof(accept, accept_public_key)? {
return Ok(None);
}
if accept.get("nonce").and_then(|v| v.as_str()) != Some(expected_nonce) {
return Ok(None);
}
let responder = accept.get("from").and_then(|v| v.as_str()).unwrap_or("");
if let Some(p) = policy {
if !p.is_trusted(responder) {
return Ok(None);
}
}
let scope = accept
.get("boundedScope")
.and_then(|v| v.as_array())
.map(|a| {
a.iter()
.filter_map(|s| s.as_str().map(String::from))
.collect()
})
.unwrap_or_default();
Ok(Some(BoundedSession {
session_id: accept
.get("sessionId")
.and_then(|v| v.as_str())
.unwrap_or("")
.to_string(),
initiator: accept
.get("to")
.and_then(|v| v.as_str())
.unwrap_or("")
.to_string(),
responder: responder.to_string(),
scope,
nonce: expected_nonce.to_string(),
valid_until: accept
.get("validUntil")
.and_then(|v| v.as_str())
.map(String::from),
}))
}
pub fn build_confirm(
signer_seed: &[u8],
from_did: &str,
session: &BoundedSession,
created: &str,
) -> Result<Value> {
let mut confirm = Map::new();
confirm.insert("type".into(), json!(CONFIRM));
confirm.insert("from".into(), json!(from_did));
confirm.insert("to".into(), json!(session.responder));
confirm.insert("sessionId".into(), json!(session.session_id));
confirm.insert("nonce".into(), json!(session.nonce));
confirm.insert("acceptedScope".into(), json!(session.scope));
let opts = BuildProofOptions::new(format!("{from_did}#key-1"), created);
data_integrity::sign(&Value::Object(confirm), signer_seed, &opts)
}
pub fn verify_confirm(
confirm: &Value,
confirm_public_key: &[u8],
session_id: &str,
expected_nonce: &str,
) -> Result<bool> {
if confirm.get("type").and_then(|v| v.as_str()) != Some(CONFIRM) {
return Ok(false);
}
if !data_integrity::verify_proof(confirm, confirm_public_key)? {
return Ok(false);
}
let sid = confirm
.get("sessionId")
.and_then(|v| v.as_str())
.unwrap_or("");
let nonce = confirm.get("nonce").and_then(|v| v.as_str()).unwrap_or("");
Ok(sid == session_id && nonce == expected_nonce)
}
pub const BLACKBOX_VERSION: &str = "1.0";
pub const KILLSWITCH_TYPE: &str = "KillSwitchCredential";
pub const EMERGENCY_STOP: &str = "emergency_stop";
pub fn genesis_prev_hash() -> String {
mb64(&[0u8; 32])
}
fn entry_hash(body: &Map<String, Value>) -> Result<String> {
let mut clean = body.clone();
clean.remove("entryHash");
let canonical = jcs::canonicalize(&Value::Object(clean));
Ok(mb64(&Sha256::digest(canonical)))
}
pub fn blackbox_append_entry(
key: &[u8],
seq: u64,
event: &str,
payload: &Value,
timestamp: &str,
prev_hash: &str,
) -> Result<Value> {
if key.len() != 32 {
return Err(CoreError::Crypto(
"black box key must be 32 bytes (AES-256)".into(),
));
}
let mut nonce = [0u8; 12];
getrandom::getrandom(&mut nonce).map_err(|e| CoreError::Crypto(format!("rng: {e}")))?;
let plaintext = jcs::canonicalize(payload);
let cipher =
Aes256Gcm::new_from_slice(key).map_err(|e| CoreError::Crypto(format!("aes key: {e}")))?;
let ct = cipher
.encrypt(Nonce::from_slice(&nonce), plaintext.as_ref())
.map_err(|e| CoreError::Crypto(format!("encrypt: {e}")))?;
let mut blob = Vec::with_capacity(nonce.len() + ct.len());
blob.extend_from_slice(&nonce);
blob.extend_from_slice(&ct);
let mut body = Map::new();
body.insert("version".into(), json!(BLACKBOX_VERSION));
body.insert("seq".into(), json!(seq));
body.insert("timestamp".into(), json!(timestamp));
body.insert("event".into(), json!(event));
body.insert("ciphertext".into(), json!(mb64(&blob)));
body.insert("prevHash".into(), json!(prev_hash));
let h = entry_hash(&body)?;
body.insert("entryHash".into(), json!(h));
Ok(Value::Object(body))
}
pub struct BlackBoxLog {
key: Vec<u8>,
genesis: String,
entries: Vec<Value>,
head: String,
}
impl BlackBoxLog {
pub fn new(key: &[u8], genesis_prev_hash_override: Option<&str>) -> Result<Self> {
if key.len() != 32 {
return Err(CoreError::Crypto(
"black box key must be 32 bytes (AES-256)".into(),
));
}
let genesis = genesis_prev_hash_override
.map(String::from)
.unwrap_or_else(genesis_prev_hash);
Ok(Self {
key: key.to_vec(),
head: genesis.clone(),
genesis,
entries: Vec::new(),
})
}
pub fn append(&mut self, event: &str, payload: &Value, timestamp: &str) -> Result<Value> {
let entry = blackbox_append_entry(
&self.key,
self.entries.len() as u64,
event,
payload,
timestamp,
&self.head,
)?;
self.head = entry
.get("entryHash")
.and_then(|v| v.as_str())
.unwrap_or("")
.to_string();
self.entries.push(entry.clone());
Ok(entry)
}
pub fn head(&self) -> &str {
&self.head
}
pub fn genesis(&self) -> &str {
&self.genesis
}
pub fn entries(&self) -> &[Value] {
&self.entries
}
pub fn open(&self, entry: &Value) -> Result<Value> {
open_entry(entry, &self.key)
}
}
pub fn open_entry(entry: &Value, key: &[u8]) -> Result<Value> {
let ct_mb = entry
.get("ciphertext")
.and_then(|v| v.as_str())
.ok_or_else(|| CoreError::Json("entry has no ciphertext".into()))?;
let blob = unmb64(ct_mb)?;
if blob.len() < 12 + 16 {
return Err(CoreError::Crypto("ciphertext too short".into()));
}
let cipher =
Aes256Gcm::new_from_slice(key).map_err(|e| CoreError::Crypto(format!("aes key: {e}")))?;
let (nonce, rest) = blob.split_at(12);
let pt = cipher
.decrypt(Nonce::from_slice(nonce), rest)
.map_err(|_| CoreError::Crypto("decryption failed".into()))?;
serde_json::from_slice(&pt).map_err(|e| CoreError::Json(format!("payload decode: {e}")))
}
#[derive(Debug, Clone)]
pub struct ChainResult {
pub ok: bool,
pub reason: Option<String>,
}
pub fn verify_blackbox_chain(
entries: &[Value],
genesis_prev_hash_override: Option<&str>,
) -> ChainResult {
let mut prev = genesis_prev_hash_override
.map(String::from)
.unwrap_or_else(genesis_prev_hash);
for (i, entry) in entries.iter().enumerate() {
if entry.get("seq").and_then(|v| v.as_u64()) != Some(i as u64) {
return ChainResult {
ok: false,
reason: Some(format!("entry {i} seq mismatch")),
};
}
if entry.get("prevHash").and_then(|v| v.as_str()).unwrap_or("") != prev {
return ChainResult {
ok: false,
reason: Some(format!("entry {i} prevHash does not link")),
};
}
let obj = match entry.as_object() {
Some(o) => o,
None => {
return ChainResult {
ok: false,
reason: Some(format!("entry {i} is not an object")),
}
}
};
let want = match entry_hash(obj) {
Ok(w) => w,
Err(_) => {
return ChainResult {
ok: false,
reason: Some(format!("entry {i} hash error")),
}
}
};
let eh = entry
.get("entryHash")
.and_then(|v| v.as_str())
.unwrap_or("");
if eh != want {
return ChainResult {
ok: false,
reason: Some(format!("entry {i} entryHash mismatch (tampered)")),
};
}
prev = eh.to_string();
}
ChainResult {
ok: true,
reason: None,
}
}
#[derive(Debug, Clone, Default)]
pub struct BuildKillswitch {
pub issuer_did: String,
pub target: String,
pub reason: String,
pub command: Option<String>,
pub scope: Option<Vec<String>>,
pub valid_from: String,
pub valid_until: Option<String>,
}
pub fn build_killswitch_credential(
authority_seed: &[u8],
params: &BuildKillswitch,
) -> Result<Value> {
let command = params
.command
.clone()
.unwrap_or_else(|| EMERGENCY_STOP.to_string());
let mut subject = Map::new();
subject.insert("id".into(), json!(params.target));
subject.insert("command".into(), json!(command));
subject.insert("reason".into(), json!(params.reason));
subject.insert("issuedBy".into(), json!(params.issuer_did));
if let Some(scope) = ¶ms.scope {
subject.insert("scope".into(), json!(scope));
}
let mut cred = Map::new();
cred.insert("@context".into(), json!([VC_CONTEXT_V2, VOUCH_CONTEXT_V1]));
cred.insert(
"type".into(),
json!(["VerifiableCredential", KILLSWITCH_TYPE]),
);
cred.insert("issuer".into(), json!(params.issuer_did));
cred.insert("validFrom".into(), json!(params.valid_from));
if let Some(vu) = ¶ms.valid_until {
cred.insert("validUntil".into(), json!(vu));
}
cred.insert("credentialSubject".into(), Value::Object(subject));
let opts = BuildProofOptions::new(format!("{}#key-1", params.issuer_did), ¶ms.valid_from);
data_integrity::sign(&Value::Object(cred), authority_seed, &opts)
}
pub fn verify_killswitch_credential(
credential: &Value,
public_key: &[u8],
trusted_authorities: Option<&HashSet<String>>,
) -> Result<Option<Value>> {
if !has_type(credential.get("type"), KILLSWITCH_TYPE) {
return Ok(None);
}
if !data_integrity::verify_proof(credential, public_key)? {
return Ok(None);
}
if let Some(trusted) = trusted_authorities {
let issuer = credential
.get("issuer")
.and_then(|v| v.as_str())
.unwrap_or("");
if !trusted.contains(issuer) {
return Ok(None);
}
}
Ok(credential.get("credentialSubject").cloned())
}
pub const ROBOT_PASSPORT_TYPE: &str = "RobotPassport";
pub const PASSPORT_URI_SCHEME: &str = "vouch-passport:";
pub const STATUS_ACTIVE: &str = "active";
pub const STATUS_SUSPENDED: &str = "suspended";
pub const STATUS_DECOMMISSIONED: &str = "decommissioned";
#[derive(Debug, Clone, Default)]
pub struct BuildPassport {
pub issuer_did: String,
pub robot_did: String,
pub make: String,
pub model: String,
pub owner: String,
pub authorized_actions: Vec<String>,
pub certification: Option<String>,
pub status: Option<String>,
pub valid_from: String,
pub valid_until: Option<String>,
}
pub fn build_passport(signer_seed: &[u8], params: &BuildPassport) -> Result<Value> {
let status = params
.status
.clone()
.unwrap_or_else(|| STATUS_ACTIVE.to_string());
let mut subject = Map::new();
subject.insert("id".into(), json!(params.robot_did));
subject.insert("make".into(), json!(params.make));
subject.insert("model".into(), json!(params.model));
subject.insert("owner".into(), json!(params.owner));
subject.insert("authorizedActions".into(), json!(params.authorized_actions));
subject.insert("status".into(), json!(status));
if let Some(c) = ¶ms.certification {
subject.insert("certification".into(), json!(c));
}
let mut cred = Map::new();
cred.insert("@context".into(), json!([VC_CONTEXT_V2, VOUCH_CONTEXT_V1]));
cred.insert(
"type".into(),
json!(["VerifiableCredential", ROBOT_PASSPORT_TYPE]),
);
cred.insert("issuer".into(), json!(params.issuer_did));
cred.insert("validFrom".into(), json!(params.valid_from));
if let Some(vu) = ¶ms.valid_until {
cred.insert("validUntil".into(), json!(vu));
}
cred.insert("credentialSubject".into(), Value::Object(subject));
let opts = BuildProofOptions::new(format!("{}#key-1", params.issuer_did), ¶ms.valid_from);
data_integrity::sign(&Value::Object(cred), signer_seed, &opts)
}
pub fn encode_passport(passport: &Value) -> String {
format!(
"{}{}",
PASSPORT_URI_SCHEME,
mb64(&jcs::canonicalize(passport))
)
}
pub fn decode_passport(uri: &str) -> Result<Value> {
let body = uri
.strip_prefix(PASSPORT_URI_SCHEME)
.ok_or_else(|| CoreError::Json(format!("not a {PASSPORT_URI_SCHEME} URI")))?;
let raw = unmb64(body)?;
serde_json::from_slice(&raw).map_err(|e| CoreError::Json(format!("passport decode: {e}")))
}
pub fn verify_passport(
passport: &Value,
public_key: &[u8],
now_iso: &str,
) -> Result<Option<Value>> {
if !has_type(passport.get("type"), ROBOT_PASSPORT_TYPE) {
return Ok(None);
}
if !data_integrity::verify_proof(passport, public_key)? {
return Ok(None);
}
if let Some(vu) = passport.get("validUntil").and_then(|v| v.as_str()) {
if let (Ok(vu_e), Ok(now_e)) = (
crate::time::iso_to_epoch_seconds(vu),
crate::time::iso_to_epoch_seconds(now_iso),
) {
if now_e > vu_e {
return Ok(None);
}
}
}
Ok(passport.get("credentialSubject").cloned())
}
pub fn verify_passport_uri(uri: &str, public_key: &[u8], now_iso: &str) -> Result<Option<Value>> {
match decode_passport(uri) {
Ok(p) => verify_passport(&p, public_key, now_iso),
Err(_) => Ok(None),
}
}
pub const ROBOT_HEARTBEAT_TYPE: &str = "RobotHeartbeatCredential";
pub const DEFAULT_GRACE_INTERVALS: i64 = 2;
#[derive(Debug, Clone, Default)]
pub struct MotionSample {
pub force_n: Option<f64>,
pub speed_mps: Option<f64>,
pub near_humans: bool,
pub zone: Option<String>,
pub time_hm: Option<String>,
}
#[derive(Debug, Clone, Default)]
pub struct MotionCollector {
scope: Option<Value>,
samples: u64,
max_force: f64,
max_speed: f64,
max_speed_near: f64,
zone_breaches: u64,
breaches: u64,
}
impl MotionCollector {
pub fn new(scope: Option<Value>) -> Self {
Self {
scope,
..Default::default()
}
}
pub fn record(&mut self, sample: &MotionSample) -> Result<()> {
if let Some(f) = sample.force_n {
if f < 0.0 {
return Err(CoreError::Json(format!(
"force_n must be non-negative, got {f}"
)));
}
}
if let Some(s) = sample.speed_mps {
if s < 0.0 {
return Err(CoreError::Json(format!(
"speed_mps must be non-negative, got {s}"
)));
}
}
self.samples += 1;
if let Some(f) = sample.force_n {
self.max_force = self.max_force.max(f);
}
if let Some(s) = sample.speed_mps {
self.max_speed = self.max_speed.max(s);
if sample.near_humans {
self.max_speed_near = self.max_speed_near.max(s);
}
}
if let Some(scope) = &self.scope {
let action = PhysicalAction {
force_n: sample.force_n,
speed_mps: sample.speed_mps,
near_humans: sample.near_humans,
zone: sample.zone.clone(),
time_hm: sample.time_hm.clone(),
};
let result = check_physical_action(scope, &action);
if !result.ok {
self.breaches += 1;
if result
.reasons
.iter()
.any(|r| r.starts_with("zone_not_allowed"))
{
self.zone_breaches += 1;
}
}
}
Ok(())
}
pub fn digest(&self) -> Value {
json!({
"samples": self.samples,
"maxForceN": self.max_force,
"maxSpeedMps": self.max_speed,
"maxSpeedNearHumansMps": self.max_speed_near,
"zoneBreaches": self.zone_breaches,
"breachCount": self.breaches,
"withinEnvelope": self.breaches == 0,
})
}
pub fn reset(&mut self) {
self.samples = 0;
self.max_force = 0.0;
self.max_speed = 0.0;
self.max_speed_near = 0.0;
self.zone_breaches = 0;
self.breaches = 0;
}
}
fn digest_non_negative_int(digest: &Map<String, Value>, name: &str) -> Result<()> {
match digest.get(name) {
Some(v) if v.is_u64() => Ok(()),
Some(Value::Number(n)) if n.as_i64().map(|i| i >= 0).unwrap_or(false) => Ok(()),
Some(_) => Err(CoreError::Json(format!(
"motionDigest.{name} must be a non-negative integer"
))),
None => Err(CoreError::Json(format!("motionDigest.{name} is required"))),
}
}
pub fn validate_motion_digest(digest: &Value) -> Result<()> {
let obj = digest
.as_object()
.ok_or_else(|| CoreError::Json("motionDigest must be an object".into()))?;
for name in ["samples", "zoneBreaches", "breachCount"] {
digest_non_negative_int(obj, name)?;
}
for name in ["maxForceN", "maxSpeedMps", "maxSpeedNearHumansMps"] {
match obj.get(name) {
Some(Value::Bool(_)) | None => {
return Err(CoreError::Json(format!("motionDigest.{name} is required")))
}
Some(v) => {
let n = v.as_f64().ok_or_else(|| {
CoreError::Json(format!("motionDigest.{name} must be a number"))
})?;
if n < 0.0 {
return Err(CoreError::Json(format!(
"motionDigest.{name} must be non-negative"
)));
}
}
}
}
match obj.get("withinEnvelope") {
Some(Value::Bool(_)) => Ok(()),
Some(_) => Err(CoreError::Json(
"motionDigest.withinEnvelope must be a boolean".into(),
)),
None => Err(CoreError::Json(
"motionDigest.withinEnvelope is required".into(),
)),
}
}
#[derive(Debug, Clone)]
pub struct BuildRobotHeartbeat {
pub robot_did: String,
pub session_id: String,
pub interval_index: i64,
pub interval_seconds: i64,
pub motion_digest: Value,
pub valid_from: String,
}
pub fn build_robot_heartbeat(robot_seed: &[u8], params: &BuildRobotHeartbeat) -> Result<Value> {
if params.interval_index < 0 {
return Err(CoreError::Json(
"interval_index must be non-negative".into(),
));
}
if params.interval_seconds <= 0 {
return Err(CoreError::Json("interval_seconds must be positive".into()));
}
validate_motion_digest(¶ms.motion_digest)?;
let mut subject = Map::new();
subject.insert("id".into(), json!(params.robot_did));
subject.insert("sessionId".into(), json!(params.session_id));
subject.insert("intervalIndex".into(), json!(params.interval_index));
subject.insert("intervalSeconds".into(), json!(params.interval_seconds));
subject.insert("motionDigest".into(), params.motion_digest.clone());
let mut cred = Map::new();
cred.insert("@context".into(), json!([VC_CONTEXT_V2, VOUCH_CONTEXT_V1]));
cred.insert(
"type".into(),
json!(["VerifiableCredential", ROBOT_HEARTBEAT_TYPE]),
);
cred.insert("issuer".into(), json!(params.robot_did));
cred.insert("validFrom".into(), json!(params.valid_from));
cred.insert("credentialSubject".into(), Value::Object(subject));
let opts = BuildProofOptions::new(format!("{}#key-1", params.robot_did), ¶ms.valid_from);
data_integrity::sign(&Value::Object(cred), robot_seed, &opts)
}
pub fn verify_robot_heartbeat(
credential: &Value,
robot_public_key: &[u8],
) -> Result<Option<Value>> {
if !has_type(credential.get("type"), ROBOT_HEARTBEAT_TYPE) {
return Ok(None);
}
if !data_integrity::verify_proof(credential, robot_public_key)? {
return Ok(None);
}
let subject = match credential.get("credentialSubject") {
Some(s) if s.is_object() => s,
_ => return Ok(None),
};
let digest = subject.get("motionDigest").cloned().unwrap_or(Value::Null);
if validate_motion_digest(&digest).is_err() {
return Ok(None);
}
Ok(Some(subject.clone()))
}
pub fn is_live(
credential: &Value,
now_iso: &str,
interval_seconds: Option<i64>,
grace_intervals: i64,
) -> Result<bool> {
let subject = credential
.get("credentialSubject")
.and_then(|s| s.as_object());
let digest = subject
.and_then(|s| s.get("motionDigest"))
.and_then(|d| d.as_object());
let within = digest
.and_then(|d| d.get("withinEnvelope"))
.and_then(|v| v.as_bool())
.unwrap_or(false);
if !within {
return Ok(false);
}
let cadence = match interval_seconds {
Some(c) => c,
None => subject
.and_then(|s| s.get("intervalSeconds"))
.and_then(|v| v.as_i64())
.unwrap_or(0),
};
if cadence <= 0 {
return Ok(false);
}
if grace_intervals < 1 {
return Err(CoreError::Json("grace_intervals must be at least 1".into()));
}
let raw = match credential.get("validFrom").and_then(|v| v.as_str()) {
Some(s) if !s.is_empty() => s,
_ => return Ok(false),
};
let issued = match crate::time::iso_to_epoch_seconds(raw) {
Ok(t) => t,
Err(_) => return Ok(false),
};
let moment = match crate::time::iso_to_epoch_seconds(now_iso) {
Ok(t) => t,
Err(_) => return Ok(false),
};
let deadline = issued + cadence * grace_intervals;
if moment + cadence < issued {
return Ok(false);
}
Ok(moment <= deadline)
}
pub use crate::status_list::{BITSTRING_STATUS_LIST_ENTRY_TYPE, STATUS_PURPOSE_REVOCATION};
pub fn build_status_list_entry(
status_list_credential: &str,
status_list_index: i64,
status_purpose: &str,
entry_id: Option<&str>,
) -> Result<Value> {
if !matches!(status_purpose, "revocation" | "suspension" | "message") {
return Err(CoreError::Json(format!(
"status_purpose must be one of revocation, suspension, message, got {status_purpose:?}"
)));
}
if status_list_index < 0 {
return Err(CoreError::Json(
"status_list_index must be non-negative".into(),
));
}
if status_list_credential.is_empty() {
return Err(CoreError::Json(
"status_list_credential URL is required".into(),
));
}
let id = entry_id
.map(String::from)
.unwrap_or_else(|| format!("{status_list_credential}#{status_list_index}"));
Ok(json!({
"id": id,
"type": BITSTRING_STATUS_LIST_ENTRY_TYPE,
"statusPurpose": status_purpose,
"statusListIndex": status_list_index.to_string(),
"statusListCredential": status_list_credential,
}))
}
#[derive(Debug, Clone)]
pub struct AttachCredentialStatus {
pub status_list_credential: String,
pub status_list_index: i64,
pub status_purpose: String,
pub entry_id: Option<String>,
pub created: String,
}
pub fn attach_credential_status(
credential: &Value,
signer_seed: &[u8],
params: &AttachCredentialStatus,
) -> Result<Value> {
let entry = build_status_list_entry(
¶ms.status_list_credential,
params.status_list_index,
¶ms.status_purpose,
params.entry_id.as_deref(),
)?;
let mut obj = credential
.as_object()
.ok_or_else(|| CoreError::Json("credential must be a JSON object".into()))?
.clone();
match obj.get("credentialStatus").cloned() {
None => {
obj.insert("credentialStatus".into(), entry);
}
Some(Value::Array(mut existing)) => {
existing.push(entry);
obj.insert("credentialStatus".into(), Value::Array(existing));
}
Some(existing) => {
obj.insert("credentialStatus".into(), json!([existing, entry]));
}
}
obj.remove("proof");
let issuer = obj.get("issuer").and_then(|v| v.as_str()).unwrap_or("");
let opts = BuildProofOptions::new(format!("{issuer}#key-1"), ¶ms.created);
data_integrity::sign(&Value::Object(obj), signer_seed, &opts)
}
fn status_entries(credential: &Value) -> Result<Vec<Value>> {
match credential.get("credentialStatus") {
None => Ok(Vec::new()),
Some(Value::Array(a)) => Ok(a.iter().filter(|e| e.is_object()).cloned().collect()),
Some(v) if v.is_object() => Ok(vec![v.clone()]),
Some(_) => Err(CoreError::Json(
"credentialStatus must be an object or a list of objects".into(),
)),
}
}
pub fn check_credential_status(
credential: &Value,
status_list_credential: &Value,
status_purpose: &str,
) -> Result<bool> {
let referenced_id = status_list_credential.get("id").and_then(|v| v.as_str());
for entry in status_entries(credential)? {
if entry.get("statusPurpose").and_then(|v| v.as_str()) != Some(status_purpose) {
continue;
}
if entry.get("statusListCredential").and_then(|v| v.as_str()) != referenced_id {
continue;
}
return crate::status_list::verify_status(&entry, status_list_credential);
}
Ok(false)
}
pub const SAFETY_RECORD_TYPE: &str = "RobotSafetyRecordCredential";
pub const SAFETY_LOG_VERSION: &str = "1.0";
pub const EVENT_TYPES: [&str; 6] = [
"incident",
"near_miss",
"manual_override",
"kill_switch",
"envelope_breach",
"maintenance",
];
pub const SEVERITIES: [&str; 5] = ["info", "low", "medium", "high", "critical"];
pub struct SafetyEventLog {
genesis: String,
entries: Vec<Value>,
head: String,
}
impl SafetyEventLog {
pub fn new(genesis_prev_hash_override: Option<&str>) -> Self {
let genesis = genesis_prev_hash_override
.map(String::from)
.unwrap_or_else(genesis_prev_hash);
Self {
head: genesis.clone(),
genesis,
entries: Vec::new(),
}
}
pub fn append(
&mut self,
event_type: &str,
severity: &str,
details: Option<&Value>,
actor: Option<&str>,
timestamp: &str,
) -> Result<Value> {
if !EVENT_TYPES.contains(&event_type) {
return Err(CoreError::Json(format!(
"event_type must be one of {EVENT_TYPES:?}, got {event_type:?}"
)));
}
if !SEVERITIES.contains(&severity) {
return Err(CoreError::Json(format!(
"severity must be one of {SEVERITIES:?}, got {severity:?}"
)));
}
let mut body = Map::new();
body.insert("version".into(), json!(SAFETY_LOG_VERSION));
body.insert("seq".into(), json!(self.entries.len() as u64));
body.insert("timestamp".into(), json!(timestamp));
body.insert("eventType".into(), json!(event_type));
body.insert("severity".into(), json!(severity));
body.insert("prevHash".into(), json!(self.head));
if let Some(d) = details {
body.insert("details".into(), d.clone());
}
if let Some(a) = actor {
body.insert("actor".into(), json!(a));
}
let h = entry_hash(&body)?;
body.insert("entryHash".into(), json!(h));
self.head = h;
let entry = Value::Object(body);
self.entries.push(entry.clone());
Ok(entry)
}
pub fn head(&self) -> &str {
&self.head
}
pub fn genesis(&self) -> &str {
&self.genesis
}
pub fn entries(&self) -> &[Value] {
&self.entries
}
pub fn summarize(&self) -> Value {
summarize_entries(&self.entries, Some(&self.head))
}
}
pub fn verify_safety_log(
entries: &[Value],
genesis_prev_hash_override: Option<&str>,
) -> ChainResult {
verify_blackbox_chain(entries, genesis_prev_hash_override)
}
pub fn summarize_entries(entries: &[Value], head: Option<&str>) -> Value {
let mut sorted_types: Vec<&str> = EVENT_TYPES.to_vec();
sorted_types.sort_unstable();
let mut event_counts = Map::new();
for t in &sorted_types {
event_counts.insert((*t).to_string(), json!(0));
}
let mut severity_counts = Map::new();
for s in SEVERITIES {
severity_counts.insert(s.to_string(), json!(0));
}
for e in entries {
if let Some(et) = e.get("eventType").and_then(|v| v.as_str()) {
if let Some(c) = event_counts.get_mut(et).and_then(|v| v.as_u64()) {
event_counts.insert(et.to_string(), json!(c + 1));
}
}
if let Some(sv) = e.get("severity").and_then(|v| v.as_str()) {
if let Some(c) = severity_counts.get_mut(sv).and_then(|v| v.as_u64()) {
severity_counts.insert(sv.to_string(), json!(c + 1));
}
}
}
let mut summary = Map::new();
summary.insert("eventCounts".into(), Value::Object(event_counts));
summary.insert("severityCounts".into(), Value::Object(severity_counts));
summary.insert("totalEvents".into(), json!(entries.len() as u64));
if let Some(h) = head {
summary.insert("logHead".into(), json!(h));
}
Value::Object(summary)
}
pub fn validate_safety_summary(summary: &Value) -> Result<()> {
let obj = summary
.as_object()
.ok_or_else(|| CoreError::Json("summary must be an object".into()))?;
for name in ["eventCounts", "severityCounts"] {
let block = obj
.get(name)
.and_then(|v| v.as_object())
.ok_or_else(|| CoreError::Json(format!("summary.{name} must be an object")))?;
for (k, v) in block {
let ok = v.is_u64() || v.as_i64().map(|i| i >= 0).unwrap_or(false);
if v.is_boolean() || !ok {
return Err(CoreError::Json(format!(
"summary.{name}[{k:?}] must be a non-negative integer"
)));
}
}
}
let total_ok = obj
.get("totalEvents")
.map(|v| (v.is_u64() || v.as_i64().map(|i| i >= 0).unwrap_or(false)) && !v.is_boolean())
.unwrap_or(false);
if !total_ok {
return Err(CoreError::Json(
"summary.totalEvents must be a non-negative integer".into(),
));
}
Ok(())
}
#[derive(Debug, Clone)]
pub struct BuildSafetyRecord {
pub issuer_did: String,
pub robot_did: String,
pub summary: Value,
pub period_start: Option<String>,
pub period_end: Option<String>,
pub valid_from: String,
pub valid_until: Option<String>,
}
pub fn build_safety_record(signer_seed: &[u8], params: &BuildSafetyRecord) -> Result<Value> {
validate_safety_summary(¶ms.summary)?;
let mut subject = Map::new();
subject.insert("id".into(), json!(params.robot_did));
if let Some(s) = params.summary.as_object() {
for (k, v) in s {
subject.insert(k.clone(), v.clone());
}
}
if params.period_start.is_some() || params.period_end.is_some() {
let mut period = Map::new();
if let Some(start) = ¶ms.period_start {
period.insert("start".into(), json!(start));
}
if let Some(end) = ¶ms.period_end {
period.insert("end".into(), json!(end));
}
subject.insert("period".into(), Value::Object(period));
}
let mut cred = Map::new();
cred.insert("@context".into(), json!([VC_CONTEXT_V2, VOUCH_CONTEXT_V1]));
cred.insert(
"type".into(),
json!(["VerifiableCredential", SAFETY_RECORD_TYPE]),
);
cred.insert("issuer".into(), json!(params.issuer_did));
cred.insert("validFrom".into(), json!(params.valid_from));
if let Some(vu) = ¶ms.valid_until {
cred.insert("validUntil".into(), json!(vu));
}
cred.insert("credentialSubject".into(), Value::Object(subject));
let opts = BuildProofOptions::new(format!("{}#key-1", params.issuer_did), ¶ms.valid_from);
data_integrity::sign(&Value::Object(cred), signer_seed, &opts)
}
pub fn verify_safety_record(credential: &Value, public_key: &[u8]) -> Result<Option<Value>> {
if !has_type(credential.get("type"), SAFETY_RECORD_TYPE) {
return Ok(None);
}
if !data_integrity::verify_proof(credential, public_key)? {
return Ok(None);
}
let subject = match credential.get("credentialSubject") {
Some(s) if s.is_object() => s,
_ => return Ok(None),
};
if validate_safety_summary(subject).is_err() {
return Ok(None);
}
Ok(Some(subject.clone()))
}
pub const PERCEPTION_TYPE: &str = "PerceptionProvenanceCredential";
pub const PERCEPTION_LOG_VERSION: &str = "1.0";
pub const MODALITIES: [&str; 6] = ["camera", "lidar", "radar", "depth", "audio", "thermal"];
pub fn hash_frame(frame: &[u8]) -> String {
mb64(&Sha256::digest(frame))
}
pub struct PerceptionLog {
genesis: String,
entries: Vec<Value>,
head: String,
}
impl PerceptionLog {
pub fn new(genesis_prev_hash_override: Option<&str>) -> Self {
let genesis = genesis_prev_hash_override
.map(String::from)
.unwrap_or_else(genesis_prev_hash);
Self {
head: genesis.clone(),
genesis,
entries: Vec::new(),
}
}
pub fn record(
&mut self,
sensor_id: &str,
modality: &str,
frame: Option<&[u8]>,
frame_hash: Option<&str>,
timestamp: &str,
) -> Result<Value> {
if !MODALITIES.contains(&modality) {
return Err(CoreError::Json(format!(
"modality must be one of {MODALITIES:?}, got {modality:?}"
)));
}
if sensor_id.is_empty() {
return Err(CoreError::Json("sensor_id is required".into()));
}
if frame.is_some() && frame_hash.is_some() {
return Err(CoreError::Json(
"provide either frame or frame_hash, not both".into(),
));
}
let resolved = match (frame, frame_hash) {
(Some(f), None) => hash_frame(f),
(None, Some(h)) if !h.is_empty() => h.to_string(),
_ => return Err(CoreError::Json("frame or frame_hash is required".into())),
};
let mut body = Map::new();
body.insert("version".into(), json!(PERCEPTION_LOG_VERSION));
body.insert("seq".into(), json!(self.entries.len() as u64));
body.insert("timestamp".into(), json!(timestamp));
body.insert("sensorId".into(), json!(sensor_id));
body.insert("modality".into(), json!(modality));
body.insert("frameHash".into(), json!(resolved));
body.insert("prevHash".into(), json!(self.head));
let h = entry_hash(&body)?;
body.insert("entryHash".into(), json!(h));
self.head = h;
let entry = Value::Object(body);
self.entries.push(entry.clone());
Ok(entry)
}
pub fn head(&self) -> &str {
&self.head
}
pub fn genesis(&self) -> &str {
&self.genesis
}
pub fn entries(&self) -> &[Value] {
&self.entries
}
}
pub fn verify_perception_log(
entries: &[Value],
genesis_prev_hash_override: Option<&str>,
) -> ChainResult {
verify_blackbox_chain(entries, genesis_prev_hash_override)
}
#[derive(Debug, Clone)]
pub struct BuildPerception {
pub robot_did: String,
pub sensor_id: String,
pub modality: String,
pub frame_hash: String,
pub captured_at: Option<String>,
pub log_head: Option<String>,
pub valid_from: String,
pub valid_until: Option<String>,
}
pub fn build_perception_attestation(robot_seed: &[u8], params: &BuildPerception) -> Result<Value> {
if !MODALITIES.contains(¶ms.modality.as_str()) {
return Err(CoreError::Json(format!(
"modality must be one of {MODALITIES:?}, got {:?}",
params.modality
)));
}
if params.frame_hash.is_empty() {
return Err(CoreError::Json("frame_hash is required".into()));
}
let captured = params
.captured_at
.clone()
.unwrap_or_else(|| params.valid_from.clone());
let mut subject = Map::new();
subject.insert("id".into(), json!(params.robot_did));
subject.insert("sensorId".into(), json!(params.sensor_id));
subject.insert("modality".into(), json!(params.modality));
subject.insert("frameHash".into(), json!(params.frame_hash));
subject.insert("capturedAt".into(), json!(captured));
if let Some(head) = ¶ms.log_head {
subject.insert("logHead".into(), json!(head));
}
let mut cred = Map::new();
cred.insert("@context".into(), json!([VC_CONTEXT_V2, VOUCH_CONTEXT_V1]));
cred.insert(
"type".into(),
json!(["VerifiableCredential", PERCEPTION_TYPE]),
);
cred.insert("issuer".into(), json!(params.robot_did));
cred.insert("validFrom".into(), json!(params.valid_from));
if let Some(vu) = ¶ms.valid_until {
cred.insert("validUntil".into(), json!(vu));
}
cred.insert("credentialSubject".into(), Value::Object(subject));
let opts = BuildProofOptions::new(format!("{}#key-1", params.robot_did), ¶ms.valid_from);
data_integrity::sign(&Value::Object(cred), robot_seed, &opts)
}
pub fn verify_perception_attestation(
credential: &Value,
public_key: &[u8],
frame: Option<&[u8]>,
) -> Result<Option<Value>> {
if !has_type(credential.get("type"), PERCEPTION_TYPE) {
return Ok(None);
}
if !data_integrity::verify_proof(credential, public_key)? {
return Ok(None);
}
let subject = match credential
.get("credentialSubject")
.and_then(|s| s.as_object())
{
Some(s) => s,
None => return Ok(None),
};
let frame_hash = subject.get("frameHash").and_then(|v| v.as_str());
let modality = subject.get("modality").and_then(|v| v.as_str());
match (frame_hash, modality) {
(Some(fh), Some(m)) if !fh.is_empty() && MODALITIES.contains(&m) => {}
_ => return Ok(None),
}
if let Some(f) = frame {
if hash_frame(f) != frame_hash.unwrap_or("") {
return Ok(None);
}
}
Ok(Some(Value::Object(subject.clone())))
}
pub const DELEGATION_LEASE_TYPE: &str = "DelegationLeaseCredential";
#[derive(Debug, Clone)]
pub struct BuildDelegationLease {
pub issuer_did: String,
pub robot_did: String,
pub lease_id: String,
pub scope: Value,
pub parent_lease_id: Option<String>,
pub valid_from: String,
pub valid_until: String,
}
pub fn build_delegation_lease(signer_seed: &[u8], params: &BuildDelegationLease) -> Result<Value> {
if params.lease_id.is_empty() {
return Err(CoreError::Json("lease_id is required".into()));
}
if !params.scope.is_object() {
return Err(CoreError::Json(
"scope must be a physicalScope object".into(),
));
}
let mut subject = Map::new();
subject.insert("id".into(), json!(params.robot_did));
subject.insert("leaseId".into(), json!(params.lease_id));
subject.insert("physicalScope".into(), params.scope.clone());
if let Some(parent) = ¶ms.parent_lease_id {
subject.insert("parentLeaseId".into(), json!(parent));
}
let mut cred = Map::new();
cred.insert("@context".into(), json!([VC_CONTEXT_V2, VOUCH_CONTEXT_V1]));
cred.insert(
"type".into(),
json!(["VerifiableCredential", DELEGATION_LEASE_TYPE]),
);
cred.insert("issuer".into(), json!(params.issuer_did));
cred.insert("validFrom".into(), json!(params.valid_from));
cred.insert("validUntil".into(), json!(params.valid_until));
cred.insert("credentialSubject".into(), Value::Object(subject));
let opts = BuildProofOptions::new(format!("{}#key-1", params.issuer_did), ¶ms.valid_from);
data_integrity::sign(&Value::Object(cred), signer_seed, &opts)
}
fn lease_window_current(credential: &Value, now_iso: Option<&str>) -> bool {
let now = match now_iso {
Some(n) => n,
None => return true,
};
let moment = match crate::time::iso_to_epoch_seconds(now) {
Ok(t) => t,
Err(_) => return false,
};
if let Some(vf) = credential.get("validFrom").and_then(|v| v.as_str()) {
match crate::time::iso_to_epoch_seconds(vf) {
Ok(t) if moment < t => return false,
Err(_) => return false,
_ => {}
}
}
if let Some(vu) = credential.get("validUntil").and_then(|v| v.as_str()) {
match crate::time::iso_to_epoch_seconds(vu) {
Ok(t) if moment > t => return false,
Err(_) => return false,
_ => {}
}
}
true
}
pub fn verify_delegation_lease(
credential: &Value,
public_key: &[u8],
now_iso: Option<&str>,
parent_scope: Option<&Value>,
) -> Result<Option<Value>> {
if !has_type(credential.get("type"), DELEGATION_LEASE_TYPE) {
return Ok(None);
}
if !data_integrity::verify_proof(credential, public_key)? {
return Ok(None);
}
if !lease_window_current(credential, now_iso) {
return Ok(None);
}
let subject = match credential
.get("credentialSubject")
.and_then(|s| s.as_object())
{
Some(s) => s,
None => return Ok(None),
};
let scope = match subject.get("physicalScope") {
Some(s) if s.is_object() => s,
_ => return Ok(None),
};
if let Some(parent) = parent_scope {
if !attenuates(parent, scope) {
return Ok(None);
}
}
Ok(Some(Value::Object(subject.clone())))
}
pub fn lease_permits(
subject: &Value,
action: &PhysicalAction,
credential: Option<&Value>,
now_iso: Option<&str>,
) -> bool {
if let Some(cred) = credential {
if !lease_window_current(cred, now_iso) {
return false;
}
}
let empty = json!({});
let scope = subject.get("physicalScope").unwrap_or(&empty);
check_physical_action(scope, action).ok
}
pub const ACTION_APPROVAL_TYPE: &str = "PhysicalActionApprovalCredential";
pub const APPROVE: &str = "approve";
pub const REJECT: &str = "reject";
#[derive(Debug, Clone)]
pub struct BuildActionApproval {
pub approver_did: String,
pub action_id: String,
pub robot_did: String,
pub decision: String,
pub valid_from: String,
pub valid_until: Option<String>,
}
pub fn build_action_approval(approver_seed: &[u8], params: &BuildActionApproval) -> Result<Value> {
if params.decision != APPROVE && params.decision != REJECT {
return Err(CoreError::Json(format!(
"decision must be {APPROVE:?} or {REJECT:?}, got {:?}",
params.decision
)));
}
if params.action_id.is_empty() {
return Err(CoreError::Json("action_id is required".into()));
}
let mut subject = Map::new();
subject.insert("id".into(), json!(params.approver_did));
subject.insert("actionId".into(), json!(params.action_id));
subject.insert("robotDid".into(), json!(params.robot_did));
subject.insert("decision".into(), json!(params.decision));
let mut cred = Map::new();
cred.insert("@context".into(), json!([VC_CONTEXT_V2, VOUCH_CONTEXT_V1]));
cred.insert(
"type".into(),
json!(["VerifiableCredential", ACTION_APPROVAL_TYPE]),
);
cred.insert("issuer".into(), json!(params.approver_did));
cred.insert("validFrom".into(), json!(params.valid_from));
if let Some(vu) = ¶ms.valid_until {
cred.insert("validUntil".into(), json!(vu));
}
cred.insert("credentialSubject".into(), Value::Object(subject));
let opts = BuildProofOptions::new(format!("{}#key-1", params.approver_did), ¶ms.valid_from);
data_integrity::sign(&Value::Object(cred), approver_seed, &opts)
}
#[derive(Debug, Clone)]
pub struct ApproverKey {
pub did: String,
pub public_key: Vec<u8>,
}
pub fn verify_action_authorization(
approvals: &[Value],
action_id: &str,
robot_did: &str,
approver_keys: &[ApproverKey],
threshold: i64,
approver_set: Option<&HashSet<String>>,
now_iso: Option<&str>,
) -> Result<(bool, Vec<String>)> {
if threshold < 1 {
return Err(CoreError::Json("threshold must be at least 1".into()));
}
let mut approvers: HashSet<String> = HashSet::new();
for approval in approvals {
if !has_type(approval.get("type"), ACTION_APPROVAL_TYPE) {
continue;
}
let subject = match approval
.get("credentialSubject")
.and_then(|s| s.as_object())
{
Some(s) => s,
None => continue,
};
let issuer = approval
.get("issuer")
.and_then(|v| v.as_str())
.unwrap_or("");
if subject.get("actionId").and_then(|v| v.as_str()) != Some(action_id)
|| subject.get("robotDid").and_then(|v| v.as_str()) != Some(robot_did)
{
continue;
}
if subject.get("decision").and_then(|v| v.as_str()) != Some(APPROVE) {
continue;
}
if let Some(set) = approver_set {
if !set.contains(issuer) {
continue;
}
}
let key = match approver_keys.iter().find(|k| k.did == issuer) {
Some(k) => k,
None => continue,
};
if !lease_window_current(approval, now_iso) {
continue;
}
if !data_integrity::verify_proof(approval, &key.public_key)? {
continue;
}
approvers.insert(issuer.to_string());
}
let mut sorted: Vec<String> = approvers.into_iter().collect();
sorted.sort();
Ok((sorted.len() as i64 >= threshold, sorted))
}
pub const OWNERSHIP_TRANSFER_TYPE: &str = "RobotOwnershipTransferCredential";
pub const KEY_ROTATION_TYPE: &str = "RobotKeyRotationCredential";
pub const DECOMMISSION_TYPE: &str = "RobotDecommissionCredential";
fn verify_typed(
credential: &Value,
public_key: &[u8],
expected_type: &str,
) -> Result<Option<Value>> {
if !has_type(credential.get("type"), expected_type) {
return Ok(None);
}
if !data_integrity::verify_proof(credential, public_key)? {
return Ok(None);
}
match credential
.get("credentialSubject")
.and_then(|s| s.as_object())
{
Some(s) => Ok(Some(Value::Object(s.clone()))),
None => Ok(None),
}
}
#[derive(Debug, Clone)]
pub struct BuildOwnershipTransfer {
pub issuer_did: String,
pub robot_did: String,
pub to_owner: String,
pub from_owner: Option<String>,
pub prev_transfer_id: Option<String>,
pub valid_from: String,
}
pub fn build_ownership_transfer(
current_owner_seed: &[u8],
params: &BuildOwnershipTransfer,
) -> Result<Value> {
if params.robot_did.is_empty() || params.to_owner.is_empty() {
return Err(CoreError::Json(
"robot_did and to_owner are required".into(),
));
}
let seller = params
.from_owner
.clone()
.unwrap_or_else(|| params.issuer_did.clone());
let mut subject = Map::new();
subject.insert("id".into(), json!(params.robot_did));
subject.insert("fromOwner".into(), json!(seller));
subject.insert("toOwner".into(), json!(params.to_owner));
if let Some(prev) = ¶ms.prev_transfer_id {
subject.insert("prevTransferId".into(), json!(prev));
}
let mut cred = Map::new();
cred.insert("@context".into(), json!([VC_CONTEXT_V2, VOUCH_CONTEXT_V1]));
cred.insert(
"type".into(),
json!(["VerifiableCredential", OWNERSHIP_TRANSFER_TYPE]),
);
cred.insert("issuer".into(), json!(params.issuer_did));
cred.insert("validFrom".into(), json!(params.valid_from));
cred.insert("credentialSubject".into(), Value::Object(subject));
let opts = BuildProofOptions::new(format!("{}#key-1", params.issuer_did), ¶ms.valid_from);
data_integrity::sign(&Value::Object(cred), current_owner_seed, &opts)
}
pub fn verify_ownership_transfer(
credential: &Value,
current_owner_public_key: &[u8],
) -> Result<Option<Value>> {
let subject = match verify_typed(
credential,
current_owner_public_key,
OWNERSHIP_TRANSFER_TYPE,
)? {
Some(s) => s,
None => return Ok(None),
};
let to_owner = subject.get("toOwner").and_then(|v| v.as_str());
let from_owner = subject.get("fromOwner").and_then(|v| v.as_str());
match (to_owner, from_owner) {
(Some(t), Some(f)) if !t.is_empty() && !f.is_empty() => {
if credential.get("issuer").and_then(|v| v.as_str()) != Some(f) {
return Ok(None);
}
}
_ => return Ok(None),
}
Ok(Some(subject))
}
#[derive(Debug, Clone)]
pub struct OwnerKey {
pub did: String,
pub public_key: Vec<u8>,
}
pub fn verify_custody_chain(
transfers: &[Value],
public_keys: &[OwnerKey],
origin_owner: Option<&str>,
) -> Result<(bool, Option<String>)> {
let mut expected_from: Option<String> = origin_owner.map(String::from);
let mut current_owner: Option<String> = origin_owner.map(String::from);
for transfer in transfers {
let issuer = transfer
.get("issuer")
.and_then(|v| v.as_str())
.unwrap_or("");
let key = match public_keys.iter().find(|k| k.did == issuer) {
Some(k) => k,
None => return Ok((false, None)),
};
let subject = match verify_ownership_transfer(transfer, &key.public_key)? {
Some(s) => s,
None => return Ok((false, None)),
};
if let Some(expected) = &expected_from {
if subject.get("fromOwner").and_then(|v| v.as_str()) != Some(expected.as_str()) {
return Ok((false, None));
}
}
current_owner = subject
.get("toOwner")
.and_then(|v| v.as_str())
.map(String::from);
expected_from = current_owner.clone();
}
Ok((true, current_owner))
}
#[derive(Debug, Clone)]
pub struct BuildKeyRotation {
pub robot_did: String,
pub new_key_multibase: String,
pub reason: Option<String>,
pub valid_from: String,
}
pub fn build_key_rotation(old_key_seed: &[u8], params: &BuildKeyRotation) -> Result<Value> {
if params.new_key_multibase.is_empty() {
return Err(CoreError::Json("new_key_multibase is required".into()));
}
let old_kp = keys::Ed25519KeyPair::from_seed_slice(old_key_seed)?;
let mut subject = Map::new();
subject.insert("id".into(), json!(params.robot_did));
subject.insert("previousKey".into(), json!(old_kp.public_multikey()));
subject.insert("newKey".into(), json!(params.new_key_multibase));
if let Some(reason) = ¶ms.reason {
subject.insert("reason".into(), json!(reason));
}
let mut cred = Map::new();
cred.insert("@context".into(), json!([VC_CONTEXT_V2, VOUCH_CONTEXT_V1]));
cred.insert(
"type".into(),
json!(["VerifiableCredential", KEY_ROTATION_TYPE]),
);
cred.insert("issuer".into(), json!(params.robot_did));
cred.insert("validFrom".into(), json!(params.valid_from));
cred.insert("credentialSubject".into(), Value::Object(subject));
let opts = BuildProofOptions::new(format!("{}#key-1", params.robot_did), ¶ms.valid_from);
data_integrity::sign(&Value::Object(cred), old_key_seed, &opts)
}
pub fn verify_key_rotation(credential: &Value, old_public_key: &[u8]) -> Result<Option<Value>> {
let subject = match verify_typed(credential, old_public_key, KEY_ROTATION_TYPE)? {
Some(s) => s,
None => return Ok(None),
};
let previous = subject.get("previousKey").and_then(|v| v.as_str());
let new_key = subject.get("newKey").and_then(|v| v.as_str());
match (previous, new_key) {
(Some(p), Some(n)) if !p.is_empty() && !n.is_empty() => {}
_ => return Ok(None),
}
Ok(Some(subject))
}
#[derive(Debug, Clone)]
pub struct KeyEntry {
pub multibase: String,
pub public_key: Vec<u8>,
}
pub fn verify_key_history(
rotations: &[Value],
origin_key_multibase: &str,
public_keys: &[KeyEntry],
) -> Result<(bool, Option<String>)> {
let mut current_key = origin_key_multibase.to_string();
for rotation in rotations {
let previous = rotation
.get("credentialSubject")
.and_then(|s| s.get("previousKey"))
.and_then(|v| v.as_str())
.unwrap_or("");
if previous != current_key {
return Ok((false, None));
}
let key = match public_keys.iter().find(|k| k.multibase == current_key) {
Some(k) => k,
None => return Ok((false, None)),
};
let subject = match verify_key_rotation(rotation, &key.public_key)? {
Some(s) => s,
None => return Ok((false, None)),
};
current_key = subject
.get("newKey")
.and_then(|v| v.as_str())
.unwrap_or("")
.to_string();
}
Ok((true, Some(current_key)))
}
#[derive(Debug, Clone)]
pub struct BuildDecommission {
pub issuer_did: String,
pub robot_did: String,
pub reason: String,
pub final_disposition: Option<String>,
pub valid_from: String,
pub valid_until: Option<String>,
}
pub fn build_decommission(signer_seed: &[u8], params: &BuildDecommission) -> Result<Value> {
if params.reason.is_empty() {
return Err(CoreError::Json("reason is required".into()));
}
let mut subject = Map::new();
subject.insert("id".into(), json!(params.robot_did));
subject.insert("reason".into(), json!(params.reason));
subject.insert("decommissionedBy".into(), json!(params.issuer_did));
if let Some(disposition) = ¶ms.final_disposition {
subject.insert("finalDisposition".into(), json!(disposition));
}
let mut cred = Map::new();
cred.insert("@context".into(), json!([VC_CONTEXT_V2, VOUCH_CONTEXT_V1]));
cred.insert(
"type".into(),
json!(["VerifiableCredential", DECOMMISSION_TYPE]),
);
cred.insert("issuer".into(), json!(params.issuer_did));
cred.insert("validFrom".into(), json!(params.valid_from));
if let Some(vu) = ¶ms.valid_until {
cred.insert("validUntil".into(), json!(vu));
}
cred.insert("credentialSubject".into(), Value::Object(subject));
let opts = BuildProofOptions::new(format!("{}#key-1", params.issuer_did), ¶ms.valid_from);
data_integrity::sign(&Value::Object(cred), signer_seed, &opts)
}
pub fn verify_decommission(
credential: &Value,
public_key: &[u8],
trusted_authorities: Option<&HashSet<String>>,
) -> Result<Option<Value>> {
let subject = match verify_typed(credential, public_key, DECOMMISSION_TYPE)? {
Some(s) => s,
None => return Ok(None),
};
if let Some(trusted) = trusted_authorities {
let issuer = credential
.get("issuer")
.and_then(|v| v.as_str())
.unwrap_or("");
if !trusted.contains(issuer) {
return Ok(None);
}
}
Ok(Some(subject))
}
pub const CONFORMANCE_ATTESTATION_TYPE: &str = "RobotConformanceAttestation";
struct ProfileRequirement {
id: &'static str,
clause: &'static str,
title: &'static str,
credential: &'static str,
fields: &'static [&'static str],
}
struct ConformanceProfile {
regime: &'static str,
version: &'static str,
requirements: &'static [ProfileRequirement],
}
const ISO_10218_REQUIREMENTS: &[ProfileRequirement] = &[
ProfileRequirement {
id: "iso10218-identification",
clause: "ISO 10218-1:2011, 5.2",
title: "Robot identification bound to its hardware",
credential: "RobotIdentityCredential",
fields: &["hardwareRoot.kind"],
},
ProfileRequirement {
id: "iso10218-software-integrity",
clause: "ISO 10218-1:2011, 5.3",
title: "Control software and configuration integrity",
credential: "ModelProvenanceAttestation",
fields: &["vla.weightsHash"],
},
ProfileRequirement {
id: "iso10218-limits",
clause: "ISO 10218-1:2011, 5.6",
title: "Limiting of speed, force, and workspace",
credential: "PhysicalCapabilityScope",
fields: &["physicalScope.maxForceN", "physicalScope.maxSpeedMps"],
},
ProfileRequirement {
id: "iso10218-records",
clause: "ISO 10218-2:2011, 5.2",
title: "Records of safety-relevant events",
credential: "RobotSafetyRecordCredential",
fields: &["totalEvents"],
},
];
const ISO_TS_15066_REQUIREMENTS: &[ProfileRequirement] = &[
ProfileRequirement {
id: "iso15066-power-force-limiting",
clause: "ISO/TS 15066:2016, 5.5.4",
title: "Power and force limiting near humans",
credential: "PhysicalCapabilityScope",
fields: &[
"physicalScope.maxSpeedNearHumansMps",
"physicalScope.maxForceN",
],
},
ProfileRequirement {
id: "iso15066-collaborative-workspace",
clause: "ISO/TS 15066:2016, 5.5.2",
title: "Defined collaborative workspace",
credential: "PhysicalCapabilityScope",
fields: &["physicalScope.allowedZones"],
},
ProfileRequirement {
id: "iso15066-monitoring",
clause: "ISO/TS 15066:2016, 5.2",
title: "Continuous monitoring of the collaborative operation",
credential: "RobotHeartbeatCredential",
fields: &["motionDigest"],
},
];
const EU_MACHINERY_REQUIREMENTS: &[ProfileRequirement] = &[
ProfileRequirement {
id: "eu-mr-identification",
clause: "Reg (EU) 2023/1230, Annex III 1.7.4",
title: "Machinery identification and traceability",
credential: "RobotIdentityCredential",
fields: &["make", "model", "serial"],
},
ProfileRequirement {
id: "eu-mr-software-integrity",
clause: "Reg (EU) 2023/1230, Annex III 1.1.9",
title: "Protection against corruption of safety software",
credential: "ModelProvenanceAttestation",
fields: &["vla.weightsHash", "vla.safetyPolicy"],
},
ProfileRequirement {
id: "eu-mr-safe-limits",
clause: "Reg (EU) 2023/1230, Annex III 1.2.1",
title: "Safety and reliability of control systems and limits",
credential: "PhysicalCapabilityScope",
fields: &["physicalScope.maxForceN"],
},
ProfileRequirement {
id: "eu-mr-records",
clause: "Reg (EU) 2023/1230, Annex III 1.2.1",
title: "Recording of safety-relevant data",
credential: "RobotSafetyRecordCredential",
fields: &["totalEvents"],
},
];
const EU_AI_ACT_REQUIREMENTS: &[ProfileRequirement] = &[
ProfileRequirement {
id: "eu-aia-record-keeping",
clause: "Reg (EU) 2024/1689, Art. 12",
title: "Automatic recording of events (logging)",
credential: "RobotSafetyRecordCredential",
fields: &["logHead"],
},
ProfileRequirement {
id: "eu-aia-transparency",
clause: "Reg (EU) 2024/1689, Art. 13",
title: "Model and configuration transparency",
credential: "ModelProvenanceAttestation",
fields: &["vla.modelName", "vla.configHash"],
},
ProfileRequirement {
id: "eu-aia-human-oversight",
clause: "Reg (EU) 2024/1689, Art. 14",
title: "Human oversight through enforced operating limits",
credential: "PhysicalCapabilityScope",
fields: &["physicalScope.maxSpeedNearHumansMps"],
},
ProfileRequirement {
id: "eu-aia-accuracy-robustness",
clause: "Reg (EU) 2024/1689, Art. 15",
title: "Accuracy and robustness traceable to a known build",
credential: "ModelProvenanceAttestation",
fields: &["vla.weightsHash"],
},
];
const UL_3300_REQUIREMENTS: &[ProfileRequirement] = &[
ProfileRequirement {
id: "ul3300-identity",
clause: "UL 3300, identification",
title: "Robot identity bound to its hardware",
credential: "RobotIdentityCredential",
fields: &["hardwareRoot.kind"],
},
ProfileRequirement {
id: "ul3300-operating-limits",
clause: "UL 3300, operating limits",
title: "Enforced speed and zone limits",
credential: "PhysicalCapabilityScope",
fields: &["physicalScope.maxSpeedMps", "physicalScope.allowedZones"],
},
ProfileRequirement {
id: "ul3300-perception-integrity",
clause: "UL 3300, sensing integrity",
title: "Integrity of perception used for safe operation",
credential: "PerceptionProvenanceCredential",
fields: &["frameHash"],
},
ProfileRequirement {
id: "ul3300-records",
clause: "UL 3300, incident records",
title: "Records of safety-relevant incidents",
credential: "RobotSafetyRecordCredential",
fields: &["totalEvents"],
},
];
fn conformance_profile(profile_id: &str) -> Option<ConformanceProfile> {
match profile_id {
"iso-10218" => Some(ConformanceProfile {
regime: "ISO 10218-1/-2 industrial robots",
version: "2011",
requirements: ISO_10218_REQUIREMENTS,
}),
"iso-ts-15066" => Some(ConformanceProfile {
regime: "ISO/TS 15066 collaborative robots",
version: "2016",
requirements: ISO_TS_15066_REQUIREMENTS,
}),
"eu-machinery-2023-1230" => Some(ConformanceProfile {
regime: "EU Machinery Regulation 2023/1230",
version: "2023",
requirements: EU_MACHINERY_REQUIREMENTS,
}),
"eu-ai-act-high-risk" => Some(ConformanceProfile {
regime: "EU AI Act high-risk systems",
version: "2024",
requirements: EU_AI_ACT_REQUIREMENTS,
}),
"ul-3300" => Some(ConformanceProfile {
regime: "UL 3300 service, communication, and mobile robots",
version: "2022",
requirements: UL_3300_REQUIREMENTS,
}),
_ => None,
}
}
fn credential_types(credential: &Value) -> Vec<&str> {
match credential.get("type") {
Some(Value::String(s)) => vec![s.as_str()],
Some(Value::Array(items)) => items.iter().filter_map(|v| v.as_str()).collect(),
_ => Vec::new(),
}
}
fn path_value<'a>(subject: &'a Value, path: &str) -> Option<&'a Value> {
let mut node = subject;
for part in path.split('.') {
match node {
Value::Object(map) => match map.get(part) {
Some(v) => node = v,
None => return None,
},
_ => return None,
}
}
Some(node)
}
fn value_present(value: &Value) -> bool {
match value {
Value::Null => false,
Value::Array(a) => !a.is_empty(),
Value::Object(o) => !o.is_empty(),
_ => true,
}
}
fn credential_satisfies(credential: &Value, requirement: &ProfileRequirement) -> bool {
if !credential_types(credential).contains(&requirement.credential) {
return false;
}
let empty = Value::Object(Map::new());
let subject = credential.get("credentialSubject").unwrap_or(&empty);
for path in requirement.fields {
match path_value(subject, path) {
Some(v) if value_present(v) => {}
_ => return false,
}
}
true
}
pub fn check_conformance(credentials: &[Value], profile_id: &str) -> Result<Value> {
let prof = conformance_profile(profile_id)
.ok_or_else(|| CoreError::Json(format!("unknown conformance profile: {profile_id}")))?;
let mut results: Vec<Value> = Vec::with_capacity(prof.requirements.len());
let mut satisfied = 0i64;
for requirement in prof.requirements {
let ok = credentials
.iter()
.any(|c| credential_satisfies(c, requirement));
if ok {
satisfied += 1;
}
results.push(json!({
"id": requirement.id,
"clause": requirement.clause,
"title": requirement.title,
"satisfied": ok,
}));
}
let total = prof.requirements.len() as i64;
let mut report = Map::new();
report.insert("profileId".into(), json!(profile_id));
report.insert("regime".into(), json!(prof.regime));
report.insert("version".into(), json!(prof.version));
report.insert("conforms".into(), json!(satisfied == total));
report.insert("satisfiedCount".into(), json!(satisfied));
report.insert("totalCount".into(), json!(total));
report.insert("requirements".into(), Value::Array(results));
Ok(Value::Object(report))
}
pub fn report_digest(report: &Value) -> String {
mb64(&Sha256::digest(jcs::canonicalize(report)))
}
#[derive(Debug, Clone)]
pub struct BuildConformanceAttestation {
pub issuer_did: String,
pub robot_did: String,
pub report: Value,
pub valid_from: String,
pub valid_until: Option<String>,
}
pub fn build_conformance_attestation(
signer_seed: &[u8],
params: &BuildConformanceAttestation,
) -> Result<Value> {
if params.robot_did.is_empty() {
return Err(CoreError::Json("robot_did is required".into()));
}
let report = params
.report
.as_object()
.ok_or_else(|| CoreError::Json("report must come from check_conformance".into()))?;
if !report.contains_key("profileId") || !report.contains_key("conforms") {
return Err(CoreError::Json(
"report must come from check_conformance".into(),
));
}
let mut subject = Map::new();
subject.insert("id".into(), json!(params.robot_did));
subject.insert(
"profileId".into(),
report.get("profileId").cloned().unwrap_or(Value::Null),
);
subject.insert(
"regime".into(),
report.get("regime").cloned().unwrap_or(Value::Null),
);
subject.insert(
"conforms".into(),
report.get("conforms").cloned().unwrap_or(Value::Null),
);
subject.insert(
"satisfiedCount".into(),
report.get("satisfiedCount").cloned().unwrap_or(Value::Null),
);
subject.insert(
"totalCount".into(),
report.get("totalCount").cloned().unwrap_or(Value::Null),
);
subject.insert("reportDigest".into(), json!(report_digest(¶ms.report)));
subject.insert("report".into(), params.report.clone());
let mut cred = Map::new();
cred.insert("@context".into(), json!([VC_CONTEXT_V2, VOUCH_CONTEXT_V1]));
cred.insert(
"type".into(),
json!(["VerifiableCredential", CONFORMANCE_ATTESTATION_TYPE]),
);
cred.insert("issuer".into(), json!(params.issuer_did));
cred.insert("validFrom".into(), json!(params.valid_from));
if let Some(vu) = ¶ms.valid_until {
cred.insert("validUntil".into(), json!(vu));
}
cred.insert("credentialSubject".into(), Value::Object(subject));
let opts = BuildProofOptions::new(format!("{}#key-1", params.issuer_did), ¶ms.valid_from);
data_integrity::sign(&Value::Object(cred), signer_seed, &opts)
}
pub fn verify_conformance_attestation(
credential: &Value,
public_key: &[u8],
) -> Result<Option<Value>> {
if !has_type(credential.get("type"), CONFORMANCE_ATTESTATION_TYPE) {
return Ok(None);
}
if !data_integrity::verify_proof(credential, public_key)? {
return Ok(None);
}
let subject = match credential
.get("credentialSubject")
.and_then(|s| s.as_object())
{
Some(s) => s,
None => return Ok(None),
};
let embedded = match subject.get("report") {
Some(r) if r.is_object() => r,
_ => return Ok(None),
};
if subject.get("reportDigest").and_then(|v| v.as_str())
!= Some(report_digest(embedded).as_str())
{
return Ok(None);
}
if subject.get("conforms") != embedded.get("conforms") {
return Ok(None);
}
Ok(Some(Value::Object(subject.clone())))
}
pub const CLASSICAL_CRYPTOSUITE: &str = data_integrity::CRYPTOSUITE_ID;
pub const HYBRID_CRYPTOSUITE: &str = hybrid::HYBRID_COMPOSITE_CRYPTOSUITE_ID;
pub fn coerce_mldsa44_public(mldsa44_public: &[u8]) -> Result<Vec<u8>> {
if mldsa44_public.len() != MLDSA44_PUBLIC_LEN {
return Err(CoreError::InvalidKeyLength {
expected: MLDSA44_PUBLIC_LEN,
got: mldsa44_public.len(),
});
}
Ok(mldsa44_public.to_vec())
}
pub fn mldsa44_public_from_multikey(multikey_str: &str) -> Result<Vec<u8>> {
let decoded = multikey::decode(multikey_str)?;
if decoded.algorithm != "ML-DSA-44" {
return Err(CoreError::InvalidMultikey(format!(
"expected an ML-DSA-44 multikey, got {}",
decoded.algorithm
)));
}
coerce_mldsa44_public(&decoded.raw_key)
}
fn pq_verification_method(credential: &Value) -> String {
let issuer = credential
.as_object()
.and_then(|o| o.get("issuer"))
.and_then(|v| v.as_str())
.unwrap_or("");
format!("{issuer}#key-1")
}
pub fn sign_pq(
credential: &Value,
ed25519_seed: &[u8],
mldsa: &MlDsa44KeyPair,
created: &str,
) -> Result<Value> {
let vm = pq_verification_method(credential);
hybrid::sign_composite(credential, ed25519_seed, mldsa, &vm, created)
}
pub fn is_pq(credential: &Value) -> bool {
credential
.as_object()
.and_then(|o| o.get("proof"))
.and_then(|p| p.as_object())
.and_then(|p| p.get("cryptosuite"))
.and_then(|v| v.as_str())
== Some(HYBRID_CRYPTOSUITE)
}
pub fn verify_pq(
credential: &Value,
ed25519_public_key: &[u8],
mldsa44_public_key: &[u8],
) -> Result<bool> {
let resolved_ml = coerce_mldsa44_public(mldsa44_public_key)?;
hybrid::verify_composite(credential, ed25519_public_key, &resolved_ml)
}
pub fn verify_robot_credential(
credential: &Value,
ed25519_public_key: &[u8],
mldsa44_public_key: Option<&[u8]>,
) -> Result<bool> {
if is_pq(credential) {
let ml = match mldsa44_public_key {
Some(k) => k,
None => return Ok(false),
};
let resolved_ml = resolve_mldsa44_public(ml)?;
return hybrid::verify_composite(credential, ed25519_public_key, &resolved_ml);
}
data_integrity::verify_proof(credential, ed25519_public_key)
}
pub fn resolve_mldsa44_public(mldsa44_public_key: &[u8]) -> Result<Vec<u8>> {
if mldsa44_public_key.len() == MLDSA44_PUBLIC_LEN {
return Ok(mldsa44_public_key.to_vec());
}
let s = std::str::from_utf8(mldsa44_public_key).map_err(|_| {
CoreError::InvalidMultikey("ML-DSA-44 key must be raw bytes or a Multikey string".into())
})?;
mldsa44_public_from_multikey(s)
}
pub fn migrate_to_pq(
credential: &Value,
ed25519_seed: &[u8],
mldsa: &MlDsa44KeyPair,
created: &str,
) -> Result<Value> {
sign_pq(credential, ed25519_seed, mldsa, created)
}
pub const EMBODIMENT_TYPE: &str = "AgentEmbodimentCredential";
#[derive(Debug, Clone)]
pub struct BuildEmbodiment {
pub agent_did: String,
pub body_did: String,
pub body_hardware_root: String,
pub from_body: Option<String>,
pub embodied_at: String,
pub valid_until: Option<String>,
}
pub fn build_embodiment(agent_seed: &[u8], params: &BuildEmbodiment) -> Result<Value> {
if params.agent_did.is_empty()
|| params.body_did.is_empty()
|| params.body_hardware_root.is_empty()
{
return Err(CoreError::Json(
"agent_did, body_did, and body_hardware_root are required".into(),
));
}
let mut subject = Map::new();
subject.insert("id".into(), json!(params.agent_did));
subject.insert("body".into(), json!(params.body_did));
subject.insert("bodyHardwareRoot".into(), json!(params.body_hardware_root));
if let Some(from_body) = ¶ms.from_body {
subject.insert("fromBody".into(), json!(from_body));
}
let mut cred = Map::new();
cred.insert("@context".into(), json!([VC_CONTEXT_V2, VOUCH_CONTEXT_V1]));
cred.insert(
"type".into(),
json!(["VerifiableCredential", EMBODIMENT_TYPE]),
);
cred.insert("issuer".into(), json!(params.agent_did));
cred.insert("validFrom".into(), json!(params.embodied_at));
if let Some(vu) = ¶ms.valid_until {
cred.insert("validUntil".into(), json!(vu));
}
cred.insert("credentialSubject".into(), Value::Object(subject));
let opts = BuildProofOptions::new(format!("{}#key-1", params.agent_did), ¶ms.embodied_at);
data_integrity::sign(&Value::Object(cred), agent_seed, &opts)
}
pub fn verify_embodiment(credential: &Value, agent_public_key: &[u8]) -> Result<Option<Value>> {
let subject = match verify_typed(credential, agent_public_key, EMBODIMENT_TYPE)? {
Some(s) => s,
None => return Ok(None),
};
let has_body = subject
.get("body")
.and_then(|v| v.as_str())
.map(|s| !s.is_empty())
.unwrap_or(false);
let has_root = subject
.get("bodyHardwareRoot")
.and_then(|v| v.as_str())
.map(|s| !s.is_empty())
.unwrap_or(false);
if !has_body || !has_root {
return Ok(None);
}
let subject_id = subject.get("id").and_then(|v| v.as_str());
if credential.get("issuer").and_then(|v| v.as_str()) != subject_id {
return Ok(None);
}
Ok(Some(subject))
}
pub fn verify_continuity_chain(
embodiments: &[Value],
agent_public_key: &[u8],
origin_body: Option<&str>,
) -> Result<(bool, Option<String>)> {
let mut expected_from: Option<String> = origin_body.map(String::from);
let mut current_body: Option<String> = origin_body.map(String::from);
for embodiment in embodiments {
let subject = match verify_embodiment(embodiment, agent_public_key)? {
Some(s) => s,
None => return Ok((false, None)),
};
if let Some(expected) = &expected_from {
if subject.get("fromBody").and_then(|v| v.as_str()) != Some(expected.as_str()) {
return Ok((false, None));
}
}
current_body = subject
.get("body")
.and_then(|v| v.as_str())
.map(String::from);
expected_from = current_body.clone();
}
Ok((true, current_body))
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ForkConflict {
pub body_a: String,
pub body_b: String,
}
fn windows_overlap(start_a: i64, end_a: Option<i64>, start_b: i64, end_b: Option<i64>) -> bool {
let a_before_b = end_a.map(|e| e <= start_b).unwrap_or(false);
let b_before_a = end_b.map(|e| e <= start_a).unwrap_or(false);
!(a_before_b || b_before_a)
}
pub fn check_no_fork(embodiments: &[Value]) -> Result<(bool, Option<ForkConflict>)> {
let mut windows: Vec<(String, i64, Option<i64>)> = Vec::with_capacity(embodiments.len());
for embodiment in embodiments {
let subject = embodiment
.get("credentialSubject")
.and_then(|s| s.as_object());
let body = subject
.and_then(|s| s.get("body"))
.and_then(|v| v.as_str())
.filter(|s| !s.is_empty());
let start = embodiment
.get("validFrom")
.and_then(|v| v.as_str())
.and_then(|s| crate::time::iso_to_epoch_seconds(s).ok());
let (body, start) = match (body, start) {
(Some(b), Some(s)) => (b.to_string(), s),
_ => return Ok((false, None)),
};
let end = embodiment
.get("validUntil")
.and_then(|v| v.as_str())
.and_then(|s| crate::time::iso_to_epoch_seconds(s).ok());
windows.push((body, start, end));
}
for i in 0..windows.len() {
let (body_i, start_i, end_i) = &windows[i];
for window_j in windows.iter().skip(i + 1) {
let (body_j, start_j, end_j) = window_j;
if body_i == body_j {
continue;
}
if windows_overlap(*start_i, *end_i, *start_j, *end_j) {
return Ok((
false,
Some(ForkConflict {
body_a: body_i.clone(),
body_b: body_j.clone(),
}),
));
}
}
}
Ok((true, None))
}
pub const CUSTODY_HANDOFF_TYPE: &str = "CustodyHandoffCredential";
#[derive(Debug, Clone)]
pub struct BuildHandoff {
pub task_id: String,
pub from_actor: String,
pub to_actor: String,
pub condition: Option<String>,
pub handoff_at: String,
pub valid_until: Option<String>,
}
pub fn build_handoff(receiver_seed: &[u8], params: &BuildHandoff) -> Result<Value> {
if params.task_id.is_empty() || params.from_actor.is_empty() || params.to_actor.is_empty() {
return Err(CoreError::Json(
"task_id, from_actor, and to_actor are required".into(),
));
}
let mut subject = Map::new();
subject.insert("id".into(), json!(params.task_id));
subject.insert("fromActor".into(), json!(params.from_actor));
subject.insert("toActor".into(), json!(params.to_actor));
if let Some(condition) = ¶ms.condition {
subject.insert("condition".into(), json!(condition));
}
let mut cred = Map::new();
cred.insert("@context".into(), json!([VC_CONTEXT_V2, VOUCH_CONTEXT_V1]));
cred.insert(
"type".into(),
json!(["VerifiableCredential", CUSTODY_HANDOFF_TYPE]),
);
cred.insert("issuer".into(), json!(params.to_actor));
cred.insert("validFrom".into(), json!(params.handoff_at));
if let Some(vu) = ¶ms.valid_until {
cred.insert("validUntil".into(), json!(vu));
}
cred.insert("credentialSubject".into(), Value::Object(subject));
let opts = BuildProofOptions::new(format!("{}#key-1", params.to_actor), ¶ms.handoff_at);
data_integrity::sign(&Value::Object(cred), receiver_seed, &opts)
}
pub fn verify_handoff(credential: &Value, receiver_public_key: &[u8]) -> Result<Option<Value>> {
let subject = match verify_typed(credential, receiver_public_key, CUSTODY_HANDOFF_TYPE)? {
Some(s) => s,
None => return Ok(None),
};
let from_actor = subject.get("fromActor").and_then(|v| v.as_str());
let to_actor = subject.get("toActor").and_then(|v| v.as_str());
match (from_actor, to_actor) {
(Some(f), Some(t)) if !f.is_empty() && !t.is_empty() => {
if credential.get("issuer").and_then(|v| v.as_str()) != Some(t) {
return Ok(None);
}
}
_ => return Ok(None),
}
Ok(Some(subject))
}
#[derive(Debug, Clone)]
pub struct ActorKey {
pub did: String,
pub public_key: Vec<u8>,
}
pub fn verify_handoff_chain(
handoffs: &[Value],
public_keys: &[ActorKey],
origin_actor: Option<&str>,
) -> Result<(bool, Option<String>)> {
let mut expected_from: Option<String> = origin_actor.map(String::from);
let mut current_holder: Option<String> = origin_actor.map(String::from);
for handoff in handoffs {
let receiver = handoff.get("issuer").and_then(|v| v.as_str()).unwrap_or("");
let key = match public_keys.iter().find(|k| k.did == receiver) {
Some(k) => k,
None => return Ok((false, None)),
};
let subject = match verify_handoff(handoff, &key.public_key)? {
Some(s) => s,
None => return Ok((false, None)),
};
if let Some(expected) = &expected_from {
if subject.get("fromActor").and_then(|v| v.as_str()) != Some(expected.as_str()) {
return Ok((false, None));
}
}
current_holder = subject
.get("toActor")
.and_then(|v| v.as_str())
.map(String::from);
expected_from = current_holder.clone();
}
Ok((true, current_holder))
}
pub fn holder_at(handoffs: &[Value], at: &str) -> Option<String> {
let when = crate::time::iso_to_epoch_seconds(at).ok()?;
let mut holder: Option<String> = None;
for handoff in handoffs {
let start = handoff
.get("validFrom")
.and_then(|v| v.as_str())
.and_then(|s| crate::time::iso_to_epoch_seconds(s).ok());
if let Some(start) = start {
if start <= when {
holder = handoff
.get("credentialSubject")
.and_then(|s| s.get("toActor"))
.and_then(|v| v.as_str())
.map(String::from);
}
}
}
holder
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ConditionChange {
pub responsible_holder: Option<String>,
pub from_condition: String,
pub to_condition: String,
}
pub fn locate_condition_change(handoffs: &[Value]) -> Option<ConditionChange> {
let mut prev_condition: Option<String> = None;
let mut prev_holder: Option<String> = None;
for handoff in handoffs {
let subject = handoff.get("credentialSubject");
let condition = subject
.and_then(|s| s.get("condition"))
.and_then(|v| v.as_str());
let condition = match condition {
Some(c) => c,
None => continue,
};
if let Some(prev) = &prev_condition {
if condition != prev {
return Some(ConditionChange {
responsible_holder: prev_holder,
from_condition: prev.clone(),
to_condition: condition.to_string(),
});
}
}
prev_condition = Some(condition.to_string());
prev_holder = subject
.and_then(|s| s.get("toActor"))
.and_then(|v| v.as_str())
.map(String::from);
}
None
}
pub const ACCESS_GRANT_TYPE: &str = "InfrastructureAccessGrant";
pub const ACCESS_REQUEST_TYPE: &str = "InfrastructureAccessRequest";
#[derive(Debug, Clone)]
pub struct BuildAccessGrant {
pub operator_did: String,
pub robot_did: String,
pub resource: String,
pub operations: Vec<String>,
pub zone: Option<String>,
pub granted_at: String,
pub valid_until: Option<String>,
}
pub fn build_access_grant(operator_seed: &[u8], params: &BuildAccessGrant) -> Result<Value> {
if params.robot_did.is_empty() || params.resource.is_empty() {
return Err(CoreError::Json(
"robot_did and resource are required".into(),
));
}
if params.operations.is_empty() {
return Err(CoreError::Json(
"operations must be a non-empty list".into(),
));
}
let mut subject = Map::new();
subject.insert("id".into(), json!(params.robot_did));
subject.insert("resource".into(), json!(params.resource));
subject.insert("operations".into(), json!(params.operations));
if let Some(zone) = ¶ms.zone {
subject.insert("zone".into(), json!(zone));
}
let mut cred = Map::new();
cred.insert("@context".into(), json!([VC_CONTEXT_V2, VOUCH_CONTEXT_V1]));
cred.insert(
"type".into(),
json!(["VerifiableCredential", ACCESS_GRANT_TYPE]),
);
cred.insert("issuer".into(), json!(params.operator_did));
cred.insert("validFrom".into(), json!(params.granted_at));
if let Some(vu) = ¶ms.valid_until {
cred.insert("validUntil".into(), json!(vu));
}
cred.insert("credentialSubject".into(), Value::Object(subject));
let opts = BuildProofOptions::new(format!("{}#key-1", params.operator_did), ¶ms.granted_at);
data_integrity::sign(&Value::Object(cred), operator_seed, &opts)
}
pub fn verify_access_grant(
credential: &Value,
operator_public_key: &[u8],
now_iso: Option<&str>,
) -> Result<Option<Value>> {
let subject = match verify_typed(credential, operator_public_key, ACCESS_GRANT_TYPE)? {
Some(s) => s,
None => return Ok(None),
};
let resource = subject.get("resource").and_then(|v| v.as_str());
let operations = subject.get("operations").and_then(|v| v.as_array());
match (resource, operations) {
(Some(r), Some(ops)) if !r.is_empty() && !ops.is_empty() => {}
_ => return Ok(None),
}
if !lease_window_current(credential, now_iso) {
return Ok(None);
}
Ok(Some(subject))
}
#[derive(Debug, Clone)]
pub struct BuildAccessRequest {
pub robot_did: String,
pub resource: String,
pub operation: String,
pub requested_at: String,
}
pub fn build_access_request(robot_seed: &[u8], params: &BuildAccessRequest) -> Result<Value> {
if params.robot_did.is_empty() || params.resource.is_empty() || params.operation.is_empty() {
return Err(CoreError::Json(
"robot_did, resource, and operation are required".into(),
));
}
let mut subject = Map::new();
subject.insert("id".into(), json!(params.robot_did));
subject.insert("resource".into(), json!(params.resource));
subject.insert("operation".into(), json!(params.operation));
let mut cred = Map::new();
cred.insert("@context".into(), json!([VC_CONTEXT_V2, VOUCH_CONTEXT_V1]));
cred.insert(
"type".into(),
json!(["VerifiableCredential", ACCESS_REQUEST_TYPE]),
);
cred.insert("issuer".into(), json!(params.robot_did));
cred.insert("validFrom".into(), json!(params.requested_at));
cred.insert("credentialSubject".into(), Value::Object(subject));
let opts = BuildProofOptions::new(format!("{}#key-1", params.robot_did), ¶ms.requested_at);
data_integrity::sign(&Value::Object(cred), robot_seed, &opts)
}
#[derive(Debug, Clone)]
pub struct AuthorizeResult {
pub ok: bool,
pub reasons: Vec<String>,
}
pub fn authorize_access(
grant: &Value,
request: &Value,
operator_public_key: &[u8],
robot_public_key: &[u8],
now_iso: Option<&str>,
) -> Result<AuthorizeResult> {
let mut reasons: Vec<String> = Vec::new();
let grant_subject = match verify_access_grant(grant, operator_public_key, now_iso)? {
Some(s) => s,
None => {
reasons.push("grant invalid or out of window".into());
return Ok(AuthorizeResult { ok: false, reasons });
}
};
let req_subject = match verify_typed(request, robot_public_key, ACCESS_REQUEST_TYPE)? {
Some(s) => s,
None => {
reasons.push("request invalid".into());
return Ok(AuthorizeResult { ok: false, reasons });
}
};
let req_id = req_subject.get("id").and_then(|v| v.as_str());
if request.get("issuer").and_then(|v| v.as_str()) != req_id {
reasons.push("request invalid".into());
return Ok(AuthorizeResult { ok: false, reasons });
}
if grant_subject.get("id").and_then(|v| v.as_str()) != req_id {
reasons.push("grant and request name different robots".into());
}
if grant_subject.get("resource").and_then(|v| v.as_str())
!= req_subject.get("resource").and_then(|v| v.as_str())
{
reasons.push("grant and request name different resources".into());
}
let operation = req_subject.get("operation").and_then(|v| v.as_str());
let permitted = grant_subject
.get("operations")
.and_then(|v| v.as_array())
.map(|ops| operation.is_some() && ops.iter().any(|o| o.as_str() == operation))
.unwrap_or(false);
if !permitted {
reasons.push("operation not permitted by the grant".into());
}
Ok(AuthorizeResult {
ok: reasons.is_empty(),
reasons,
})
}
pub fn attenuates_grant(parent: &Value, child: &Value) -> bool {
let p = parent.get("credentialSubject");
let c = child.get("credentialSubject");
let p_resource = p.and_then(|s| s.get("resource")).and_then(|v| v.as_str());
let c_resource = c.and_then(|s| s.get("resource")).and_then(|v| v.as_str());
if p_resource != c_resource {
return false;
}
let p_ops: HashSet<&str> = p
.and_then(|s| s.get("operations"))
.and_then(|v| v.as_array())
.map(|a| a.iter().filter_map(|o| o.as_str()).collect())
.unwrap_or_default();
let c_ops: HashSet<&str> = c
.and_then(|s| s.get("operations"))
.and_then(|v| v.as_array())
.map(|a| a.iter().filter_map(|o| o.as_str()).collect())
.unwrap_or_default();
if !c_ops.is_subset(&p_ops) {
return false;
}
let p_zone = p.and_then(|s| s.get("zone")).and_then(|v| v.as_str());
let c_zone = c.and_then(|s| s.get("zone")).and_then(|v| v.as_str());
if p_zone.is_some() && c_zone != p_zone {
return false;
}
true
}
pub const FUSED_PERCEPTION_TYPE: &str = "FusedPerceptionAttestation";
pub fn hash_fused_output(output: &[u8]) -> String {
mb64(&Sha256::digest(output))
}
pub fn fusion_inputs_digest(input_frame_hashes: &[String]) -> Result<String> {
if input_frame_hashes.is_empty() {
return Err(CoreError::Json(
"input_frame_hashes must be a non-empty list".into(),
));
}
for h in input_frame_hashes {
if h.is_empty() {
return Err(CoreError::Json(
"each input frame hash must be a non-empty string".into(),
));
}
}
let joined = input_frame_hashes.join("\n");
Ok(mb64(&Sha256::digest(joined.as_bytes())))
}
#[derive(Debug, Clone)]
pub struct BuildFusedAttestation {
pub robot_did: String,
pub fusion_method: String,
pub input_frame_hashes: Vec<String>,
pub fused_output: Option<Vec<u8>>,
pub fused_output_hash: Option<String>,
pub captured_at: Option<String>,
pub valid_from: String,
pub valid_until: Option<String>,
}
pub fn build_fused_attestation(robot_seed: &[u8], params: &BuildFusedAttestation) -> Result<Value> {
if params.robot_did.is_empty() {
return Err(CoreError::Json("robot_did is required".into()));
}
if params.fusion_method.is_empty() {
return Err(CoreError::Json("fusion_method is required".into()));
}
if params.input_frame_hashes.is_empty() {
return Err(CoreError::Json(
"input_frame_hashes must be a non-empty list".into(),
));
}
if params.fused_output.is_some() && params.fused_output_hash.is_some() {
return Err(CoreError::Json(
"provide either fused_output or fused_output_hash, not both".into(),
));
}
let fused_output_hash = match (¶ms.fused_output, ¶ms.fused_output_hash) {
(Some(o), None) => hash_fused_output(o),
(None, Some(h)) if !h.is_empty() => h.clone(),
_ => {
return Err(CoreError::Json(
"fused_output or fused_output_hash is required".into(),
))
}
};
let captured = params
.captured_at
.clone()
.unwrap_or_else(|| params.valid_from.clone());
let mut subject = Map::new();
subject.insert("id".into(), json!(params.robot_did));
subject.insert("fusionMethod".into(), json!(params.fusion_method));
subject.insert("fusedOutputHash".into(), json!(fused_output_hash));
subject.insert("inputFrameHashes".into(), json!(params.input_frame_hashes));
subject.insert(
"inputsDigest".into(),
json!(fusion_inputs_digest(¶ms.input_frame_hashes)?),
);
subject.insert("capturedAt".into(), json!(captured));
let mut cred = Map::new();
cred.insert("@context".into(), json!([VC_CONTEXT_V2, VOUCH_CONTEXT_V1]));
cred.insert(
"type".into(),
json!(["VerifiableCredential", FUSED_PERCEPTION_TYPE]),
);
cred.insert("issuer".into(), json!(params.robot_did));
cred.insert("validFrom".into(), json!(params.valid_from));
if let Some(vu) = ¶ms.valid_until {
cred.insert("validUntil".into(), json!(vu));
}
cred.insert("credentialSubject".into(), Value::Object(subject));
let opts = BuildProofOptions::new(format!("{}#key-1", params.robot_did), ¶ms.valid_from);
data_integrity::sign(&Value::Object(cred), robot_seed, &opts)
}
pub fn verify_fused_attestation(
credential: &Value,
public_key: &[u8],
fused_output: Option<&[u8]>,
) -> Result<Option<Value>> {
let subject = match verify_typed(credential, public_key, FUSED_PERCEPTION_TYPE)? {
Some(s) => s,
None => return Ok(None),
};
let fused_output_hash = subject.get("fusedOutputHash").and_then(|v| v.as_str());
let inputs: Vec<String> = subject
.get("inputFrameHashes")
.and_then(|v| v.as_array())
.map(|a| {
a.iter()
.filter_map(|e| e.as_str().map(String::from))
.collect()
})
.unwrap_or_default();
match fused_output_hash {
Some(h) if !h.is_empty() && !inputs.is_empty() => {}
_ => return Ok(None),
}
match fusion_inputs_digest(&inputs) {
Ok(d) => {
if Some(d.as_str()) != subject.get("inputsDigest").and_then(|v| v.as_str()) {
return Ok(None);
}
}
Err(_) => return Ok(None),
}
if let Some(o) = fused_output {
if hash_fused_output(o) != fused_output_hash.unwrap_or("") {
return Ok(None);
}
}
Ok(Some(subject))
}
#[derive(Debug, Clone)]
pub struct FusionInputsResult {
pub ok: bool,
pub missing: Vec<String>,
}
pub fn verify_fusion_inputs(credential: &Value, log_entries: &[Value]) -> FusionInputsResult {
let recorded: HashSet<&str> = log_entries
.iter()
.filter_map(|e| e.get("frameHash").and_then(|v| v.as_str()))
.collect();
let inputs: Vec<String> = credential
.get("credentialSubject")
.and_then(|s| s.get("inputFrameHashes"))
.and_then(|v| v.as_array())
.map(|a| {
a.iter()
.filter_map(|e| e.as_str().map(String::from))
.collect()
})
.unwrap_or_default();
let missing: Vec<String> = inputs
.into_iter()
.filter(|h| !recorded.contains(h.as_str()))
.collect();
FusionInputsResult {
ok: missing.is_empty(),
missing,
}
}
pub const WEAR_ATTESTATION_TYPE: &str = "RobotWearAttestation";
const WEAR_DERATED_CAPS: [&str; 3] = ["maxForceN", "maxSpeedMps", "maxSpeedNearHumansMps"];
#[derive(Debug, Clone)]
pub struct BuildWearAttestation {
pub robot_did: String,
pub wear_level: f64,
pub metrics: Option<Value>,
pub prev_proof: Option<String>,
pub attested_at: Option<String>,
pub valid_from: String,
pub valid_until: Option<String>,
}
pub fn build_wear_attestation(robot_seed: &[u8], params: &BuildWearAttestation) -> Result<Value> {
if params.robot_did.is_empty() {
return Err(CoreError::Json("robot_did is required".into()));
}
if params.wear_level < 0.0 || params.wear_level > 1.0 {
return Err(CoreError::Json(
"wear_level must be between 0.0 and 1.0".into(),
));
}
let attested = params
.attested_at
.clone()
.unwrap_or_else(|| params.valid_from.clone());
let mut subject = Map::new();
subject.insert("id".into(), json!(params.robot_did));
subject.insert("wearLevel".into(), json!(params.wear_level));
subject.insert("attestedAt".into(), json!(attested));
if let Some(m) = ¶ms.metrics {
subject.insert("metrics".into(), m.clone());
}
if let Some(prev) = ¶ms.prev_proof {
subject.insert("prevProof".into(), json!(prev));
}
let mut cred = Map::new();
cred.insert("@context".into(), json!([VC_CONTEXT_V2, VOUCH_CONTEXT_V1]));
cred.insert(
"type".into(),
json!(["VerifiableCredential", WEAR_ATTESTATION_TYPE]),
);
cred.insert("issuer".into(), json!(params.robot_did));
cred.insert("validFrom".into(), json!(params.valid_from));
if let Some(vu) = ¶ms.valid_until {
cred.insert("validUntil".into(), json!(vu));
}
cred.insert("credentialSubject".into(), Value::Object(subject));
let opts = BuildProofOptions::new(format!("{}#key-1", params.robot_did), ¶ms.valid_from);
data_integrity::sign(&Value::Object(cred), robot_seed, &opts)
}
pub fn verify_wear_attestation(credential: &Value, public_key: &[u8]) -> Result<Option<Value>> {
let subject = match verify_typed(credential, public_key, WEAR_ATTESTATION_TYPE)? {
Some(s) => s,
None => return Ok(None),
};
if credential.get("issuer") != subject.get("id") {
return Ok(None);
}
match subject.get("wearLevel").and_then(|v| v.as_f64()) {
Some(level) if (0.0..=1.0).contains(&level) => {}
_ => return Ok(None),
}
Ok(Some(subject))
}
pub fn verify_wear_chain(attestations: &[Value], public_key: &[u8]) -> Result<Option<Value>> {
if attestations.is_empty() {
return Ok(None);
}
let mut prev_proof: Option<String> = None;
let mut latest: Option<Value> = None;
for att in attestations {
let subject = match verify_wear_attestation(att, public_key)? {
Some(s) => s,
None => return Ok(None),
};
if let Some(prev) = &prev_proof {
let linked = subject.get("prevProof").and_then(|v| v.as_str());
if linked != Some(prev.as_str()) {
return Ok(None);
}
}
prev_proof = att
.get("proof")
.and_then(|p| p.get("proofValue"))
.and_then(|v| v.as_str())
.map(String::from);
latest = Some(subject);
}
Ok(latest)
}
pub fn attenuate_for_wear(scope: &Value, wear_level: f64) -> Result<Value> {
if wear_level < 0.0 || wear_level > 1.0 {
return Err(CoreError::Json(
"wear_level must be between 0.0 and 1.0".into(),
));
}
let factor = 1.0 - wear_level;
let empty = Map::new();
let src = scope.as_object().unwrap_or(&empty);
let mut narrowed = Map::new();
for (key, value) in src {
if WEAR_DERATED_CAPS.contains(&key.as_str()) {
if let Some(n) = value.as_f64() {
narrowed.insert(key.clone(), json!(n * factor));
continue;
}
}
narrowed.insert(key.clone(), value.clone());
}
Ok(Value::Object(narrowed))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::keys::Ed25519KeyPair;
use std::fs;
fn load_vector() -> Value {
let raw = fs::read_to_string("../../test-vectors/robotics/vector.json")
.expect("interop vector present");
serde_json::from_str(&raw).unwrap()
}
fn software_root_attest(root_seed: &[u8], robot_did: &str, robot_key_mb: &str) -> Vec<u8> {
let kp = Ed25519KeyPair::from_seed_slice(root_seed).unwrap();
let binding = robot_identity_binding(robot_did, robot_key_mb);
kp.sign(&binding).to_vec()
}
#[test]
fn verifies_python_interop_vector() {
let raw = fs::read_to_string("../../test-vectors/robotics/vector.json")
.expect("interop vector present");
let vector: Value = serde_json::from_str(&raw).unwrap();
let jwk_x = vector["robot_public_key_jwk"]["x"].as_str().unwrap();
let robot_pub = URL_SAFE_NO_PAD.decode(jwk_x).unwrap();
let cred = vector["robot_identity_credential"].clone();
let subject = verify_robot_identity(&cred, &robot_pub).unwrap();
let subject = subject.expect("the Python interop vector must verify in Rust");
assert_eq!(subject["make"], json!("Acme Robotics"));
}
#[test]
fn mint_verify_roundtrip() {
let robot_seed = [3u8; 32];
let root_seed = [7u8; 32];
let robot_did = "did:web:robot.example.com";
let robot_kp = Ed25519KeyPair::from_seed(&robot_seed);
let root_kp = Ed25519KeyPair::from_seed(&root_seed);
let attestation = software_root_attest(&root_seed, robot_did, &robot_kp.public_multikey());
let params = MintRobotIdentity {
robot_did: robot_did.into(),
make: "Acme".into(),
model: "AR-7".into(),
serial: "SN-1".into(),
owner: Some("did:web:owner.example.com".into()),
root_kind: "TPM".into(),
root_public_multibase: root_kp.public_multikey(),
attestation,
lifecycle: None,
valid_from: "2026-01-01T00:00:00Z".into(),
valid_until: None,
};
let cred = mint_robot_identity(&robot_seed, ¶ms).unwrap();
let subject = verify_robot_identity(&cred, &robot_kp.public_key()).unwrap();
assert!(subject.is_some(), "round-trip identity must verify");
}
#[test]
fn forged_hardware_root_fails() {
let robot_seed = [3u8; 32];
let root_seed = [7u8; 32];
let attacker_seed = [9u8; 32];
let robot_did = "did:web:robot.example.com";
let robot_kp = Ed25519KeyPair::from_seed(&robot_seed);
let root_kp = Ed25519KeyPair::from_seed(&root_seed);
let attacker_kp = Ed25519KeyPair::from_seed(&attacker_seed);
let attestation = software_root_attest(&root_seed, robot_did, &robot_kp.public_multikey());
let params = MintRobotIdentity {
robot_did: robot_did.into(),
make: "Acme".into(),
model: "AR-7".into(),
serial: "SN-1".into(),
owner: None,
root_kind: "TPM".into(),
root_public_multibase: root_kp.public_multikey(),
attestation,
lifecycle: None,
valid_from: "2026-01-01T00:00:00Z".into(),
valid_until: None,
};
let mut cred = mint_robot_identity(&robot_seed, ¶ms).unwrap();
cred["credentialSubject"]["hardwareRoot"]["publicKeyMultibase"] =
json!(attacker_kp.public_multikey());
let resigned = {
let mut bare = cred.clone();
bare.as_object_mut().unwrap().remove("proof");
let opts = BuildProofOptions::new(format!("{robot_did}#key-1"), "2026-01-01T00:00:00Z");
data_integrity::sign(&bare, &robot_seed, &opts).unwrap()
};
assert!(verify_robot_identity(&resigned, &robot_kp.public_key())
.unwrap()
.is_none());
}
#[test]
fn config_hash_matches_interop_vector() {
let vector = load_vector();
let got = config_hash(&vector["config"]);
assert_eq!(got, vector["expected_config_hash"].as_str().unwrap());
assert_eq!(got, "uMh9_H2Lk51-m9SpBhiEa1oqIwwwA7yT27e2QFS0YKUs");
}
#[test]
fn provenance_roundtrip_and_config_tamper() {
let seed = [4u8; 32];
let kp = Ed25519KeyPair::from_seed(&seed);
let config = load_vector()["config"].clone();
let params = BuildProvenance {
issuer_did: "did:web:robot.example.com".into(),
robot_did: "did:web:robot.example.com".into(),
model_name: "openvla-7b".into(),
weights_hash: "uABCDEF".into(),
safety_policy: "did:web:authority.example.com#policy-v3".into(),
config: Some(config.clone()),
version: Some("2026.06".into()),
supersedes: None,
valid_from: "2026-01-01T00:00:00Z".into(),
valid_until: None,
};
let att = build_provenance_attestation(&seed, ¶ms).unwrap();
assert!(
verify_provenance_attestation(&att, &kp.public_key(), Some(&config))
.unwrap()
.is_some()
);
let tampered = json!({"temperature": 1.0, "max_torque": 99.0});
assert!(
verify_provenance_attestation(&att, &kp.public_key(), Some(&tampered))
.unwrap()
.is_none()
);
}
#[test]
fn capability_build_check_attenuate() {
let seed = [6u8; 32];
let kp = Ed25519KeyPair::from_seed(&seed);
let params = BuildPhysicalScope {
issuer_did: "did:web:fleet.example.com".into(),
subject_did: "did:web:robot.example.com".into(),
max_force_n: Some(80.0),
max_speed_mps: Some(2.0),
max_speed_near_humans_mps: Some(0.5),
allowed_zones: Some(vec!["warehouse-a".into(), "dock-3".into()]),
shift_windows: Some(vec![ShiftWindow {
start: "08:00".into(),
end: "18:00".into(),
}]),
valid_from: "2026-01-01T00:00:00Z".into(),
valid_until: None,
};
let cred = build_physical_scope_credential(&seed, ¶ms).unwrap();
assert!(has_type(cred.get("type"), PHYSICAL_SCOPE_TYPE));
assert!(data_integrity::verify_proof(&cred, &kp.public_key()).unwrap());
let scope = cred["credentialSubject"]["physicalScope"].clone();
let within = check_physical_action(
&scope,
&PhysicalAction {
force_n: Some(50.0),
speed_mps: Some(1.5),
zone: Some("warehouse-a".into()),
time_hm: Some("09:30".into()),
..Default::default()
},
);
assert!(within.ok, "within-scope action: {:?}", within.reasons);
assert!(
!check_physical_action(
&scope,
&PhysicalAction {
force_n: Some(120.0),
..Default::default()
}
)
.ok
);
assert!(
!check_physical_action(
&scope,
&PhysicalAction {
speed_mps: Some(1.5),
near_humans: true,
..Default::default()
}
)
.ok
);
assert!(
check_physical_action(
&scope,
&PhysicalAction {
speed_mps: Some(1.5),
near_humans: false,
..Default::default()
}
)
.ok
);
assert!(
!check_physical_action(
&scope,
&PhysicalAction {
zone: Some("loading-bay-9".into()),
..Default::default()
}
)
.ok
);
assert!(
!check_physical_action(
&scope,
&PhysicalAction {
time_hm: Some("23:00".into()),
..Default::default()
}
)
.ok
);
}
#[test]
fn attenuation_rules() {
let parent = json!({
"maxForceN": 80.0, "maxSpeedMps": 2.0, "maxSpeedNearHumansMps": 0.5,
"allowedZones": ["warehouse-a", "dock-3"],
"shiftWindows": [{"start": "08:00", "end": "18:00"}]
});
let narrower = json!({
"maxForceN": 50.0, "maxSpeedMps": 1.0, "maxSpeedNearHumansMps": 0.3,
"allowedZones": ["warehouse-a"],
"shiftWindows": [{"start": "09:00", "end": "17:00"}]
});
assert!(attenuates(&parent, &narrower));
assert!(!attenuates(
&parent,
&json!({"maxForceN": 100.0, "maxSpeedMps": 1.0, "maxSpeedNearHumansMps": 0.3, "allowedZones": ["warehouse-a"]})
));
assert!(!attenuates(
&parent,
&json!({"maxForceN": 50.0, "allowedZones": ["warehouse-a"]})
));
assert!(!attenuates(
&parent,
&json!({"maxForceN": 50.0, "maxSpeedMps": 1.0, "maxSpeedNearHumansMps": 0.3, "allowedZones": ["loading-bay-9"]})
));
assert!(!attenuates(
&parent,
&json!({"maxForceN": 50.0, "maxSpeedMps": 1.0, "maxSpeedNearHumansMps": 0.3, "allowedZones": ["warehouse-a"], "shiftWindows": [{"start": "06:00", "end": "20:00"}]})
));
}
#[test]
fn handshake_full_flow() {
let a_seed = [11u8; 32];
let b_seed = [12u8; 32];
let a = Ed25519KeyPair::from_seed(&a_seed);
let b = Ed25519KeyPair::from_seed(&b_seed);
let a_did = "did:web:robot-a.example.com";
let b_did = "did:web:robot-b.example.com";
let policy_b = TrustPolicy::new(["robot-a.example.com".to_string()], false);
let hello = build_hello(
&a_seed,
&BuildHello {
from_did: a_did.into(),
proposed_scope: vec!["lift".into(), "carry".into(), "scan".into()],
nonce: "abc123".into(),
peer_did: Some(b_did.into()),
issued_at: "2026-01-01T00:00:00Z".into(),
},
)
.unwrap();
let accept = build_accept(
&b_seed,
&hello,
&a.public_key(),
&policy_b,
&BuildAccept {
from_did: b_did.into(),
offered_scope: vec!["carry".into(), "scan".into(), "weld".into()],
session_id: "urn:uuid:sess-1".into(),
valid_until: "2026-01-01T00:05:00Z".into(),
created: "2026-01-01T00:00:01Z".into(),
},
)
.unwrap();
let session = verify_accept(&accept, &b.public_key(), "abc123", None)
.unwrap()
.expect("accept verifies");
assert_eq!(session.scope, vec!["carry".to_string(), "scan".to_string()]);
assert_eq!(session.initiator, a_did);
assert_eq!(session.responder, b_did);
let confirm = build_confirm(&a_seed, a_did, &session, "2026-01-01T00:00:02Z").unwrap();
assert!(verify_confirm(&confirm, &a.public_key(), &session.session_id, "abc123").unwrap());
}
#[test]
fn handshake_untrusted_tamper_and_nonce() {
let a_seed = [11u8; 32];
let b_seed = [12u8; 32];
let a = Ed25519KeyPair::from_seed(&a_seed);
let b = Ed25519KeyPair::from_seed(&b_seed);
let a_did = "did:web:robot-a.example.com";
let b_did = "did:web:robot-b.example.com";
let hello = build_hello(
&a_seed,
&BuildHello {
from_did: a_did.into(),
proposed_scope: vec!["lift".into()],
nonce: "n1".into(),
peer_did: None,
issued_at: "2026-01-01T00:00:00Z".into(),
},
)
.unwrap();
let strict = TrustPolicy::new(["someone-else.example.com".to_string()], false);
assert!(build_accept(
&b_seed,
&hello,
&a.public_key(),
&strict,
&BuildAccept {
from_did: b_did.into(),
offered_scope: vec!["lift".into()],
session_id: "s".into(),
valid_until: "2026-01-01T00:05:00Z".into(),
created: "2026-01-01T00:00:01Z".into(),
},
)
.is_err());
let open = TrustPolicy::new(Vec::<String>::new(), true);
let mut tampered = hello.clone();
tampered["proposedScope"] = json!(["lift", "weld"]);
assert!(build_accept(
&b_seed,
&tampered,
&a.public_key(),
&open,
&BuildAccept {
from_did: b_did.into(),
offered_scope: vec!["lift".into(), "weld".into()],
session_id: "s".into(),
valid_until: "2026-01-01T00:05:00Z".into(),
created: "2026-01-01T00:00:01Z".into(),
},
)
.is_err());
let accept = build_accept(
&b_seed,
&hello,
&a.public_key(),
&open,
&BuildAccept {
from_did: b_did.into(),
offered_scope: vec!["lift".into()],
session_id: "s".into(),
valid_until: "2026-01-01T00:05:00Z".into(),
created: "2026-01-01T00:00:01Z".into(),
},
)
.unwrap();
assert!(verify_accept(&accept, &b.public_key(), "wrong-nonce", None)
.unwrap()
.is_none());
}
#[test]
fn did_web_domain_parsing() {
assert_eq!(did_web_domain("did:web:example.com"), Some("example.com"));
assert_eq!(
did_web_domain("did:web:robot.example.com:agent"),
Some("robot.example.com")
);
assert_eq!(did_web_domain("did:key:z6Mk"), None);
}
#[test]
fn blackbox_append_open_and_chain() {
let key = [0x11u8; 32];
let mut log = BlackBoxLog::new(&key, None).unwrap();
let e0 = log
.append(
"motion",
&json!({"speed": 1.5, "joint": "elbow"}),
"2026-01-01T00:00:00Z",
)
.unwrap();
log.append("stop", &json!({"reason": "human"}), "2026-01-01T00:00:01Z")
.unwrap();
let opened = log.open(&e0).unwrap();
assert_eq!(opened["joint"], json!("elbow"));
assert!(verify_blackbox_chain(log.entries(), None).ok);
let wire: Vec<Value> =
serde_json::from_str(&serde_json::to_string(log.entries()).unwrap()).unwrap();
assert!(verify_blackbox_chain(&wire, None).ok);
assert_eq!(open_entry(&wire[0], &key).unwrap()["joint"], json!("elbow"));
let mut tampered = wire.clone();
tampered[1]["event"] = json!("tampered");
assert!(!verify_blackbox_chain(&tampered, None).ok);
assert!(open_entry(&e0, &[0x22u8; 32]).is_err());
assert!(BlackBoxLog::new(&[0u8; 16], None).is_err());
assert_eq!(genesis_prev_hash(), format!("u{}", "A".repeat(43)));
}
#[test]
fn killswitch_build_verify_and_trust() {
let seed = [13u8; 32];
let kp = Ed25519KeyPair::from_seed(&seed);
let cred = build_killswitch_credential(
&seed,
&BuildKillswitch {
issuer_did: "did:web:safety.example.com".into(),
target: "did:web:robot.example.com".into(),
reason: "human in path".into(),
command: None,
scope: Some(vec!["arm".into(), "drive".into()]),
valid_from: "2026-01-01T00:00:00Z".into(),
valid_until: None,
},
)
.unwrap();
let mut trusted = HashSet::new();
trusted.insert("did:web:safety.example.com".to_string());
let subject = verify_killswitch_credential(&cred, &kp.public_key(), Some(&trusted))
.unwrap()
.expect("trusted authority verifies");
assert_eq!(subject["command"], json!(EMERGENCY_STOP));
let mut other = HashSet::new();
other.insert("did:web:someone-else.example.com".to_string());
assert!(
verify_killswitch_credential(&cred, &kp.public_key(), Some(&other))
.unwrap()
.is_none()
);
let mut wrong = cred.clone();
wrong["type"] = json!(["VerifiableCredential"]);
assert!(verify_killswitch_credential(&wrong, &kp.public_key(), None)
.unwrap()
.is_none());
}
#[test]
fn passport_build_encode_verify() {
let seed = [14u8; 32];
let kp = Ed25519KeyPair::from_seed(&seed);
let did = "did:web:robot.example.com";
let pass = build_passport(
&seed,
&BuildPassport {
issuer_did: did.into(),
robot_did: did.into(),
make: "Acme Robotics".into(),
model: "AR-7".into(),
owner: "did:web:owner.example.com".into(),
authorized_actions: vec!["lift".into(), "carry".into()],
certification: Some("ISO-10218".into()),
status: None,
valid_from: "2026-01-01T00:00:00Z".into(),
valid_until: None,
},
)
.unwrap();
let subject = verify_passport(&pass, &kp.public_key(), "2026-06-01T00:00:00Z")
.unwrap()
.expect("passport verifies");
assert_eq!(subject["owner"], json!("did:web:owner.example.com"));
assert_eq!(subject["status"], json!(STATUS_ACTIVE));
let uri = encode_passport(&pass);
assert!(uri.starts_with("vouch-passport:u"));
let decoded = decode_passport(&uri).unwrap();
assert!(
verify_passport(&decoded, &kp.public_key(), "2026-06-01T00:00:00Z")
.unwrap()
.is_some()
);
assert!(
verify_passport_uri(&uri, &kp.public_key(), "2026-06-01T00:00:00Z")
.unwrap()
.is_some()
);
let mut tampered = pass.clone();
tampered["credentialSubject"]["owner"] = json!("did:web:attacker.example.com");
assert!(
verify_passport(&tampered, &kp.public_key(), "2026-06-01T00:00:00Z")
.unwrap()
.is_none()
);
assert!(decode_passport("https://example.com/x").is_err());
}
#[test]
fn passport_expiry_and_suspended() {
let seed = [15u8; 32];
let kp = Ed25519KeyPair::from_seed(&seed);
let did = "did:web:robot.example.com";
let timed = build_passport(
&seed,
&BuildPassport {
issuer_did: did.into(),
robot_did: did.into(),
make: "Acme".into(),
model: "AR-7".into(),
owner: "did:web:owner.example.com".into(),
authorized_actions: vec!["lift".into()],
certification: None,
status: None,
valid_from: "2026-01-01T00:00:00Z".into(),
valid_until: Some("2026-01-01T00:01:00Z".into()),
},
)
.unwrap();
assert!(
verify_passport(&timed, &kp.public_key(), "2026-01-01T00:00:30Z")
.unwrap()
.is_some()
);
assert!(
verify_passport(&timed, &kp.public_key(), "2026-01-01T00:02:00Z")
.unwrap()
.is_none()
);
let suspended = build_passport(
&seed,
&BuildPassport {
issuer_did: did.into(),
robot_did: did.into(),
make: "Acme".into(),
model: "AR-7".into(),
owner: "did:web:owner.example.com".into(),
authorized_actions: vec!["lift".into()],
certification: None,
status: Some(STATUS_SUSPENDED.into()),
valid_from: "2026-01-01T00:00:00Z".into(),
valid_until: None,
},
)
.unwrap();
let subject = verify_passport(&suspended, &kp.public_key(), "2026-06-01T00:00:00Z")
.unwrap()
.expect("a suspended passport still verifies");
assert_eq!(subject["status"], json!(STATUS_SUSPENDED));
}
#[test]
fn motion_digest_matches_interop_vector() {
let vector = load_vector();
let scope = vector["physical_scope"].clone();
let mut collector = MotionCollector::new(Some(scope));
collector
.record(&MotionSample {
force_n: Some(12.0),
speed_mps: Some(0.4),
near_humans: false,
zone: Some("cell-3".into()),
time_hm: None,
})
.unwrap();
collector
.record(&MotionSample {
force_n: Some(20.0),
speed_mps: Some(0.2),
near_humans: true,
zone: Some("cell-3".into()),
time_hm: None,
})
.unwrap();
assert_eq!(collector.digest(), vector["expected_motion_digest"]);
}
#[test]
fn heartbeat_build_verify_and_liveness() {
let seed = [21u8; 32];
let kp = Ed25519KeyPair::from_seed(&seed);
let did = "did:web:robot.example.com";
let digest = json!({
"samples": 2, "maxForceN": 20.0, "maxSpeedMps": 0.4,
"maxSpeedNearHumansMps": 0.2, "zoneBreaches": 0, "breachCount": 0,
"withinEnvelope": true
});
let hb = build_robot_heartbeat(
&seed,
&BuildRobotHeartbeat {
robot_did: did.into(),
session_id: "urn:uuid:sess-1".into(),
interval_index: 0,
interval_seconds: 60,
motion_digest: digest,
valid_from: "2026-01-01T00:00:00Z".into(),
},
)
.unwrap();
let subject = verify_robot_heartbeat(&hb, &kp.public_key())
.unwrap()
.expect("heartbeat verifies");
assert_eq!(subject["sessionId"], json!("urn:uuid:sess-1"));
assert!(is_live(&hb, "2026-01-01T00:01:00Z", None, DEFAULT_GRACE_INTERVALS).unwrap());
assert!(!is_live(&hb, "2026-01-01T01:00:00Z", None, DEFAULT_GRACE_INTERVALS).unwrap());
let breached = json!({
"samples": 1, "maxForceN": 99.0, "maxSpeedMps": 0.4,
"maxSpeedNearHumansMps": 0.2, "zoneBreaches": 0, "breachCount": 1,
"withinEnvelope": false
});
let hb2 = build_robot_heartbeat(
&seed,
&BuildRobotHeartbeat {
robot_did: did.into(),
session_id: "s".into(),
interval_index: 1,
interval_seconds: 60,
motion_digest: breached,
valid_from: "2026-01-01T00:00:00Z".into(),
},
)
.unwrap();
assert!(!is_live(&hb2, "2026-01-01T00:00:30Z", None, DEFAULT_GRACE_INTERVALS).unwrap());
let mut tampered = hb.clone();
tampered["credentialSubject"]["sessionId"] = json!("urn:uuid:other");
assert!(verify_robot_heartbeat(&tampered, &kp.public_key())
.unwrap()
.is_none());
}
#[test]
fn safety_log_matches_interop_vector() {
let vector = load_vector();
let mut log = SafetyEventLog::new(None);
log.append(
"near_miss",
"low",
Some(&json!({"zone": "cell-3"})),
None,
"2026-01-01T00:00:00Z",
)
.unwrap();
log.append(
"envelope_breach",
"high",
None,
None,
"2026-01-01T00:01:00Z",
)
.unwrap();
assert_eq!(
Value::Array(log.entries().to_vec()),
vector["safety_log_entries"]
);
assert_eq!(
log.head(),
vector["expected_safety_log_head"].as_str().unwrap()
);
assert_eq!(log.summarize(), vector["expected_safety_summary"]);
assert!(verify_safety_log(log.entries(), None).ok);
}
#[test]
fn safety_record_build_verify_and_tamper() {
let seed = [22u8; 32];
let kp = Ed25519KeyPair::from_seed(&seed);
let mut log = SafetyEventLog::new(None);
log.append("near_miss", "low", None, None, "2026-01-01T00:00:00Z")
.unwrap();
let summary = log.summarize();
let record = build_safety_record(
&seed,
&BuildSafetyRecord {
issuer_did: "did:web:owner.example.com".into(),
robot_did: "did:web:robot.example.com".into(),
summary,
period_start: Some("2026-01-01T00:00:00Z".into()),
period_end: Some("2026-01-31T00:00:00Z".into()),
valid_from: "2026-02-01T00:00:00Z".into(),
valid_until: None,
},
)
.unwrap();
let subject = verify_safety_record(&record, &kp.public_key())
.unwrap()
.expect("safety record verifies");
assert_eq!(subject["totalEvents"], json!(1));
assert_eq!(subject["period"]["start"], json!("2026-01-01T00:00:00Z"));
let mut tampered = record.clone();
tampered["credentialSubject"]["totalEvents"] = json!(99);
assert!(verify_safety_record(&tampered, &kp.public_key())
.unwrap()
.is_none());
let mut entries = log.entries().to_vec();
entries[0]["severity"] = json!("critical");
assert!(!verify_safety_log(&entries, None).ok);
}
#[test]
fn status_entry_matches_interop_vector() {
let vector = load_vector();
let entry = build_status_list_entry(
"https://fleet.example.com/status/1",
42,
STATUS_PURPOSE_REVOCATION,
None,
)
.unwrap();
assert_eq!(entry, vector["expected_credential_status_entry"]);
}
#[test]
fn attach_and_check_credential_status() {
let seed = [23u8; 32];
let kp = Ed25519KeyPair::from_seed(&seed);
let did = "did:web:robot.example.com";
let pass = build_passport(
&seed,
&BuildPassport {
issuer_did: did.into(),
robot_did: did.into(),
make: "Acme".into(),
model: "AR-7".into(),
owner: "did:web:owner.example.com".into(),
authorized_actions: vec!["lift".into()],
certification: None,
status: None,
valid_from: "2026-01-01T00:00:00Z".into(),
valid_until: None,
},
)
.unwrap();
let with_status = attach_credential_status(
&pass,
&seed,
&AttachCredentialStatus {
status_list_credential: "https://fleet.example.com/status/1".into(),
status_list_index: 42,
status_purpose: STATUS_PURPOSE_REVOCATION.into(),
entry_id: None,
created: "2026-01-01T00:00:00Z".into(),
},
)
.unwrap();
assert!(data_integrity::verify_proof(&with_status, &kp.public_key()).unwrap());
assert_eq!(
with_status["credentialStatus"]["statusListIndex"],
json!("42")
);
let mut bits = vec![0u8; crate::status_list::DEFAULT_BITSTRING_LENGTH / 8];
crate::status_list::set_status(&mut bits, 42, true).unwrap();
let encoded = crate::status_list::encode_bitstring(&bits).unwrap();
let status_list = json!({
"id": "https://fleet.example.com/status/1",
"type": ["VerifiableCredential", "BitstringStatusListCredential"],
"credentialSubject": {
"type": "BitstringStatusList",
"statusPurpose": "revocation",
"encodedList": encoded,
}
});
assert!(
check_credential_status(&with_status, &status_list, STATUS_PURPOSE_REVOCATION).unwrap()
);
assert!(!check_credential_status(&pass, &status_list, STATUS_PURPOSE_REVOCATION).unwrap());
let clear_bits = vec![0u8; crate::status_list::DEFAULT_BITSTRING_LENGTH / 8];
let clear_encoded = crate::status_list::encode_bitstring(&clear_bits).unwrap();
let mut clear_list = status_list.clone();
clear_list["credentialSubject"]["encodedList"] = json!(clear_encoded);
assert!(
!check_credential_status(&with_status, &clear_list, STATUS_PURPOSE_REVOCATION).unwrap()
);
}
#[test]
fn frame_hash_matches_interop_vector() {
let vector = load_vector();
let sample_frame: Vec<u8> = (0u8..64).collect();
let got = hash_frame(&sample_frame);
assert_eq!(got, vector["expected_frame_hash"].as_str().unwrap());
assert_eq!(got, "u_eq5rPNxA2K9JljNyaKej5x1f8-YEWA6jER80dkVEQg");
}
#[test]
fn perception_log_matches_interop_vector() {
let vector = load_vector();
let sample_frame: Vec<u8> = (0u8..64).collect();
let mut log = PerceptionLog::new(None);
log.record(
"cam-front",
"camera",
Some(&sample_frame),
None,
"2026-01-01T00:00:00Z",
)
.unwrap();
log.record(
"lidar-top",
"lidar",
None,
Some(&hash_frame(b"scan-0")),
"2026-01-01T00:00:01Z",
)
.unwrap();
assert_eq!(
Value::Array(log.entries().to_vec()),
vector["perception_log_entries"]
);
assert_eq!(
log.head(),
vector["expected_perception_log_head"].as_str().unwrap()
);
assert!(verify_perception_log(log.entries(), None).ok);
}
#[test]
fn perception_record_validation_and_tamper() {
let mut log = PerceptionLog::new(None);
assert!(log
.record("cam", "sonar", Some(b"x"), None, "2026-01-01T00:00:00Z")
.is_err());
assert!(log
.record(
"cam",
"camera",
Some(b"x"),
Some("uABC"),
"2026-01-01T00:00:00Z"
)
.is_err());
assert!(log
.record("cam", "camera", None, None, "2026-01-01T00:00:00Z")
.is_err());
assert!(log
.record("", "camera", Some(b"x"), None, "2026-01-01T00:00:00Z")
.is_err());
log.record(
"cam",
"camera",
Some(b"frame-0"),
None,
"2026-01-01T00:00:00Z",
)
.unwrap();
log.record(
"cam",
"camera",
Some(b"frame-1"),
None,
"2026-01-01T00:00:01Z",
)
.unwrap();
assert!(verify_perception_log(log.entries(), None).ok);
let mut entries = log.entries().to_vec();
entries[0]["frameHash"] = json!("uTAMPERED");
assert!(!verify_perception_log(&entries, None).ok);
}
#[test]
fn perception_attestation_build_verify_and_tamper() {
let seed = [24u8; 32];
let kp = Ed25519KeyPair::from_seed(&seed);
let did = "did:web:robot.example.com";
let frame = b"sensor-frame-bytes";
let frame_hash = hash_frame(frame);
let cred = build_perception_attestation(
&seed,
&BuildPerception {
robot_did: did.into(),
sensor_id: "cam-front".into(),
modality: "camera".into(),
frame_hash: frame_hash.clone(),
captured_at: Some("2026-01-01T00:00:00Z".into()),
log_head: Some("uHEAD".into()),
valid_from: "2026-01-01T00:00:00Z".into(),
valid_until: None,
},
)
.unwrap();
let subject = verify_perception_attestation(&cred, &kp.public_key(), Some(frame))
.unwrap()
.expect("perception attestation verifies");
assert_eq!(subject["sensorId"], json!("cam-front"));
assert_eq!(subject["frameHash"], json!(frame_hash));
assert_eq!(subject["logHead"], json!("uHEAD"));
assert!(verify_perception_attestation(&cred, &kp.public_key(), None)
.unwrap()
.is_some());
assert!(
verify_perception_attestation(&cred, &kp.public_key(), Some(b"other-frame"))
.unwrap()
.is_none()
);
let mut tampered = cred.clone();
tampered["credentialSubject"]["sensorId"] = json!("cam-rear");
assert!(
verify_perception_attestation(&tampered, &kp.public_key(), None)
.unwrap()
.is_none()
);
let mut wrong = cred.clone();
wrong["type"] = json!(["VerifiableCredential"]);
assert!(
verify_perception_attestation(&wrong, &kp.public_key(), None)
.unwrap()
.is_none()
);
assert!(build_perception_attestation(
&seed,
&BuildPerception {
robot_did: did.into(),
sensor_id: "s".into(),
modality: "sonar".into(),
frame_hash: frame_hash.clone(),
captured_at: None,
log_head: None,
valid_from: "2026-01-01T00:00:00Z".into(),
valid_until: None,
},
)
.is_err());
assert!(build_perception_attestation(
&seed,
&BuildPerception {
robot_did: did.into(),
sensor_id: "s".into(),
modality: "camera".into(),
frame_hash: String::new(),
captured_at: None,
log_head: None,
valid_from: "2026-01-01T00:00:00Z".into(),
valid_until: None,
},
)
.is_err());
}
fn jwk_pub(jwk: &Value) -> Vec<u8> {
URL_SAFE_NO_PAD
.decode(jwk["x"].as_str().unwrap())
.expect("base64url JWK x")
}
#[test]
fn verifies_python_lease_interop_vector() {
let vector = load_vector();
let robot_pub = jwk_pub(&vector["robot_public_key_jwk"]);
let lease = vector["delegation_lease_credential"].clone();
let subject = verify_delegation_lease(&lease, &robot_pub, None, None)
.unwrap()
.expect("the Python lease interop vector must verify in Rust");
assert_eq!(subject["leaseId"], json!("lease-vector-1"));
}
#[test]
fn verifies_python_quorum_interop_vector() {
let vector = load_vector();
let approvals = vector["quorum_approvals"].as_array().unwrap().clone();
let action_id = vector["quorum_action_id"].as_str().unwrap();
let keys: Vec<ApproverKey> = vector["quorum_approver_keys"]
.as_object()
.unwrap()
.iter()
.map(|(did, jwk)| ApproverKey {
did: did.clone(),
public_key: jwk_pub(jwk),
})
.collect();
let (ok, approvers) = verify_action_authorization(
&approvals,
action_id,
"did:web:robot.example.com",
&keys,
2,
None,
None,
)
.unwrap();
assert!(
ok,
"the Python quorum interop vector must authorize in Rust"
);
assert_eq!(approvers.len(), 2);
assert_eq!(approvers[0], "did:web:approver-1.example.com");
assert_eq!(approvers[1], "did:web:approver-2.example.com");
}
#[test]
fn lease_build_verify_permits_and_expiry() {
let seed = [31u8; 32];
let kp = Ed25519KeyPair::from_seed(&seed);
let did = "did:web:robot.example.com";
let scope = json!({
"maxForceN": 80.0, "maxSpeedMps": 2.0, "maxSpeedNearHumansMps": 0.5,
"allowedZones": ["warehouse-a", "dock-3"]
});
let lease = build_delegation_lease(
&seed,
&BuildDelegationLease {
issuer_did: did.into(),
robot_did: did.into(),
lease_id: "lease-1".into(),
scope: scope.clone(),
parent_lease_id: None,
valid_from: "2026-01-01T00:00:00Z".into(),
valid_until: "2026-01-01T01:00:00Z".into(),
},
)
.unwrap();
let subject =
verify_delegation_lease(&lease, &kp.public_key(), Some("2026-01-01T00:30:00Z"), None)
.unwrap()
.expect("lease verifies in window");
assert_eq!(subject["leaseId"], json!("lease-1"));
assert!(lease_permits(
&subject,
&PhysicalAction {
force_n: Some(50.0),
zone: Some("warehouse-a".into()),
..Default::default()
},
Some(&lease),
Some("2026-01-01T00:30:00Z"),
));
assert!(!lease_permits(
&subject,
&PhysicalAction {
force_n: Some(120.0),
..Default::default()
},
Some(&lease),
Some("2026-01-01T00:30:00Z"),
));
assert!(verify_delegation_lease(
&lease,
&kp.public_key(),
Some("2026-01-01T02:00:00Z"),
None,
)
.unwrap()
.is_none());
assert!(!lease_permits(
&subject,
&PhysicalAction {
force_n: Some(50.0),
..Default::default()
},
Some(&lease),
Some("2026-01-01T02:00:00Z"),
));
let mut tampered = lease.clone();
tampered["credentialSubject"]["leaseId"] = json!("lease-other");
assert!(
verify_delegation_lease(&tampered, &kp.public_key(), None, None)
.unwrap()
.is_none()
);
}
#[test]
fn lease_sub_grant_attenuation() {
let parent_seed = [32u8; 32];
let child_seed = [33u8; 32];
let parent_kp = Ed25519KeyPair::from_seed(&parent_seed);
let child_kp = Ed25519KeyPair::from_seed(&child_seed);
let parent_scope = json!({
"maxForceN": 80.0, "maxSpeedMps": 2.0,
"allowedZones": ["warehouse-a", "dock-3"]
});
let narrower = build_delegation_lease(
&child_seed,
&BuildDelegationLease {
issuer_did: "did:web:integrator.example.com".into(),
robot_did: "did:web:robot.example.com".into(),
lease_id: "lease-2".into(),
scope: json!({
"maxForceN": 50.0, "maxSpeedMps": 1.0,
"allowedZones": ["warehouse-a"]
}),
parent_lease_id: Some("lease-1".into()),
valid_from: "2026-01-01T00:00:00Z".into(),
valid_until: "2026-01-01T01:00:00Z".into(),
},
)
.unwrap();
assert!(verify_delegation_lease(
&narrower,
&child_kp.public_key(),
None,
Some(&parent_scope),
)
.unwrap()
.is_some());
let wider = build_delegation_lease(
&parent_seed,
&BuildDelegationLease {
issuer_did: "did:web:integrator.example.com".into(),
robot_did: "did:web:robot.example.com".into(),
lease_id: "lease-3".into(),
scope: json!({
"maxForceN": 200.0, "maxSpeedMps": 1.0,
"allowedZones": ["warehouse-a"]
}),
parent_lease_id: Some("lease-1".into()),
valid_from: "2026-01-01T00:00:00Z".into(),
valid_until: "2026-01-01T01:00:00Z".into(),
},
)
.unwrap();
assert!(verify_delegation_lease(
&wider,
&parent_kp.public_key(),
None,
Some(&parent_scope),
)
.unwrap()
.is_none());
}
#[test]
fn quorum_threshold_and_distinct_counting() {
let s1 = [41u8; 32];
let s2 = [42u8; 32];
let k1 = Ed25519KeyPair::from_seed(&s1);
let k2 = Ed25519KeyPair::from_seed(&s2);
let a1 = "did:web:approver-1.example.com";
let a2 = "did:web:approver-2.example.com";
let robot = "did:web:robot.example.com";
let action = "action-x";
let build = |seed: &[u8], did: &str, decision: &str| {
build_action_approval(
seed,
&BuildActionApproval {
approver_did: did.into(),
action_id: action.into(),
robot_did: robot.into(),
decision: decision.into(),
valid_from: "2026-01-01T00:00:00Z".into(),
valid_until: None,
},
)
.unwrap()
};
let ap1 = build(&s1, a1, APPROVE);
let ap2 = build(&s2, a2, APPROVE);
let keys = vec![
ApproverKey {
did: a1.into(),
public_key: k1.public_key().to_vec(),
},
ApproverKey {
did: a2.into(),
public_key: k2.public_key().to_vec(),
},
];
let (ok, approvers) =
verify_action_authorization(&[ap1.clone(), ap2], action, robot, &keys, 2, None, None)
.unwrap();
assert!(ok);
assert_eq!(approvers, vec![a1.to_string(), a2.to_string()]);
let dup = build(&s1, a1, APPROVE);
let (ok2, approvers2) =
verify_action_authorization(&[ap1.clone(), dup], action, robot, &keys, 2, None, None)
.unwrap();
assert!(!ok2);
assert_eq!(approvers2, vec![a1.to_string()]);
let (ok3, _) =
verify_action_authorization(&[ap1], action, robot, &keys, 2, None, None).unwrap();
assert!(!ok3);
let rej = build(&s1, a1, REJECT);
let approve2 = build(&s2, a2, APPROVE);
let (ok4, approvers4) =
verify_action_authorization(&[rej, approve2], action, robot, &keys, 2, None, None)
.unwrap();
assert!(!ok4);
assert_eq!(approvers4, vec![a2.to_string()]);
}
#[test]
fn verifies_python_ownership_transfer_interop_vector() {
let vector = load_vector();
let owner_pub = jwk_pub(&vector["ownership_transfer_owner_key"]);
let cred = vector["ownership_transfer_credential"].clone();
let subject = verify_ownership_transfer(&cred, &owner_pub)
.unwrap()
.expect("the Python ownership-transfer interop vector must verify in Rust");
assert_eq!(subject["toOwner"], json!("did:web:owner-b.example.com"));
}
#[test]
fn verifies_python_key_rotation_interop_vector() {
let vector = load_vector();
let robot_pub = jwk_pub(&vector["robot_public_key_jwk"]);
let cred = vector["key_rotation_credential"].clone();
let subject = verify_key_rotation(&cred, &robot_pub)
.unwrap()
.expect("the Python key-rotation interop vector must verify in Rust");
assert_eq!(
subject["newKey"],
json!("z6MkmtWtY63GQVBrpMyRJWEzsnxfsGkemu6CtMDwGTv4RYj2")
);
}
#[test]
fn verifies_python_decommission_interop_vector() {
let vector = load_vector();
let authority_pub = jwk_pub(&vector["decommission_authority_key"]);
let cred = vector["decommission_credential"].clone();
let subject = verify_decommission(&cred, &authority_pub, None)
.unwrap()
.expect("the Python decommission interop vector must verify in Rust");
assert_eq!(subject["reason"], json!("end of service life"));
assert_eq!(subject["finalDisposition"], json!("recycled"));
}
#[test]
fn ownership_transfer_roundtrip_and_issuer_rule() {
let owner_a_seed = [51u8; 32];
let owner_b_seed = [52u8; 32];
let a = Ed25519KeyPair::from_seed(&owner_a_seed);
let b = Ed25519KeyPair::from_seed(&owner_b_seed);
let owner_a = "did:web:owner-a.example.com";
let owner_b = "did:web:owner-b.example.com";
let robot = "did:web:robot.example.com";
let transfer = build_ownership_transfer(
&owner_a_seed,
&BuildOwnershipTransfer {
issuer_did: owner_a.into(),
robot_did: robot.into(),
to_owner: owner_b.into(),
from_owner: None,
prev_transfer_id: None,
valid_from: "2026-01-01T00:00:00Z".into(),
},
)
.unwrap();
let subject = verify_ownership_transfer(&transfer, &a.public_key())
.unwrap()
.expect("transfer verifies under the current owner key");
assert_eq!(subject["fromOwner"], json!(owner_a));
assert_eq!(subject["toOwner"], json!(owner_b));
let mut wrong = transfer.clone();
wrong["type"] = json!(["VerifiableCredential"]);
assert!(verify_ownership_transfer(&wrong, &a.public_key())
.unwrap()
.is_none());
let forged = build_ownership_transfer(
&owner_b_seed,
&BuildOwnershipTransfer {
issuer_did: owner_b.into(),
robot_did: robot.into(),
to_owner: "did:web:owner-c.example.com".into(),
from_owner: Some(owner_a.into()),
prev_transfer_id: None,
valid_from: "2026-01-01T00:00:00Z".into(),
},
)
.unwrap();
assert!(verify_ownership_transfer(&forged, &b.public_key())
.unwrap()
.is_none());
}
#[test]
fn custody_chain_links_and_rejects() {
let owner_a_seed = [51u8; 32];
let owner_b_seed = [52u8; 32];
let owner_c_seed = [53u8; 32];
let a = Ed25519KeyPair::from_seed(&owner_a_seed);
let b = Ed25519KeyPair::from_seed(&owner_b_seed);
let c = Ed25519KeyPair::from_seed(&owner_c_seed);
let owner_a = "did:web:owner-a.example.com";
let owner_b = "did:web:owner-b.example.com";
let owner_c = "did:web:owner-c.example.com";
let robot = "did:web:robot.example.com";
let t1 = build_ownership_transfer(
&owner_a_seed,
&BuildOwnershipTransfer {
issuer_did: owner_a.into(),
robot_did: robot.into(),
to_owner: owner_b.into(),
from_owner: None,
prev_transfer_id: None,
valid_from: "2026-01-01T00:00:00Z".into(),
},
)
.unwrap();
let t2 = build_ownership_transfer(
&owner_b_seed,
&BuildOwnershipTransfer {
issuer_did: owner_b.into(),
robot_did: robot.into(),
to_owner: owner_c.into(),
from_owner: None,
prev_transfer_id: Some("transfer-1".into()),
valid_from: "2026-02-01T00:00:00Z".into(),
},
)
.unwrap();
let keys = vec![
OwnerKey {
did: owner_a.into(),
public_key: a.public_key().to_vec(),
},
OwnerKey {
did: owner_b.into(),
public_key: b.public_key().to_vec(),
},
OwnerKey {
did: owner_c.into(),
public_key: c.public_key().to_vec(),
},
];
let (ok, current) =
verify_custody_chain(&[t1.clone(), t2.clone()], &keys, Some(owner_a)).unwrap();
assert!(ok);
assert_eq!(current.as_deref(), Some(owner_c));
let (bad_origin, _) =
verify_custody_chain(&[t1.clone(), t2.clone()], &keys, Some(owner_c)).unwrap();
assert!(!bad_origin);
let (gap, _) =
verify_custody_chain(std::slice::from_ref(&t2), &keys, Some(owner_a)).unwrap();
assert!(!gap);
let partial = vec![OwnerKey {
did: owner_a.into(),
public_key: a.public_key().to_vec(),
}];
let (no_key, _) = verify_custody_chain(&[t1, t2], &partial, Some(owner_a)).unwrap();
assert!(!no_key);
}
#[test]
fn key_rotation_roundtrip_and_history() {
let k1_seed = [61u8; 32];
let k2_seed = [62u8; 32];
let k3_seed = [63u8; 32];
let k1 = Ed25519KeyPair::from_seed(&k1_seed);
let k2 = Ed25519KeyPair::from_seed(&k2_seed);
let k3 = Ed25519KeyPair::from_seed(&k3_seed);
let robot = "did:web:robot.example.com";
let r1 = build_key_rotation(
&k1_seed,
&BuildKeyRotation {
robot_did: robot.into(),
new_key_multibase: k2.public_multikey(),
reason: Some("scheduled rotation".into()),
valid_from: "2026-01-01T00:00:00Z".into(),
},
)
.unwrap();
let subject = verify_key_rotation(&r1, &k1.public_key())
.unwrap()
.expect("rotation verifies under the old key");
assert_eq!(subject["previousKey"], json!(k1.public_multikey()));
assert_eq!(subject["newKey"], json!(k2.public_multikey()));
assert!(verify_key_rotation(&r1, &k2.public_key())
.unwrap()
.is_none());
let mut wrong = r1.clone();
wrong["type"] = json!(["VerifiableCredential"]);
assert!(verify_key_rotation(&wrong, &k1.public_key())
.unwrap()
.is_none());
let r2 = build_key_rotation(
&k2_seed,
&BuildKeyRotation {
robot_did: robot.into(),
new_key_multibase: k3.public_multikey(),
reason: None,
valid_from: "2026-02-01T00:00:00Z".into(),
},
)
.unwrap();
let public_keys = vec![
KeyEntry {
multibase: k1.public_multikey(),
public_key: k1.public_key().to_vec(),
},
KeyEntry {
multibase: k2.public_multikey(),
public_key: k2.public_key().to_vec(),
},
KeyEntry {
multibase: k3.public_multikey(),
public_key: k3.public_key().to_vec(),
},
];
let (ok, current) = verify_key_history(
&[r1.clone(), r2.clone()],
&k1.public_multikey(),
&public_keys,
)
.unwrap();
assert!(ok);
assert_eq!(current.as_deref(), Some(k3.public_multikey().as_str()));
let (bad_origin, _) =
verify_key_history(&[r1, r2], &k3.public_multikey(), &public_keys).unwrap();
assert!(!bad_origin);
}
#[test]
fn decommission_roundtrip_and_trusted_authority() {
let seed = [71u8; 32];
let kp = Ed25519KeyPair::from_seed(&seed);
let authority = "did:web:authority.example.com";
let robot = "did:web:robot.example.com";
let cred = build_decommission(
&seed,
&BuildDecommission {
issuer_did: authority.into(),
robot_did: robot.into(),
reason: "end of service life".into(),
final_disposition: Some("recycled".into()),
valid_from: "2026-01-01T00:00:00Z".into(),
valid_until: None,
},
)
.unwrap();
let mut trusted = HashSet::new();
trusted.insert(authority.to_string());
let subject = verify_decommission(&cred, &kp.public_key(), Some(&trusted))
.unwrap()
.expect("a trusted authority decommission verifies");
assert_eq!(subject["decommissionedBy"], json!(authority));
let mut other = HashSet::new();
other.insert("did:web:someone-else.example.com".to_string());
assert!(verify_decommission(&cred, &kp.public_key(), Some(&other))
.unwrap()
.is_none());
assert!(verify_decommission(&cred, &kp.public_key(), None)
.unwrap()
.is_some());
let mut wrong = cred.clone();
wrong["type"] = json!(["VerifiableCredential"]);
assert!(verify_decommission(&wrong, &kp.public_key(), None)
.unwrap()
.is_none());
assert!(build_decommission(
&seed,
&BuildDecommission {
issuer_did: authority.into(),
robot_did: robot.into(),
reason: String::new(),
final_disposition: None,
valid_from: "2026-01-01T00:00:00Z".into(),
valid_until: None,
},
)
.is_err());
}
#[test]
fn conformance_matches_interop_vector() {
let vector = load_vector();
let credentials: Vec<Value> = vector["conformance_credentials"]
.as_array()
.expect("conformance_credentials present")
.clone();
let profile_id = vector["conformance_profile_id"].as_str().unwrap();
let report = check_conformance(&credentials, profile_id).unwrap();
assert_eq!(report, vector["expected_conformance_report"]);
let digest = report_digest(&report);
assert_eq!(
digest,
vector["expected_conformance_report_digest"]
.as_str()
.unwrap()
);
}
#[test]
fn conformance_unknown_profile_and_missing_field() {
assert!(check_conformance(&[], "no-such-profile").is_err());
let empty = check_conformance(&[], "eu-ai-act-high-risk").unwrap();
assert_eq!(empty["conforms"], json!(false));
assert_eq!(empty["satisfiedCount"], json!(0));
assert_eq!(empty["totalCount"], json!(4));
let creds = vec![json!({
"type": ["VerifiableCredential", "RobotSafetyRecordCredential"],
"credentialSubject": { "id": "did:web:robot.example.com", "totalEvents": 2 }
})];
let report = check_conformance(&creds, "eu-ai-act-high-risk").unwrap();
let record_keeping = report["requirements"]
.as_array()
.unwrap()
.iter()
.find(|r| r["id"] == json!("eu-aia-record-keeping"))
.unwrap();
assert_eq!(record_keeping["satisfied"], json!(false));
let zone_creds = vec![json!({
"type": ["VerifiableCredential", "PhysicalCapabilityScope"],
"credentialSubject": {
"id": "did:web:robot.example.com",
"physicalScope": { "maxSpeedMps": 1.5, "allowedZones": [] }
}
})];
let ul = check_conformance(&zone_creds, "ul-3300").unwrap();
let limits = ul["requirements"]
.as_array()
.unwrap()
.iter()
.find(|r| r["id"] == json!("ul3300-operating-limits"))
.unwrap();
assert_eq!(limits["satisfied"], json!(false));
}
#[test]
fn conformance_attestation_roundtrip_and_rejections() {
let seed = [77u8; 32];
let kp = Ed25519KeyPair::from_seed(&seed);
let vector = load_vector();
let credentials: Vec<Value> = vector["conformance_credentials"]
.as_array()
.unwrap()
.clone();
let report = check_conformance(&credentials, "eu-ai-act-high-risk").unwrap();
let att = build_conformance_attestation(
&seed,
&BuildConformanceAttestation {
issuer_did: "did:web:authority.example.com".into(),
robot_did: "did:web:robot.example.com".into(),
report: report.clone(),
valid_from: "2026-01-01T00:00:00Z".into(),
valid_until: Some("2027-01-01T00:00:00Z".into()),
},
)
.unwrap();
let subject = verify_conformance_attestation(&att, &kp.public_key())
.unwrap()
.expect("a valid conformance attestation verifies");
assert_eq!(subject["profileId"], json!("eu-ai-act-high-risk"));
assert_eq!(subject["conforms"], json!(true));
assert_eq!(
subject["reportDigest"],
vector["expected_conformance_report_digest"]
);
let wrong_kp = Ed25519KeyPair::from_seed(&[88u8; 32]);
assert!(verify_conformance_attestation(&att, &wrong_kp.public_key())
.unwrap()
.is_none());
let mut tampered = att.clone();
tampered["credentialSubject"]["report"]["conforms"] = json!(false);
assert!(verify_conformance_attestation(&tampered, &kp.public_key())
.unwrap()
.is_none());
let mut wrong_type = att.clone();
wrong_type["type"] = json!(["VerifiableCredential"]);
assert!(
verify_conformance_attestation(&wrong_type, &kp.public_key())
.unwrap()
.is_none()
);
assert!(build_conformance_attestation(
&seed,
&BuildConformanceAttestation {
issuer_did: "did:web:authority.example.com".into(),
robot_did: String::new(),
report,
valid_from: "2026-01-01T00:00:00Z".into(),
valid_until: None,
},
)
.is_err());
}
fn classical_robot_credential(issuer: &str, seed: &[u8]) -> Value {
let cred = json!({
"@context": [VC_CONTEXT_V2, VOUCH_CONTEXT_V1],
"type": ["VerifiableCredential", ROBOT_IDENTITY_TYPE],
"issuer": issuer,
"validFrom": "2026-01-01T00:00:00Z",
"credentialSubject": {
"id": issuer,
"make": "Acme Robotics",
"model": "AR-7",
"serial": "SN-000123"
}
});
let opts = BuildProofOptions::new(format!("{issuer}#key-1"), "2026-01-01T00:00:00Z");
data_integrity::sign(&cred, seed, &opts).unwrap()
}
#[test]
fn verifies_python_pq_robot_identity_interop_vector() {
let vector = load_vector();
let ed_pub = jwk_pub(&vector["robot_public_key_jwk"]);
let ml_multikey = vector["robot_mldsa44_public_multikey"].as_str().unwrap();
let cred = vector["pq_robot_identity_credential"].clone();
assert!(is_pq(&cred));
let ok = verify_robot_credential(&cred, &ed_pub, Some(ml_multikey.as_bytes()))
.expect("the Python hybrid PQ interop vector must verify in Rust");
assert!(ok);
let resolved = mldsa44_public_from_multikey(ml_multikey).unwrap();
assert!(verify_pq(&cred, &ed_pub, &resolved).unwrap());
}
#[test]
fn pq_sign_verify_roundtrip_and_rejects() {
let issuer = "did:web:robot.example.com";
let seed = [21u8; 32];
let kp = Ed25519KeyPair::from_seed(&seed);
let ml = MlDsa44KeyPair::generate().unwrap();
let classical = classical_robot_credential(issuer, &seed);
assert!(!is_pq(&classical));
let signed = sign_pq(&classical, &seed, &ml, "2026-02-01T00:00:00Z").unwrap();
assert!(is_pq(&signed));
assert!(signed["proof"].is_object());
assert_eq!(signed["proof"]["cryptosuite"], json!(HYBRID_CRYPTOSUITE));
assert!(verify_pq(&signed, &kp.public_key(), &ml.public_key()).unwrap());
assert!(
verify_robot_credential(&signed, &kp.public_key(), Some(&ml.public_key())).unwrap()
);
let ml_mk = multikey::encode_mldsa44_public(&ml.public_key()).unwrap();
assert!(
verify_robot_credential(&signed, &kp.public_key(), Some(ml_mk.as_bytes())).unwrap()
);
let mut tampered = signed.clone();
tampered["credentialSubject"]["model"] = json!("AR-9");
assert!(!verify_pq(&tampered, &kp.public_key(), &ml.public_key()).unwrap());
let other_ed = Ed25519KeyPair::from_seed(&[99u8; 32]);
assert!(!verify_pq(&signed, &other_ed.public_key(), &ml.public_key()).unwrap());
let other_ml = MlDsa44KeyPair::generate().unwrap();
assert!(!verify_pq(&signed, &kp.public_key(), &other_ml.public_key()).unwrap());
assert!(!verify_robot_credential(&signed, &kp.public_key(), None).unwrap());
}
#[test]
fn classical_credential_passes_dual_verify_without_pq_key() {
let issuer = "did:web:robot.example.com";
let seed = [22u8; 32];
let kp = Ed25519KeyPair::from_seed(&seed);
let classical = classical_robot_credential(issuer, &seed);
assert!(!is_pq(&classical));
assert!(verify_robot_credential(&classical, &kp.public_key(), None).unwrap());
let other = Ed25519KeyPair::from_seed(&[7u8; 32]);
assert!(!verify_robot_credential(&classical, &other.public_key(), None).unwrap());
}
#[test]
fn migrate_to_pq_resigns_classical_credential() {
let issuer = "did:web:robot.example.com";
let seed = [23u8; 32];
let kp = Ed25519KeyPair::from_seed(&seed);
let ml = MlDsa44KeyPair::generate().unwrap();
let classical = classical_robot_credential(issuer, &seed);
assert!(verify_robot_credential(&classical, &kp.public_key(), None).unwrap());
let migrated = migrate_to_pq(&classical, &seed, &ml, "2026-03-01T00:00:00Z").unwrap();
assert!(is_pq(&migrated));
assert_eq!(
migrated["credentialSubject"],
classical["credentialSubject"]
);
assert!(
verify_robot_credential(&migrated, &kp.public_key(), Some(&ml.public_key())).unwrap()
);
}
#[test]
fn verifies_python_embodiment_interop_vector() {
let vector = load_vector();
let agent_pub = jwk_pub(&vector["embodiment_agent_key"]);
let chain: Vec<Value> = vector["embodiment_chain"]
.as_array()
.expect("embodiment_chain array")
.clone();
let (ok, current) = verify_continuity_chain(&chain, &agent_pub, None).unwrap();
assert!(ok, "the Python embodiment chain must verify in Rust");
assert_eq!(
current.as_deref(),
Some("did:web:body-b.example.com"),
"the chain ends on body-b"
);
let (no_fork, conflict) = check_no_fork(&chain).unwrap();
assert!(no_fork, "the Python embodiment chain has no fork");
assert!(conflict.is_none());
}
#[test]
fn embodiment_roundtrip_and_issuer_rule() {
let agent_seed = [61u8; 32];
let other_seed = [62u8; 32];
let agent = Ed25519KeyPair::from_seed(&agent_seed);
let other = Ed25519KeyPair::from_seed(&other_seed);
let agent_did = "did:web:agent.example.com";
let body_a = "did:web:body-a.example.com";
let cred = build_embodiment(
&agent_seed,
&BuildEmbodiment {
agent_did: agent_did.into(),
body_did: body_a.into(),
body_hardware_root: "uROOTA".into(),
from_body: None,
embodied_at: "2026-01-01T00:00:00Z".into(),
valid_until: None,
},
)
.unwrap();
let subject = verify_embodiment(&cred, &agent.public_key())
.unwrap()
.expect("embodiment verifies under the agent key");
assert_eq!(subject["body"], json!(body_a));
assert_eq!(subject["bodyHardwareRoot"], json!("uROOTA"));
let forged = {
let mut bare = cred.clone();
bare.as_object_mut().unwrap().remove("proof");
bare["issuer"] = json!("did:web:impostor.example.com");
let opts = BuildProofOptions::new(
"did:web:impostor.example.com#key-1".to_string(),
"2026-01-01T00:00:00Z",
);
data_integrity::sign(&bare, &other_seed, &opts).unwrap()
};
assert!(verify_embodiment(&forged, &other.public_key())
.unwrap()
.is_none());
}
#[test]
fn continuity_chain_links_and_rejects() {
let agent_seed = [61u8; 32];
let stranger_seed = [63u8; 32];
let agent = Ed25519KeyPair::from_seed(&agent_seed);
let stranger = Ed25519KeyPair::from_seed(&stranger_seed);
let agent_did = "did:web:agent.example.com";
let body_a = "did:web:body-a.example.com";
let body_b = "did:web:body-b.example.com";
let e1 = build_embodiment(
&agent_seed,
&BuildEmbodiment {
agent_did: agent_did.into(),
body_did: body_a.into(),
body_hardware_root: "uROOTA".into(),
from_body: None,
embodied_at: "2026-01-01T00:00:00Z".into(),
valid_until: Some("2026-01-01T01:00:00Z".into()),
},
)
.unwrap();
let e2 = build_embodiment(
&agent_seed,
&BuildEmbodiment {
agent_did: agent_did.into(),
body_did: body_b.into(),
body_hardware_root: "uROOTB".into(),
from_body: Some(body_a.into()),
embodied_at: "2026-01-01T01:00:00Z".into(),
valid_until: None,
},
)
.unwrap();
let (ok, current) =
verify_continuity_chain(&[e1.clone(), e2.clone()], &agent.public_key(), None).unwrap();
assert!(ok);
assert_eq!(current.as_deref(), Some(body_b));
let (bad_origin, _) =
verify_continuity_chain(&[e1.clone(), e2.clone()], &agent.public_key(), Some(body_b))
.unwrap();
assert!(!bad_origin);
let broken = build_embodiment(
&agent_seed,
&BuildEmbodiment {
agent_did: agent_did.into(),
body_did: body_b.into(),
body_hardware_root: "uROOTB".into(),
from_body: Some("did:web:body-x.example.com".into()),
embodied_at: "2026-01-01T01:00:00Z".into(),
valid_until: None,
},
)
.unwrap();
let (bad_link, _) =
verify_continuity_chain(&[e1.clone(), broken], &agent.public_key(), None).unwrap();
assert!(!bad_link);
let e2_stranger = build_embodiment(
&stranger_seed,
&BuildEmbodiment {
agent_did: agent_did.into(),
body_did: body_b.into(),
body_hardware_root: "uROOTB".into(),
from_body: Some(body_a.into()),
embodied_at: "2026-01-01T01:00:00Z".into(),
valid_until: None,
},
)
.unwrap();
assert!(verify_embodiment(&e2_stranger, &stranger.public_key())
.unwrap()
.is_some());
let (mixed_keys, _) =
verify_continuity_chain(&[e1, e2_stranger], &agent.public_key(), None).unwrap();
assert!(!mixed_keys);
}
#[test]
fn fork_detection_flags_overlapping_bodies() {
let agent_seed = [61u8; 32];
let agent_did = "did:web:agent.example.com";
let body_a = "did:web:body-a.example.com";
let body_b = "did:web:body-b.example.com";
let build = |body: &str, root: &str, from: &str, until: &str| {
build_embodiment(
&agent_seed,
&BuildEmbodiment {
agent_did: agent_did.into(),
body_did: body.into(),
body_hardware_root: root.into(),
from_body: None,
embodied_at: from.into(),
valid_until: Some(until.into()),
},
)
.unwrap()
};
let clean_a = build(
body_a,
"uROOTA",
"2026-01-01T00:00:00Z",
"2026-01-01T01:00:00Z",
);
let clean_b = build(
body_b,
"uROOTB",
"2026-01-01T01:00:00Z",
"2026-01-01T02:00:00Z",
);
let (clean, conflict) = check_no_fork(&[clean_a, clean_b]).unwrap();
assert!(clean);
assert!(conflict.is_none());
let fork_a = build(
body_a,
"uROOTA",
"2026-01-01T00:00:00Z",
"2026-01-01T02:00:00Z",
);
let fork_b = build(
body_b,
"uROOTB",
"2026-01-01T01:00:00Z",
"2026-01-01T03:00:00Z",
);
let (forked, conflict) = check_no_fork(&[fork_a, fork_b]).unwrap();
assert!(!forked);
let conflict = conflict.expect("a fork names the two conflicting bodies");
assert_eq!(conflict.body_a, body_a);
assert_eq!(conflict.body_b, body_b);
}
fn actor_keys_from_vector(map: &Value) -> Vec<ActorKey> {
map.as_object()
.expect("custody_actor_keys object")
.iter()
.map(|(did, jwk)| ActorKey {
did: did.clone(),
public_key: jwk_pub(jwk),
})
.collect()
}
#[test]
fn verifies_python_custody_interop_vector() {
let vector = load_vector();
let chain: Vec<Value> = vector["custody_chain"]
.as_array()
.expect("custody_chain array")
.clone();
let keys = actor_keys_from_vector(&vector["custody_actor_keys"]);
let origin = vector["custody_origin_actor"].as_str().unwrap();
let (ok, current) = verify_handoff_chain(&chain, &keys, Some(origin)).unwrap();
assert!(ok, "the Python custody chain must verify in Rust");
assert_eq!(
current.as_deref(),
Some("did:web:robot-b.example.com"),
"the chain ends holding at robot-b"
);
let change = locate_condition_change(&chain).expect("the condition changed");
assert_eq!(
change.responsible_holder.as_deref(),
Some("did:web:robot-a.example.com"),
"robot-a held the task while its condition changed"
);
assert_eq!(change.from_condition, "intact");
assert_eq!(change.to_condition, "damaged");
}
#[test]
fn handoff_roundtrip_and_receiver_rule() {
let robot_seed = [71u8; 32];
let other_seed = [72u8; 32];
let robot = Ed25519KeyPair::from_seed(&robot_seed);
let other = Ed25519KeyPair::from_seed(&other_seed);
let worker = "did:web:worker-jane.example.com";
let robot_a = "did:web:robot-a.example.com";
let cred = build_handoff(
&robot_seed,
&BuildHandoff {
task_id: "tote-42".into(),
from_actor: worker.into(),
to_actor: robot_a.into(),
condition: Some("intact".into()),
handoff_at: "2026-01-01T00:00:00Z".into(),
valid_until: None,
},
)
.unwrap();
let subject = verify_handoff(&cred, &robot.public_key())
.unwrap()
.expect("handoff verifies under the receiver key");
assert_eq!(subject["fromActor"], json!(worker));
assert_eq!(subject["toActor"], json!(robot_a));
let forged = {
let mut bare = cred.clone();
bare.as_object_mut().unwrap().remove("proof");
bare["issuer"] = json!("did:web:impostor.example.com");
let opts = BuildProofOptions::new(
"did:web:impostor.example.com#key-1".to_string(),
"2026-01-01T00:00:00Z",
);
data_integrity::sign(&bare, &other_seed, &opts).unwrap()
};
assert!(verify_handoff(&forged, &other.public_key())
.unwrap()
.is_none());
}
#[test]
fn handoff_chain_links_and_rejects() {
let a_seed = [73u8; 32];
let b_seed = [74u8; 32];
let a = Ed25519KeyPair::from_seed(&a_seed);
let b = Ed25519KeyPair::from_seed(&b_seed);
let worker = "did:web:worker-jane.example.com";
let robot_a = "did:web:robot-a.example.com";
let robot_b = "did:web:robot-b.example.com";
let h1 = build_handoff(
&a_seed,
&BuildHandoff {
task_id: "tote-42".into(),
from_actor: worker.into(),
to_actor: robot_a.into(),
condition: Some("intact".into()),
handoff_at: "2026-01-01T00:00:00Z".into(),
valid_until: None,
},
)
.unwrap();
let h2 = build_handoff(
&b_seed,
&BuildHandoff {
task_id: "tote-42".into(),
from_actor: robot_a.into(),
to_actor: robot_b.into(),
condition: Some("damaged".into()),
handoff_at: "2026-01-01T00:10:00Z".into(),
valid_until: None,
},
)
.unwrap();
let keys = vec![
ActorKey {
did: robot_a.into(),
public_key: a.public_key().to_vec(),
},
ActorKey {
did: robot_b.into(),
public_key: b.public_key().to_vec(),
},
];
let (ok, current) =
verify_handoff_chain(&[h1.clone(), h2.clone()], &keys, Some(worker)).unwrap();
assert!(ok);
assert_eq!(current.as_deref(), Some(robot_b));
let (bad_origin, _) =
verify_handoff_chain(&[h1.clone(), h2.clone()], &keys, Some(robot_b)).unwrap();
assert!(!bad_origin);
let broken = build_handoff(
&b_seed,
&BuildHandoff {
task_id: "tote-42".into(),
from_actor: "did:web:robot-x.example.com".into(),
to_actor: robot_b.into(),
condition: Some("damaged".into()),
handoff_at: "2026-01-01T00:10:00Z".into(),
valid_until: None,
},
)
.unwrap();
let (bad_link, _) =
verify_handoff_chain(&[h1.clone(), broken], &keys, Some(worker)).unwrap();
assert!(!bad_link);
let partial = vec![ActorKey {
did: robot_a.into(),
public_key: a.public_key().to_vec(),
}];
let (no_key, _) =
verify_handoff_chain(&[h1.clone(), h2.clone()], &partial, Some(worker)).unwrap();
assert!(!no_key);
assert_eq!(
holder_at(&[h1.clone(), h2.clone()], "2026-01-01T00:05:00Z").as_deref(),
Some(robot_a)
);
assert_eq!(
holder_at(&[h1.clone(), h2.clone()], "2026-01-01T00:15:00Z").as_deref(),
Some(robot_b)
);
let steady = build_handoff(
&b_seed,
&BuildHandoff {
task_id: "tote-42".into(),
from_actor: robot_a.into(),
to_actor: robot_b.into(),
condition: Some("intact".into()),
handoff_at: "2026-01-01T00:10:00Z".into(),
valid_until: None,
},
)
.unwrap();
assert!(locate_condition_change(&[h1, steady]).is_none());
}
#[test]
fn authorizes_python_access_interop_vector() {
let vector = load_vector();
let grant = vector["access_grant_credential"].clone();
let request = vector["access_request_credential"].clone();
let operator_pub = jwk_pub(&vector["access_operator_key"]);
let robot_pub = jwk_pub(&vector["access_robot_key"]);
let res = authorize_access(
&grant,
&request,
&operator_pub,
&robot_pub,
Some("2026-01-01T00:05:00Z"),
)
.unwrap();
assert!(
res.ok,
"the Python grant and request must authorize in Rust"
);
assert!(res.reasons.is_empty());
}
#[test]
fn verifies_python_fused_interop_vector() {
let vector = load_vector();
let robot_pub = jwk_pub(&vector["robot_public_key_jwk"]);
let inputs: Vec<String> = vector["fused_input_frame_hashes"]
.as_array()
.unwrap()
.iter()
.map(|v| v.as_str().unwrap().to_string())
.collect();
assert_eq!(
fusion_inputs_digest(&inputs).unwrap(),
vector["expected_fusion_inputs_digest"].as_str().unwrap()
);
assert_eq!(
hash_fused_output(b"world-model-0"),
vector["expected_fused_output_hash"].as_str().unwrap()
);
let cred = vector["fused_perception_attestation"].clone();
let subject = verify_fused_attestation(&cred, &robot_pub, None)
.unwrap()
.expect("fused attestation verifies");
assert_eq!(subject["fusionMethod"], "occupancy-grid-v1");
assert_eq!(
subject["inputsDigest"].as_str().unwrap(),
vector["expected_fusion_inputs_digest"].as_str().unwrap()
);
}
#[test]
fn verifies_python_wear_interop_vector() {
let vector = load_vector();
let robot_pub = jwk_pub(&vector["robot_public_key_jwk"]);
let chain: Vec<Value> = vector["wear_chain"].as_array().unwrap().clone();
let latest = verify_wear_chain(&chain, &robot_pub)
.unwrap()
.expect("wear chain verifies");
assert_eq!(latest["wearLevel"].as_f64().unwrap(), 0.3);
let scope = vector["wear_input_scope"].clone();
let level = vector["wear_attenuation_level"].as_f64().unwrap();
let narrowed = attenuate_for_wear(&scope, level).unwrap();
assert_eq!(narrowed, vector["expected_attenuated_scope"]);
assert_eq!(narrowed["maxForceN"].as_f64().unwrap(), 60.0);
assert_eq!(narrowed["maxSpeedMps"].as_f64().unwrap(), 1.125);
assert_eq!(narrowed["maxSpeedNearHumansMps"].as_f64().unwrap(), 0.1875);
assert!(attenuates(&scope, &narrowed));
}
}