vouch_core/
verification_method.rs1#[allow(unused_imports)]
15use crate::prelude::*;
16
17use serde_json::{Map, Value};
18
19pub const ED25519_SLOT: &str = "key-1";
22
23const DID_KEY_PREFIX: &str = "did:key:";
24
25fn 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
32pub fn method_did(verification_method: &str) -> &str {
34 verification_method
35 .split('#')
36 .next()
37 .unwrap_or(verification_method)
38}
39
40pub 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
49pub 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
68pub 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 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 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}