shared_protocol.rs (7886B)
1 //! Prospective wire assignments, without authoring or qualification authority. 2 3 use radroots_protocol::profile; 4 use serde::Deserialize; 5 use std::path::Path; 6 7 const CONTRACT: &str = "contracts/architecture/decisions/shared_sdk_protocol.v1.json"; 8 const MAX_BYTES: u64 = 8192; 9 const INVALID: &str = "shared protocol assignment contract is invalid"; 10 11 #[derive(Deserialize)] 12 #[serde(deny_unknown_fields)] 13 struct Decision { 14 schema_version: u32, 15 contract_id: String, 16 state: String, 17 profile_version: u32, 18 commerce: Assignment, 19 capabilities: Assignment, 20 structured_authoring_qualified: bool, 21 collision_review: CollisionReview, 22 rules: Vec<String>, 23 } 24 25 #[derive(Deserialize)] 26 #[serde(deny_unknown_fields)] 27 struct Assignment { 28 kind: u16, 29 class: String, 30 profile: String, 31 } 32 33 #[derive(Deserialize)] 34 #[serde(deny_unknown_fields)] 35 struct CollisionReview { 36 reviewed_at: String, 37 nips_repository: String, 38 nips_revision: String, 39 nips_readme_sha256: String, 40 registry_repository: String, 41 registry_revision: String, 42 registry_schema_sha256: String, 43 registry_numeric_entries: usize, 44 nips_source_files_scanned: usize, 45 known_collisions: Vec<u16>, 46 coordination: String, 47 proposal_search: String, 48 limits: String, 49 } 50 51 pub(crate) fn validate_contract(root: &Path) -> Result<(), String> { 52 let bytes = crate::safe_artifact_io::read_regular(root, Path::new(CONTRACT), MAX_BYTES) 53 .map_err(|_| INVALID.to_string())?; 54 validate_bytes(&bytes).map_err(str::to_string) 55 } 56 57 fn validate_bytes(bytes: &[u8]) -> Result<(), &'static str> { 58 let shape: serde_json::Value = serde_json::from_slice(bytes).map_err(|_| INVALID)?; 59 if !shape.is_object() 60 || ["commerce", "capabilities", "collision_review"] 61 .iter() 62 .any(|key| !shape[key].is_object()) 63 { 64 return Err(INVALID); 65 } 66 // The independent typed pass retains duplicate, required-field and closure checks. 67 let value: Decision = serde_json::from_slice(bytes).map_err(|_| INVALID)?; 68 let review = &value.collision_review; 69 let exact = value.schema_version == 1 70 && value.contract_id == "radroots.shared_sdk.protocol.v1" 71 && value.state == "assigned_implementation_and_qualification_pending" 72 && value.profile_version == profile::PROFILE_VERSION 73 && value.commerce.kind == profile::COMMERCE_KIND 74 && (1000..10000).contains(&value.commerce.kind) 75 && value.commerce.class == "regular" 76 && value.commerce.profile == profile::COMMERCE_PROFILE 77 && value.capabilities.kind == profile::CAPABILITIES_KIND 78 && (10000..20000).contains(&value.capabilities.kind) 79 && value.capabilities.class == "replaceable" 80 && value.capabilities.profile == profile::CAPABILITIES_PROFILE 81 && !value.structured_authoring_qualified 82 && review.reviewed_at == "2026-10-22" 83 && review.nips_repository == "https://github.com/nostr-protocol/nips" 84 && review.nips_revision == "d47a2ec8aacb9d01ac00e6294ae77582f483ff68" 85 && review.nips_readme_sha256 86 == "6db76d7a87f9f0997fe10f323d68fdde38fe5a2505b0679172703a9d6ce6c692" 87 && review.registry_repository == "https://github.com/nostr-protocol/registry-of-kinds" 88 && review.registry_revision == "2ae5c82dc82e0d1e62dc23f9895e5ef1d4a922de" 89 && review.registry_schema_sha256 90 == "560821b6dff52a4faf7b736aeca958011a0f7a3070aae7da717e47ba79e21445" 91 && review.registry_numeric_entries == 281 92 && review.nips_source_files_scanned == 101 93 && review.known_collisions.is_empty() 94 && !review.coordination.is_empty() 95 && !review.proposal_search.is_empty() 96 && !review.limits.is_empty() 97 && value.rules.len() == 9 98 && value.rules.iter().all(|rule| !rule.is_empty()); 99 if exact { Ok(()) } else { Err(INVALID) } 100 } 101 102 #[cfg(test)] 103 mod tests { 104 use super::*; 105 use serde_json::{Value, json}; 106 107 const SOURCE: &[u8] = 108 include_bytes!("../../../contracts/architecture/decisions/shared_sdk_protocol.v1.json"); 109 110 #[test] 111 fn assignments_preserve_classes_profiles_and_unqualified_authoring() { 112 validate_bytes(SOURCE).unwrap(); 113 let root = Path::new(env!("CARGO_MANIFEST_DIR")) 114 .parent() 115 .unwrap() 116 .parent() 117 .unwrap(); 118 validate_contract(root).unwrap(); 119 for (pointer, replacement) in [ 120 ("/commerce/kind", json!(14)), 121 ("/commerce/kind", json!(65536)), 122 ("/commerce/kind", json!(18890)), 123 ("/capabilities/kind", json!(38990)), 124 ("/capabilities/class", json!("addressable")), 125 ("/commerce/profile", json!("radroots.capabilities")), 126 ("/profile_version", json!(2)), 127 ("/structured_authoring_qualified", json!(true)), 128 ("/collision_review/known_collisions", json!([8890])), 129 ("/collision_review/registry_schema_sha256", json!("changed")), 130 ("/rules", json!([])), 131 ] { 132 let mut value: Value = serde_json::from_slice(SOURCE).unwrap(); 133 *value.pointer_mut(pointer).unwrap() = replacement; 134 assert_eq!( 135 validate_bytes(&serde_json::to_vec(&value).unwrap()), 136 Err(INVALID) 137 ); 138 } 139 } 140 141 #[test] 142 fn contract_rejects_duplicate_extra_missing_and_oversized_inputs() { 143 for (pointer, keys) in [ 144 ( 145 "", 146 &[ 147 "schema_version", 148 "contract_id", 149 "state", 150 "profile_version", 151 "commerce", 152 "capabilities", 153 "structured_authoring_qualified", 154 "collision_review", 155 "rules", 156 ][..], 157 ), 158 ("/commerce", &["kind", "class", "profile"][..]), 159 ("/capabilities", &["kind", "class", "profile"][..]), 160 ( 161 "/collision_review", 162 &[ 163 "reviewed_at", 164 "nips_repository", 165 "nips_revision", 166 "nips_readme_sha256", 167 "registry_repository", 168 "registry_revision", 169 "registry_schema_sha256", 170 "registry_numeric_entries", 171 "nips_source_files_scanned", 172 "known_collisions", 173 "coordination", 174 "proposal_search", 175 "limits", 176 ][..], 177 ), 178 ] { 179 let mut value: Value = serde_json::from_slice(SOURCE).unwrap(); 180 let array = Value::Array( 181 keys.iter() 182 .map(|key| value.pointer(pointer).unwrap()[key].clone()) 183 .collect(), 184 ); 185 *value.pointer_mut(pointer).unwrap() = array; 186 assert_eq!( 187 validate_bytes(&serde_json::to_vec(&value).unwrap()), 188 Err(INVALID) 189 ); 190 } 191 for bytes in [ 192 [b"{\"schema_version\":1,".as_slice(), &SOURCE[1..]].concat(), 193 [b"{\"unexpected\":true,".as_slice(), &SOURCE[1..]].concat(), 194 b"{}".to_vec(), 195 b"[]".to_vec(), 196 ] { 197 assert_eq!(validate_bytes(&bytes), Err(INVALID)); 198 } 199 let root = tempfile::tempdir().unwrap(); 200 assert!(validate_contract(root.path()).is_err()); 201 let path = root.path().join(CONTRACT); 202 std::fs::create_dir_all(path.parent().unwrap()).unwrap(); 203 let mut bytes = SOURCE.to_vec(); 204 bytes.resize(MAX_BYTES as usize + 1, b' '); 205 std::fs::write(path, bytes).unwrap(); 206 assert!(validate_contract(root.path()).is_err()); 207 } 208 }