Skip to main content

agent_toolprint/
chain.rs

1//! Receipt chaining — parent.id must equal child.parent.
2
3use serde_json::Value;
4
5use crate::envelope::envelope_payload_bytes;
6use crate::types::validate_receipt;
7
8pub fn chain(parent: &Value, child: &Value) -> bool {
9    let p_bytes = match envelope_payload_bytes(parent) {
10        Ok(b) => b,
11        Err(_) => return false,
12    };
13    let c_bytes = match envelope_payload_bytes(child) {
14        Ok(b) => b,
15        Err(_) => return false,
16    };
17    let p: Value = match serde_json::from_slice(&p_bytes) {
18        Ok(v) => v,
19        Err(_) => return false,
20    };
21    let c: Value = match serde_json::from_slice(&c_bytes) {
22        Ok(v) => v,
23        Err(_) => return false,
24    };
25    if validate_receipt(&p).is_err() || validate_receipt(&c).is_err() {
26        return false;
27    }
28    c.get("parent").and_then(Value::as_str) == p.get("id").and_then(Value::as_str)
29}