open-standards/smart-byte/smart-byte-rs/crates/smart-byte-didcomm/src/message.rs
Transaction Science 6575ff1eb7 Ingest batch 3: 9 more crates across openpay / smart-byte / eoc
openpay/ (+3 crates)
  op-bnpl/        Affirm + Klarna + Afterpay/Clearpay rails. Full lifecycle
                  typestate (Initiated→Approved→Captured→Settled→Refunded).
                  Per-provider sandbox+production base URLs, regions (NA/EU/OC
                  for Klarna; US/AuNz/Gb/Ca/Eu for Afterpay). HMAC-SHA256
                  webhook verification with constant-time compare. 61 tests.
  op-batch/       NACHA ACH (94-char records, PPD/CCD/WEB/TEL/CTX, R01-R99
                  return codes, Same-Day ACH cutoffs); SEPA pain.001.001.09
                  (CT) + pain.008.001.08 (DD) with SCT-Inst flagging; Fedwire
                  MT103/MT202/CHIPS + pacs.008.001.12 / pacs.009.001.10 +
                  UETR; Bacs Standard 18. Per-rail cutoff scheduler. Exception
                  taxonomy unified across rails. 51 tests.
  op-screening/   OFAC SDN + EU consolidated + UN consolidated + HMT UK
                  consolidated + Australia DFAT + Canada SEMA + Japan MOF.
                  Levenshtein + Jaro-Winkler + Soundex + Metaphone + token
                  Jaccard scoring. Unicode NFKD normalisation + Cyrillic /
                  Arabic transliteration. Ed25519-signed BLAKE3-chained
                  audit log. BSA helpers: CTR + SAR pattern detection
                  (structuring, round-dollar, rapid-in-out). 43 tests.

smart-byte/smart-byte-rs/ (+3 crates)
  smart-byte-bbs/      BBS+ signatures (IETF draft-irtf-cfrg-bbs-signatures-08).
                       BLS12-381 keys, signatures, disclosure proofs with
                       cryptographic unlinkability. bbs-2023 Data Integrity
                       cryptosuite. AnonCreds-shaped link secrets +
                       predicate proofs. SecretKey zeroize-on-drop. 49 tests.
  smart-byte-didcomm/  DIDComm v2 messaging per DIF spec. Pack-encrypted
                       (ECDH-1PU + ECDH-ES via X25519), pack-signed (JWS),
                       pack-plaintext. Aries protocols: issue-credential v3,
                       present-proof v3, basic-message, trust-ping, discover-
                       features, out-of-band, coordinate-mediation, message-
                       pickup. Mediator role with pluggable storage. 23 tests.
  smart-byte-mdl/      ISO/IEC 18013-5 mobile driver's licence + 18013-7
                       online presentment. mDoc + MSO with salted-hash
                       selective disclosure (per item; SHA-256 digest tree).
                       org.iso.18013.5.1 namespace + AAMVA add-on. ES256
                       device-auth + issuer-auth. QR-code device-engagement
                       scaffolding. 28 tests.

eoc/eoc-rs/ (+3 crates)
  eoc-local/      Local-inference backends behind one NeuralBackend:
                  llama.cpp (GGUF, Q4/Q5/Q6/Q8/F16/F32 quantisation),
                  MLX (Apple Silicon, feature-gated), MLC-LLM (TVM, feature-
                  gated marker), ONNX Runtime. GGUF file-format reader.
                  HuggingFace tokenizers. Greedy / top-k / top-p / temperature /
                  Mirostat samplers. Model registry scanning ~/.eoc/models/.
                  Joule attribution via eoc-meter. 39 tests.
  eoc-rerank/     BM25 sparse + HNSW dense + RRF + min-max hybrid fusion.
                  Cohere v3.5 + Voyage v2 + bge-reranker v2-m3 + ms-marco
                  MiniLM + ColBERT v2 rerankers. Two-stage pipeline:
                  retrieve top-K hybrid → cross-encoder rerank → top-N.
                  KV-stage recovery shim into the cascade. 31 tests.
  eoc-eval/       15 canonical eval harnesses: MMLU, MMLU-Pro, GPQA,
                  HumanEval (pass@1 with python3 sandbox), BBH, IFEval (25
                  verifiable constraints), AlpacaEval, AGIEval, HellaSwag,
                  ARC (Easy + Challenge), TruthfulQA (MC1+MC2), BoolQ,
                  GSM8K, MATH (boxed-answer equivalence), Winogrande. Real
                  questions in BuiltinSamples. EvalRunner emits joules-per-
                  correct alongside accuracy. 49 tests.

Quality:
- cargo check across all three workspaces: clean
- cargo clippy --all-targets -- -D warnings on every new crate: clean
- 374 new tests, all passing
- #![forbid(unsafe_code)] across every new crate
- No unwrap/expect outside #[cfg(test)]

Rust 1.95.0 edition 2024 (inherited via root rust-toolchain.toml).
2026-05-24 01:09:35 -04:00

178 lines
6.2 KiB
Rust

