datumesh-core 0.3.1

Datumesh protocol core: strict JSON and JCS, canonical forms, cell URIs, receipts, keys, envelopes and offline verification.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
//! Function descriptors (7), units (7.1, `units.md`) and unit resolution (`units.md` 3).

use std::collections::BTreeMap;

use crate::error::{err, index, member, ErrorCode, Result};
use crate::json::{self, jcs_sorted, Value};
use crate::lexical::{self, limits};
use crate::model::{Arg, Cell, Scalar, Type};
use crate::schema;

/// One side of a rate: a currency code or an argument reference.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum RateSide {
    /// A currency code from `units.md` 2.1.
    Currency(String),
    /// `{"arg": name}`: the value of a string argument.
    Arg(String),
}

/// A unit in a signature (7.1).
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum Unit {
    /// A unit listed in `units.md`, or an index unit.
    Literal(String),
    /// `{"arg": name}`: the value of the named string argument.
    Arg(String),
    /// `{"of": X, "per": Y}`: units of X per one unit of Y.
    Rate {
        /// The numerator.
        of: RateSide,
        /// The denominator.
        per: RateSide,
    },
}

impl Unit {
    /// The JSON form.
    pub fn to_value(&self) -> Value {
        fn arg(n: &str) -> Value {
            Value::Object(vec![("arg".into(), Value::String(n.into()))])
        }
        fn side(s: &RateSide) -> Value {
            match s {
                RateSide::Currency(c) => Value::String(c.clone()),
                RateSide::Arg(n) => arg(n),
            }
        }
        match self {
            Unit::Literal(s) => Value::String(s.clone()),
            Unit::Arg(n) => arg(n),
            Unit::Rate { of, per } => Value::Object(vec![("of".into(), side(of)), ("per".into(), side(per))]),
        }
    }
}

/// An argument declaration: `{"type", "valid"}`, plus `unit` for numbers.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct ArgDecl {
    /// The argument's type.
    pub ty: Type,
    /// The unit; present exactly when the type is `number`.
    pub unit: Option<Unit>,
    /// Whether this is a valid-time argument.
    pub valid: bool,
}

/// The declared return: a type, with a unit exactly when it is `number`.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Returns {
    /// The value type.
    pub ty: Type,
    /// The unit of a number return.
    pub unit: Option<Unit>,
}

/// A function signature.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Signature {
    /// Argument declarations by name.
    pub args: BTreeMap<String, ArgDecl>,
    /// The return type.
    pub returns: Returns,
}

impl Signature {
    /// The JSON form, as it appears in a descriptor.
    pub fn to_value(&self) -> Value {
        let args = self
            .args
            .iter()
            .map(|(k, d)| {
                let mut m = vec![("type".to_string(), Value::String(d.ty.as_str().into()))];
                if let Some(u) = &d.unit {
                    m.push(("unit".into(), u.to_value()));
                }
                m.push(("valid".into(), Value::Bool(d.valid)));
                (k.clone(), Value::Object(m))
            })
            .collect();
        let returns = match &self.returns.unit {
            Some(u) => Value::Object(vec![
                ("type".into(), Value::String(self.returns.ty.as_str().into())),
                ("unit".into(), u.to_value()),
            ]),
            None => Value::String(self.returns.ty.as_str().into()),
        };
        Value::Object(vec![("args".into(), Value::Object(args)), ("returns".into(), returns)])
    }
}

/// A function descriptor (7): signature, code and declared inputs.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Descriptor {
    /// `code.lang`, e.g. `stored`.
    pub lang: String,
    /// `code.body`, opaque to this crate.
    pub body: String,
    /// Fully qualified names of the functions this one may read, ascending.
    pub inputs: Vec<String>,
    /// The signature.
    pub signature: Signature,
}

impl Descriptor {
    /// Parses a descriptor from JSON text: strict JSON, structure, canonical form (7).
    pub fn from_json(bytes: &[u8]) -> Result<Descriptor> {
        Descriptor::from_value(&json::parse(bytes)?)
    }

    /// Checks a parsed JSON value as a descriptor.
    pub fn from_value(v: &Value) -> Result<Descriptor> {
        json::check_value_depth(v)?;
        descriptor_structure(v)?;
        descriptor_canonical(v)
    }

