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).
225 lines
7.9 KiB
Rust
225 lines
7.9 KiB
Rust
//! `did:peer` — inline-encoded DID documents (Aries / DIDComm).
|
|
//!
|
|
//! We implement the two practical numalgos used in deployments:
|
|
//!
|
|
//! * **numalgo 2** — `did:peer:2.<purpose><multikey>...[<service>]`.
|
|
//! The method-specific id encodes one verification method per purpose
|
|
//! ("V" = verification, "A" = assertion, "E" = key-agreement / encryption,
|
|
//! "I" = capability-invocation, "D" = capability-delegation), each
|
|
//! followed by a multibase Multikey; trailing `.S` segments carry
|
|
//! base64url-encoded JSON service entries.
|
|
//! * **numalgo 4** — `did:peer:4<hash>:<encoded-document>`. The
|
|
//! `<encoded-document>` is the multibase-encoded canonical document;
|
|
//! `<hash>` is the SHA-256 of the encoded document (also multibased).
|
|
//! This crate parses numalgo 4 strictly enough to expose the
|
|
//! embedded document.
|
|
|
|
use async_trait::async_trait;
|
|
use base64::Engine;
|
|
use base64::engine::general_purpose::URL_SAFE_NO_PAD;
|
|
use sha2::Digest;
|
|
|
|
use crate::did::{Did, DidMethod};
|
|
use crate::document::{
|
|
DidDocument, Service, VerificationMethod, VerificationRelationship,
|
|
};
|
|
use crate::error::DidError;
|
|
use crate::resolver::{DocumentMetadata, ResolutionMetadata, ResolutionResult, Resolver};
|
|
|
|
/// `did:peer` resolver.
|
|
pub struct PeerResolver;
|
|
|
|
impl PeerResolver {
|
|
/// Construct a new resolver.
|
|
pub fn new() -> Self {
|
|
PeerResolver
|
|
}
|
|
}
|
|
|
|
impl Default for PeerResolver {
|
|
fn default() -> Self {
|
|
Self::new()
|
|
}
|
|
}
|
|
|
|
#[async_trait]
|
|
impl Resolver for PeerResolver {
|
|
async fn resolve(&self, did: &Did) -> Result<ResolutionResult, DidError> {
|
|
let doc = resolve_peer(did)?;
|
|
Ok(ResolutionResult {
|
|
did_document: Some(doc),
|
|
did_resolution_metadata: ResolutionMetadata {
|
|
content_type: Some("application/did+json".into()),
|
|
error: None,
|
|
},
|
|
did_document_metadata: DocumentMetadata::default(),
|
|
})
|
|
}
|
|
}
|
|
|
|
/// Synthesise the DID document for a `did:peer` DID.
|
|
pub fn resolve_peer(did: &Did) -> Result<DidDocument, DidError> {
|
|
if did.method != DidMethod::Peer {
|
|
return Err(DidError::InvalidIdentifier(format!(
|
|
"not a did:peer: {did}"
|
|
)));
|
|
}
|
|
let msid = &did.method_specific_id;
|
|
let numalgo = msid.chars().next().ok_or_else(|| {
|
|
DidError::InvalidIdentifier("empty did:peer body".into())
|
|
})?;
|
|
match numalgo {
|
|
'2' => resolve_numalgo2(did, &msid[1..]),
|
|
'4' => resolve_numalgo4(did, &msid[1..]),
|
|
other => Err(DidError::MethodNotSupported(format!(
|
|
"did:peer numalgo {other} not implemented"
|
|
))),
|
|
}
|
|
}
|
|
|
|
/// Build a numalgo 2 method-specific id from a list of (purpose, multikey).
|
|
pub fn encode_numalgo2(
|
|
purposes: &[(char, String)],
|
|
services_b64: &[String],
|
|
) -> String {
|
|
let mut out = String::from("2");
|
|
for (p, k) in purposes {
|
|
out.push('.');
|
|
out.push(*p);
|
|
out.push_str(k);
|
|
}
|
|
for s in services_b64 {
|
|
out.push_str(".S");
|
|
out.push_str(s);
|
|
}
|
|
out
|
|
}
|
|
|
|
fn resolve_numalgo2(did: &Did, body: &str) -> Result<DidDocument, DidError> {
|
|
// Body starts with `.` then has a sequence of `<purpose><multikey>`
|
|
// segments separated by `.`, possibly followed by `.S<b64>` services.
|
|
let segments: Vec<&str> = body.split('.').filter(|s| !s.is_empty()).collect();
|
|
if segments.is_empty() {
|
|
return Err(DidError::InvalidIdentifier(
|
|
"did:peer numalgo 2 has no segments".into(),
|
|
));
|
|
}
|
|
let mut doc = DidDocument::new(did.clone());
|
|
for seg in segments {
|
|
let mut chars = seg.chars();
|
|
let purpose = chars.next().ok_or_else(|| {
|
|
DidError::InvalidIdentifier(
|
|
"empty did:peer segment".into(),
|
|
)
|
|
})?;
|
|
let rest = &seg[purpose.len_utf8()..];
|
|
if purpose == 'S' {
|
|
// Base64url-decoded JSON service.
|
|
let bytes = URL_SAFE_NO_PAD.decode(rest).map_err(|e| {
|
|
DidError::InvalidIdentifier(format!(
|
|
"did:peer service b64 decode: {e}"
|
|
))
|
|
})?;
|
|
let svc: Service = serde_json::from_slice(&bytes)?;
|
|
doc.service.push(svc);
|
|
continue;
|
|
}
|
|
// Otherwise: <purpose><multikey>. The multikey is multibase
|
|
// (z-prefixed base58btc) carrying multicodec || key.
|
|
if !rest.starts_with('z') {
|
|
return Err(DidError::InvalidIdentifier(format!(
|
|
"did:peer multikey must be base58btc (z-prefix), got {rest}"
|
|
)));
|
|
}
|
|
let vm_id = format!("{did}#{rest}");
|
|
let vm = VerificationMethod {
|
|
id: vm_id.clone(),
|
|
type_: "Multikey".into(),
|
|
controller: did.clone(),
|
|
public_key_multibase: Some(rest.to_string()),
|
|
public_key_jwk: None,
|
|
};
|
|
doc.verification_method.push(vm);
|
|
let rel = VerificationRelationship::Reference(vm_id);
|
|
match purpose {
|
|
'V' => doc.authentication.push(rel),
|
|
'A' => doc.assertion_method.push(rel),
|
|
'E' => doc.key_agreement.push(rel),
|
|
'I' => doc.capability_invocation.push(rel),
|
|
'D' => doc.capability_delegation.push(rel),
|
|
other => {
|
|
return Err(DidError::InvalidIdentifier(format!(
|
|
"unknown did:peer purpose '{other}'"
|
|
)));
|
|
}
|
|
}
|
|
}
|
|
Ok(doc)
|
|
}
|
|
|
|
fn resolve_numalgo4(did: &Did, body: &str) -> Result<DidDocument, DidError> {
|
|
// Body is `<hash>:<encoded-document>`. Both are multibase strings.
|
|
let (hash_part, encoded_part) = body.split_once(':').ok_or_else(|| {
|
|
DidError::InvalidIdentifier(
|
|
"did:peer numalgo 4 missing ':' separator".into(),
|
|
)
|
|
})?;
|
|
let (_, hash_bytes) = multibase::decode(hash_part)?;
|
|
let (_, doc_bytes) = multibase::decode(encoded_part)?;
|
|
// Verify the embedded document hashes correctly.
|
|
let mut hasher = sha2::Sha256::new();
|
|
hasher.update(encoded_part.as_bytes());
|
|
let computed = hasher.finalize();
|
|
if hash_bytes.len() < 2 || computed[..] != hash_bytes[2..] {
|
|
// The hash is allowed to be a multihash with a 2-byte prefix
|
|
// (0x12 0x20 for sha256-256). Accept either form by checking
|
|
// the suffix. If neither matches, error.
|
|
if computed[..] != hash_bytes[..] {
|
|
return Err(DidError::InvalidDocument(
|
|
"did:peer numalgo 4 hash mismatch".into(),
|
|
));
|
|
}
|
|
}
|
|
let doc: DidDocument = serde_json::from_slice(&doc_bytes)?;
|
|
if doc.id != *did {
|
|
return Err(DidError::InvalidDocument(format!(
|
|
"embedded did:peer document id {} does not match {}",
|
|
doc.id, did
|
|
)));
|
|
}
|
|
Ok(doc)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn numalgo2_round_trip_one_key() {
|
|
// Use a known Ed25519 multikey from the W3C registry.
|
|
let mk = "z6MkiTBz1ymuepAQ4HEHYSF1H8quG5GLVVQR3djdX3mDooWp";
|
|
let msid = encode_numalgo2(&[('V', mk.into())], &[]);
|
|
let did_str = format!("did:peer:{msid}");
|
|
let did: Did = did_str.parse().unwrap();
|
|
let doc = resolve_peer(&did).unwrap();
|
|
assert_eq!(doc.verification_method.len(), 1);
|
|
assert_eq!(doc.authentication.len(), 1);
|
|
}
|
|
|
|
#[test]
|
|
fn numalgo2_with_service() {
|
|
let mk = "z6MkiTBz1ymuepAQ4HEHYSF1H8quG5GLVVQR3djdX3mDooWp";
|
|
let svc = serde_json::json!({
|
|
"id": "#didcomm",
|
|
"type": "DIDCommMessaging",
|
|
"serviceEndpoint": "https://example.com/endpoint",
|
|
});
|
|
let s_b64 = URL_SAFE_NO_PAD
|
|
.encode(serde_json::to_vec(&svc).unwrap());
|
|
let msid = encode_numalgo2(&[('V', mk.into())], &[s_b64]);
|
|
let did: Did = format!("did:peer:{msid}").parse().unwrap();
|
|
let doc = resolve_peer(&did).unwrap();
|
|
assert_eq!(doc.service.len(), 1);
|
|
assert_eq!(doc.service[0].type_, "DIDCommMessaging");
|
|
}
|
|
}
|