lib

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

selection.rs (7669B)


      1 //! Caller-independent seller namespace metadata, without authority or custody.
      2 use crate::workspace::{AttachmentInput, WorkspaceError};
      3 use radroots_domain::{AuthorityRef, CheckpointRef, GrantId, PublicKey, RouteDigest, WorkspaceId};
      4 
      5 /// The source-owned concrete local inventory contract supported by this host.
      6 /// This is neither a public event-kind allocation nor remote pairing evidence.
      7 #[derive(Clone, Copy, Debug, Eq, PartialEq)]
      8 pub struct StockCapabilityContract;
      9 impl StockCapabilityContract {
     10     pub const fn id(self) -> &'static str {
     11         "radroots.seller_stock.native_inventory"
     12     }
     13     pub const fn version(self) -> u16 {
     14         1
     15     }
     16 }
     17 #[cfg(feature = "serde")]
     18 impl serde::Serialize for StockCapabilityContract {
     19     fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
     20         use serde::ser::SerializeStruct;
     21         let mut value = serializer.serialize_struct("StockCapabilityContract", 2)?;
     22         value.serialize_field("id", self.id())?;
     23         value.serialize_field("version", "1")?;
     24         value.end()
     25     }
     26 }
     27 #[cfg(feature = "serde")]
     28 impl<'de> serde::Deserialize<'de> for StockCapabilityContract {
     29     fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
     30         #[derive(serde::Deserialize)]
     31         #[serde(deny_unknown_fields)]
     32         struct Wire {
     33             id: String,
     34             version: String,
     35         }
     36         struct Visitor;
     37         impl<'de> serde::de::Visitor<'de> for Visitor {
     38             type Value = Wire;
     39             fn expecting(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
     40                 f.write_str("the supported native inventory contract object")
     41             }
     42             fn visit_map<A: serde::de::MapAccess<'de>>(self, map: A) -> Result<Wire, A::Error> {
     43                 <Wire as serde::Deserialize>::deserialize(
     44                     serde::de::value::MapAccessDeserializer::new(map),
     45                 )
     46             }
     47         }
     48         let v = deserializer.deserialize_map(Visitor)?;
     49         if v.id != Self.id() || v.version != "1" {
     50             return Err(serde::de::Error::custom(
     51                 "unsupported native inventory contract",
     52             ));
     53         }
     54         Ok(Self)
     55     }
     56 }
     57 /// Exact namespace, authority and route selection. A caller grant is supplied
     58 /// separately. This metadata authenticates neither an endpoint nor a writer,
     59 /// and its checkpoint establishes no independently protected provenance.
     60 #[derive(Clone, Eq, PartialEq)]
     61 #[cfg_attr(feature = "serde", derive(serde::Serialize))]
     62 pub struct SellerAuthoritySelection {
     63     capability_contract: StockCapabilityContract,
     64     workspace_id: WorkspaceId,
     65     authority: AuthorityRef,
     66     participant: PublicKey,
     67     endpoint: Box<str>,
     68     endpoint_identity: PublicKey,
     69     checkpoint: CheckpointRef,
     70     route_digest: RouteDigest,
     71 }
     72 impl SellerAuthoritySelection {
     73     /// Preserves all validated namespace fields while omitting the caller grant.
     74     pub fn from_attachment(input: &AttachmentInput) -> Self {
     75         Self {
     76             capability_contract: StockCapabilityContract,
     77             workspace_id: input.workspace_id(),
     78             authority: input.authority(),
     79             participant: input.participant(),
     80             endpoint: input.endpoint().into(),
     81             endpoint_identity: input.endpoint_identity(),
     82             checkpoint: input.checkpoint().clone(),
     83             route_digest: input.route_digest(),
     84         }
     85     }
     86     /// Selects a claimed caller grant; this creates no credential or permission.
     87     pub fn attachment(&self, grant: GrantId) -> Result<AttachmentInput, WorkspaceError> {
     88         AttachmentInput::new(
     89             self.workspace_id,
     90             self.authority,
     91             self.participant,
     92             self.endpoint.to_string(),
     93             self.endpoint_identity,
     94             grant,
     95             self.checkpoint.clone(),
     96             self.route_digest,
     97         )
     98     }
     99     /// Compares complete namespace metadata independently of the caller grant.
    100     pub fn matches_attachment(&self, input: &AttachmentInput) -> bool {
    101         self.workspace_id == input.workspace_id()
    102             && self.authority == input.authority()
    103             && self.participant == input.participant()
    104             && self.endpoint.as_ref() == input.endpoint()
    105             && self.endpoint_identity == input.endpoint_identity()
    106             && &self.checkpoint == input.checkpoint()
    107             && self.route_digest == input.route_digest()
    108     }
    109     pub const fn capability_contract(&self) -> StockCapabilityContract {
    110         self.capability_contract
    111     }
    112     /// Selected private namespace identity.
    113     pub const fn workspace_id(&self) -> WorkspaceId {
    114         self.workspace_id
    115     }
    116     /// Selected executor and authority generation.
    117     pub const fn authority(&self) -> AuthorityRef {
    118         self.authority
    119     }
    120     /// Declared stock owner, without signer or actor proof.
    121     pub const fn participant(&self) -> PublicKey {
    122         self.participant
    123     }
    124     /// Explicit selected route endpoint, without pairing or endpoint trust.
    125     pub fn endpoint(&self) -> &str {
    126         &self.endpoint
    127     }
    128     /// Pinned claimed application identity.
    129     pub const fn endpoint_identity(&self) -> PublicKey {
    130         self.endpoint_identity
    131     }
    132     /// Required checkpoint claim, without custody or coverage provenance.
    133     pub const fn checkpoint(&self) -> &CheckpointRef {
    134         &self.checkpoint
    135     }
    136     /// Exact declared route commitment.
    137     pub const fn route_digest(&self) -> RouteDigest {
    138         self.route_digest
    139     }
    140 }
    141 impl core::fmt::Debug for SellerAuthoritySelection {
    142     fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
    143         f.write_str("SellerAuthoritySelection(<untrusted descriptor>)")
    144     }
    145 }
    146 #[cfg(feature = "serde")]
    147 impl<'de> serde::Deserialize<'de> for SellerAuthoritySelection {
    148     fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
    149         #[derive(serde::Deserialize)]
    150         #[serde(deny_unknown_fields)]
    151         struct Wire {
    152             capability_contract: StockCapabilityContract,
    153             workspace_id: WorkspaceId,
    154             authority: AuthorityRef,
    155             participant: PublicKey,
    156             endpoint: String,
    157             endpoint_identity: PublicKey,
    158             checkpoint: CheckpointRef,
    159             route_digest: RouteDigest,
    160         }
    161         struct Visitor;
    162         impl<'de> serde::de::Visitor<'de> for Visitor {
    163             type Value = Wire;
    164             fn expecting(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
    165                 f.write_str("a closed seller namespace object")
    166             }
    167             fn visit_map<A: serde::de::MapAccess<'de>>(self, map: A) -> Result<Wire, A::Error> {
    168                 <Wire as serde::Deserialize>::deserialize(
    169                     serde::de::value::MapAccessDeserializer::new(map),
    170                 )
    171             }
    172         }
    173         let v = deserializer.deserialize_map(Visitor)?;
    174         if v.endpoint.is_empty()
    175             || v.endpoint.len() > 2048
    176             || v.endpoint.chars().any(char::is_control)
    177         {
    178             return Err(serde::de::Error::custom(
    179                 "invalid seller namespace selection",
    180             ));
    181         }
    182         Ok(Self {
    183             capability_contract: v.capability_contract,
    184             workspace_id: v.workspace_id,
    185             authority: v.authority,
    186             participant: v.participant,
    187             endpoint: v.endpoint.into_boxed_str(),
    188             endpoint_identity: v.endpoint_identity,
    189             checkpoint: v.checkpoint,
    190             route_digest: v.route_digest,
    191         })
    192     }
    193 }