    /// The JSON form.
    pub fn to_value(&self) -> Value {
        Value::Object(vec![
            (
                "code".into(),
                Value::Object(vec![
                    ("body".into(), Value::String(self.body.clone())),
                    ("lang".into(), Value::String(self.lang.clone())),
                ]),
            ),
            ("inputs".into(), Value::Array(self.inputs.iter().map(|s| Value::String(s.clone())).collect())),
            ("signature".into(), self.signature.to_value()),
        ])
    }

    /// The canonical bytes (6.2).
    pub fn canonical(&self) -> Vec<u8> {
        self.to_value().to_jcs()
    }

    /// The function hash: SHA-256 of the canonical bytes, lowercase hex.
    pub fn hash(&self) -> String {
        crate::crypto::sha256_hex(&self.canonical())
    }
}

fn unit_shape(v: &Value, path: &str) -> Result<()> {
    let bad = || err(ErrorCode::Schema, path, "a unit is a string, {\"arg\": name} or {\"of\", \"per\"}");
    match v {
        Value::String(_) => Ok(()),
        Value::Object(m) if m.len() == 1 && m[0].0 == "arg" => match m[0].1 {
            Value::String(_) => Ok(()),
            _ => bad(),
        },
        Value::Object(m) if m.len() == 2 && schema::opt(m, "of").is_some() && schema::opt(m, "per").is_some() => {
            for side in ["of", "per"] {
                let ok = match schema::get(m, side) {
                    Value::String(_) => true,
                    Value::Object(a) => a.len() == 1 && a[0].0 == "arg" && matches!(a[0].1, Value::String(_)),
                    _ => false,
                };
                if !ok {
                    return err(
                        ErrorCode::Schema,
                        &member(path, side),
                        "a rate side is a currency code or {\"arg\": name}",
                    );
                }
            }
            Ok(())
        }
        _ => bad(),
    }
}

pub(crate) fn descriptor_structure(v: &Value) -> Result<()> {
    let m = schema::object(v, "$")?;
    schema::exact(m, &["code", "inputs", "signature"], &[], "$", ErrorCode::Schema)?;
    let code = schema::object(schema::get(m, "code"), "$.code")?;
    schema::exact(code, &["body", "lang"], &[], "$.code", ErrorCode::Schema)?;
    schema::string(code, "body", "$.code")?;
    schema::string(code, "lang", "$.code")?;
    match schema::get(m, "inputs") {
        Value::Array(items) if items.iter().all(|i| matches!(i, Value::String(_))) => {}
        _ => return err(ErrorCode::Schema, "$.inputs", "inputs must be an array of strings"),
    }
    let sig = schema::object(schema::get(m, "signature"), "$.signature")?;
    schema::exact(sig, &["args", "returns"], &[], "$.signature", ErrorCode::Schema)?;
    let args = schema::object(schema::get(sig, "args"), "$.signature.args")?;
    for (k, d) in jcs_sorted(args) {
        let p = member("$.signature.args", k);
        let dm = schema::object(d, &p)?;
        schema::exact(dm, &["type", "valid"], &["unit"], &p, ErrorCode::Schema)?;
        schema::string(dm, "type", &p)?;
        if !matches!(schema::get(dm, "valid"), Value::Bool(_)) {
            return err(ErrorCode::Schema, &member(&p, "valid"), "valid must be a boolean");
        }
        if let Some(u) = schema::opt(dm, "unit") {
            unit_shape(u, &member(&p, "unit"))?;
        }
    }
    let rp = "$.signature.returns";
    match schema::get(sig, "returns") {
        Value::String(_) => {}
        Value::Object(rm) => {
            schema::exact(rm, &["type"], &["unit"], rp, ErrorCode::Schema)?;
            schema::string(rm, "type", rp)?;
            if let Some(u) = schema::opt(rm, "unit") {
                unit_shape(u, &member(rp, "unit"))?;
            }
        }
        _ => return err(ErrorCode::Schema, rp, "returns must be a type name or an object"),
    }
    Ok(())
}

fn unit_arg_ref(name: &str, args: &[(String, Value)], path: &str) -> Result<()> {
    let is_string = schema::opt(args, name)
        .and_then(Value::as_object)
        .map(|d| schema::get(d, "type").as_str() == Some("string"))
        .unwrap_or(false);
    if !is_string {
        return err(ErrorCode::CanonUnit, path, format!("a unit names a string argument of the signature: {name:?}"));
    }
    Ok(())
}

