currency_registry.rs (11186B)
1 use std::{collections::BTreeSet, path::Path}; 2 3 use serde::Deserialize; 4 use sha2::{Digest, Sha256}; 5 6 const CONTRACT: &str = "contracts/architecture/decisions/currency_registry.v1.json"; 7 const DATA: &str = "crates/domain/data/currency_registry_v1.csv"; 8 const LICENSE: &str = "crates/domain/data/currency_registry_v1.license"; 9 const NOTICE: &str = "crates/domain/data/currency_registry_v1.notice"; 10 const DATA_SHA256: &str = "60d8282b2d7ddc27929b097d4de9e76a900a64f1de4a5aa7a4cb3308b55b82d0"; 11 const LICENSE_SHA256: &str = "665618a8c4b9ef6ceafb35482a3e010834fae9e9cbb22041b74310bcc52ac500"; 12 const NOTICE_SHA256: &str = "c43ef0d0f9d6f64656ca6a619c3c43cd9cc2b378217ab19a083ca92255a91d7d"; 13 14 #[derive(Deserialize)] 15 #[serde(deny_unknown_fields)] 16 struct Policy { 17 schema_version: u16, 18 contract_id: String, 19 state: String, 20 registry_id: String, 21 implementation: String, 22 upstream: Upstream, 23 data: Data, 24 notices: Notices, 25 independent_crosscheck: Crosscheck, 26 limits: Limits, 27 } 28 29 #[derive(Deserialize)] 30 #[serde(deny_unknown_fields)] 31 struct Upstream { 32 repository: String, 33 revision: String, 34 path: String, 35 sha256: String, 36 license: String, 37 license_sha256: String, 38 copyright: String, 39 } 40 41 #[derive(Deserialize)] 42 #[serde(deny_unknown_fields)] 43 struct Data { 44 path: String, 45 sha256: String, 46 entries: usize, 47 minimum_exponent: u8, 48 maximum_exponent: u8, 49 current_currency_and_funds_only: bool, 50 historic_entries_excluded: bool, 51 no_minor_unit_entries_excluded: bool, 52 guessed_default_exponents_allowed: bool, 53 } 54 55 #[derive(Deserialize)] 56 #[serde(deny_unknown_fields)] 57 struct Notices { 58 license_path: String, 59 license_sha256: String, 60 notice_path: String, 61 notice_sha256: String, 62 source_distribution_required: bool, 63 derived_artifact_inclusion_required_before_adoption: bool, 64 crate_owned_rust_license_unchanged: bool, 65 } 66 67 #[derive(Deserialize)] 68 #[serde(deny_unknown_fields)] 69 struct Crosscheck { 70 authority: String, 71 url: String, 72 publication_date: String, 73 source_sha256: String, 74 all_selected_pairs_equal: bool, 75 official_xml_redistributed: bool, 76 } 77 78 #[derive(Deserialize)] 79 #[serde(deny_unknown_fields)] 80 struct Limits { 81 runtime_io_allowed: bool, 82 automatic_updates_allowed: bool, 83 exchange_or_payment_semantics_allowed: bool, 84 qualified_economics_or_agreement_authoring_claimed: bool, 85 } 86 87 pub(super) fn validate(root: &Path) -> Result<(), String> { 88 let read = |path: &str, max: u64| { 89 crate::safe_artifact_io::read_regular(root, Path::new(path), max) 90 .map_err(|_| "currency registry: bounded regular source admission failed".to_owned()) 91 }; 92 validate_materials( 93 &read(CONTRACT, 16_384)?, 94 &read(DATA, 4_096)?, 95 &read(LICENSE, 16_384)?, 96 &read(NOTICE, 4_096)?, 97 ) 98 } 99 100 fn require(condition: bool, message: &'static str) -> Result<(), String> { 101 if condition { 102 Ok(()) 103 } else { 104 Err(format!("currency registry: {message}")) 105 } 106 } 107 108 fn digest(bytes: &[u8]) -> String { 109 hex::encode(Sha256::digest(bytes)) 110 } 111 112 fn validate_materials( 113 raw: &[u8], 114 data: &[u8], 115 license: &[u8], 116 notice: &[u8], 117 ) -> Result<(), String> { 118 require(raw.len() <= 16_384, "policy exceeds bound")?; 119 let policy: Policy = serde_json::from_slice(raw) 120 .map_err(|_| "currency registry: invalid closed policy".to_owned())?; 121 require( 122 policy.schema_version == 1 123 && policy.contract_id == "radroots.currency_registry.v1" 124 && policy.registry_id == policy.contract_id 125 && policy.state == "pinned_data_source" 126 && policy.implementation == "independent_rust_api_over_minimal_pinned_data", 127 "identity or implementation scope changed", 128 )?; 129 validate_upstream(&policy.upstream, &policy.independent_crosscheck)?; 130 let p = &policy.data; 131 require( 132 p.path == DATA 133 && p.sha256 == DATA_SHA256 134 && p.entries == 165 135 && p.minimum_exponent == 0 136 && p.maximum_exponent == 4 137 && p.current_currency_and_funds_only 138 && p.historic_entries_excluded 139 && p.no_minor_unit_entries_excluded 140 && !p.guessed_default_exponents_allowed, 141 "fixed supported data contract changed", 142 )?; 143 validate_notices(&policy.notices, license, notice)?; 144 require( 145 !policy.limits.runtime_io_allowed 146 && !policy.limits.automatic_updates_allowed 147 && !policy.limits.exchange_or_payment_semantics_allowed 148 && !policy 149 .limits 150 .qualified_economics_or_agreement_authoring_claimed, 151 "metadata cannot imply effects or qualification", 152 )?; 153 require( 154 data.len() <= 4_096 && digest(data) == DATA_SHA256, 155 "pinned data digest mismatch", 156 )?; 157 let text = 158 std::str::from_utf8(data).map_err(|_| "currency registry: invalid data UTF8".to_owned())?; 159 let mut codes = BTreeSet::new(); 160 let mut previous = None; 161 require(text.ends_with('\n'), "unterminated data")?; 162 for line in text.lines().filter(|line| !line.starts_with('#')) { 163 let bytes = line.as_bytes(); 164 require( 165 bytes.len() == 5 166 && bytes[..3].iter().all(u8::is_ascii_uppercase) 167 && bytes[3] == b',' 168 && (b'0'..=b'4').contains(&bytes[4]), 169 "invalid data row", 170 )?; 171 let code = &line[..3]; 172 require( 173 previous.is_none_or(|p| p < code) && codes.insert(code), 174 "duplicate or unsorted code", 175 )?; 176 previous = Some(code); 177 } 178 require(codes.len() == 165, "incomplete fixed registry") 179 } 180 181 fn validate_upstream(upstream: &Upstream, crosscheck: &Crosscheck) -> Result<(), String> { 182 require( 183 upstream.repository == "https://github.com/rust-iso/rust_iso4217" 184 && upstream.revision == "ce6b74546b3645ce11866acc93117128d2f7b1e0" 185 && upstream.path == "src/lib.rs" 186 && upstream.sha256 187 == "7401727ea4da05fcbf0d1d679705c0245989e83dbc697e831066f992875e183d" 188 && upstream.license == "Apache-2.0" 189 && upstream.license_sha256 == LICENSE_SHA256 190 && upstream.copyright == "Copyright 2023-2026 rust_iso4217 contributors", 191 "source or license provenance changed", 192 )?; 193 require( 194 crosscheck.authority == "SIX ISO 4217 Maintenance Agency" 195 && crosscheck.url 196 == "https://www.six-group.com/dam/download/financial-information/data-center/iso-currrency/lists/list-one.xml" 197 && crosscheck.publication_date == "2026-09-17" 198 && crosscheck.source_sha256 199 == "33139b438657d1cee116ba737807ea71d19d6de4b90f799a09c56f0cc6a1b0ff" 200 && crosscheck.all_selected_pairs_equal 201 && !crosscheck.official_xml_redistributed, 202 "independent factual crosscheck scope changed", 203 ) 204 } 205 206 fn validate_notices(policy: &Notices, license: &[u8], notice: &[u8]) -> Result<(), String> { 207 require( 208 policy.license_path == LICENSE 209 && policy.license_sha256 == LICENSE_SHA256 210 && policy.notice_path == NOTICE 211 && policy.notice_sha256 == NOTICE_SHA256 212 && policy.source_distribution_required 213 && policy.derived_artifact_inclusion_required_before_adoption 214 && policy.crate_owned_rust_license_unchanged, 215 "notice distribution requirement changed", 216 )?; 217 require( 218 license.len() <= 16_384 219 && digest(license) == LICENSE_SHA256 220 && notice.len() <= 4_096 221 && digest(notice) == NOTICE_SHA256, 222 "license or notice digest mismatch", 223 ) 224 } 225 226 #[cfg(test)] 227 mod tests { 228 use super::*; 229 use serde_json::json; 230 231 const POLICY: &[u8] = 232 include_bytes!("../../../../../contracts/architecture/decisions/currency_registry.v1.json"); 233 const CSV: &[u8] = include_bytes!("../../../../../crates/domain/data/currency_registry_v1.csv"); 234 const LICENSE_BYTES: &[u8] = 235 include_bytes!("../../../../../crates/domain/data/currency_registry_v1.license"); 236 const NOTICE_BYTES: &[u8] = 237 include_bytes!("../../../../../crates/domain/data/currency_registry_v1.notice"); 238 239 #[test] 240 fn fixed_source_data_license_and_notice_bindings_are_valid() { 241 validate_materials(POLICY, CSV, LICENSE_BYTES, NOTICE_BYTES).unwrap(); 242 } 243 244 #[test] 245 fn forged_self_consistent_hashes_or_stripped_attribution_cannot_replace_registry_v1() { 246 let changed = String::from_utf8(CSV.to_vec()) 247 .unwrap() 248 .replace("CAD,2", "CAD,4"); 249 let mut policy: serde_json::Value = serde_json::from_slice(POLICY).unwrap(); 250 policy["data"]["sha256"] = json!(digest(changed.as_bytes())); 251 let raw = serde_json::to_vec(&policy).unwrap(); 252 assert!(validate_materials(&raw, changed.as_bytes(), LICENSE_BYTES, NOTICE_BYTES).is_err()); 253 assert!( 254 validate_materials(POLICY, changed.as_bytes(), LICENSE_BYTES, NOTICE_BYTES).is_err() 255 ); 256 assert!(validate_materials(POLICY, CSV, b"Apache-2.0", NOTICE_BYTES).is_err()); 257 assert!(validate_materials(POLICY, CSV, LICENSE_BYTES, b"notice").is_err()); 258 } 259 260 #[test] 261 fn closed_policy_rejects_unpinned_defaults_wrong_sources_and_effect_claims() { 262 for (pointer, replacement) in [ 263 ("/upstream/license", json!("MIT")), 264 ("/upstream/revision", json!("master")), 265 ("/data/path", json!("../private.csv")), 266 ("/data/entries", json!(164)), 267 ("/data/guessed_default_exponents_allowed", json!(true)), 268 ( 269 "/independent_crosscheck/official_xml_redistributed", 270 json!(true), 271 ), 272 ( 273 "/notices/derived_artifact_inclusion_required_before_adoption", 274 json!(false), 275 ), 276 ("/limits/automatic_updates_allowed", json!(true)), 277 ( 278 "/limits/qualified_economics_or_agreement_authoring_claimed", 279 json!(true), 280 ), 281 ] { 282 let mut value: serde_json::Value = serde_json::from_slice(POLICY).unwrap(); 283 *value.pointer_mut(pointer).unwrap() = replacement; 284 assert!( 285 validate_materials( 286 &serde_json::to_vec(&value).unwrap(), 287 CSV, 288 LICENSE_BYTES, 289 NOTICE_BYTES 290 ) 291 .is_err(), 292 "{pointer}" 293 ); 294 } 295 let mut value: serde_json::Value = serde_json::from_slice(POLICY).unwrap(); 296 value["unknown"] = json!(true); 297 assert!( 298 validate_materials( 299 &serde_json::to_vec(&value).unwrap(), 300 CSV, 301 LICENSE_BYTES, 302 NOTICE_BYTES 303 ) 304 .is_err() 305 ); 306 assert!(validate_materials(b"{}", CSV, LICENSE_BYTES, NOTICE_BYTES).is_err()); 307 assert!(validate_materials(&vec![b' '; 16_385], CSV, LICENSE_BYTES, NOTICE_BYTES).is_err()); 308 } 309 }