open-standards/smart-byte/smart-byte-rs/crates/smart-byte-vc/src/presentation.rs
Transaction Science 01d4ad9c7b Ingest batch 2: 9 new crates across openpay / smart-byte / eoc
openpay/ (+3 crates, ~8400 LOC, 162 tests)
  op-iso8583/         ISO 8583 messages for direct acquirer / card-network
                      connectivity. MTIs, primary+secondary bitmaps, full
                      DE catalogue, BCD/ASCII/EBCDIC/LLVAR/LLLVAR codecs,
                      five dialects (Visa Base I, Mastercard MDS, Amex GNS,
                      Discover, JCB), network-management messages
                      (sign-on/echo/cutover), DE 55 EMV TLV.
  op-mobile-wallets/  Apple Pay (ECDH-P256 + AES-256-GCM), Google Pay ECv2
                      (ECDH + HKDF + AES-256-CTR + HMAC), Samsung Pay. PAN
                      never leaves the decryption boundary as a String;
                      lands in VaultRef. No-PAN-leakage assertions per
                      wallet.
  op-routing/         Least-cost routing + MCC-aware routing + intelligent
                      retry / soft-decline recovery. 231-MCC catalogue.
                      Real ISO 8583 DE-39 soft/hard decline taxonomies.

smart-byte/smart-byte-rs/ (+3 crates, ~7100 LOC, 96 tests)
  smart-byte-vc/      W3C Verifiable Credentials 2.0: VCDM, presentations,
                      Data Integrity proofs (eddsa-jcs-2022), VC-JWT,
                      SD-JWT (selective disclosure), Bitstring Status
                      List 2021. Cargo-bridge maps VCs to Smart Byte
                      envelopes with SAID stability.
  smart-byte-did/     W3C DID resolution: did:key (Ed25519/P-256/secp256k1),
                      did:web (HTTPS), did:peer (numalgo 2+4), did:jwk,
                      did:ion (feature-gated stub). DID URL dereferencer.
  smart-byte-pq/      Post-quantum signatures: ML-DSA-44/65/87 (FIPS 204),
                      SLH-DSA across 12 parameter sets (FIPS 205), FN-DSA
                      (draft FIPS 206, feature-gated). Hybrid Ed25519 +
                      ML-DSA pattern for the transition. Secret-key
                      zeroize-on-drop.

eoc/eoc-rs/ (+3 crates, ~6450 LOC, 76 tests)
  eoc-vendor-api/     NeuralBackend implementations: Anthropic, OpenAI,
                      Google, Mistral, Cohere, Groq, Together, Fireworks.
                      SSE streaming. Prompt-caching where supported.
                      Joule estimates per published energy coefficient
                      (12-model curated table).
  eoc-embeddings/     21 embedding models behind one Embedder trait:
                      OpenAI v3 (3), Cohere v3 (3), Voyage (5), Mistral,
                      Jina, plus local ONNX (BGE x3, nomic, mxbai, GTE,
                      E5) behind 'local' feature. Matryoshka-style
                      dimension projection. Per-content-hash cache.
  eoc-route-learned/  RouteLLM-style matrix-factorisation router +
                      multinomial-logreg classifier + LinUCB / Thompson
                      bandits for online learning. LearnedCascade wraps
                      the rule-based cascade with a confidence-threshold
                      skip policy. Synthetic-eval accuracy: MF 0.935,
                      LogReg 0.980.

Quality:
- cargo check across all three workspaces: clean
- cargo clippy --all-targets -- -D warnings on every new crate: clean
- 334 new tests total; 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-23 18:12:24 -04:00

117 lines
3.6 KiB
Rust

//! W3C Verifiable Presentation.
//!
//! A presentation bundles one or more credentials with a holder-bound
//! proof (typically using `proofPurpose: authentication`).
use iref::IriBuf;
use serde::{Deserialize, Serialize};
use crate::credential::{VC_CONTEXT_V2, VerifiableCredential};
use crate::did::Did;
use crate::error::VcError;
use crate::proof::Proof;
/// Verifiable Presentation per VCDM 2.0 §6.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct VerifiablePresentation {
/// `@context`.
#[serde(rename = "@context")]
pub context: Vec<IriBuf>,
/// Optional presentation id.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub id: Option<IriBuf>,
/// `type`. MUST include `VerifiablePresentation`.
#[serde(rename = "type")]
pub type_: Vec<String>,
/// Embedded credentials (0..n).
#[serde(
default,
rename = "verifiableCredential",
skip_serializing_if = "Vec::is_empty"
)]
pub verifiable_credential: Vec<VerifiableCredential>,
/// Optional holder DID.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub holder: Option<Did>,
/// Holder-bound proofs (0..n).
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub proof: Vec<Proof>,
}
impl VerifiablePresentation {
/// New empty presentation with the v2 context and default type.
pub fn new() -> Result<Self, VcError> {
let ctx: IriBuf = VC_CONTEXT_V2
.parse()
.map_err(|e: iref::InvalidIri<_>| VcError::Iri(e.to_string()))?;
Ok(Self {
context: vec![ctx],
id: None,
type_: vec!["VerifiablePresentation".to_string()],
verifiable_credential: Vec::new(),
holder: None,
proof: Vec::new(),
})
}
/// Attach a credential.
pub fn with_credential(mut self, vc: VerifiableCredential) -> Self {
self.verifiable_credential.push(vc);
self
}
/// Set the holder DID.
pub fn with_holder(mut self, did: Did) -> Self {
self.holder = Some(did);
self
}
/// Validate structural requirements.
pub fn validate_shape(&self) -> Result<(), VcError> {
match self.context.first() {
Some(c) if c.as_str() == VC_CONTEXT_V2 => Ok(()),
_ => Err(VcError::Credential(format!(
"@context must begin with {VC_CONTEXT_V2}"
))),
}?;
if !self.type_.iter().any(|t| t == "VerifiablePresentation") {
return Err(VcError::Credential(
"type must include VerifiablePresentation".into(),
));
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::credential::{CredentialSubject, VcBuilder};
use crate::issuer::Issuer;
#[test]
fn new_presentation_has_defaults() {
let vp = VerifiablePresentation::new().unwrap();
assert!(vp.type_.contains(&"VerifiablePresentation".to_string()));
vp.validate_shape().unwrap();
}
#[test]
fn presentation_with_credential() {
let subj = CredentialSubject {
id: Some("did:example:alice".parse().unwrap()),
claims: serde_json::Map::new(),
};
let vc = VcBuilder::new()
.issuer(Issuer::Uri("did:example:issuer".parse().unwrap()))
.subject(subj)
.build()
.unwrap();
let holder: Did = "did:example:alice".parse().unwrap();
let vp = VerifiablePresentation::new()
.unwrap()
.with_holder(holder)
.with_credential(vc);
assert_eq!(vp.verifiable_credential.len(), 1);
}
}