vouch-core 4.3.0

Vouch Protocol canonical core: JCS canonicalization, Ed25519 + ML-DSA proofs, did:key/multikey, Data Integrity (eddsa-jcs-2022), credentials, delegation, revocation. One byte-exact implementation shared by every language SDK via WASM and UniFFI/C-FFI.
Documentation
//! The name a signer gives its Ed25519 key in a proof's `verificationMethod`,
//! and the rule a verifier uses to tie that name back to the issuer.
//!
//! A did:key names its one key the way the did:key method does: the fragment
//! is the key value itself, `did:key:<mb>#<mb>`. A did:web or did:webvh signer
//! names the `#key-1` entry its own DID document lists.
//!
//! On verify, an Ed25519 proof by a did:key signer names exactly one of two
//! methods: `did:key:<mb>#<mb>` or the earlier `did:key:<mb>#key-1`. Any
//! other fragment (another key's value, an unknown label) or no fragment is
//! refused. Other DID methods, and every post-quantum proof, keep the earlier
//! rule: the part before `#` is the issuer.

#[allow(unused_imports)]
use crate::prelude::*;

use serde_json::{Map, Value};

/// The fragment of the Ed25519 key in a did:web or did:webvh document, and
/// the earlier fragment a did:key signer used.
pub const ED25519_SLOT: &str = "key-1";

const DID_KEY_PREFIX: &str = "did:key:";

/// The multibase key value of a did:key, or None when `did` is no did:key
/// (or is one with an empty value or a fragment of its own).
fn did_key_value(did: &str) -> Option<&str> {
    let mb = did.strip_prefix(DID_KEY_PREFIX)?;
    (!mb.is_empty() && !mb.contains('#')).then_some(mb)
}

/// The DID part of a verificationMethod (everything before the first `#`).
pub fn method_did(verification_method: &str) -> &str {
    verification_method
        .split('#')
        .next()
        .unwrap_or(verification_method)
}

/// The verificationMethod an Ed25519 proof by `did` names:
/// `did:key:<mb>#<mb>` for a did:key, `<did>#key-1` for any other DID.
pub fn ed25519_method(did: &str) -> String {
    match did_key_value(did) {
        Some(mb) => format!("{did}#{mb}"),
        None => format!("{did}#{ED25519_SLOT}"),
    }
}

/// True when an Ed25519 (eddsa-jcs-2022) proof's `verificationMethod` names
/// a key of `issuer`. For a did:key issuer, exactly `<issuer>#<mb>` or
/// `<issuer>#key-1`; for any other DID, the part before `#` is the issuer.
pub fn ed25519_method_names(verification_method: &str, issuer: &str) -> bool {
    if issuer.starts_with(DID_KEY_PREFIX) {
        let Some(mb) = did_key_value(issuer) else {
            return false;
        };
        let Some(fragment) = verification_method
            .strip_prefix(issuer)
            .and_then(|rest| rest.strip_prefix('#'))
        else {
            return false;
        };
        return fragment == mb || fragment == ED25519_SLOT;
    }
    method_did(verification_method) == issuer
}

/// True when a proof's `verificationMethod` names a key of `issuer`, by its
/// cryptosuite: a post-quantum proof (ML-DSA-44, SLH-DSA) keeps the DID-part
/// rule; every other proof follows [`ed25519_method_names`]. A proof with no
/// verificationMethod names nothing.
pub fn proof_method_names(proof: &Map<String, Value>, issuer: &str) -> bool {
    crate::nesting::deep_guard!(false; proof);
    use crate::hybrid::Suite;
    let Some(vm) = proof.get("verificationMethod").and_then(Value::as_str) else {
        return false;
    };
    let suite = proof
        .get("cryptosuite")
        .and_then(Value::as_str)
        .and_then(Suite::of);
    match suite {
        Some(Suite::Mldsa44 | Suite::Slhdsa128) => method_did(vm) == issuer,
        _ => ed25519_method_names(vm, issuer),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    const MB: &str = "z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK";
    const OTHER: &str = "z6MkmtWtY63GQVBrpMyRJWEzsnxfsGkemu6CtMDwGTv4RYj2";

    fn did() -> String {
        format!("did:key:{MB}")
    }

    #[test]
    fn did_key_names_its_key_by_value() {
        assert_eq!(ed25519_method(&did()), format!("did:key:{MB}#{MB}"));
    }

    #[test]
    fn did_web_and_webvh_keep_key_1() {
        assert_eq!(
            ed25519_method("did:web:a.example"),
            "did:web:a.example#key-1"
        );
        assert_eq!(
            ed25519_method("did:webvh:Qm:a.example"),
            "did:webvh:Qm:a.example#key-1"
        );
    }

    #[test]
    fn did_key_accepts_exactly_two_names() {
        let d = did();
        assert!(ed25519_method_names(&format!("{d}#{MB}"), &d));
        assert!(ed25519_method_names(&format!("{d}#key-1"), &d));
        // Another key's value, an unknown label, no fragment, an empty one.
        assert!(!ed25519_method_names(&format!("{d}#{OTHER}"), &d));
        assert!(!ed25519_method_names(&format!("{d}#primary"), &d));
        assert!(!ed25519_method_names(&format!("{d}#key-2"), &d));
        assert!(!ed25519_method_names(&d, &d));
        assert!(!ed25519_method_names(&format!("{d}#"), &d));
        assert!(!ed25519_method_names(&format!("{d}#{MB}#{MB}"), &d));
        // The DID part must be the issuer.
        let other = format!("did:key:{OTHER}");
        assert!(!ed25519_method_names(&format!("{other}#{OTHER}"), &d));
        assert!(!ed25519_method_names(&format!("{other}#{MB}"), &d));
        assert!(!ed25519_method_names(&format!("{d}x#{MB}"), &d));
    }

    #[test]
    fn a_malformed_did_key_issuer_names_nothing() {
        assert!(!ed25519_method_names("did:key:#key-1", "did:key:"));
        assert!(!ed25519_method_names("did:key:a#b#key-1", "did:key:a#b"));
    }

    #[test]
    fn other_methods_keep_the_did_part_rule() {
        assert!(ed25519_method_names(
            "did:web:a.example#key-1",
            "did:web:a.example"
        ));
        assert!(ed25519_method_names(
            "did:web:a.example#primary",
            "did:web:a.example"
        ));
        assert!(ed25519_method_names(
            "did:web:a.example",
            "did:web:a.example"
        ));
        assert!(!ed25519_method_names(
            "did:web:b.example#key-1",
            "did:web:a.example"
        ));
    }

    #[test]
    fn post_quantum_proofs_keep_the_did_part_rule() {
        let d = did();
        let pq = |vm: &str| {
            json!({"cryptosuite": "mldsa44-jcs-2024", "verificationMethod": vm})
                .as_object()
                .unwrap()
                .clone()
        };
        assert!(proof_method_names(&pq(&format!("{d}#key-2")), &d));
        let ed =
            json!({"cryptosuite": "eddsa-jcs-2022", "verificationMethod": format!("{d}#key-2")});
        assert!(!proof_method_names(ed.as_object().unwrap(), &d));
    }
}