lib

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

inputs.rs (6382B)


      1 use super::Error;
      2 use js_sys::JsString;
      3 use radroots_domain::{CorrelationId, OperationId, PublicKey, SourceId, ThreadId, UnixSeconds};
      4 use wasm_bindgen::prelude::*;
      5 
      6 /// Pure untrusted first-issue descriptor, captured only against a real scope.
      7 #[wasm_bindgen]
      8 pub struct CommandRequest {
      9     operation: OperationId,
     10     deadline: UnixSeconds,
     11     correlation: Option<CorrelationId>,
     12 }
     13 #[wasm_bindgen]
     14 impl CommandRequest {
     15     #[wasm_bindgen(constructor)]
     16     pub fn new(
     17         operation_id: JsString,
     18         first_issue_not_after: JsString,
     19         correlation_id: Option<JsString>,
     20     ) -> Result<CommandRequest, JsValue> {
     21         Ok(Self {
     22             operation: bounded_string(operation_id, 64, "operation_id")?
     23                 .parse()
     24                 .map_err(|_| Error::input("operation_id"))?,
     25             deadline: bounded_string(first_issue_not_after, 12, "first_issue_not_after")?
     26                 .parse()
     27                 .map_err(|_| Error::input("first_issue_not_after"))?,
     28             correlation: correlation_id
     29                 .map(|v| {
     30                     bounded_string(v, 128, "correlation_id").and_then(|v| {
     31                         CorrelationId::new(v).map_err(|_| Error::input("correlation_id"))
     32                     })
     33                 })
     34                 .transpose()?,
     35         })
     36     }
     37 }
     38 impl CommandRequest {
     39     pub(super) fn capture(
     40         &self,
     41         scope: &crate::WorkspaceSession,
     42     ) -> Result<crate::CommandContext, Error> {
     43         scope
     44             .command_context_with_correlation(
     45                 self.operation,
     46                 None,
     47                 None,
     48                 self.deadline,
     49                 self.correlation.clone(),
     50             )
     51             .map_err(Error::from)
     52     }
     53 }
     54 /// Shared bounded initial listing input. Parsing has no host effects.
     55 #[wasm_bindgen]
     56 pub struct ListingInput {
     57     pub(super) inner: radroots_protocol::listing::PublishInput,
     58 }
     59 #[wasm_bindgen]
     60 impl ListingInput {
     61     pub fn parse(input: js_sys::Uint8Array) -> Result<ListingInput, JsValue> {
     62         if !input.is_instance_of::<js_sys::Uint8Array>() {
     63             return Err(Error::input("listing_input").into());
     64         }
     65         if input.length() as usize > radroots_protocol::command::COMMAND_MAX_BYTES {
     66             return Err(Error::command(
     67                 "listing_input",
     68                 radroots_protocol::command::CommandError::LimitExceeded,
     69             )
     70             .into());
     71         }
     72         let inner = radroots_protocol::listing::PublishInput::parse(&input.to_vec())
     73             .map_err(|v| Error::command("listing_input", v))?;
     74         Ok(Self { inner })
     75     }
     76 }
     77 /// Canonical public reference descriptor, without event or terms authentication.
     78 #[wasm_bindgen]
     79 pub struct ListingReference {
     80     inner: radroots_execution::inbox::ListingRevisionRef,
     81 }
     82 #[wasm_bindgen]
     83 impl ListingReference {
     84     #[wasm_bindgen(constructor)]
     85     pub fn new(
     86         author: JsString,
     87         discriminator: JsString,
     88         event_id: JsString,
     89     ) -> Result<ListingReference, JsValue> {
     90         let author = public_key(
     91             &bounded_string(author, 64, "listing_author")?,
     92             "listing_author",
     93         )?;
     94         let discriminator = bounded_string(discriminator, 32, "listing_discriminator")?;
     95         let event: SourceId = bounded_string(event_id, 64, "listing_event_id")?
     96             .parse()
     97             .map_err(|_| Error::input("listing_event_id"))?;
     98         let inner =
     99             radroots_execution::inbox::ListingRevisionRef::new(author, &discriminator, event)
    100                 .map_err(|v| Error::command("listing_reference", v))?;
    101         Ok(Self { inner })
    102     }
    103 }
    104 /// Private input retains the shared zeroizing owner, with no foreign text cache.
    105 #[wasm_bindgen]
    106 pub struct InquiryInput {
    107     inner: radroots_execution::inbox::InquireInput,
    108 }
    109 #[wasm_bindgen]
    110 impl InquiryInput {
    111     #[wasm_bindgen(constructor)]
    112     pub fn new(
    113         thread_id: JsString,
    114         peer: JsString,
    115         source: Option<ListingReference>,
    116         text: JsString,
    117     ) -> Result<InquiryInput, JsValue> {
    118         let text = zeroize::Zeroizing::new(bounded_string(
    119             text,
    120             radroots_domain::CHAT_MAX_BYTES,
    121             "inquiry_input",
    122         )?);
    123         let thread: ThreadId = bounded_string(thread_id, 64, "thread_id")?
    124             .parse()
    125             .map_err(|_| Error::input("thread_id"))?;
    126         let peer = public_key(&bounded_string(peer, 64, "peer")?, "peer")?;
    127         let inner = radroots_execution::inbox::InquireInput::new(
    128             thread,
    129             peer,
    130             source.map(|v| v.inner),
    131             &text,
    132         )
    133         .map_err(|v| Error::command("inquiry_input", v))?;
    134         Ok(Self { inner })
    135     }
    136 }
    137 impl InquiryInput {
    138     pub(super) fn captured_copy(&self) -> Result<radroots_execution::inbox::InquireInput, Error> {
    139         radroots_execution::inbox::InquireInput::new(
    140             self.inner.thread_id(),
    141             self.inner.peer(),
    142             self.inner.source().cloned(),
    143             self.inner.text(),
    144         )
    145         .map_err(|v| Error::command("inquiry_input", v))
    146     }
    147 }
    148 // Check UTF-16 size before allocating a Rust copy, reject malformed surrogate
    149 // sequences instead of replacing them, then enforce exact UTF-8 size. Shared
    150 // parsers still own every semantic invariant. Partial private copies zeroize.
    151 pub(super) fn bounded_string(
    152     value: JsString,
    153     max: usize,
    154     field: &'static str,
    155 ) -> Result<String, Error> {
    156     if !value.is_string() {
    157         return Err(Error::input(field));
    158     }
    159     let units = value.length() as usize;
    160     let limit = || {
    161         Error::command(
    162             field,
    163             radroots_protocol::command::CommandError::LimitExceeded,
    164         )
    165     };
    166     if units > max {
    167         return Err(limit());
    168     }
    169     let mut output = zeroize::Zeroizing::new(String::with_capacity(units));
    170     for value in char::decode_utf16(value.iter()) {
    171         let value = value.map_err(|_| Error::input(field))?;
    172         if output.len() + value.len_utf8() > max {
    173             return Err(limit());
    174         }
    175         output.push(value);
    176     }
    177     Ok(std::mem::take(&mut *output))
    178 }
    179 fn public_key(value: &str, field: &'static str) -> Result<PublicKey, Error> {
    180     let key = PublicKey::from_hex(value).map_err(|_| Error::input(field))?;
    181     if key.to_hex() != value {
    182         return Err(Error::input(field));
    183     }
    184     Ok(key)
    185 }