fn side_value(v: &Value) -> RateSide {
    match v {
        Value::String(s) => RateSide::Currency(s.clone()),
        other => RateSide::Arg(other.get("arg").and_then(Value::as_str).unwrap_or("").into()),
    }
}

/// Checks a structurally valid unit against 7.1 and returns it.
fn unit_canonical(v: &Value, args: &[(String, Value)], path: &str) -> Result<Unit> {
    match v {
        Value::String(s) => {
            if !lexical::is_literal_unit(s) {
                return err(ErrorCode::CanonUnit, path, format!("not a unit listed in units.md: {s:?}"));
            }
            Ok(Unit::Literal(s.clone()))
        }
        Value::Object(m) if m.len() == 1 => {
            let name = schema::get(m, "arg").as_str().unwrap_or("");
            unit_arg_ref(name, args, path)?;
            Ok(Unit::Arg(name.into()))
        }
        Value::Object(m) => {
            for side in ["of", "per"] {
                let sp = member(path, side);
                match schema::get(m, side) {
                    Value::String(s) => {
                        if !lexical::is_currency(s) {
                            return err(ErrorCode::CanonUnit, &sp, format!("a rate side is a currency code: {s:?}"));
                        }
                    }
                    other => unit_arg_ref(other.get("arg").and_then(Value::as_str).unwrap_or(""), args, &sp)?,
                }
            }
            Ok(Unit::Rate { of: side_value(schema::get(m, "of")), per: side_value(schema::get(m, "per")) })
        }
        _ => err(ErrorCode::Schema, path, "not a unit"),
    }
}

pub(crate) fn descriptor_canonical(v: &Value) -> Result<Descriptor> {
    let m = schema::object(v, "$")?;
    let code = schema::object(schema::get(m, "code"), "$.code")?;
    let body = schema::string(code, "body", "$.code")?;
    lexical::check_text(body, limits::BODY, true, "$.code.body")?;
    let lang = schema::string(code, "lang", "$.code")?;
    lexical::check_name(lang, "$.code.lang")?;

    let items: Vec<&str> = match schema::get(m, "inputs") {
        Value::Array(items) => items.iter().filter_map(Value::as_str).collect(),
        _ => Vec::new(),
    };
    for (i, s) in items.iter().enumerate() {
        lexical::check_function_name(s, &index("$.inputs", i))?;
    }
    if items.windows(2).any(|w| w[0] >= w[1]) {
        return err(ErrorCode::CanonSet, "$.inputs", "entries must be strictly ascending");
    }
    if items.len() > limits::INPUTS {
        return err(ErrorCode::CanonLimit, "$.inputs", "more than 4096 inputs");
    }

    let sig = schema::object(schema::get(m, "signature"), "$.signature")?;
    let args = schema::object(schema::get(sig, "args"), "$.signature.args")?;
    if args.len() > limits::ARGS {
        return err(ErrorCode::CanonLimit, "$.signature.args", "more than 32 arguments");
    }
    let mut decls = BTreeMap::new();
    for (k, d) in jcs_sorted(args) {
        let p = member("$.signature.args", k);
        lexical::check_arg_name(k, &p)?;
        let dm = schema::object(d, &p)?;
        let ty = Type::parse(schema::string(dm, "type", &p)?, &member(&p, "type"))?;
        let unit = match (ty, schema::opt(dm, "unit")) {
            (Type::Number, None) => return err(ErrorCode::CanonUnit, &p, "a number argument declares its unit"),
            (Type::Number, Some(u)) => Some(unit_canonical(u, args, &member(&p, "unit"))?),
            (_, Some(_)) => return err(ErrorCode::CanonUnit, &member(&p, "unit"), "only number arguments have a unit"),
            (_, None) => None,
        };
        let valid = matches!(schema::get(dm, "valid"), Value::Bool(true));
        if valid && !ty.may_be_valid_time() {
            return err(ErrorCode::CanonType, &p, "a valid-time argument must be a date, instant or string");
        }
        decls.insert(k.clone(), ArgDecl { ty, unit, valid });
    }

    let rp = "$.signature.returns";
    let returns = match schema::get(sig, "returns") {
        Value::String(name) => {
            let ty = Type::parse(name, rp)?;
            if ty == Type::Number {
                return err(
                    ErrorCode::CanonUnit,
                    rp,
                    "a number return is written {\"type\": \"number\", \"unit\": ...}",
                );
            }
            Returns { ty, unit: None }
        }
        Value::Object(rm) => {
            let ty = Type::parse(schema::string(rm, "type", rp)?, &member(rp, "type"))?;
            if ty != Type::Number {
                return err(ErrorCode::CanonUnit, rp, "only a number return is written as an object");
            }
            let Some(u) = schema::opt(rm, "unit") else {
                return err(ErrorCode::CanonUnit, rp, "a number return declares its unit");
            };
            Returns { ty, unit: Some(unit_canonical(u, args, &member(rp, "unit"))?) }
        }
        _ => return err(ErrorCode::Schema, rp, "returns must be a type name or an object"),
    };
    Ok(Descriptor {
        lang: lang.into(),
        body: body.into(),
        inputs: items.iter().map(|s| s.to_string()).collect(),
        signature: Signature { args: decls, returns },
    })
}