//! DIDComm v2 plaintext message envelope (DIF DIDComm Messaging v2.1 § 3).
//!
//! A DIDComm message is a JSON object with a fixed set of envelope
//! attributes and a `body` of arbitrary protocol-specific JSON. Optional
//! `attachments` carry binary payloads outside the body.
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use smart_byte_did::Did;
/// A DIDComm v2 message in its plaintext form.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct DidcommMessage {
/// Globally unique message id (DIDComm v2 § 3.1).
pub id: String,
/// Message type URI, of the form
/// `<protocol-uri>/<version>/<message-name>` (DIDComm v2 § 3.1).
#[serde(rename = "type")]
pub type_: String,
/// Sender DID. Optional for anoncrypt and plaintext.
#[serde(skip_serializing_if = "Option::is_none", default)]
pub from: Option<Did>,
/// Recipient DIDs (DIDComm v2 § 3.2 — `to` is an array).
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub to: Vec<Did>,
/// Thread id (`thid`) — id of the first message in the thread.
#[serde(skip_serializing_if = "Option::is_none", default)]
pub thid: Option<String>,
/// Parent thread id (`pthid`) — for nested protocols.
#[serde(skip_serializing_if = "Option::is_none", default)]
pub pthid: Option<String>,
/// Creation timestamp.
#[serde(skip_serializing_if = "Option::is_none", default)]
pub created_time: Option<DateTime<Utc>>,
/// Expiry timestamp. Recipients SHOULD discard expired messages.
#[serde(skip_serializing_if = "Option::is_none", default)]
pub expires_time: Option<DateTime<Utc>>,
/// Application body — protocol-specific JSON.
#[serde(default)]
pub body: serde_json::Value,
/// Out-of-line binary attachments (DIDComm v2 § 5).
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub attachments: Vec<Attachment>,
}
impl DidcommMessage {
/// Build a new message with a freshly-minted UUID id.
pub fn new(type_: impl Into<String>) -> Self {
Self {
id: uuid::Uuid::new_v4().to_string(),
type_: type_.into(),
from: None,
to: vec![],
thid: None,
pthid: None,
created_time: Some(Utc::now()),
expires_time: None,
body: serde_json::Value::Object(Default::default()),
attachments: vec![],
}
}
/// Set `from` and return the message (builder style).
pub fn from_did(mut self, from: Did) -> Self {
self.from = Some(from);
self
}
/// Set `to` and return the message (builder style).
pub fn to_dids(mut self, to: Vec<Did>) -> Self {
self.to = to;
self
}
/// Set `thid` and return the message (builder style).
pub fn thread(mut self, thid: impl Into<String>) -> Self {
self.thid = Some(thid.into());
self
}
/// Set `body` and return the message (builder style).
pub fn body(mut self, body: serde_json::Value) -> Self {
self.body = body;
self
}
/// `true` if `expires_time` is set and is in the past.
pub fn is_expired(&self) -> bool {
matches!(self.expires_time, Some(t) if t < Utc::now())
}
}
/// A DIDComm v2 attachment (§ 5).
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct Attachment {
/// Optional attachment id (referenced from the body by `@id`).
#[serde(skip_serializing_if = "Option::is_none", default)]
pub id: Option<String>,
/// Human-friendly description.
#[serde(skip_serializing_if = "Option::is_none", default)]
pub description: Option<String>,
/// Filename.
#[serde(skip_serializing_if = "Option::is_none", default)]
pub filename: Option<String>,
/// MIME type.
#[serde(rename = "media_type", skip_serializing_if = "Option::is_none", default)]
pub media_type: Option<String>,
/// Attachment format identifier (e.g. `dif/[email protected]`).
#[serde(skip_serializing_if = "Option::is_none", default)]
pub format: Option<String>,
/// Last-modified timestamp.
#[serde(skip_serializing_if = "Option::is_none", default)]
pub lastmod_time: Option<DateTime<Utc>>,
/// Byte count.
#[serde(skip_serializing_if = "Option::is_none", default)]
pub byte_count: Option<u64>,
/// Attachment data — base64, JSON, or links.
pub data: AttachmentData,
}
/// Attachment data variants (DIDComm v2 § 5.2).
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct AttachmentData {
/// Inline base64 data.
#[serde(skip_serializing_if = "Option::is_none", default)]
pub base64: Option<String>,
/// Inline JSON data.
#[serde(skip_serializing_if = "Option::is_none", default)]
pub json: Option<serde_json::Value>,
/// External links the recipient can fetch.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub links: Vec<String>,
/// SHA-256 hash (multihash, base64url) for link integrity.
#[serde(skip_serializing_if = "Option::is_none", default)]
pub hash: Option<String>,
}
impl AttachmentData {
/// Wrap an inline JSON payload.
pub fn from_json(v: serde_json::Value) -> Self {
Self {
base64: None,
json: Some(v),
links: vec![],
hash: None,
}
}
/// Wrap an inline base64 payload.
pub fn from_base64(b64: impl Into<String>) -> Self {
Self {
base64: Some(b64.into()),
json: None,
links: vec![],
hash: None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn build_message_round_trip() {
let alice: Did = "did:example:alice".parse().unwrap();
let bob: Did = "did:example:bob".parse().unwrap();
let m = DidcommMessage::new("https://didcomm.org/basicmessage/2.0/message")
.from_did(alice.clone())
.to_dids(vec![bob.clone()])
.body(serde_json::json!({"content": "hello"}));
let j = serde_json::to_string(&m).unwrap();
let back: DidcommMessage = serde_json::from_str(&j).unwrap();
assert_eq!(back.from.unwrap(), alice);
assert_eq!(back.to, vec![bob]);
assert_eq!(back.body["content"], "hello");
}
}