local_outbox.rs (5178B)
1 //! Shared pure enrollment and protected grant rules for concrete local hosts. 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 /// Explicit local enrollment inputs, supplied by a trusted host interaction. 12 /// Creation is limited to an unrelated new outbox, never seller stock. 13 pub struct LocalOutboxConfig { 14 scope: WorkspaceScope, 15 participant: PublicKey, 16 endpoint_identity: PublicKey, 17 principal: String, 18 expires_at: UnixSeconds, 19 } 20 impl LocalOutboxConfig { 21 /// Selects a fresh buyer outbox with participant, local application identity, 22 /// device audit label and positive expiry. This performs no authentication 23 /// or filesystem work. Use `seller` for explicit fresh seller enrollment. 24 pub fn new( 25 participant: PublicKey, 26 endpoint_identity: PublicKey, 27 principal: impl AsRef<str>, 28 expires_at: UnixSeconds, 29 ) -> Result<Self, WorkspaceError> { 30 let principal = principal.as_ref(); 31 if principal.is_empty() 32 || principal.len() > 128 33 || principal.chars().any(char::is_control) 34 || expires_at.get() == 0 35 { 36 return Err(WorkspaceError::InvalidInput); 37 } 38 Ok(Self { 39 scope: WorkspaceScope::NewBuyerOutbox, 40 participant, 41 endpoint_identity, 42 principal: principal.to_owned(), 43 expires_at, 44 }) 45 } 46 /// Explicitly selects a fresh seller public-authoring/communication outbox. 47 /// This cannot upgrade a buyer grant or recover stock and old obligations. 48 pub fn seller( 49 participant: PublicKey, 50 endpoint_identity: PublicKey, 51 principal: impl AsRef<str>, 52 expires_at: UnixSeconds, 53 ) -> Result<Self, WorkspaceError> { 54 let mut config = Self::new(participant, endpoint_identity, principal, expires_at)?; 55 config.scope = WorkspaceScope::NewSellerOutbox; 56 Ok(config) 57 } 58 } 59 impl std::fmt::Debug for LocalOutboxConfig { 60 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 61 f.write_str("LocalOutboxConfig(<redacted>)") 62 } 63 } 64 65 impl LocalOutboxConfig { 66 /// Checks enrollment time without acquiring entropy or a host resource. 67 pub fn validate_at(&self, now: UnixSeconds) -> Result<(), WorkspaceError> { 68 if now.get() == 0 { 69 return Err(WorkspaceError::Unavailable); 70 } 71 if self.expires_at <= now { 72 return Err(WorkspaceError::GrantExpired); 73 } 74 Ok(()) 75 } 76 /// Pure trusted-host enrollment. Concrete hosts supply qualified fresh IDs 77 /// and a protected proof; this does not acquire or persist any authority. 78 pub fn enroll( 79 self, 80 now: UnixSeconds, 81 workspace: WorkspaceId, 82 executor: ExecutorId, 83 grant: GrantId, 84 proof: AttachmentProof, 85 ) -> Result<(AttachmentInput, AttachmentProof, WorkspaceGrant), WorkspaceError> { 86 self.validate_at(now)?; 87 let authority = AuthorityRef::new( 88 executor, 89 Generation::new(1).map_err(|_| WorkspaceError::InvalidInput)?, 90 ); 91 let endpoint = format!("local://workspace/{workspace}"); 92 let mut route = Sha256::new(); 93 route.update(self.scope.route_domain()); 94 route.update(workspace.as_bytes()); 95 route.update(executor.as_bytes()); 96 route.update(self.endpoint_identity.as_bytes()); 97 route.update(endpoint.as_bytes()); 98 let route = RouteDigest::from_bytes(route.finalize().into()); 99 let mut checkpoint = Sha256::new(); 100 checkpoint.update(self.scope.genesis_domain()); 101 checkpoint.update(workspace.as_bytes()); 102 checkpoint.update(executor.as_bytes()); 103 checkpoint.update(grant.as_bytes()); 104 // Genesis certifies only a fresh unrelated outbox. Partial coverage is 105 // deliberate: no old order, private history or stock has been recovered. 106 let checkpoint = CheckpointRef::new( 107 CheckpointDigest::from_bytes(checkpoint.finalize().into()), 108 Sequence::new(0).map_err(|_| WorkspaceError::InvalidInput)?, 109 Coverage::Partial, 110 vec![], 111 ) 112 .map_err(|_| WorkspaceError::InvalidInput)?; 113 let input = AttachmentInput::new( 114 workspace, 115 authority, 116 self.participant, 117 endpoint, 118 self.endpoint_identity, 119 grant, 120 checkpoint, 121 route, 122 )?; 123 let actor = ActorRef::new(self.participant, self.scope.actor_role(), None, Some(grant)); 124 let record = WorkspaceGrant::with_scope( 125 input.clone(), 126 self.scope, 127 actor, 128 self.principal, 129 self.expires_at, 130 &proof, 131 )?; 132 Ok((input, proof, record)) 133 } 134 }