projection.rs (8184B)
1 //! Exact body-derived original, wrapping-admission and capture indexes. 2 use super::*; 3 #[derive(Clone, Copy)] 4 pub(in crate::workspace::stock) enum Projection { 5 Original { 6 operation: OperationId, 7 recipient: PublicKey, 8 }, 9 Admission { 10 operation: OperationId, 11 recipient: PublicKey, 12 request: OperationId, 13 }, 14 Rewrap { 15 operation: OperationId, 16 recipient: PublicKey, 17 admission: OperationId, 18 event: EnvelopeEventId, 19 }, 20 } 21 impl Projection { 22 pub(in crate::workspace::stock) fn digest( 23 self, 24 actor: PublicKey, 25 entry: OperationId, 26 ) -> [u8; 32] { 27 let mut hash = Sha256::new(); 28 let (operation, recipient) = match self { 29 Self::Original { 30 operation, 31 recipient, 32 } => { 33 hash.update(b"radroots.seller_stock.envelope_projection.v1\0"); 34 (operation, recipient) 35 } 36 Self::Admission { 37 operation, 38 recipient, 39 .. 40 } => { 41 hash.update(b"radroots.seller_stock.rewrap_admission_projection.v1\0"); 42 (operation, recipient) 43 } 44 Self::Rewrap { 45 operation, 46 recipient, 47 .. 48 } => { 49 hash.update(b"radroots.seller_stock.rewrap_projection.v1\0"); 50 (operation, recipient) 51 } 52 }; 53 hash.update(actor.as_bytes()); 54 hash.update(entry.as_bytes()); 55 hash.update(operation.as_bytes()); 56 hash.update(recipient.as_bytes()); 57 match self { 58 Self::Original { .. } => {} 59 Self::Admission { request, .. } => hash.update(request.as_bytes()), 60 Self::Rewrap { 61 admission, event, .. 62 } => { 63 hash.update(admission.as_bytes()); 64 hash.update(event.as_bytes()); 65 } 66 } 67 hash.finalize().into() 68 } 69 pub(super) async fn insert( 70 self, 71 connection: &mut SqliteConnection, 72 actor: PublicKey, 73 entry: OperationId, 74 ) -> Result<(), OperationError> { 75 match self { 76 Self::Original { 77 operation, 78 recipient, 79 } => { 80 sqlx::query("INSERT INTO stock_envelopes(actor,operation_id,recipient,entry_id) VALUES(?,?,?,?)") 81 .bind(actor.as_bytes().as_slice()).bind(operation.as_bytes().as_slice()).bind(recipient.as_bytes().as_slice()).bind(entry.as_bytes().as_slice()) 82 .execute(connection).await.map_err(|_| OperationError::StorageFailure)?; 83 } 84 Self::Admission { 85 operation, 86 recipient, 87 request, 88 } => { 89 sqlx::query("INSERT INTO stock_rewrap_admissions(actor,operation_id,recipient,request_id,entry_id) VALUES(?,?,?,?,?)") 90 .bind(actor.as_bytes().as_slice()).bind(operation.as_bytes().as_slice()).bind(recipient.as_bytes().as_slice()) 91 .bind(request.as_bytes().as_slice()).bind(entry.as_bytes().as_slice()).execute(connection).await.map_err(|_| OperationError::StorageFailure)?; 92 } 93 Self::Rewrap { 94 operation, 95 recipient, 96 admission, 97 event, 98 } => { 99 sqlx::query("INSERT INTO stock_rewraps(actor,operation_id,recipient,admission_entry,event_id,entry_id) VALUES(?,?,?,?,?,?)") 100 .bind(actor.as_bytes().as_slice()).bind(operation.as_bytes().as_slice()).bind(recipient.as_bytes().as_slice()) 101 .bind(admission.as_bytes().as_slice()).bind(event.as_bytes().as_slice()).bind(entry.as_bytes().as_slice()).execute(connection).await.map_err(|_| OperationError::StorageFailure)?; 102 } 103 } 104 Ok(()) 105 } 106 pub(super) async fn confirm( 107 self, 108 connection: &mut SqliteConnection, 109 actor: PublicKey, 110 entry: OperationId, 111 ) -> Result<(), OperationError> { 112 let found = match self { 113 Self::Original { .. } => { 114 raw_projection(connection, actor.as_bytes(), entry.as_bytes()).await? 115 } 116 Self::Admission { .. } => { 117 raw_admission_projection(connection, actor.as_bytes(), entry.as_bytes()).await? 118 } 119 Self::Rewrap { .. } => { 120 raw_rewrap_projection(connection, actor.as_bytes(), entry.as_bytes()).await? 121 } 122 }; 123 if found != self.digest(actor, entry) { 124 return Err(OperationError::RecoveryRequired); 125 } 126 Ok(()) 127 } 128 } 129 fn bytes(row: &sqlx::sqlite::SqliteRow, index: usize) -> Result<[u8; 32], OperationError> { 130 row.try_get::<Vec<u8>, _>(index) 131 .map_err(|_| OperationError::RecoveryRequired)? 132 .try_into() 133 .map_err(|_| OperationError::RecoveryRequired) 134 } 135 pub(in crate::workspace::stock) async fn raw_projection( 136 connection: &mut SqliteConnection, 137 actor: &[u8; 32], 138 entry: &[u8; 32], 139 ) -> Result<[u8; 32], OperationError> { 140 let row = sqlx::query( 141 "SELECT CASE WHEN length(operation_id)=32 THEN operation_id END,CASE WHEN length(recipient)=32 THEN recipient END FROM stock_envelopes WHERE actor=? AND entry_id=?", 142 ) 143 .bind(actor.as_slice()) 144 .bind(entry.as_slice()) 145 .fetch_optional(connection) 146 .await 147 .map_err(|_| OperationError::RecoveryRequired)? 148 .ok_or(OperationError::RecoveryRequired)?; 149 Ok(Projection::Original { 150 operation: OperationId::from_bytes(bytes(&row, 0)?), 151 recipient: PublicKey::from_bytes(bytes(&row, 1)?) 152 .map_err(|_| OperationError::RecoveryRequired)?, 153 } 154 .digest( 155 PublicKey::from_bytes(*actor).map_err(|_| OperationError::RecoveryRequired)?, 156 OperationId::from_bytes(*entry), 157 )) 158 } 159 pub(in crate::workspace::stock) async fn raw_admission_projection( 160 connection: &mut SqliteConnection, 161 actor: &[u8; 32], 162 entry: &[u8; 32], 163 ) -> Result<[u8; 32], OperationError> { 164 let row = sqlx::query("SELECT CASE WHEN length(operation_id)=32 THEN operation_id END,CASE WHEN length(recipient)=32 THEN recipient END,CASE WHEN length(request_id)=32 THEN request_id END FROM stock_rewrap_admissions WHERE actor=? AND entry_id=?") 165 .bind(actor.as_slice()).bind(entry.as_slice()).fetch_optional(connection).await.map_err(|_| OperationError::RecoveryRequired)?.ok_or(OperationError::RecoveryRequired)?; 166 Ok(Projection::Admission { 167 operation: OperationId::from_bytes(bytes(&row, 0)?), 168 recipient: PublicKey::from_bytes(bytes(&row, 1)?) 169 .map_err(|_| OperationError::RecoveryRequired)?, 170 request: OperationId::from_bytes(bytes(&row, 2)?), 171 } 172 .digest( 173 PublicKey::from_bytes(*actor).map_err(|_| OperationError::RecoveryRequired)?, 174 OperationId::from_bytes(*entry), 175 )) 176 } 177 pub(in crate::workspace::stock) async fn raw_rewrap_projection( 178 connection: &mut SqliteConnection, 179 actor: &[u8; 32], 180 entry: &[u8; 32], 181 ) -> Result<[u8; 32], OperationError> { 182 let row = sqlx::query("SELECT CASE WHEN length(operation_id)=32 THEN operation_id END,CASE WHEN length(recipient)=32 THEN recipient END,CASE WHEN length(admission_entry)=32 THEN admission_entry END,CASE WHEN length(event_id)=32 THEN event_id END FROM stock_rewraps WHERE actor=? AND entry_id=?") 183 .bind(actor.as_slice()).bind(entry.as_slice()).fetch_optional(connection).await.map_err(|_| OperationError::RecoveryRequired)?.ok_or(OperationError::RecoveryRequired)?; 184 Ok(Projection::Rewrap { 185 operation: OperationId::from_bytes(bytes(&row, 0)?), 186 recipient: PublicKey::from_bytes(bytes(&row, 1)?) 187 .map_err(|_| OperationError::RecoveryRequired)?, 188 admission: OperationId::from_bytes(bytes(&row, 2)?), 189 event: EnvelopeEventId::from_bytes(bytes(&row, 3)?), 190 } 191 .digest( 192 PublicKey::from_bytes(*actor).map_err(|_| OperationError::RecoveryRequired)?, 193 OperationId::from_bytes(*entry), 194 )) 195 }