lib

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

seller_stock.rs (4982B)


      1 //! Explicit pure enrollment policy for a fresh seller stock authority.
      2 //! No database, runtime, signer or provider is acquired here.
      3 use super::{
      4     AttachmentInput, AttachmentProof, WorkspaceError, WorkspaceScope, grant::WorkspaceGrant,
      5 };
      6 use radroots_domain::{
      7     ActorRef, AuthorityRef, CheckpointDigest, CheckpointRef, Coverage, ExecutorId, Generation,
      8     GrantId, PublicKey, RouteDigest, Sequence, UnixSeconds, WorkspaceId,
      9 };
     10 use sha2::{Digest, Sha256};
     11 mod authorization;
     12 pub use authorization::{StockAuthorizationRef, StockGrantRecord};
     13 mod selection;
     14 pub use selection::{SellerAuthoritySelection, StockCapabilityContract};
     15 mod permissions;
     16 pub use permissions::{StockAction, StockGrantScope, StockResources};
     17 mod source;
     18 pub use source::{StockListingCheckpointRef, StockSourceCheckpointRef};
     19 /// Explicit local enrollment inputs, supplied by a trusted host interaction.
     20 /// Only a qualified stock host may persist the resulting authority.
     21 pub struct SellerStockConfig {
     22     scope: WorkspaceScope,
     23     participant: PublicKey,
     24     endpoint_identity: PublicKey,
     25     principal: String,
     26     expires_at: UnixSeconds,
     27 }
     28 impl SellerStockConfig {
     29     /// Selects fresh seller stock with participant, local application identity,
     30     /// device audit label and positive expiry. This performs no authentication
     31     /// or filesystem work. It never recovers existing obligations.
     32     pub fn new(
     33         participant: PublicKey,
     34         endpoint_identity: PublicKey,
     35         principal: impl AsRef<str>,
     36         expires_at: UnixSeconds,
     37     ) -> Result<Self, WorkspaceError> {
     38         let principal = principal.as_ref();
     39         if principal.is_empty()
     40             || principal.len() > 128
     41             || principal.chars().any(char::is_control)
     42             || expires_at.get() == 0
     43         {
     44             return Err(WorkspaceError::InvalidInput);
     45         }
     46         Ok(Self {
     47             scope: WorkspaceScope::SellerStockOwner,
     48             participant,
     49             endpoint_identity,
     50             principal: principal.to_owned(),
     51             expires_at,
     52         })
     53     }
     54 }
     55 impl std::fmt::Debug for SellerStockConfig {
     56     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
     57         f.write_str("SellerStockConfig(<redacted>)")
     58     }
     59 }
     60 
     61 impl SellerStockConfig {
     62     /// Checks enrollment time without acquiring entropy or a host resource.
     63     pub fn validate_at(&self, now: UnixSeconds) -> Result<(), WorkspaceError> {
     64         if now.get() == 0 {
     65             return Err(WorkspaceError::Unavailable);
     66         }
     67         if self.expires_at <= now {
     68             return Err(WorkspaceError::GrantExpired);
     69         }
     70         Ok(())
     71     }
     72     /// Pure trusted-host enrollment. Concrete hosts supply qualified fresh IDs
     73     /// and a protected proof; this does not acquire or persist any authority.
     74     pub fn enroll(
     75         self,
     76         now: UnixSeconds,
     77         workspace: WorkspaceId,
     78         executor: ExecutorId,
     79         grant: GrantId,
     80         proof: AttachmentProof,
     81     ) -> Result<(AttachmentInput, AttachmentProof, WorkspaceGrant), WorkspaceError> {
     82         self.validate_at(now)?;
     83         let authority = AuthorityRef::new(
     84             executor,
     85             Generation::new(1).map_err(|_| WorkspaceError::InvalidInput)?,
     86         );
     87         let endpoint = format!("local://seller-stock/{workspace}");
     88         let mut route = Sha256::new();
     89         route.update(self.scope.route_domain());
     90         route.update(workspace.as_bytes());
     91         route.update(executor.as_bytes());
     92         route.update(self.endpoint_identity.as_bytes());
     93         route.update(endpoint.as_bytes());
     94         let route = RouteDigest::from_bytes(route.finalize().into());
     95         let mut checkpoint = Sha256::new();
     96         checkpoint.update(self.scope.genesis_domain());
     97         checkpoint.update(workspace.as_bytes());
     98         checkpoint.update(executor.as_bytes());
     99         checkpoint.update(grant.as_bytes());
    100         // Genesis certifies only a fresh unrelated stock namespace. Partial coverage is
    101         // deliberate: no old order, private history or inventory has been recovered.
    102         let checkpoint = CheckpointRef::new(
    103             CheckpointDigest::from_bytes(checkpoint.finalize().into()),
    104             Sequence::new(0).map_err(|_| WorkspaceError::InvalidInput)?,
    105             Coverage::Partial,
    106             vec![],
    107         )
    108         .map_err(|_| WorkspaceError::InvalidInput)?;
    109         let input = AttachmentInput::new(
    110             workspace,
    111             authority,
    112             self.participant,
    113             endpoint,
    114             self.endpoint_identity,
    115             grant,
    116             checkpoint,
    117             route,
    118         )?;
    119         let actor = ActorRef::new(self.participant, self.scope.actor_role(), None, Some(grant));
    120         let record = WorkspaceGrant::with_scope(
    121             input.clone(),
    122             self.scope,
    123             actor,
    124             self.principal,
    125             self.expires_at,
    126             &proof,
    127         )?;
    128         Ok((input, proof, record))
    129     }
    130 }