listing.rs (6497B)
1 //! Atomic initial-coordinate ownership, separate from stock authority. 2 use super::{Mutation, RecordKey, State, crypto, journal::storage_error}; 3 use radroots_domain::{ 4 CommandDigest, OperationId, PublicKey, TransactionId, WorkspaceId, listing::ListingId, 5 }; 6 use radroots_secrets::context::{ 7 EnvelopeContext, EnvelopePurpose, EnvelopeSubject, PayloadSchemaId, 8 }; 9 use radroots_storage::command::{OperationError, PreparedOperation}; 10 use serde::{Deserialize, Serialize}; 11 use sha2::{Digest, Sha256}; 12 use zeroize::Zeroizing; 13 14 #[derive(Serialize, Deserialize, Eq, PartialEq)] 15 #[serde(deny_unknown_fields)] 16 struct Decision { 17 version: u8, 18 workspace: WorkspaceId, 19 operation: OperationId, 20 command: CommandDigest, 21 author: PublicKey, 22 listing: ListingId, 23 transaction: TransactionId, 24 } 25 impl Decision { 26 fn new(prepared: &PreparedOperation) -> Result<Self, OperationError> { 27 let request = prepared.request(); 28 let command = request 29 .command() 30 .listing() 31 .ok_or(OperationError::UnsupportedOperation)?; 32 let context = request.command().context(); 33 Ok(Self { 34 version: 1, 35 workspace: context.workspace_id(), 36 operation: context.operation_id(), 37 command: request.command().digest(), 38 author: context.actor().participant(), 39 listing: command.input().id(), 40 transaction: prepared.transaction_id(), 41 }) 42 } 43 fn digest(&self) -> [u8; 32] { 44 Sha256::new() 45 .chain_update(b"radroots.browser.local_outbox.listing_coordinate.v1\0") 46 .chain_update(self.workspace.as_bytes()) 47 .chain_update(self.author.as_bytes()) 48 .chain_update(self.listing.as_bytes()) 49 .finalize() 50 .into() 51 } 52 fn key(&self) -> RecordKey { 53 RecordKey::new(self.digest()) 54 } 55 fn context(&self) -> Result<EnvelopeContext, OperationError> { 56 let subject = self 57 .digest() 58 .iter() 59 .map(|b| format!("{b:02x}")) 60 .collect::<String>(); 61 Ok(EnvelopeContext::new( 62 EnvelopePurpose::parse("radroots.browser.workspace.listing_coordinate") 63 .map_err(|_| OperationError::InvalidCommand)?, 64 EnvelopeSubject::parse("listing_coordinate", subject) 65 .map_err(|_| OperationError::InvalidCommand)?, 66 PayloadSchemaId::parse("radroots.browser.workspace.listing_coordinate.v1") 67 .map_err(|_| OperationError::InvalidCommand)?, 68 )) 69 } 70 } 71 impl State { 72 pub(super) async fn coordinate_creation( 73 &self, 74 prepared: &PreparedOperation, 75 ) -> Result<Vec<Mutation>, OperationError> { 76 if prepared.request().command().listing().is_none() { 77 return Ok(Vec::new()); 78 } 79 let decision = Decision::new(prepared)?; 80 if let Some(protected) = self 81 .database() 82 .get(decision.key()) 83 .await 84 .map_err(storage_error)? 85 { 86 let raw = crypto::open_bound( 87 self.provider.as_ref(), 88 &self.reference, 89 decision.context()?, 90 &protected, 91 ) 92 .await?; 93 let original: Decision = raw.expose_secret(|bytes| { 94 if bytes.len() > 2048 { 95 return Err(OperationError::RecoveryRequired); 96 } 97 serde_json::from_slice(bytes).map_err(|_| OperationError::RecoveryRequired) 98 })?; 99 if original.version != 1 100 || original.workspace != decision.workspace 101 || original.author != decision.author 102 || original.listing != decision.listing 103 { 104 return Err(OperationError::RecoveryRequired); 105 } 106 let view = self.read_view(original.operation).await?; 107 let records = self.operation_records(&view, original.operation).await?; 108 let prepared = self 109 .preparation(original.operation, &records) 110 .await? 111 .ok_or(OperationError::RecoveryRequired)?; 112 if Decision::new(&prepared)? != original { 113 return Err(OperationError::RecoveryRequired); 114 } 115 return Err(OperationError::StaleState); 116 } 117 let raw = Zeroizing::new( 118 serde_json::to_vec(&decision).map_err(|_| OperationError::InvalidCommand)?, 119 ); 120 let protected = crypto::seal_bound( 121 self.provider.as_ref(), 122 &self.reference, 123 decision.context()?, 124 &raw, 125 ) 126 .await?; 127 Ok(vec![ 128 Mutation::new(decision.key(), None, Some(protected)).map_err(storage_error)?, 129 ]) 130 } 131 /// Verifies the exact T0 decision and returns an unchanged CAS entry. It is 132 /// included with every later write, even though the index is outside the 133 /// operation prefix. There is no overwrite, lease or automatic reclamation. 134 pub(super) async fn coordinate_guard( 135 &self, 136 prepared: &PreparedOperation, 137 ) -> Result<Vec<Mutation>, OperationError> { 138 if prepared.request().command().listing().is_none() { 139 return Ok(Vec::new()); 140 } 141 if prepared.request().binding().scope() 142 != radroots_storage::workspace::WorkspaceScope::NewSellerOutbox 143 { 144 return Err(OperationError::RecoveryRequired); 145 } 146 let expected = Decision::new(prepared)?; 147 let protected = self 148 .database() 149 .get(expected.key()) 150 .await 151 .map_err(storage_error)? 152 .ok_or(OperationError::RecoveryRequired)?; 153 let raw = crypto::open_bound( 154 self.provider.as_ref(), 155 &self.reference, 156 expected.context()?, 157 &protected, 158 ) 159 .await?; 160 let decision: Decision = raw.expose_secret(|bytes| { 161 if bytes.len() > 2048 { 162 return Err(OperationError::RecoveryRequired); 163 } 164 serde_json::from_slice(bytes).map_err(|_| OperationError::RecoveryRequired) 165 })?; 166 if decision != expected { 167 return Err(OperationError::RecoveryRequired); 168 } 169 Ok(vec![ 170 Mutation::new(expected.key(), Some(protected.clone()), Some(protected)) 171 .map_err(storage_error)?, 172 ]) 173 } 174 }