receipt.rs (15286B)
1 use super::{ 2 Admission, ArtifactRole, BusinessInstallation, Delivery, Error, Failure, Issuance, 3 OperationKind, RecoveryRequirement, SatisfactionClass, TargetObservation, Value, 4 WaitTermination, 5 }; 6 use wasm_bindgen::prelude::*; 7 8 /// Exact protected artifact references, without payload or invocation rights. 9 #[derive(Clone)] 10 #[wasm_bindgen] 11 pub struct Artifact { 12 #[wasm_bindgen(readonly)] 13 pub role: ArtifactRole, 14 #[wasm_bindgen(readonly, getter_with_clone)] 15 pub recipient: Option<String>, 16 #[wasm_bindgen(readonly, getter_with_clone)] 17 pub expected_event: Option<String>, 18 #[wasm_bindgen(readonly)] 19 pub issuance: Issuance, 20 #[wasm_bindgen(readonly, getter_with_clone)] 21 pub attempt_id: Option<String>, 22 #[wasm_bindgen(readonly, getter_with_clone)] 23 pub claim_transaction: Option<String>, 24 #[wasm_bindgen(readonly, getter_with_clone)] 25 pub intent_digest: Option<String>, 26 #[wasm_bindgen(readonly, getter_with_clone)] 27 pub capture_transaction: Option<String>, 28 #[wasm_bindgen(readonly, getter_with_clone)] 29 pub obtained_digest: Option<String>, 30 #[wasm_bindgen(readonly, getter_with_clone)] 31 pub publication_transaction: Option<String>, 32 #[wasm_bindgen(readonly, getter_with_clone)] 33 pub observed_at_unix_ms: Option<String>, 34 } 35 #[derive(Clone)] 36 #[wasm_bindgen] 37 pub struct Observation { 38 #[wasm_bindgen(readonly, getter_with_clone)] 39 pub event_id: String, 40 #[wasm_bindgen(readonly, getter_with_clone)] 41 pub recipient: Option<String>, 42 #[wasm_bindgen(readonly, getter_with_clone)] 43 pub target: String, 44 #[wasm_bindgen(readonly, getter_with_clone)] 45 pub request_id: String, 46 #[wasm_bindgen(readonly, getter_with_clone)] 47 pub target_fingerprint: String, 48 #[wasm_bindgen(readonly)] 49 pub target_ordinal: u8, 50 #[wasm_bindgen(readonly)] 51 pub outcome: TargetObservation, 52 #[wasm_bindgen(readonly, getter_with_clone)] 53 pub observed_at_unix_ms: Option<String>, 54 } 55 /// Closed target policy without inventing a default for future variants. 56 #[derive(Clone)] 57 #[wasm_bindgen] 58 pub struct TargetPolicy { 59 #[wasm_bindgen(readonly, getter_with_clone)] 60 pub kind: String, 61 #[wasm_bindgen(readonly)] 62 pub threshold: Option<u16>, 63 #[wasm_bindgen(readonly, getter_with_clone)] 64 pub fingerprints: Vec<String>, 65 } 66 #[derive(Clone)] 67 #[wasm_bindgen] 68 pub struct Target { 69 #[wasm_bindgen(readonly, getter_with_clone)] 70 pub uri: String, 71 #[wasm_bindgen(readonly, getter_with_clone)] 72 pub kind: String, 73 #[wasm_bindgen(readonly, getter_with_clone)] 74 pub fingerprint: String, 75 } 76 #[derive(Clone)] 77 #[wasm_bindgen] 78 pub struct DeliveryAttempt { 79 #[wasm_bindgen(readonly, getter_with_clone)] 80 pub request_id: String, 81 #[wasm_bindgen(readonly, getter_with_clone)] 82 pub event_id: String, 83 #[wasm_bindgen(readonly)] 84 pub role: ArtifactRole, 85 #[wasm_bindgen(readonly, getter_with_clone)] 86 pub recipient: Option<String>, 87 #[wasm_bindgen(readonly, getter_with_clone)] 88 pub targets: Vec<Target>, 89 #[wasm_bindgen(readonly, getter_with_clone)] 90 pub selected_fingerprints: Vec<String>, 91 #[wasm_bindgen(readonly)] 92 pub satisfaction_class: SatisfactionClass, 93 #[wasm_bindgen(readonly, getter_with_clone)] 94 pub policy: TargetPolicy, 95 #[wasm_bindgen(readonly, getter_with_clone)] 96 pub publication_policy_digest: String, 97 #[wasm_bindgen(readonly, getter_with_clone)] 98 pub intent_digest: String, 99 #[wasm_bindgen(readonly, getter_with_clone)] 100 pub original_bytes_digest: String, 101 #[wasm_bindgen(readonly, getter_with_clone)] 102 pub transaction_id: String, 103 #[wasm_bindgen(readonly, getter_with_clone)] 104 pub work_transaction_id: String, 105 #[wasm_bindgen(readonly, getter_with_clone)] 106 pub retained_at: String, 107 #[wasm_bindgen(readonly, getter_with_clone)] 108 pub deadline_unix_ms: String, 109 #[wasm_bindgen(readonly, getter_with_clone)] 110 pub target_ordinals: Vec<u8>, 111 } 112 113 /// Source-projected current facts. This data is never an executable context or 114 /// a new authority to sign, install, dispatch or infer recipient delivery. 115 #[derive(Clone)] 116 #[wasm_bindgen] 117 pub struct Receipt { 118 #[wasm_bindgen(readonly)] 119 pub kind: OperationKind, 120 #[wasm_bindgen(readonly, getter_with_clone)] 121 pub operation_id: String, 122 #[wasm_bindgen(readonly, getter_with_clone)] 123 pub captured_context_json: Vec<u8>, 124 #[wasm_bindgen(readonly, getter_with_clone)] 125 pub command_digest: String, 126 #[wasm_bindgen(readonly)] 127 pub admission: Admission, 128 #[wasm_bindgen(readonly, getter_with_clone)] 129 pub admission_transaction_id: String, 130 #[wasm_bindgen(readonly, getter_with_clone)] 131 pub plan_transaction_id: Option<String>, 132 #[wasm_bindgen(readonly, getter_with_clone)] 133 pub installation_transaction_id: Option<String>, 134 #[wasm_bindgen(readonly, getter_with_clone)] 135 pub installed_at: Option<String>, 136 #[wasm_bindgen(readonly, getter_with_clone)] 137 pub prepared_at: String, 138 #[wasm_bindgen(readonly, getter_with_clone)] 139 pub first_issue_not_after: String, 140 #[wasm_bindgen(readonly, getter_with_clone)] 141 pub authority_checkpoint_json: Vec<u8>, 142 #[wasm_bindgen(readonly, getter_with_clone)] 143 pub operation_checkpoint: String, 144 #[wasm_bindgen(readonly, getter_with_clone)] 145 pub observed_at: String, 146 #[wasm_bindgen(readonly)] 147 pub business_installation: BusinessInstallation, 148 #[wasm_bindgen(readonly, getter_with_clone)] 149 pub business_result: Option<Value>, 150 #[wasm_bindgen(readonly)] 151 pub issuance: Issuance, 152 #[wasm_bindgen(readonly)] 153 pub delivery: Delivery, 154 #[wasm_bindgen(readonly)] 155 pub wait_termination: WaitTermination, 156 #[wasm_bindgen(readonly, getter_with_clone)] 157 pub message_id: Option<String>, 158 #[wasm_bindgen(readonly, getter_with_clone)] 159 pub record_ids: Vec<String>, 160 #[wasm_bindgen(readonly, getter_with_clone)] 161 pub missing_sources: Vec<String>, 162 #[wasm_bindgen(readonly, getter_with_clone)] 163 pub conflicting_records: Vec<String>, 164 #[wasm_bindgen(readonly, getter_with_clone)] 165 pub conflicting_sources: Vec<String>, 166 #[wasm_bindgen(readonly, getter_with_clone)] 167 pub artifacts: Vec<Artifact>, 168 #[wasm_bindgen(readonly, getter_with_clone)] 169 pub delivery_attempts: Vec<DeliveryAttempt>, 170 #[wasm_bindgen(readonly, getter_with_clone)] 171 pub delivery_observations: Vec<Observation>, 172 recovery: Vec<RecoveryRequirement>, 173 #[wasm_bindgen(readonly, getter_with_clone)] 174 pub error: Option<Failure>, 175 } 176 #[wasm_bindgen] 177 impl Receipt { 178 /// Exact closed shared recovery states, never an untyped or defaulted axis. 179 #[wasm_bindgen(getter, unchecked_return_type = "RecoveryRequirement[]")] 180 pub fn recovery(&self) -> Vec<RecoveryRequirement> { 181 self.recovery.clone() 182 } 183 } 184 impl Receipt { 185 pub(crate) fn listing( 186 value: &radroots_execution::OperationReceipt<radroots_domain::listing::Listing>, 187 ) -> Result<Self, Error> { 188 project(value, value.business_result().map(Value::listing)) 189 } 190 pub(crate) fn chat( 191 value: &radroots_execution::OperationReceipt<radroots_domain::ChatMessage>, 192 ) -> Result<Self, Error> { 193 project(value, value.business_result().map(Value::chat)) 194 } 195 pub(crate) fn status(value: &radroots_execution::OperationStatus) -> Result<Self, Error> { 196 let result = value.business_result().map(|v| match v { 197 radroots_execution::OperationValue::Listing(v) => Value::listing(v), 198 radroots_execution::OperationValue::Chat(v) => Value::chat(v), 199 }); 200 project(value, result) 201 } 202 } 203 fn project<T>( 204 value: &radroots_execution::OperationReceipt<T>, 205 business_result: Option<Value>, 206 ) -> Result<Receipt, Error> { 207 let operation = value.context().operation_id().to_string(); 208 project_inner(value, business_result).map_err(|v| v.with_operation(operation)) 209 } 210 fn project_inner<T>( 211 value: &radroots_execution::OperationReceipt<T>, 212 business_result: Option<Value>, 213 ) -> Result<Receipt, Error> { 214 let context = || Error::UnsupportedState { 215 axis: "receipt_data".to_owned(), 216 operation_id: Some(value.context().operation_id().to_string()), 217 signer_rejection: None, 218 }; 219 let artifacts = value 220 .artifacts() 221 .iter() 222 .map(|v| { 223 Ok(Artifact { 224 role: v.role().try_into()?, 225 recipient: v.recipient().map(|v| v.to_hex()), 226 expected_event: v.expected_event().map(|v| v.to_string()), 227 issuance: v.issuance().try_into()?, 228 attempt_id: v.attempt_id().map(|v| v.to_string()), 229 claim_transaction: v.claim_transaction_id().map(|v| v.to_string()), 230 intent_digest: v.intent_digest().map(|v| hex(&v)), 231 capture_transaction: v.capture_transaction_id().map(|v| v.to_string()), 232 obtained_digest: v.obtained_digest().map(|v| hex(&v)), 233 publication_transaction: v.publication_transaction_id().map(|v| v.to_string()), 234 observed_at_unix_ms: v.observed_at_unix_ms().map(|v| v.to_string()), 235 }) 236 }) 237 .collect::<Result<Vec<_>, Error>>()?; 238 let observations = value 239 .delivery_observations() 240 .iter() 241 .map(|v| { 242 Ok(Observation { 243 event_id: v.event_id().to_string(), 244 recipient: v.recipient().map(|v| v.to_hex()), 245 target: v.target().to_owned(), 246 request_id: v.request_id().to_owned(), 247 target_fingerprint: v.target_fingerprint().to_owned(), 248 target_ordinal: v.target_ordinal(), 249 outcome: v.outcome().try_into()?, 250 observed_at_unix_ms: v.observed_at_unix_ms().map(|v| v.to_string()), 251 }) 252 }) 253 .collect::<Result<Vec<_>, Error>>()?; 254 let attempts = value 255 .delivery_attempts() 256 .iter() 257 .map(|v| { 258 let intent = v.intent(); 259 let selected = intent.policy().targets(); 260 let policy = if selected.is_any() { 261 TargetPolicy { 262 kind: "any".into(), 263 threshold: None, 264 fingerprints: vec![], 265 } 266 } else if selected.is_all() { 267 TargetPolicy { 268 kind: "all".into(), 269 threshold: None, 270 fingerprints: vec![], 271 } 272 } else if let Some(threshold) = selected.quorum_threshold() { 273 TargetPolicy { 274 kind: "quorum".into(), 275 threshold: Some(threshold), 276 fingerprints: vec![], 277 } 278 } else if let Some(required) = selected.required_targets() { 279 TargetPolicy { 280 kind: "required".into(), 281 threshold: None, 282 fingerprints: required.iter().map(|v| v.as_str().to_owned()).collect(), 283 } 284 } else { 285 return Err(context()); 286 }; 287 Ok(DeliveryAttempt { 288 request_id: intent.request_id().as_str().to_owned(), 289 event_id: intent.event_id().to_string(), 290 role: intent.role().try_into()?, 291 recipient: v.work().target().recipient().map(|v| v.to_hex()), 292 targets: intent 293 .targets() 294 .targets() 295 .iter() 296 .map(|v| Target { 297 uri: v.uri().as_str().to_owned(), 298 kind: v.kind().as_str().to_owned(), 299 fingerprint: v.fingerprint().as_str().to_owned(), 300 }) 301 .collect(), 302 selected_fingerprints: intent 303 .selection() 304 .iter() 305 .map(|v| v.as_str().to_owned()) 306 .collect(), 307 satisfaction_class: intent.policy().class().into(), 308 policy, 309 publication_policy_digest: hex(intent.publication_policy_digest()), 310 intent_digest: hex(intent.digest()), 311 original_bytes_digest: hex(v.envelope_digest()), 312 transaction_id: v.transaction_id().to_string(), 313 work_transaction_id: v.work().transaction_id().to_string(), 314 retained_at: v.retained_at().to_string(), 315 deadline_unix_ms: intent.deadline_unix_ms().to_string(), 316 target_ordinals: v.target_ordinals().to_vec(), 317 }) 318 }) 319 .collect::<Result<Vec<_>, Error>>()?; 320 Ok(Receipt { 321 kind: value.kind().try_into()?, 322 operation_id: value.context().operation_id().to_string(), 323 captured_context_json: serde_json::to_vec(value.context()).map_err(|_| context())?, 324 command_digest: value.command_digest().to_string(), 325 admission: value.admission().try_into()?, 326 admission_transaction_id: value.admission_transaction_id().to_string(), 327 plan_transaction_id: value.plan_transaction_id().map(|v| v.to_string()), 328 installation_transaction_id: value.installation_transaction_id().map(|v| v.to_string()), 329 installed_at: value.installed_at().map(|v| v.to_string()), 330 prepared_at: value.issuance_window().prepared_at().to_string(), 331 first_issue_not_after: value.issuance_window().first_issue_not_after().to_string(), 332 authority_checkpoint_json: serde_json::to_vec(value.authority_checkpoint()) 333 .map_err(|_| context())?, 334 operation_checkpoint: value.operation_checkpoint().to_string(), 335 observed_at: value.observed_at().to_string(), 336 business_installation: value.business_installation().try_into()?, 337 business_result, 338 issuance: value.issuance().try_into()?, 339 delivery: value.delivery().try_into()?, 340 wait_termination: value.wait_termination().try_into()?, 341 message_id: value.message_id().map(|v| v.to_string()), 342 record_ids: value.record_ids().iter().map(ToString::to_string).collect(), 343 missing_sources: value 344 .missing_sources() 345 .iter() 346 .map(ToString::to_string) 347 .collect(), 348 conflicting_records: value 349 .conflicting_records() 350 .iter() 351 .map(ToString::to_string) 352 .collect(), 353 conflicting_sources: value 354 .conflicting_sources() 355 .iter() 356 .map(ToString::to_string) 357 .collect(), 358 artifacts, 359 delivery_attempts: attempts, 360 delivery_observations: observations, 361 recovery: value 362 .recovery() 363 .iter() 364 .copied() 365 .map(TryInto::try_into) 366 .collect::<Result<Vec<_>, Error>>()?, 367 error: value.error().map(Error::from).map(Failure::from), 368 }) 369 } 370 fn hex(bytes: &[u8]) -> String { 371 const DIGITS: &[u8; 16] = b"0123456789abcdef"; 372 let mut result = String::with_capacity(bytes.len() * 2); 373 for byte in bytes { 374 result.push(DIGITS[(byte >> 4) as usize] as char); 375 result.push(DIGITS[(byte & 15) as usize] as char); 376 } 377 result 378 }