use serde_json::Value;
use std::fs;
use std::path::Path;
use vouch_core::credentials::verify_with_keys;
use vouch_core::delegation::ChainOptions;
use vouch_core::multikey;
fn load(rel: &str) -> Value {
let path = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../test-vectors")
.join(rel);
let text = fs::read_to_string(&path).unwrap_or_else(|e| panic!("read {rel}: {e}"));
serde_json::from_str(&text).expect("parse vector")
}
#[test]
fn full_verifier_matches_proof_set_profile() {
let v = load("proof-set/vector.json");
let ed = multikey::decode(v["publicKey"].as_str().unwrap())
.unwrap()
.raw_key;
let ml = multikey::decode(v["mldsa44PublicKey"].as_str().unwrap())
.unwrap()
.raw_key;
let now = v["now"].as_str().unwrap();
let opts = ChainOptions::default();
for case in v["cases"].as_array().unwrap() {
let name = case["name"].as_str().unwrap();
let got = verify_with_keys(&case["credential"], &ed, Some(&ml), now, 30, &opts)
.map(|(r, _)| r.is_valid())
.unwrap_or(false);
assert_eq!(Some(got), case["valid"].as_bool(), "case {name}");
}
}
fn unhex(h: &str) -> Vec<u8> {
(0..h.len())
.step_by(2)
.map(|i| u8::from_str_radix(&h[i..i + 2], 16).unwrap())
.collect()
}
#[test]
fn strict_ed25519_vector_matches_reference() {
let v = load("ed25519-strict/vector.json");
for k in v["publicKeys"].as_array().unwrap() {
let name = k["name"].as_str().unwrap();
let raw = unhex(k["hex"].as_str().unwrap());
let got = vouch_core::keys::strict_verifying_key(&raw).is_ok();
assert_eq!(Some(got), k["valid"].as_bool(), "public key {name}");
let mk = multikey::encode_ed25519_public(&raw).unwrap();
assert_eq!(multikey::decode(&mk).is_ok(), got, "multikey {name}");
}
for s in v["signatures"].as_array().unwrap() {
let name = s["name"].as_str().unwrap();
let got = vouch_core::keys::verify(
&unhex(s["publicKey"].as_str().unwrap()),
&unhex(s["message"].as_str().unwrap()),
&unhex(s["signature"].as_str().unwrap()),
)
.unwrap_or(false);
assert_eq!(Some(got), s["valid"].as_bool(), "signature {name}");
}
for c in v["credentials"].as_array().unwrap() {
let name = c["name"].as_str().unwrap();
let cred = &c["credential"];
let issuer = cred["issuer"].as_str().unwrap_or("");
let got = match vouch_core::keys::did_key_to_ed25519(issuer) {
Ok(k) => vouch_core::credentials::verify(cred, &k, c["now"].as_str().unwrap(), 30)
.map(|r| r.is_valid())
.unwrap_or(false),
Err(_) => false,
};
assert_eq!(Some(got), c["valid"].as_bool(), "credential {name}");
}
}