lib

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

order_terms.rs (7214B)


      1 //! Exact `orders.install_terms` capture. Data capture supplies no source,
      2 //! consent, current agreement/residual, stock effect or signing permission.
      3 use super::{
      4     AllocationSelection, COMMAND_MAX_BYTES, CommandError, Context, check_frame, hash,
      5     order_acceptance::parse_selection, parse_object,
      6 };
      7 use alloc::{boxed::Box, string::String, vec::Vec};
      8 use core::fmt;
      9 use radroots_domain::{
     10     ActorRole, ApprovalIntentDigest, CommandContextData, CommandDigest, RecordId, TermsDigest,
     11 };
     12 use serde::{Deserialize, Serialize, ser::SerializeStruct};
     13 use serde_json::value::RawValue;
     14 use zeroize::Zeroizing;
     15 
     16 /// Complete proposal/authorization selectors and allocation claims. The
     17 /// executor independently authenticates their terms, base and current state.
     18 pub struct InstallTermsInput {
     19     proposal: RecordId,
     20     authorization: RecordId,
     21     terms: TermsDigest,
     22     allocation: AllocationSelection,
     23 }
     24 impl InstallTermsInput {
     25     /// Captures data only; both records and the actual digest remain unverified.
     26     pub const fn new(
     27         proposal: RecordId,
     28         authorization: RecordId,
     29         terms: TermsDigest,
     30         allocation: AllocationSelection,
     31     ) -> Self {
     32         Self {
     33             proposal,
     34             authorization,
     35             terms,
     36             allocation,
     37         }
     38     }
     39     /// Exact selected complete proposal.
     40     pub const fn proposal(&self) -> RecordId {
     41         self.proposal
     42     }
     43     /// Exact selected counterparty authorization.
     44     pub const fn authorization(&self) -> RecordId {
     45         self.authorization
     46     }
     47     /// Expected complete canonical terms digest, not a hash proof.
     48     pub const fn terms_digest(&self) -> TermsDigest {
     49         self.terms
     50     }
     51     /// Automatic resolution or explicit bounded allocation claims.
     52     pub const fn allocation(&self) -> &AllocationSelection {
     53         &self.allocation
     54     }
     55 }
     56 impl fmt::Debug for InstallTermsInput {
     57     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
     58         f.write_str("InstallTermsInput(<redacted>)")
     59     }
     60 }
     61 
     62 /// Closed normalized `orders.install_terms` data and separately domain-bound
     63 /// command/approval digests. Construction grants no execution or stock right.
     64 pub struct CapturedTermsInstallation {
     65     context: CommandContextData,
     66     input: InstallTermsInput,
     67     wire: Zeroizing<Box<[u8]>>,
     68     digest: CommandDigest,
     69     approval: ApprovalIntentDigest,
     70 }
     71 impl CapturedTermsInstallation {
     72     /// Requires seller and explicit order descriptors. Approval may be absent
     73     /// while obtaining consent; actual approval verification belongs to T0.
     74     pub fn new(
     75         context: CommandContextData,
     76         input: InstallTermsInput,
     77     ) -> Result<Self, CommandError> {
     78         if context.actor().role() != ActorRole::Seller || context.expected().is_none() {
     79             return Err(CommandError::InvalidInput);
     80         }
     81         let encode = |approval, correlation| -> Result<Zeroizing<Vec<u8>>, CommandError> {
     82             let bytes = Zeroizing::new(
     83                 serde_json::to_vec(&Wire {
     84                     context: &context,
     85                     input: &input,
     86                     approval,
     87                     correlation,
     88                 })
     89                 .map_err(|_| CommandError::InvalidInput)?,
     90             );
     91             if bytes.len() > COMMAND_MAX_BYTES {
     92                 return Err(CommandError::LimitExceeded);
     93             }
     94             Ok(bytes)
     95         };
     96         let wire = Zeroizing::new(encode(true, true)?.to_vec().into_boxed_slice());
     97         let digest =
     98             CommandDigest::from_bytes(hash(b"radroots.command.v1\0", &encode(true, false)?));
     99         let approval = ApprovalIntentDigest::from_bytes(hash(
    100             b"radroots.approval.v1\0",
    101             &encode(false, false)?,
    102         ));
    103         Ok(Self {
    104             context,
    105             input,
    106             wire,
    107             digest,
    108             approval,
    109         })
    110     }
    111     /// Parses only the bounded exact command/context/input shape, without IO.
    112     pub fn parse(raw: &[u8]) -> Result<Self, CommandError> {
    113         check_frame(raw)?;
    114         #[derive(Deserialize)]
    115         #[serde(deny_unknown_fields)]
    116         struct Root<'a> {
    117             #[serde(rename = "type")]
    118             kind: String,
    119             context: CommandContextData,
    120             #[serde(borrow)]
    121             input: &'a RawValue,
    122         }
    123         #[derive(Deserialize)]
    124         #[serde(deny_unknown_fields)]
    125         struct Input<'a> {
    126             proposal: RecordId,
    127             authorization: RecordId,
    128             terms_digest: TermsDigest,
    129             #[serde(borrow)]
    130             allocation: &'a RawValue,
    131         }
    132         let root: Root<'_> = parse_object(raw)?;
    133         if root.kind != "orders.install_terms" {
    134             return Err(CommandError::UnsupportedCommand);
    135         }
    136         let input: Input<'_> = parse_object(root.input.get().as_bytes())?;
    137         Self::new(
    138             root.context,
    139             InstallTermsInput::new(
    140                 input.proposal,
    141                 input.authorization,
    142                 input.terms_digest,
    143                 parse_selection(input.allocation.get().as_bytes())?,
    144             ),
    145         )
    146     }
    147     /// Captured context descriptors, not current native authority.
    148     pub const fn context(&self) -> &CommandContextData {
    149         &self.context
    150     }
    151     /// Exact selected source and allocation claims.
    152     pub const fn input(&self) -> &InstallTermsInput {
    153         &self.input
    154     }
    155     /// Bounded protected normalized command bytes, including correlation.
    156     pub fn protected_wire(&self) -> &[u8] {
    157         &self.wire
    158     }
    159     /// Command digest includes approval and excludes observer correlation.
    160     pub const fn digest(&self) -> CommandDigest {
    161         self.digest
    162     }
    163     /// Intent digest excludes approval and observer correlation.
    164     pub const fn approval_intent_digest(&self) -> ApprovalIntentDigest {
    165         self.approval
    166     }
    167 }
    168 impl fmt::Debug for CapturedTermsInstallation {
    169     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    170         f.write_str("CapturedTermsInstallation(<redacted>)")
    171     }
    172 }
    173 struct Wire<'a> {
    174     context: &'a CommandContextData,
    175     input: &'a InstallTermsInput,
    176     approval: bool,
    177     correlation: bool,
    178 }
    179 impl Serialize for Wire<'_> {
    180     fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
    181         let mut out = serializer.serialize_struct("TermsInstallation", 3)?;
    182         out.serialize_field(
    183             "context",
    184             &Context(self.context, self.approval, self.correlation),
    185         )?;
    186         out.serialize_field("input", &InputWire(self.input))?;
    187         out.serialize_field("type", "orders.install_terms")?;
    188         out.end()
    189     }
    190 }
    191 struct InputWire<'a>(&'a InstallTermsInput);
    192 impl Serialize for InputWire<'_> {
    193     fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
    194         let mut out = serializer.serialize_struct("InstallTermsInput", 4)?;
    195         out.serialize_field("allocation", &self.0.allocation)?;
    196         out.serialize_field("authorization", &self.0.authorization)?;
    197         out.serialize_field("proposal", &self.0.proposal)?;
    198         out.serialize_field("terms_digest", &self.0.terms)?;
    199         out.end()
    200     }
    201 }