evidence.rs (6075B)
1 //! Bounded original evidence input and confirmed append metadata. 2 use super::*; 3 use core::num::NonZeroU64; 4 use radroots_domain::{PublicKey, SigningAttemptId}; 5 use zeroize::Zeroizing; 6 7 /// Bounded original signed bytes read by the qualified protected owner. 8 /// Construction checks representation identity, not current access or issuance. 9 pub struct StockEvidenceSource { 10 receipt: StockEvidenceReceipt, 11 raw: Zeroizing<String>, 12 } 13 impl StockEvidenceSource { 14 pub fn from_retained_wire( 15 receipt: StockEvidenceReceipt, 16 raw: &str, 17 ) -> Result<Self, OperationError> { 18 use sha2::{Digest, Sha256}; 19 if raw.is_empty() 20 || raw.len() > 64 * 1024 21 || <[u8; 32]>::from(Sha256::digest(raw.as_bytes())) != *receipt.digest() 22 { 23 return Err(OperationError::InvalidEvidence); 24 } 25 Ok(Self { 26 receipt, 27 raw: Zeroizing::new(raw.to_string()), 28 }) 29 } 30 pub const fn receipt(&self) -> StockEvidenceReceipt { 31 self.receipt 32 } 33 pub fn with_json<T>(&self, use_json: impl FnOnce(&str) -> T) -> T { 34 use_json(&self.raw) 35 } 36 } 37 impl core::fmt::Debug for StockEvidenceSource { 38 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 39 f.write_str("StockEvidenceSource(<redacted>)") 40 } 41 } 42 43 /// Obtained original wire bytes, never proof of a protected claim or authority. 44 /// The concrete store must authenticate the actual retained claim and verify 45 /// these bytes against it before append. Construction performs no cryptography. 46 pub struct StockEvidenceRequest { 47 binding: WorkspaceBinding, 48 operation: OperationId, 49 digest: CommandDigest, 50 claim: SigningAttemptId, 51 recipient: PublicKey, 52 intent: [u8; 32], 53 raw: Zeroizing<String>, 54 observed: Option<NonZeroU64>, 55 } 56 impl StockEvidenceRequest { 57 #[allow(clippy::too_many_arguments)] 58 pub fn from_obtained_wire( 59 binding: WorkspaceBinding, 60 operation: OperationId, 61 digest: CommandDigest, 62 claim: SigningAttemptId, 63 recipient: PublicKey, 64 intent: [u8; 32], 65 raw: &str, 66 observed: Option<NonZeroU64>, 67 ) -> Result<Self, OperationError> { 68 if binding.scope() != WorkspaceScope::SellerStockOwner { 69 return Err(OperationError::Unauthorized); 70 } 71 if raw.is_empty() || raw.len() > 64 * 1024 { 72 return Err(OperationError::InvalidEvidence); 73 } 74 Ok(Self { 75 binding, 76 operation, 77 digest, 78 claim, 79 recipient, 80 intent, 81 raw: Zeroizing::new(raw.to_string()), 82 observed, 83 }) 84 } 85 pub const fn binding(&self) -> &WorkspaceBinding { 86 &self.binding 87 } 88 pub const fn operation_id(&self) -> OperationId { 89 self.operation 90 } 91 pub const fn command_digest(&self) -> CommandDigest { 92 self.digest 93 } 94 pub const fn signing_attempt_id(&self) -> SigningAttemptId { 95 self.claim 96 } 97 pub const fn recipient(&self) -> PublicKey { 98 self.recipient 99 } 100 pub const fn intent_digest(&self) -> &[u8; 32] { 101 &self.intent 102 } 103 /// Digest of these exact obtained bytes, not a signature or commit proof. 104 pub fn evidence_digest(&self) -> [u8; 32] { 105 use sha2::{Digest, Sha256}; 106 self.with_json(|raw| Sha256::digest(raw.as_bytes()).into()) 107 } 108 pub const fn observed_at_unix_ms(&self) -> Option<NonZeroU64> { 109 self.observed 110 } 111 pub fn with_json<T>(&self, use_json: impl FnOnce(&str) -> T) -> T { 112 use_json(&self.raw) 113 } 114 /// Bounded protected copy for the host's existing owned worker. The caller 115 /// retains its original bytes across a failed or uncertain append. 116 pub fn retained_copy(&self) -> Self { 117 Self { 118 binding: self.binding.clone(), 119 operation: self.operation, 120 digest: self.digest, 121 claim: self.claim, 122 recipient: self.recipient, 123 intent: self.intent, 124 raw: Zeroizing::new(self.raw.to_string()), 125 observed: self.observed, 126 } 127 } 128 pub fn attempt(&self) -> CommitAttempt { 129 CommitAttempt::new( 130 self.binding.input().workspace_id(), 131 self.operation, 132 self.digest, 133 None, 134 ) 135 } 136 } 137 impl core::fmt::Debug for StockEvidenceRequest { 138 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 139 f.write_str("StockEvidenceRequest(<redacted>)") 140 } 141 } 142 143 /// Confirmed append identity, independent of installation or delivery. 144 #[derive(Clone, Copy, Debug, Eq, PartialEq)] 145 pub struct StockEvidenceReceipt { 146 attempt: CommitAttempt, 147 claim: SigningAttemptId, 148 recipient: PublicKey, 149 digest: [u8; 32], 150 observed: Option<NonZeroU64>, 151 } 152 impl StockEvidenceReceipt { 153 /// Validates report shape only. The concrete host confirms the actual 154 /// authenticated append and original observation; metadata grants nothing. 155 pub fn from_committed_store( 156 attempt: CommitAttempt, 157 claim: SigningAttemptId, 158 recipient: PublicKey, 159 digest: [u8; 32], 160 observed: Option<NonZeroU64>, 161 ) -> Result<Self, OperationError> { 162 if attempt.transaction_id().is_none() { 163 return Err(OperationError::RecoveryRequired); 164 } 165 Ok(Self { 166 attempt, 167 claim, 168 recipient, 169 digest, 170 observed, 171 }) 172 } 173 pub const fn attempt(&self) -> CommitAttempt { 174 self.attempt 175 } 176 pub const fn signing_attempt_id(&self) -> SigningAttemptId { 177 self.claim 178 } 179 pub const fn recipient(&self) -> PublicKey { 180 self.recipient 181 } 182 pub const fn digest(&self) -> &[u8; 32] { 183 &self.digest 184 } 185 pub const fn observed_at_unix_ms(&self) -> Option<NonZeroU64> { 186 self.observed 187 } 188 } 189 #[derive(Debug)] 190 pub enum StockEvidenceOutcome { 191 Committed(StockEvidenceReceipt), 192 Unknown(CommitAttempt), 193 }