#[allow(unused_imports)]
use crate::prelude::*;
use serde_json::{Map, Value};
pub const ED25519_SLOT: &str = "key-1";
const DID_KEY_PREFIX: &str = "did:key:";
fn did_key_value(did: &str) -> Option<&str> {
let mb = did.strip_prefix(DID_KEY_PREFIX)?;
(!mb.is_empty() && !mb.contains('#')).then_some(mb)
}
pub fn method_did(verification_method: &str) -> &str {
verification_method
.split('#')
.next()
.unwrap_or(verification_method)
}
pub fn ed25519_method(did: &str) -> String {
match did_key_value(did) {
Some(mb) => format!("{did}#{mb}"),
None => format!("{did}#{ED25519_SLOT}"),
}
}
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
}
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));
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));
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));
}
}