Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/interplane-mv/blob/main/provenance/src/binding.rs # interplane-mv INTERPLANE: open interoperability protocol between AI models and agent runtimes. Models express intent. Runtimes retain authority. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `interplane-mv / provenance / src / binding.rs` 155 lines ยท 5913 bytes Raw Blame History 1 //! The versioned byte forms the effect binding hashes. Producer (aien-cli, the daemon) and 2 //! verifier share these definitions; `provenance/BINDING.md` states them in prose. 3 //! 4 //! Nothing here is JCS. Each form below is named and fixed, and each function writes the exact 5 //! bytes the producer hashes. A form this module cannot reproduce byte for byte is refused 6 //! (`Err`), never approximated. 7 //! 8 //! Provenance is evidence, never authority: these functions compute digests and compare. They 9 //! decide and grant nothing. 10 11 use serde_json::Value; 12 13 /// Effect binding over the daemon-written ledger records (strong). 14 pub const LEDGER_BINDING: &str = "aien-ledger-slice/1"; 15 /// Effect binding over aien-cli's loose `record_effect_receipt` v1 file (weaker). 16 pub const RECEIPT_BINDING: &str = "record_effect_receipt/1"; 17 /// The daemon's approval binding version (sovereign-core `APPROVAL_BINDING_VERSION`). 18 pub const APPROVAL_BINDING: &str = "aien.approval.v2"; 19 /// The digest form aien-cli's `record_effect_receipt` v1 uses: `serde_json::to_vec` on a map 20 /// whose keys iterate in sorted (UTF-8 byte) order. 21 pub const SERDE_DIGEST_FORM: &str = "serde_json.to_vec.sorted-keys/1"; 22 23 /// RFC 8259 string with only the mandatory escapes: `"` and `\` get a backslash; U+0008, U+000C, 24 /// U+000A, U+000D, U+0009 use `\b \f \n \r \t`; every other control character below U+0020 is 25 /// `\u00xx` (lowercase hex). Everything else, including U+007F, U+2028 and all non-ASCII, is 26 /// written as UTF-8 unchanged. This is what `serde_json` writes. 27 pub fn json_string(s: &str) -> String { 28 let mut o = String::with_capacity(s.len() + 2); 29 o.push('"'); 30 for c in s.chars() { 31 match c { 32 '"' => o.push_str("\\\""), 33 '\\' => o.push_str("\\\\"), 34 '\u{8}' => o.push_str("\\b"), 35 '\u{c}' => o.push_str("\\f"), 36 '\n' => o.push_str("\\n"), 37 '\r' => o.push_str("\\r"), 38 '\t' => o.push_str("\\t"), 39 c if (c as u32) < 0x20 => o.push_str(&format!("\\u{:04x}", c as u32)), 40 c => o.push(c), 41 } 42 } 43 o.push('"'); 44 o 45 } 46 47 /// Form `serde_json.to_vec.sorted-keys/1`: compact JSON, object keys in UTF-8 byte order, no 48 /// whitespace, integers in decimal, strings as [`json_string`]. A number that is not an integer 49 /// is refused: the producer prints it with the shortest round-trip float algorithm, which this 50 /// verifier does not reimplement, so it cannot promise the same bytes. 51 pub fn compact_sorted(v: &Value) -> Result, String> { 52 let mut o = String::new(); 53 write_sorted(v, &mut o)?; 54 Ok(o.into_bytes()) 55 } 56 57 fn write_sorted(v: &Value, o: &mut String) -> Result<(), String> { 58 match v { 59 Value::Null => o.push_str("null"), 60 Value::Bool(b) => o.push_str(if *b { "true" } else { "false" }), 61 Value::Number(n) => { 62 if let Some(i) = n.as_i64() { 63 o.push_str(&i.to_string()); 64 } else if let Some(u) = n.as_u64() { 65 o.push_str(&u.to_string()); 66 } else { 67 return Err(format!("non-integer number {n}")); 68 } 69 } 70 Value::String(s) => o.push_str(&json_string(s)), 71 Value::Array(a) => { 72 o.push('['); 73 for (i, x) in a.iter().enumerate() { 74 if i > 0 { 75 o.push(','); 76 } 77 write_sorted(x, o)?; 78 } 79 o.push(']'); 80 } 81 Value::Object(m) => { 82 let mut keys: Vec<&String> = m.keys().collect(); 83 keys.sort_by(|a, b| a.as_bytes().cmp(b.as_bytes())); 84 o.push('{'); 85 for (i, k) in keys.iter().enumerate() { 86 if i > 0 { 87 o.push(','); 88 } 89 o.push_str(&json_string(k)); 90 o.push(':'); 91 write_sorted(&m[*k], o)?; 92 } 93 o.push('}'); 94 } 95 } 96 Ok(()) 97 } 98 99 /// The nine fields one approval covers, plus nothing else. 100 pub struct ApprovalFields<'a> { 101 pub approval_id: &'a str, 102 pub approved_proposal_sha256: &'a str, 103 pub approver: &'a str, 104 pub content_sha256: &'a str, 105 pub desk_key_id: &'a str, 106 pub path: &'a str, 107 pub request_id: &'a str, 108 pub trace_id: &'a str, 109 pub workspace: &'a str, 110 } 111 112 /// The exact bytes the daemon hashes into the approval key (`aien.approval.v2`): one compact 113 /// JSON object, these ten keys in exactly this order (their UTF-8 byte order; `v` sorts before 114 /// `workspace`), every value a [`json_string`], `v` = "aien.approval.v2". The approval key is the SHA-256 of 115 /// these bytes. 116 pub fn approval_binding_bytes(f: &ApprovalFields<'_>) -> Vec { 117 let mut o = String::from("{"); 118 for (k, v) in [ 119 ("approval_id", f.approval_id), 120 ("approved_proposal_sha256", f.approved_proposal_sha256), 121 ("approver", f.approver), 122 ("content_sha256", f.content_sha256), 123 ("desk_key_id", f.desk_key_id), 124 ("path", f.path), 125 ("request_id", f.request_id), 126 ("trace_id", f.trace_id), 127 ("v", APPROVAL_BINDING), 128 ("workspace", f.workspace), 129 ] { 130 if o.len() > 1 { 131 o.push(','); 132 } 133 o.push_str(&json_string(k)); 134 o.push(':'); 135 o.push_str(&json_string(v)); 136 } 137 o.push('}'); 138 o.into_bytes() 139 } 140 141 /// `approved_proposal_sha256` input: the compact object `{"content":..,"path":..}` (those two 142 /// keys, in that order). 143 pub fn approved_proposal_bytes(path: &str, content: &str) -> Vec { 144 format!( 145 "{{\"content\":{},\"path\":{}}}", 146 json_string(content), 147 json_string(path) 148 ) 149 .into_bytes() 150 } 151 152 /// `compose_proposal_sha256` input: the proposal text `filename: \n`. 153 pub fn compose_proposal_bytes(path: &str, content: &str) -> Vec { 154 format!("filename: {path}\n{content}").into_bytes() 155 }