lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README

wire.rs (3555B)


      1 //! Closed bounded records and exact references on the stock protected chain.
      2 use super::*;
      3 use serde::{Deserialize, Serialize};
      4 use std::io::{Error, ErrorKind, Write};
      5 use zeroize::Zeroizing;
      6 
      7 pub(super) use super::super::super::delivery::json::{bounded, policy, small_vec, targets};
      8 
      9 #[derive(Clone, Copy, Serialize, Deserialize)]
     10 #[serde(deny_unknown_fields)]
     11 pub(super) struct Reference {
     12     pub entry: OperationId,
     13     pub transaction: TransactionId,
     14     pub head: CheckpointDigest,
     15 }
     16 impl Reference {
     17     pub fn new(header: &ledger::Header) -> Self {
     18         Self {
     19             entry: header.operation,
     20             transaction: header.transaction,
     21             head: header.head,
     22         }
     23     }
     24     pub fn confirm(self, header: &ledger::Header) -> Result<(), OperationError> {
     25         if self.entry != header.operation
     26             || self.transaction != header.transaction
     27             || self.head != header.head
     28         {
     29             return Err(OperationError::RecoveryRequired);
     30         }
     31         Ok(())
     32     }
     33 }
     34 struct Writer {
     35     bytes: Zeroizing<Vec<u8>>,
     36     limit: usize,
     37 }
     38 impl Write for Writer {
     39     fn write(&mut self, bytes: &[u8]) -> std::io::Result<usize> {
     40         if self
     41             .bytes
     42             .len()
     43             .checked_add(bytes.len())
     44             .is_none_or(|len| len > self.limit)
     45         {
     46             return Err(Error::new(
     47                 ErrorKind::InvalidInput,
     48                 "stock delivery record limit",
     49             ));
     50         }
     51         self.bytes.extend_from_slice(bytes);
     52         Ok(bytes.len())
     53     }
     54     fn flush(&mut self) -> std::io::Result<()> {
     55         Ok(())
     56     }
     57 }
     58 pub(super) fn encode<T: Serialize>(
     59     value: &T,
     60     limit: usize,
     61 ) -> Result<Zeroizing<Vec<u8>>, OperationError> {
     62     let mut writer = Writer {
     63         bytes: Zeroizing::new(Vec::with_capacity(limit.min(4096))),
     64         limit,
     65     };
     66     serde_json::to_writer(&mut writer, value).map_err(|_| OperationError::InvalidEvidence)?;
     67     Ok(writer.bytes)
     68 }
     69 pub(super) fn decode<T: for<'de> Deserialize<'de>>(
     70     raw: &[u8],
     71     limit: usize,
     72 ) -> Result<T, OperationError> {
     73     if raw.len() > limit || !bounded(raw, 32) {
     74         return Err(OperationError::RecoveryRequired);
     75     }
     76     serde_json::from_slice(raw).map_err(|_| OperationError::RecoveryRequired)
     77 }
     78 pub(super) fn head(header: &ledger::Header, body: &[u8]) -> CheckpointDigest {
     79     let mut hash = Sha256::new();
     80     hash.update(b"radroots.seller_stock.delivery_chain.v1\0");
     81     hash.update([header.family.code() as u8]);
     82     hash.update(header.namespace.workspace_id().as_bytes());
     83     hash.update(header.namespace.authority().executor_id().as_bytes());
     84     hash.update(
     85         header
     86             .namespace
     87             .authority()
     88             .generation()
     89             .get()
     90             .to_be_bytes(),
     91     );
     92     hash.update(header.namespace.route_digest().as_bytes());
     93     hash.update(header.actor.participant().as_bytes());
     94     hash.update(
     95         header
     96             .actor
     97             .grant_id()
     98             .map(|id| *id.as_bytes())
     99             .unwrap_or([0; 32]),
    100     );
    101     hash.update(header.operation.as_bytes());
    102     hash.update(header.digest.as_bytes());
    103     hash.update(header.transaction.as_bytes());
    104     hash.update(header.revision.get().to_be_bytes());
    105     hash.update([u8::from(header.previous.is_some())]);
    106     hash.update(
    107         header
    108             .previous
    109             .map(|value| *value.as_bytes())
    110             .unwrap_or([0; 32]),
    111     );
    112     hash.update(Sha256::digest(body));
    113     CheckpointDigest::from_bytes(hash.finalize().into())
    114 }