Skip to main content

datumesh_core/
envelope.rs

1//! Response envelopes (5.5): the mesh's unsigned statement around a receipt.
2
3use crate::error::{err, ErrorCode, Result};
4use crate::json::{self, Value};
5use crate::keys::Keys;
6use crate::lexical::{self, limits};
7use crate::receipt::Receipt;
8use crate::schema;
9
10/// The eight outcomes of 5.5.
11#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
12pub enum Outcome {
13    /// 200: the receipt answers the cell as of the requested `asof`: its `asof` is at or
14    /// before the requested one, and the mesh asserts it is the cell's latest version known
15    /// as of then.
16    Ok,
17    /// 200: as `Ok`, except that the mesh cannot assert the receipt is current; it serves the
18    /// last receipt it knows.
19    Stale,
20    /// 404: no receipt for this cell. With a `fault` (`spec-0.4.0`, see
21    /// [`check_envelope_extras`]), the cell is a computed cell whose evaluation faulted.
22    NotCovered,
23    /// 403: the requester may not receive this cell.
24    Unlicensed,
25    /// 402: available for the quoted price.
26    PaymentRequired,
27    /// 429: too many requests.
28    RateLimited,
29    /// 400: the cell URI does not parse.
30    BadRequest,
31    /// 5xx: the mesh failed.
32    Error,
33}
34
35impl Outcome {
36    /// The outcome as written in JSON.
37    pub const fn as_str(self) -> &'static str {
38        match self {
39            Outcome::Ok => "ok",
40            Outcome::Stale => "stale",
41            Outcome::NotCovered => "not_covered",
42            Outcome::Unlicensed => "unlicensed",
43            Outcome::PaymentRequired => "payment_required",
44            Outcome::RateLimited => "rate_limited",
45            Outcome::BadRequest => "bad_request",
46            Outcome::Error => "error",
47        }
48    }
49}
50
51/// The mesh's trust label (5.5).
52#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
53pub enum Trust {
54    /// Signed by the namespace's host, with no inputs.
55    Primary,
56    /// The mesh says it recomputed the receipt.
57    Verified,
58    /// Neither.
59    Unverified,
60}
61
62impl Trust {
63    /// The label as written in JSON.
64    pub const fn as_str(self) -> &'static str {
65        match self {
66            Trust::Primary => "primary",
67            Trust::Verified => "verified",
68            Trust::Unverified => "unverified",
69        }
70    }
71}
72
73/// A formula evaluation fault (SPEC 5.5, 15.11; `spec-0.4.0`): the optional `fault` member
74/// of a `not_covered` envelope. The cell has no value, deterministically; this is not
75/// missing data. These are formula codes, not verification codes, so they are not
76/// [`ErrorCode`]s.
77#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
78#[non_exhaustive]
79pub enum Fault {
80    /// `E_FORMULA_DIV_ZERO`
81    DivZero,
82    /// `E_FORMULA_DOMAIN`
83    Domain,
84    /// `E_FORMULA_RANGE`
85    Range,
86    /// `E_FORMULA_EMPTY`
87    Empty,
88    /// `E_FORMULA_ABSENT`
89    Absent,
90    /// `E_FORMULA_FUTURE`
91    Future,
92}
93
94impl Fault {
95    /// Every fault an envelope may carry, in the order of SPEC 5.5.
96    pub const ALL: &'static [Fault] =
97        &[Fault::DivZero, Fault::Domain, Fault::Range, Fault::Empty, Fault::Absent, Fault::Future];
98
99    /// The code as written, e.g. `"E_FORMULA_DIV_ZERO"`.
100    pub const fn as_str(self) -> &'static str {
101        match self {
102            Fault::DivZero => "E_FORMULA_DIV_ZERO",
103            Fault::Domain => "E_FORMULA_DOMAIN",
104            Fault::Range => "E_FORMULA_RANGE",
105            Fault::Empty => "E_FORMULA_EMPTY",
106            Fault::Absent => "E_FORMULA_ABSENT",
107            Fault::Future => "E_FORMULA_FUTURE",
108        }
109    }
110
111    /// Looks a fault up by its code; `None` for any other text (a budget, a cycle or too deep
112    /// an evaluation is not a fault but the mesh's failure, SPEC 5.5).
113    pub fn from_code(text: &str) -> Option<Fault> {
114        Fault::ALL.iter().copied().find(|f| f.as_str() == text)
115    }
116}
117
118impl core::fmt::Display for Fault {
119    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
120        f.write_str(self.as_str())
121    }
122}
123
124/// The members of a checked envelope that [`Envelope`] has no field for, because they were
125/// added to SPEC 5.5 after it was frozen (0.3.0): see [`check_envelope_extras`].
126#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
127#[non_exhaustive]
128pub struct EnvelopeExtras {
129    /// The formula evaluation fault of a `not_covered` envelope (`spec-0.4.0`).
130    pub fault: Option<Fault>,
131    /// The function the answer is of: the echo of a pin, or the hash the name resolved to
132    /// (`spec-0.4.2`).
133    pub fn_hash: Option<String>,
134}
135
136/// A price quote (`payment_required`).
137#[derive(Clone, Debug, PartialEq, Eq, Hash)]
138pub struct Quote {
139    /// A canonical decimal greater than zero.
140    pub amount: String,
141    /// A currency code from `units.md`.
142    pub currency: String,
143    /// Where to pay: an https URL.
144    pub authorise: String,
145}
146
147/// What the client asked for, for the binding of 5.5 step 5.
148#[derive(Clone, Debug, PartialEq, Eq, Hash)]
149pub struct Request {
150    pub(crate) cell_id: String,
151    pub(crate) asof: Option<String>,
152    pub(crate) job: Option<String>,
153    pub(crate) pin: Option<String>,
154    pub(crate) mesh: Option<String>,
155}
156
157/// A malformed request description (5.5 step 5): the caller's mistake, not an `E_*` code.
158#[derive(Clone, Debug, PartialEq, Eq, Hash)]
159pub struct InvalidRequest(String);
160
161impl core::fmt::Display for InvalidRequest {
162    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
163        write!(f, "invalid request: {}", self.0)
164    }
165}
166
167impl std::error::Error for InvalidRequest {}
168
169impl Request {
170    /// A request for the cell with id `cell_id` (64 lowercase hex digits) as of `asof` (an
171    /// instant) or now. Anything else is [`InvalidRequest`]: SPEC 5.5 makes a malformed
172    /// request the caller's error, reported outside the `E_*` codes.
173    pub fn new(cell_id: &str, asof: Option<&str>) -> core::result::Result<Request, InvalidRequest> {
174        if lexical::check_hex(cell_id, 64, "").is_err() {
175            return Err(InvalidRequest(format!("cell_id must be 64 lowercase hex digits: {cell_id:?}")));
176        }
177        if let Some(a) = asof {
178            if !lexical::is_instant(a) {
179                return Err(InvalidRequest(format!("asof must be an instant: {a:?}")));
180            }
181        }
182        Ok(Request { cell_id: cell_id.into(), asof: asof.map(String::from), job: None, pin: None, mesh: None })
183    }
184
185    /// The request description for `GET` of a job's `poll` URL (5.5, `spec-0.4.1`): the
186    /// original request (`cell_id`, `asof`) and the job polled, 32 to 64 lowercase hex digits
187    /// (otherwise [`InvalidRequest`]). With it, an envelope may be `bad_request` with
188    /// `E_JOB_UNKNOWN`, and a `pending` one must carry this `job`; any other answer is bound
189    /// to the original request, as a `/cell` answer is.
190    pub fn with_job(cell_id: &str, asof: Option<&str>, job: &str) -> core::result::Result<Request, InvalidRequest> {
191        if !is_job(job) {
192            return Err(InvalidRequest(format!("job must be 32 to 64 lowercase hex digits: {job:?}")));
193        }
194        let mut r = Request::new(cell_id, asof)?;
195        r.job = Some(job.into());
196        Ok(r)
197    }
198
199    /// The same request, pinned to the function `fn_hash` (64 lowercase hex digits; otherwise
200    /// [`InvalidRequest`]): the answer must echo it (5.5, `spec-0.4.2`). `cell_id` is then the
201    /// id of the cell of that function.
202    pub fn with_pin(self, fn_hash: &str) -> core::result::Result<Request, InvalidRequest> {
203        if lexical::check_hex(fn_hash, 64, "").is_err() {
204            return Err(InvalidRequest(format!("fn_hash must be 64 lowercase hex digits: {fn_hash:?}")));
205        }
206        Ok(Request { pin: Some(fn_hash.into()), ..self })
207    }
208
209    /// The same request, naming the host it was sent to (a DNS name; otherwise
210    /// [`InvalidRequest`]): `computed` is then checked against the receipt's signer, and
211    /// `poll` against the host (5.5, `spec-0.4.2`). Without it those checks are the caller's.
212    pub fn with_mesh(self, host: &str) -> core::result::Result<Request, InvalidRequest> {
213        if lexical::check_dns_name(host, "").is_err() {
214            return Err(InvalidRequest(format!("mesh must be a DNS name: {host:?}")));
215        }
216        Ok(Request { mesh: Some(host.into()), ..self })
217    }
218
219    /// The pin, for a pinned request ([`Request::with_pin`]).
220    pub fn fn_hash(&self) -> Option<&str> {
221        self.pin.as_deref()
222    }
223
224    /// The host the request was sent to, when given ([`Request::with_mesh`]).
225    pub fn mesh(&self) -> Option<&str> {
226        self.mesh.as_deref()
227    }
228
229    /// The job polled, for a request made at a job URL ([`Request::with_job`]).
230    pub fn job(&self) -> Option<&str> {
231        self.job.as_deref()
232    }
233
234    /// The requested cell id.
235    pub fn cell_id(&self) -> &str {
236        &self.cell_id
237    }
238
239    /// The requested `asof`, if any.
240    pub fn asof(&self) -> Option<&str> {
241        self.asof.as_deref()
242    }
243}
244
245/// A checked envelope.
246#[derive(Clone, Debug, PartialEq, Eq)]
247pub struct Envelope {
248    /// The outcome.
249    pub outcome: Outcome,
250    /// The receipt (`ok`, `stale`), signature-valid and bound to the request.
251    pub receipt: Option<Receipt>,
252    /// The trust label (`ok`, `stale`).
253    pub trust: Option<Trust>,
254    /// Seconds since the mesh last confirmed the receipt (`stale`).
255    pub age_seconds: Option<u64>,
256    /// Hash of a newer receipt for the same cell.
257    pub superseded_by: Option<String>,
258    /// Why the receipt was superseded.
259    pub reason: Option<String>,
260    /// The quote (`payment_required`).
261    pub quote: Option<Quote>,
262    /// Seconds to wait (`rate_limited`).
263    pub retry_after_seconds: Option<u64>,
264    /// The failure code (`bad_request`, `error`).
265    pub error: Option<ErrorCode>,
266}
267
268pub(crate) const BAD_REQUEST_CODES: [ErrorCode; 15] = [
269    ErrorCode::ParseUri,
270    ErrorCode::CanonName,
271    ErrorCode::CanonArgName,
272    ErrorCode::CanonArgDuplicate,
273    ErrorCode::CanonArgOrder,
274    ErrorCode::CanonPercent,
275    ErrorCode::CanonType,
276    ErrorCode::CanonNumber,
277    ErrorCode::CanonDate,
278    ErrorCode::CanonInstant,
279    ErrorCode::CanonString,
280    ErrorCode::CanonBlob,
281    ErrorCode::CanonLimit,
282    ErrorCode::ArgUnknown,
283    ErrorCode::ArgMissing,
284];
285
286pub(crate) fn env_err<T>(path: &str, detail: impl Into<String>) -> Result<T> {
287    err(ErrorCode::Envelope, path, detail)
288}
289
290/// `0` or `[1-9][0-9]*`, at most 2^53 - 1, judged on the number's text.
291pub(crate) fn integer_member(v: &Value, path: &str) -> Result<u64> {
292    let Some(t) = v.as_number_text() else {
293        return env_err(path, "must be a non-negative integer");
294    };
295    let ok =
296        !t.is_empty() && t.bytes().all(|c| c.is_ascii_digit()) && (t == "0" || !t.starts_with('0')) && t.len() <= 16;
297    match (ok, t.parse::<u64>()) {
298        (true, Ok(x)) if x <= limits::BLOB_LENGTH => Ok(x),
299        _ => env_err(path, format!("must be written 0|[1-9][0-9]* and at most 2^53 - 1, not {t}")),
300    }
301}
302
303/// Checks a response envelope (5.5) in the order of its procedure: strict JSON, members,
304/// HTTP status, the receipt through step 6 of 10.1, the binding to `request`, then `trust`
305/// and `superseded_by`. Never touches the network. A `not_covered` envelope's `fault`
306/// (`spec-0.4.0`) is checked and accepted; [`check_envelope_extras`] also returns it.
307pub fn check_envelope(status: u16, body: &[u8], keys: &Keys, request: &Request) -> Result<Envelope> {
308    check_envelope_extras(status, body, keys, request).map(|(env, _)| env)
309}
310
311/// [`check_envelope`], also returning the members [`Envelope`] has no field for
312/// ([`EnvelopeExtras`]): the `fault` of a `not_covered` envelope, one of the six formula
313/// evaluation faults of SPEC 5.5 (`E_ENVELOPE` for any other value, or on another outcome).
314///
315/// Its [`Envelope`] has no `pending` outcome, so a `pending` envelope is `E_ENVELOPE` here, as
316/// in 0.3.4; [`check_envelope_any`] accepts it. Nor has its [`Trust`] `computed` (`spec-0.4.2`):
317/// an envelope with it is `E_ENVELOPE` here and in [`check_envelope_any`], as in 0.3.5;
318/// [`crate::check_answer`] accepts it. Everything else of `spec-0.4.2` (the `fn_hash` echo, in
319/// [`EnvelopeExtras`]; `not_covered`'s `error`; the new codes) is checked as there.
320pub fn check_envelope_extras(
321    status: u16,
322    body: &[u8],
323    keys: &Keys,
324    request: &Request,
325) -> Result<(Envelope, EnvelopeExtras)> {
326    crate::answer::check_answer(status, body, keys, request)?.into_envelope()
327}
328
329/// Checks a response envelope (5.5) of any of the nine outcomes, `pending` included
330/// (`spec-0.4.1`), in the order of its procedure, and binds it to `request`: a `pending`
331/// envelope's echoed request to the request made (and its `job` to the job polled, when
332/// `request` has one), any other outcome to the original request's cell id and `asof`,
333/// whether it answers the `/cell` request or the job URL. `E_JOB_UNKNOWN` is accepted only in a
334/// `bad_request` answering a job URL (`request` with a job). Never touches the network.
335pub fn check_envelope_any(status: u16, body: &[u8], keys: &Keys, request: &Request) -> Result<AnyEnvelope> {
336    let a = crate::answer::check_answer(status, body, keys, request)?;
337    if let Some(p) = a.pending {
338        return Ok(AnyEnvelope::Pending(p));
339    }
340    a.into_envelope().map(|(e, x)| AnyEnvelope::Envelope(e, x))
341}
342
343/// `[0-9a-f]{32,64}`.
344pub(crate) fn is_job(s: &str) -> bool {
345    (32..=64).contains(&s.len()) && s.bytes().all(|c| c.is_ascii_digit() || (b'a'..=b'f').contains(&c))
346}
347
348/// The answer to a request: a [`Pending`] envelope, or one of the eight outcomes an
349/// [`Envelope`] holds, with its [`EnvelopeExtras`] (see [`check_envelope_any`]).
350// Not boxed: one value per checked response, and matching on it stays plain.
351#[allow(clippy::large_enum_variant)]
352#[derive(Clone, Debug, PartialEq, Eq)]
353#[non_exhaustive]
354pub enum AnyEnvelope {
355    /// `ok`, `stale`, `not_covered`, `unlicensed`, `payment_required`, `rate_limited`,
356    /// `bad_request` or `error`.
357    Envelope(Envelope, EnvelopeExtras),
358    /// `pending` (202): the mesh is still computing; the answer will be at `poll`.
359    Pending(Pending),
360}
361
362impl AnyEnvelope {
363    /// The outcome as written in JSON, `"pending"` included.
364    pub fn outcome_str(&self) -> &'static str {
365        match self {
366            AnyEnvelope::Envelope(e, _) => e.outcome.as_str(),
367            AnyEnvelope::Pending(_) => "pending",
368        }
369    }
370}
371
372/// A `pending` envelope (5.5, `spec-0.4.1`): the job, an estimate, the URL to poll and the
373/// original request echoed. Build one with [`Pending::new`] (a mesh), or get one from
374/// [`check_envelope_any`] (a client).
375#[derive(Clone, Debug, PartialEq, Eq, Hash)]
376#[non_exhaustive]
377pub struct Pending {
378    /// The job id: 32 to 64 lowercase hex digits, a capability.
379    pub job: String,
380    /// The mesh's estimate of the seconds until the result is ready; a hint.
381    pub estimate_seconds: u64,
382    /// The job's URL: `https://` and printable ASCII, at most 512 characters, ending
383    /// `/jobs/<job>`.
384    pub poll: String,
385    /// The original request, echoed: its `cell_id`, `asof` and pin (never a job or a mesh).
386    pub request: Request,
387}
388
389impl Pending {
390    /// The HTTP status of a `pending` envelope.
391    pub const STATUS: u16 = 202;
392
393    /// A `pending` envelope answering `request` (its `cell_id`, `asof` and pin are echoed; a
394    /// job or mesh in it is not), checked by the rules of 5.5: `job` 32 to 64 lowercase hex digits,
395    /// `estimate_seconds` at most 2^53 - 1, `poll` an `https://` URL of printable ASCII of at
396    /// most 512 characters whose host (up to the first `/`) is a DNS name (2.1), ending
397    /// `/jobs/<job>`. That `poll` names the mesh's own host is the caller's to ensure. A failure
398    /// is `E_ENVELOPE`, at the member's path.
399    pub fn new(job: &str, estimate_seconds: u64, poll: &str, request: Request) -> Result<Pending> {
400        check_job_and_poll(job, poll)?;
401        if estimate_seconds > limits::BLOB_LENGTH {
402            return env_err("$.estimate_seconds", "estimate_seconds is at most 2^53 - 1");
403        }
404        let request = Request { job: None, mesh: None, ..request };
405        Ok(Pending { job: job.into(), estimate_seconds, poll: poll.into(), request })
406    }
407
408    /// The JSON form of the envelope.
409    pub fn to_value(&self) -> Value {
410        // Value has no public number constructor: the canonical text, parsed.
411        json::parse(&self.to_json()).unwrap_or(Value::Null)
412    }
413
414    /// The envelope's body: its canonical JSON text (JCS), served with [`Pending::STATUS`].
415    pub fn to_json(&self) -> Vec<u8> {
416        let mut out = String::new();
417        out.push_str("{\"estimate_seconds\":");
418        out.push_str(&self.estimate_seconds.to_string());
419        out.push_str(",\"job\":");
420        json::write_string(&self.job, &mut out);
421        out.push_str(",\"outcome\":\"pending\",\"poll\":");
422        json::write_string(&self.poll, &mut out);
423        out.push_str(",\"request\":{");
424        if let Some(a) = &self.request.asof {
425            out.push_str("\"asof\":");
426            json::write_string(a, &mut out);
427            out.push(',');
428        }
429        out.push_str("\"cell_id\":");
430        json::write_string(&self.request.cell_id, &mut out);
431        if let Some(p) = &self.request.pin {
432            out.push_str(",\"fn_hash\":");
433            json::write_string(p, &mut out);
434        }
435        out.push_str("}}");
436        out.into_bytes()
437    }
438}
439
440pub(crate) fn check_job_and_poll(job: &str, poll: &str) -> Result<()> {
441    if !is_job(job) {
442        return env_err("$.job", "job is 32 to 64 lowercase hex digits");
443    }
444    let url_ok =
445        poll.strip_prefix("https://").is_some_and(|r| !r.is_empty() && r.bytes().all(|c| (0x21..=0x7E).contains(&c)))
446            && poll.len() <= limits::LICENCE_URL;
447    if !url_ok {
448        return env_err("$.poll", "poll is an https URL of printable ASCII, at most 512 characters");
449    }
450    // The text between https:// and the next / is a DNS name (2.1; 3f79313).
451    let host = poll["https://".len()..].split('/').next().unwrap_or("");
452    if !poll["https://".len()..].contains('/') || lexical::check_dns_name(host, "").is_err() {
453        return env_err("$.poll", "poll's host is a DNS name");
454    }
455    if !poll.ends_with(&format!("/jobs/{job}")) {
456        return env_err("$.poll", "poll ends /jobs/<job>");
457    }
458    Ok(())
459}
460
461pub(crate) fn quote(q: &Value) -> Result<Quote> {
462    let m = schema::object_or(q, "$.quote", ErrorCode::Envelope, "quote")?;
463    schema::exact(m, &["amount", "authorise", "currency"], &[], "$.quote", ErrorCode::Envelope)?;
464    let amount = schema::string_or(m, "amount", "$.quote", ErrorCode::Envelope)?;
465    let authorise = schema::string_or(m, "authorise", "$.quote", ErrorCode::Envelope)?;
466    let currency = schema::string_or(m, "currency", "$.quote", ErrorCode::Envelope)?;
467    if lexical::check_number(amount, "").is_err() || amount.starts_with('-') || amount == "0" {
468        return env_err("$.quote.amount", "amount is a canonical decimal greater than zero");
469    }
470    let url_ok = authorise
471        .strip_prefix("https://")
472        .is_some_and(|r| !r.is_empty() && r.bytes().all(|c| (0x21..=0x7E).contains(&c)))
473        && authorise.len() <= limits::LICENCE_URL;
474    if !url_ok {
475        return env_err("$.quote.authorise", "authorise is an https URL");
476    }
477    if !lexical::is_currency(currency) {
478        return env_err("$.quote.currency", "currency is a currency code from units.md");
479    }
480    Ok(Quote { amount: amount.into(), currency: currency.into(), authorise: authorise.into() })
481}