lib

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

mod.rs (5385B)


      1 //! Protected exact transport intent; persistence never performs delivery.
      2 mod codec;
      3 mod graph;
      4 mod json;
      5 mod observation;
      6 mod observation_operations;
      7 mod operations;
      8 
      9 use super::State;
     10 use radroots_domain::{CommandDigest, EnvelopeEventId, OperationId, UnixSeconds};
     11 use radroots_secrets::context::{
     12     EnvelopeContext, EnvelopePurpose, EnvelopeSubject, PayloadSchemaId,
     13 };
     14 use radroots_storage::command::{
     15     ArtifactEvidence, BoundSigningPlan, DeliveryAttempt, DeliveryIntent, OperationError,
     16     OperationSnapshot, SignedDmRelaySource, SigningTarget,
     17 };
     18 use sha2::{Digest, Sha256};
     19 
     20 #[derive(Clone, Copy)]
     21 struct Slot {
     22     operation: OperationId,
     23     recipient: u8,
     24     ordinal: u16,
     25     commitment: [u8; 32],
     26 }
     27 fn context(
     28     state: &State,
     29     slot: Slot,
     30     part: u8,
     31     index: bool,
     32 ) -> Result<EnvelopeContext, OperationError> {
     33     let subject = Sha256::new()
     34         .chain_update(b"radroots.browser.local_outbox.delivery.context.v2\0")
     35         .chain_update(state.selected.workspace_id().as_bytes())
     36         .chain_update(slot.operation.as_bytes())
     37         .chain_update([slot.recipient])
     38         .chain_update(slot.ordinal.to_be_bytes())
     39         .chain_update([part, u8::from(index)])
     40         .chain_update(slot.commitment)
     41         .finalize()
     42         .iter()
     43         .map(|b| format!("{b:02x}"))
     44         .collect::<String>();
     45     Ok(EnvelopeContext::new(
     46         EnvelopePurpose::parse("radroots.browser.workspace.delivery")
     47             .map_err(|_| OperationError::InvalidCommand)?,
     48         EnvelopeSubject::parse("workspace_delivery", subject)
     49             .map_err(|_| OperationError::InvalidCommand)?,
     50         PayloadSchemaId::parse(if index {
     51             "radroots.browser.workspace.delivery_index.v2"
     52         } else {
     53             "radroots.browser.workspace.delivery.v2"
     54         })
     55         .map_err(|_| OperationError::InvalidCommand)?,
     56     ))
     57 }
     58 fn routes(
     59     bound: &BoundSigningPlan,
     60     recipient: SigningTarget,
     61     intent: &DeliveryIntent,
     62 ) -> Result<(), OperationError> {
     63     if recipient == SigningTarget::PublicEvent {
     64         if bound.plan().listing().is_none()
     65             || bound
     66                 .plan()
     67                 .artifact(recipient)
     68                 .map_err(|_| OperationError::RecoveryRequired)?
     69                 .event_id()
     70                 != *intent.event_id().as_bytes()
     71         {
     72             return Err(OperationError::InvalidEvidence);
     73         }
     74         return Ok(());
     75     }
     76     let recipient = recipient
     77         .recipient()
     78         .ok_or(OperationError::InvalidEvidence)?;
     79     let mut selected = None;
     80     for route in bound
     81         .chat_plan()?
     82         .routes()
     83         .iter()
     84         .filter(|route| route.participant() == recipient)
     85     {
     86         let source = SignedDmRelaySource::verify(route.raw_json())?;
     87         source.validate_capture(route)?;
     88         if selected.replace(route).is_some() || route.selected_relays().is_empty() {
     89             return Err(OperationError::UnverifiedApproval);
     90         }
     91     }
     92     let route = selected.ok_or(OperationError::UnverifiedApproval)?;
     93     if intent.targets().targets().iter().any(|target| {
     94         !route
     95             .selected_relays()
     96             .iter()
     97             .any(|raw| matches!(target.matches_nostr_relay(raw.as_ref()), Ok(true)))
     98     }) {
     99         return Err(OperationError::UnverifiedApproval);
    100     }
    101     Ok(())
    102 }
    103 pub(super) use graph::Graph;
    104 
    105 #[derive(Clone, Copy)]
    106 pub(super) struct Selection<'a> {
    107     pub recipient: SigningTarget,
    108     pub request: &'a radroots_transport::sink::DeliveryRequestId,
    109     pub event: Option<EnvelopeEventId>,
    110     pub retain_seal: bool,
    111 }
    112 pub(super) struct Facts {
    113     pub snapshot: Option<OperationSnapshot>,
    114     pub selected: Option<DeliveryAttempt>,
    115     selected_slot: Option<Slot>,
    116     pub original: Option<ArtifactEvidence>,
    117     pub slots: [u16; 3],
    118 }
    119 impl State {
    120     pub(super) async fn delivery_facts(
    121         &self,
    122         view: &crate::ReadSnapshot,
    123         operation: OperationId,
    124         digest: Option<CommandDigest>,
    125         t0: &super::journal::Records,
    126         observed: UnixSeconds,
    127         selected: Option<Selection<'_>>,
    128     ) -> Result<Facts, OperationError> {
    129         let mut original = super::retained::Graph::read_for_output(
    130             self,
    131             view,
    132             operation,
    133             digest,
    134             t0,
    135             selected.filter(|s| s.retain_seal).map(|s| s.recipient),
    136         )
    137         .await?;
    138         let installed =
    139             super::installation::Records::read(self, view, &original, operation).await?;
    140         let deliveries =
    141             Graph::read(self, view, &original, &installed, operation, selected).await?;
    142         let output = original.take_output();
    143         let snapshot = original
    144             .into_snapshot(observed)?
    145             .map(|snapshot| {
    146                 snapshot
    147                     .with_installation(installed.message, installed.work)?
    148                     .with_public_installation(
    149                         installed.listing,
    150                         installed.public.into_iter().collect(),
    151                     )?
    152                     .with_delivery(deliveries.summaries, deliveries.observations)
    153             })
    154             .transpose()?;
    155         Ok(Facts {
    156             snapshot,
    157             selected: deliveries.selected,
    158             selected_slot: deliveries.selected_slot,
    159             original: output,
    160             slots: deliveries.slots,
    161         })
    162     }
    163 }