/// The unit of a cell's value (7.1, `units.md` 3), from the descriptor and cell JSON texts.
///
/// Checks, in order: the cell (structure, canonical form; slices allowed), that the
/// descriptor hashes to `cell.fn_hash` (`E_FN_HASH_MISMATCH`), the descriptor, then
/// substitution. Returns `None` when the function does not return a number.
pub fn resolved_unit(descriptor: &[u8], cell: &[u8]) -> Result<Option<String>> {
    let cell_v = json::parse(cell)?;
    let desc_v = json::parse(descriptor)?;
    resolved_unit_value(&desc_v, &cell_v)
}

/// [`resolved_unit`] over parsed JSON values.
pub fn resolved_unit_value(descriptor: &Value, cell: &Value) -> Result<Option<String>> {
    json::check_value_depth(descriptor)?;
    json::check_value_depth(cell)?;
    let cell = Cell::from_value(cell)?;
    let hash = crate::crypto::sha256_hex(&descriptor.to_jcs());
    if hash != cell.fn_hash {
        return err(ErrorCode::FnHashMismatch, "$.descriptor", "the descriptor does not hash to cell.fn_hash");
    }
    let d = Descriptor::from_value(descriptor)?;
    resolve_with(&d, &cell)
}

/// Substitutes a checked cell's arguments into a checked descriptor's return unit.
pub fn resolve_with(d: &Descriptor, cell: &Cell) -> Result<Option<String>> {
    let Some(unit) = &d.signature.returns.unit else { return Ok(None) };
    let arg_value = |name: &str| -> Result<String> {
        let p = member("$.cell.args", name);
        match cell.args.get(name) {
            None => err(
                ErrorCode::ArgMissing,
                &p,
                format!("the unit refers to argument {name:?}, which the cell does not bind"),
            ),
            Some(Arg::Slice) => err(ErrorCode::Slice, &p, "a slice has no single unit"),
            Some(Arg::Value(Scalar::String(s))) => Ok(s.clone()),
            Some(Arg::Value(other)) => {
                err(ErrorCode::ArgType, &p, format!("bound as {}, not string", other.ty().as_str()))
            }
        }
    };
    let side = |s: &RateSide| -> Result<String> {
        match s {
            RateSide::Currency(c) => Ok(c.clone()),
            RateSide::Arg(n) => {
                let v = arg_value(n)?;
                if !lexical::is_currency(&v) {
                    return err(
                        ErrorCode::CanonUnit,
                        &member("$.cell.args", n),
                        format!("a rate side must be a currency code: {v:?}"),
                    );
                }
                Ok(v)
            }
        }
    };
    Ok(Some(match unit {
        Unit::Literal(s) => s.clone(),
        Unit::Arg(n) => {
            let v = arg_value(n)?;
            if !lexical::is_literal_unit(&v) {
                return err(ErrorCode::CanonUnit, &member("$.cell.args", n), format!("not a unit: {v:?}"));
            }
            v
        }
        Unit::Rate { of, per } => {
            let o = side(of)?;
            let p = side(per)?;
            format!("{o}/{p}")
        }
    }))
}