Skip to main content

vouch_core/
verification_method.rs

1//! The name a signer gives its Ed25519 key in a proof's `verificationMethod`,
2//! and the rule a verifier uses to tie that name back to the issuer.
3//!
4//! A did:key names its one key the way the did:key method does: the fragment
5//! is the key value itself, `did:key:<mb>#<mb>`. A did:web or did:webvh signer
6//! names the `#key-1` entry its own DID document lists.
7//!
8//! On verify, an Ed25519 proof by a did:key signer names exactly one of two
9//! methods: `did:key:<mb>#<mb>` or the earlier `did:key:<mb>#key-1`. Any
10//! other fragment (another key's value, an unknown label) or no fragment is
11//! refused. Other DID methods, and every post-quantum proof, keep the earlier
12//! rule: the part before `#` is the issuer.
13
14#[allow(unused_imports)]
15use crate::prelude::*;
16
17use serde_json::{Map, Value};
18
19/// The fragment of the Ed25519 key in a did:web or did:webvh document, and
20/// the earlier fragment a did:key signer used.
21pub const ED25519_SLOT: &str = "key-1";
22
23const DID_KEY_PREFIX: &str = "did:key:";
24
25/// The multibase key value of a did:key, or None when `did` is no did:key
26/// (or is one with an empty value or a fragment of its own).
27fn did_key_value(did: &str) -> Option<&str> {
28    let mb = did.strip_prefix(DID_KEY_PREFIX)?;
29    (!mb.is_empty() && !mb.contains('#')).then_some(mb)
30}
31
32/// The DID part of a verificationMethod (everything before the first `#`).
33pub fn method_did(verification_method: &str) -> &str {
34    verification_method
35        .split('#')
36        .next()
37        .unwrap_or(verification_method)
38}
39
40/// The verificationMethod an Ed25519 proof by `did` names:
41/// `did:key:<mb>#<mb>` for a did:key, `<did>#key-1` for any other DID.
42pub fn ed25519_method(did: &str) -> String {
43    match did_key_value(did) {
44        Some(mb) => format!("{did}#{mb}"),
45        None => format!("{did}#{ED25519_SLOT}"),
46    }
47}
48
49/// True when an Ed25519 (eddsa-jcs-2022) proof's `verificationMethod` names
50/// a key of `issuer`. For a did:key issuer, exactly `<issuer>#<mb>` or
51/// `<issuer>#key-1`; for any other DID, the part before `#` is the issuer.
52pub fn ed25519_method_names(verification_method: &str, issuer: &str) -> bool {
53    if issuer.starts_with(DID_KEY_PREFIX) {
54        let Some(mb) = did_key_value(issuer) else {
55            return false;
56        };
57        let Some(fragment) = verification_method
58            .strip_prefix(issuer)
59            .and_then(|rest| rest.strip_prefix('#'))
60        else {
61            return false;
62        };
63        return fragment == mb || fragment == ED25519_SLOT;
64    }
65    method_did(verification_method) == issuer
66}
67
68/// True when a proof's `verificationMethod` names a key of `issuer`, by its
69/// cryptosuite: a post-quantum proof (ML-DSA-44, SLH-DSA) keeps the DID-part
70/// rule; every other proof follows [`ed25519_method_names`]. A proof with no
71/// verificationMethod names nothing.
72pub fn proof_method_names(proof: &Map<String, Value>, issuer: &str) -> bool {
73    crate::nesting::deep_guard!(false; proof);
74    use crate::hybrid::Suite;
75    let Some(vm) = proof.get("verificationMethod").and_then(Value::as_str) else {
76        return false;
77    };
78    let suite = proof
79        .get("cryptosuite")
80        .and_then(Value::as_str)
81        .and_then(Suite::of);
82    match suite {
83        Some(Suite::Mldsa44 | Suite::Slhdsa128) => method_did(vm) == issuer,
84        _ => ed25519_method_names(vm, issuer),
85    }
86}
87
88#[cfg(test)]
89mod tests {
90    use super::*;
91    use serde_json::json;
92
93    const MB: &str = "z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK";
94    const OTHER: &str = "z6MkmtWtY63GQVBrpMyRJWEzsnxfsGkemu6CtMDwGTv4RYj2";
95
96    fn did() -> String {
97        format!("did:key:{MB}")
98    }
99
100    #[test]
101    fn did_key_names_its_key_by_value() {
102        assert_eq!(ed25519_method(&did()), format!("did:key:{MB}#{MB}"));
103    }
104
105    #[test]
106    fn did_web_and_webvh_keep_key_1() {
107        assert_eq!(
108            ed25519_method("did:web:a.example"),
109            "did:web:a.example#key-1"
110        );
111        assert_eq!(
112            ed25519_method("did:webvh:Qm:a.example"),
113            "did:webvh:Qm:a.example#key-1"
114        );
115    }
116
117    #[test]
118    fn did_key_accepts_exactly_two_names() {
119        let d = did();
120        assert!(ed25519_method_names(&format!("{d}#{MB}"), &d));
121        assert!(ed25519_method_names(&format!("{d}#key-1"), &d));
122        // Another key's value, an unknown label, no fragment, an empty one.
123        assert!(!ed25519_method_names(&format!("{d}#{OTHER}"), &d));
124        assert!(!ed25519_method_names(&format!("{d}#primary"), &d));
125        assert!(!ed25519_method_names(&format!("{d}#key-2"), &d));
126        assert!(!ed25519_method_names(&d, &d));
127        assert!(!ed25519_method_names(&format!("{d}#"), &d));
128        assert!(!ed25519_method_names(&format!("{d}#{MB}#{MB}"), &d));
129        // The DID part must be the issuer.
130        let other = format!("did:key:{OTHER}");
131        assert!(!ed25519_method_names(&format!("{other}#{OTHER}"), &d));
132        assert!(!ed25519_method_names(&format!("{other}#{MB}"), &d));
133        assert!(!ed25519_method_names(&format!("{d}x#{MB}"), &d));
134    }
135
136    #[test]
137    fn a_malformed_did_key_issuer_names_nothing() {
138        assert!(!ed25519_method_names("did:key:#key-1", "did:key:"));
139        assert!(!ed25519_method_names("did:key:a#b#key-1", "did:key:a#b"));
140    }
141
142    #[test]
143    fn other_methods_keep_the_did_part_rule() {
144        assert!(ed25519_method_names(
145            "did:web:a.example#key-1",
146            "did:web:a.example"
147        ));
148        assert!(ed25519_method_names(
149            "did:web:a.example#primary",
150            "did:web:a.example"
151        ));
152        assert!(ed25519_method_names(
153            "did:web:a.example",
154            "did:web:a.example"
155        ));
156        assert!(!ed25519_method_names(
157            "did:web:b.example#key-1",
158            "did:web:a.example"
159        ));
160    }
161
162    #[test]
163    fn post_quantum_proofs_keep_the_did_part_rule() {
164        let d = did();
165        let pq = |vm: &str| {
166            json!({"cryptosuite": "mldsa44-jcs-2024", "verificationMethod": vm})
167                .as_object()
168                .unwrap()
169                .clone()
170        };
171        assert!(proof_method_names(&pq(&format!("{d}#key-2")), &d));
172        let ed =
173            json!({"cryptosuite": "eddsa-jcs-2022", "verificationMethod": format!("{d}#key-2")});
174        assert!(!proof_method_names(ed.as_object().unwrap(), &d));
175    }
176}