projection.rs (4839B)
1 use super::*; 2 /// Immutable body-derived selector, independently compared with its SQL index. 3 #[derive(Clone, Copy)] 4 pub(in crate::workspace::stock) struct Projection { 5 pub operation: OperationId, 6 pub recipient: PublicKey, 7 pub kind: ProjectionKind, 8 } 9 #[derive(Clone, Copy)] 10 pub(in crate::workspace::stock) enum ProjectionKind { 11 Claim, 12 Obtained([u8; 32]), 13 OriginalUnissued(u8), 14 ReplayAdmission, 15 } 16 impl ProjectionKind { 17 fn family(self) -> i64 { 18 match self { 19 Self::Claim => 4, 20 Self::Obtained(_) => 5, 21 Self::OriginalUnissued(_) => 8, 22 Self::ReplayAdmission => 9, 23 } 24 } 25 } 26 impl Projection { 27 pub fn digest(self, actor: PublicKey, entry: OperationId) -> [u8; 32] { 28 let mut hash = Sha256::new(); 29 hash.update(b"radroots.seller_stock.issuance_projection.v1\0"); 30 hash.update(actor.as_bytes()); 31 hash.update(entry.as_bytes()); 32 hash.update(self.operation.as_bytes()); 33 hash.update(self.recipient.as_bytes()); 34 match self.kind { 35 ProjectionKind::Claim => hash.update([0]), 36 ProjectionKind::ReplayAdmission => hash.update([3]), 37 ProjectionKind::Obtained(value) => { 38 hash.update([1]); 39 hash.update(value); 40 } 41 ProjectionKind::OriginalUnissued(reason) => { 42 hash.update([2]); 43 hash.update([reason]); 44 } 45 } 46 hash.finalize().into() 47 } 48 pub async fn confirm( 49 self, 50 connection: &mut SqliteConnection, 51 actor: PublicKey, 52 entry: OperationId, 53 ) -> Result<(), OperationError> { 54 let actual = raw_projection( 55 connection, 56 actor.as_bytes(), 57 entry.as_bytes(), 58 self.kind.family(), 59 ) 60 .await?; 61 if actual != self.digest(actor, entry) { 62 return Err(OperationError::RecoveryRequired); 63 } 64 Ok(()) 65 } 66 } 67 pub(in crate::workspace::stock) async fn raw_projection( 68 connection: &mut SqliteConnection, 69 actor: &[u8; 32], 70 entry: &[u8; 32], 71 family: i64, 72 ) -> Result<[u8; 32], OperationError> { 73 let row = match family { 74 4 => sqlx::query("SELECT CASE WHEN length(operation_id)=32 THEN operation_id END,CASE WHEN length(recipient)=32 THEN recipient END,NULL FROM stock_signing_claims WHERE actor=? AND entry_id=?").bind(actor.as_slice()).bind(entry.as_slice()).fetch_optional(connection).await, 75 5 => sqlx::query("SELECT CASE WHEN length(operation_id)=32 THEN operation_id END,CASE WHEN length(recipient)=32 THEN recipient END,CASE WHEN length(evidence_digest)=32 THEN evidence_digest END FROM stock_signing_evidence WHERE actor=? AND entry_id=?").bind(actor.as_slice()).bind(entry.as_slice()).fetch_optional(connection).await, 76 8 => sqlx::query("SELECT CASE WHEN length(operation_id)=32 THEN operation_id END,CASE WHEN length(recipient)=32 THEN recipient END,reason FROM stock_original_unissued WHERE actor=? AND entry_id=?").bind(actor.as_slice()).bind(entry.as_slice()).fetch_optional(connection).await, 77 9 => sqlx::query("SELECT CASE WHEN length(operation_id)=32 THEN operation_id END,CASE WHEN length(recipient)=32 THEN recipient END,NULL FROM stock_replay_admissions WHERE actor=? AND entry_id=?").bind(actor.as_slice()).bind(entry.as_slice()).fetch_optional(connection).await, 78 _ => return Err(OperationError::RecoveryRequired), 79 }.map_err(|_|OperationError::RecoveryRequired)?.ok_or(OperationError::RecoveryRequired)?; 80 let field = |index| { 81 row.try_get::<Option<Vec<u8>>, _>(index) 82 .map_err(|_| OperationError::RecoveryRequired)? 83 .ok_or(OperationError::RecoveryRequired)? 84 .try_into() 85 .map_err(|_| OperationError::RecoveryRequired) 86 }; 87 let projection = Projection { 88 operation: OperationId::from_bytes(field(0)?), 89 recipient: PublicKey::from_bytes(field(1)?) 90 .map_err(|_| OperationError::RecoveryRequired)?, 91 kind: match family { 92 4 => ProjectionKind::Claim, 93 9 => ProjectionKind::ReplayAdmission, 94 5 => ProjectionKind::Obtained(field(2)?), 95 8 => { 96 let reason: u8 = row 97 .try_get::<i64, _>(2) 98 .ok() 99 .and_then(|v| v.try_into().ok()) 100 .filter(|v| (1..=9).contains(v)) 101 .ok_or(OperationError::RecoveryRequired)?; 102 ProjectionKind::OriginalUnissued(reason) 103 } 104 _ => return Err(OperationError::RecoveryRequired), 105 }, 106 }; 107 Ok(projection.digest( 108 PublicKey::from_bytes(*actor).map_err(|_| OperationError::RecoveryRequired)?, 109 OperationId::from_bytes(*entry), 110 )) 111 }