rewrap_selection.rs (4643B)
1 //! Bounded natural-index witness, including rows outside the protected chain. 2 use super::*; 3 #[derive(Eq, PartialEq)] 4 pub(in crate::workspace::stock::envelope) struct SelectionWitness { 5 pub count: usize, 6 digest: [u8; 32], 7 } 8 impl SelectionWitness { 9 pub(in crate::workspace::stock::envelope) async fn read( 10 connection: &mut SqliteConnection, 11 request: &StockEnvelopeRequest, 12 ) -> Result<Self, OperationError> { 13 let admissions = sqlx::query("SELECT CASE WHEN length(r.request_id)=32 THEN r.request_id END,CASE WHEN length(r.entry_id)=32 THEN r.entry_id END,o.family,CASE WHEN length(o.related_operation)=32 THEN o.related_operation END FROM stock_rewrap_admissions r LEFT JOIN stock_operations o ON o.actor=r.actor AND o.operation_id=r.entry_id WHERE r.actor=? AND r.operation_id=? AND r.recipient=? ORDER BY r.request_id,r.entry_id LIMIT 193") 14 .bind(request.binding().input().participant().as_bytes().as_slice()) 15 .bind(request.operation_id().as_bytes().as_slice()).bind(request.recipient().as_bytes().as_slice()) 16 .fetch_all(&mut *connection).await.map_err(|_| OperationError::RecoveryRequired)?; 17 let captures = sqlx::query("SELECT CASE WHEN length(r.event_id)=32 THEN r.event_id END,CASE WHEN length(r.entry_id)=32 THEN r.entry_id END,o.family,CASE WHEN length(o.related_operation)=32 THEN o.related_operation END,CASE WHEN length(r.admission_entry)=32 THEN r.admission_entry END,CASE WHEN length(a.entry_id)=32 THEN a.entry_id END FROM stock_rewraps r LEFT JOIN stock_operations o ON o.actor=r.actor AND o.operation_id=r.entry_id LEFT JOIN stock_rewrap_admissions a ON a.actor=r.actor AND a.operation_id=r.operation_id AND a.recipient=r.recipient AND a.entry_id=r.admission_entry WHERE r.actor=? AND r.operation_id=? AND r.recipient=? ORDER BY r.event_id,r.entry_id LIMIT 193") 18 .bind(request.binding().input().participant().as_bytes().as_slice()) 19 .bind(request.operation_id().as_bytes().as_slice()).bind(request.recipient().as_bytes().as_slice()) 20 .fetch_all(connection).await.map_err(|_| OperationError::RecoveryRequired)?; 21 if admissions.len() > COUNT_MAX as usize || captures.len() > admissions.len() { 22 return Err(OperationError::RecoveryRequired); 23 } 24 let mut hash = Sha256::new(); 25 hash.update(b"radroots.seller_stock.rewrap_selection.v1\0"); 26 for (rows, family) in [(&admissions, 13), (&captures, 14)] { 27 hash.update([family as u8]); 28 hash.update((rows.len() as u16).to_be_bytes()); 29 for row in rows { 30 if row 31 .try_get::<Option<i64>, _>(2) 32 .map_err(|_| OperationError::RecoveryRequired)? 33 != Some(family) 34 || row 35 .try_get::<Option<Vec<u8>>, _>(3) 36 .map_err(|_| OperationError::RecoveryRequired)? 37 .as_deref() 38 != Some(request.operation_id().as_bytes().as_slice()) 39 { 40 return Err(OperationError::RecoveryRequired); 41 } 42 for index in 0..2 { 43 let bytes: Vec<u8> = row 44 .try_get(index) 45 .map_err(|_| OperationError::RecoveryRequired)?; 46 if bytes.len() != 32 { 47 return Err(OperationError::RecoveryRequired); 48 } 49 hash.update(bytes); 50 } 51 if family == 14 { 52 let admission: Vec<u8> = row 53 .try_get(4) 54 .map_err(|_| OperationError::RecoveryRequired)?; 55 let matched: Option<Vec<u8>> = row 56 .try_get(5) 57 .map_err(|_| OperationError::RecoveryRequired)?; 58 if admission.len() != 32 || matched.as_deref() != Some(admission.as_slice()) { 59 return Err(OperationError::RecoveryRequired); 60 } 61 hash.update(admission); 62 } 63 } 64 } 65 Ok(Self { 66 count: admissions.len(), 67 digest: hash.finalize().into(), 68 }) 69 } 70 pub(in crate::workspace::stock::envelope) async fn confirm( 71 &self, 72 connection: &mut SqliteConnection, 73 request: &StockEnvelopeRequest, 74 ) -> Result<(), OperationError> { 75 if Self::read(connection, request).await? != *self { 76 return Err(OperationError::RecoveryRequired); 77 } 78 Ok(()) 79 } 80 }