use crate::error::{err, index, member, ErrorCode, Result};
use crate::json::{self, jcs_sorted, Value};
use crate::lexical::{self, limits};
use crate::model::{self, Arg, Args, Cell, Scalar, Type};
use crate::schema;
const MEMBERS: [&str; 14] = [
"asof",
"cell",
"computed_by",
"inputs",
"issued",
"licence",
"proto",
"publisher",
"sig",
"summary",
"tx",
"type",
"valid",
"value",
];
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Source {
name: String,
url: String,
retrieved: String,
sha256: Option<String>,
takeover: Option<String>,
}
impl Source {
pub fn new(name: &str, url: &str, retrieved: &str) -> Source {
Source { name: name.into(), url: url.into(), retrieved: retrieved.into(), sha256: None, takeover: None }
}
pub fn with_sha256(self, sha256: &str) -> Source {
Source { sha256: Some(sha256.into()), ..self }
}
pub fn with_takeover(self, takeover: &str) -> Source {
Source { takeover: Some(takeover.into()), ..self }
}
pub fn name(&self) -> &str {
&self.name
}
pub fn url(&self) -> &str {
&self.url
}
pub fn retrieved(&self) -> &str {
&self.retrieved
}
pub fn sha256(&self) -> Option<&str> {
self.sha256.as_deref()
}
pub fn takeover(&self) -> Option<&str> {
self.takeover.as_deref()
}
#[cfg(feature = "sign")]
pub(crate) fn to_value(&self) -> Value {
let s = |x: &str| Value::String(x.into());
let mut m = vec![("name".to_string(), s(&self.name)), ("retrieved".to_string(), s(&self.retrieved))];
if let Some(h) = &self.sha256 {
m.push(("sha256".into(), s(h)));
}
if let Some(t) = &self.takeover {
m.push(("takeover".into(), s(t)));
}
m.push(("url".into(), s(&self.url)));
Value::Object(m)
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Receipt {
asof: String,
cell: Cell,
computed_by: String,
inputs: Vec<String>,
issued: String,
licence: String,
publisher: String,
sig: String,
summary: String,
tx: String,
value: Scalar,
valid: Args,
source: Option<Source>,
payload: Vec<u8>,
hash: String,
}
impl Receipt {
pub fn from_json(bytes: &[u8]) -> Result<Receipt> {
Receipt::from_value(&json::parse(bytes)?)
}
pub fn from_value(v: &Value) -> Result<Receipt> {
json::check_value_depth(v)?;
structure(v)?;
let r = canonical(v)?;
r.consistency()?;
Ok(r)
}
pub fn payload(&self) -> &[u8] {
&self.payload
}
pub fn hash(&self) -> &str {
&self.hash
}
pub fn asof(&self) -> &str {
&self.asof
}
pub fn cell(&self) -> &Cell {
&self.cell
}
pub fn computed_by(&self) -> &str {
&self.computed_by
}
pub fn inputs(&self) -> &[String] {
&self.inputs
}
pub fn issued(&self) -> &str {
&self.issued
}
pub fn licence(&self) -> &str {
&self.licence
}
pub fn publisher(&self) -> &str {
&self.publisher
}
pub fn sig(&self) -> &str {
&self.sig
}
pub fn summary(&self) -> &str {
&self.summary
}
pub fn tx(&self) -> &str {
&self.tx
}
pub fn value(&self) -> &Scalar {
&self.value
}
pub fn valid(&self) -> &Args {
&self.valid
}
pub fn ty(&self) -> Type {
self.value.ty()
}
pub fn signer_host(&self) -> &str {
self.computed_by.split_once('#').map_or("", |(h, _)| h)
}
pub fn key_id(&self) -> &str {
self.computed_by.split_once('#').map_or("", |(_, k)| k)
}
pub fn source(&self) -> Option<&Source> {
self.source.as_ref()
}
#[cfg(feature = "sign")]
pub(crate) fn with_sig(mut self, sig: String) -> Receipt {
self.sig = sig;
self
}
fn consistency(&self) -> Result<()> {
for (k, a) in &self.cell.args {
if *a == Arg::Slice {
return err(ErrorCode::Slice, &member("$.cell.args", k), "a receipt is for one cell, not a slice");
}
}
for (k, a) in &self.valid {
if *a == Arg::Slice {
return err(ErrorCode::Slice, &member("$.valid", k), "a receipt is for one cell, not a slice");
}
}
if self.publisher != self.cell.ns {
return err(ErrorCode::PublisherMismatch, "$.publisher", "publisher must equal cell.ns");
}
for (k, a) in &self.valid {
if self.cell.args.get(k) != Some(a) {
return err(ErrorCode::ValidMismatch, "$.valid", format!("valid.{k} does not echo cell.args.{k}"));
}
if !a.scalar().is_some_and(|s| s.ty().may_be_valid_time()) {
return err(ErrorCode::ValidMismatch, "$.valid", "valid-time arguments are dates, instants or strings");
}
}
if !(self.tx <= self.asof && self.asof <= self.issued) {
return err(ErrorCode::TimeOrder, "$", "requires tx <= asof <= issued");
}
if let Some(s) = &self.source {
if s.retrieved > self.issued {
return err(ErrorCode::TimeOrder, "$.source.retrieved", "requires retrieved <= issued");
}
}
Ok(())
}
}
fn structure(v: &Value) -> Result<()> {
let m = schema::object(v, "$")?;
schema::exact(m, &MEMBERS, &["source"], "$", ErrorCode::Schema)?;
if schema::get(m, "proto").as_str() != Some("datumesh/1") {
return err(ErrorCode::Schema, "$.proto", "proto must be 'datumesh/1'");
}
let has_inputs = matches!(schema::get(m, "inputs"), Value::Array(a) if !a.is_empty());
let publisher = schema::get(m, "publisher").as_str().unwrap_or("");
for (k, val) in jcs_sorted(m) {
let p = member("$", k);
match k.as_str() {
"cell" => model::cell_structure(val, &p)?,
"source" => source_structure(val, &p, publisher, has_inputs)?,
"valid" => model::args_structure(val, &p)?,
"inputs" => {
if !matches!(val, Value::Array(a) if a.iter().all(|x| matches!(x, Value::String(_)))) {
return err(ErrorCode::Schema, &p, "inputs must be an array of strings");
}
}
"value" => {
if !matches!(val, Value::String(_) | Value::Bool(_)) {
return err(ErrorCode::Schema, &p, "value must be a string or a boolean");
}
}
_ => {
if !matches!(val, Value::String(_)) {
return err(ErrorCode::Schema, &p, format!("{k} must be a string"));
}
}
}
}
Ok(())
}
fn source_structure(v: &Value, path: &str, publisher: &str, has_inputs: bool) -> Result<()> {
if has_inputs {
return err(ErrorCode::Schema, path, "source is only on a receipt with no inputs");
}
let m = schema::object_or(v, path, ErrorCode::Schema, "source")?;
schema::exact(m, &["name", "retrieved", "url"], &["sha256", "takeover"], path, ErrorCode::Schema)?;
for (k, val) in m {
if !matches!(val, Value::String(_)) {
return err(ErrorCode::Schema, &member(path, k), format!("{k} must be a string"));
}
}
if schema::get(m, "name").as_str() == Some(lexical::host_of(publisher)) {
return err(ErrorCode::Schema, &member(path, "name"), "a publisher does not relay itself");
}
Ok(())
}
fn source_canonical(v: &Value, path: &str) -> Result<Source> {
let m = schema::object(v, path)?;
let s = |name: &str| schema::string(m, name, path);
let name = s("name")?;
lexical::check_dns_name(name, &member(path, "name"))?;
let retrieved = s("retrieved")?;
lexical::check_instant(retrieved, &member(path, "retrieved"))?;
let mut out = Source::new(name, "", retrieved);
if let Some(h) = schema::opt(m, "sha256") {
let h = h.as_str().unwrap_or("");
lexical::check_hex(h, 64, &member(path, "sha256"))?;
out = out.with_sha256(h);
}
let url_check = |u: &str, at: &str| -> Result<()> {
if !u.starts_with("https://") {
return err(ErrorCode::CanonLicence, at, "a source URL is https://");
}
lexical::check_licence(u, at)
};
if let Some(t) = schema::opt(m, "takeover") {
let t = t.as_str().unwrap_or("");
url_check(t, &member(path, "takeover"))?;
out = out.with_takeover(t);
}
let url = s("url")?;
url_check(url, &member(path, "url"))?;
Ok(Source { url: url.into(), ..out })
}
fn canonical(v: &Value) -> Result<Receipt> {
let m = schema::object(v, "$")?;
let s = |name: &str| schema::string(m, name, "$");
let asof = s("asof")?;
lexical::check_instant(asof, "$.asof")?;
let cell = model::cell_canonical(schema::get(m, "cell"), "$.cell")?;
let computed_by = s("computed_by")?;
lexical::check_key_ref(computed_by, "$.computed_by")?;
let inputs: Vec<&str> = match schema::get(m, "inputs") {
Value::Array(a) => a.iter().filter_map(Value::as_str).collect(),
_ => Vec::new(),
};
for (i, h) in inputs.iter().enumerate() {
lexical::check_input_entry(h, &index("$.inputs", i))?;
}
if inputs.windows(2).any(|w| w[0] >= w[1]) {
return err(ErrorCode::CanonSet, "$.inputs", "entries must be strictly ascending");
}
if inputs.len() > limits::INPUTS {
return err(ErrorCode::CanonLimit, "$.inputs", "more than 4096 inputs");
}
let issued = s("issued")?;
lexical::check_instant(issued, "$.issued")?;
let licence = s("licence")?;
lexical::check_licence(licence, "$.licence")?;
let publisher = s("publisher")?;
lexical::check_namespace(publisher, "$.publisher")?;
let sig = s("sig")?;
lexical::check_hex(sig, 128, "$.sig")?;
let summary = s("summary")?;
lexical::check_text(summary, limits::SUMMARY, false, "$.summary")?;
if summary.is_empty() {
return err(ErrorCode::CanonString, "$.summary", "summary must not be empty");
}
let tx = s("tx")?;
lexical::check_instant(tx, "$.tx")?;
let ty = Type::parse(s("type")?, "$.type")?;
let valid = model::args_canonical(schema::get(m, "valid"), "$.valid")?;
let value = match (ty, schema::get(m, "value")) {
(Type::Boolean, Value::Bool(b)) => Scalar::Boolean(*b),
(Type::Boolean, _) => return err(ErrorCode::CanonType, "$.value", "boolean value must be JSON true or false"),
(t, Value::String(text)) => {
model::check_lexical(t, text, "$.value")?;
model::unchecked(t, text)
}
(t, _) => return err(ErrorCode::CanonType, "$.value", format!("{} value must be a JSON string", t.as_str())),
};
let source = match schema::opt(m, "source") {
Some(sv) => Some(source_canonical(sv, "$.source")?),
None => None,
};
let without_sig: Vec<(String, Value)> = m.iter().filter(|(k, _)| k != "sig").cloned().collect();
let payload = Value::Object(without_sig).to_jcs();
let hash = crate::crypto::sha256_hex(&payload);
Ok(Receipt {
asof: asof.into(),
cell,
computed_by: computed_by.into(),
inputs: inputs.iter().map(|x| x.to_string()).collect(),
issued: issued.into(),
licence: licence.into(),
publisher: publisher.into(),
sig: sig.into(),
summary: summary.into(),
tx: tx.into(),
value,
valid,
source,
payload,
hash,
})
}
pub fn receipt_payload(bytes: &[u8]) -> Result<Vec<u8>> {
Ok(Receipt::from_json(bytes)?.payload)
}
pub fn receipt_hash(bytes: &[u8]) -> Result<String> {
Ok(Receipt::from_json(bytes)?.hash)
}