use crate::stdout::println;
use prikk_store::{AdoptedMaintainerKey, MaintainerTrustPolicy};
use super::verification::escape_json_string;
const NOT_REF_AUTHORITY_NOTE: &str = "note: this is object trust, not ref authority -- an adopted \
key's signatures are accepted on objects it signed, but adopting a key never lets it move a \
ref by itself";
pub(crate) fn print_trust_list(policy: &MaintainerTrustPolicy) {
if policy.keys.is_empty() {
println!("no maintainer keys adopted");
return;
}
println!("adopted maintainer keys, in adoption order:");
for (index, key) in policy.keys.iter().enumerate() {
println!(
" {}. {} {}",
index + 1,
key.key_id,
prikk_hash::to_hex(&key.public_key)
);
}
println!("{NOT_REF_AUTHORITY_NOTE}");
}
pub(crate) fn print_trust_list_json(policy: &MaintainerTrustPolicy) {
let mut json = String::new();
json.push_str("{\n");
json.push_str(" \"schema_version\": \"trust-list-v1\",\n");
json.push_str(" \"keys\": [");
for (index, key) in policy.keys.iter().enumerate() {
if index > 0 {
json.push(',');
}
json.push_str("\n {\"key_id\": ");
json.push_str(&escape_json_string(&key.key_id));
json.push_str(", \"public_key\": ");
json.push_str(&escape_json_string(&prikk_hash::to_hex(&key.public_key)));
json.push('}');
}
if !policy.keys.is_empty() {
json.push_str("\n ");
}
json.push_str("]\n}");
println!("{json}");
}
pub(crate) fn print_trust_check(key_id: &str, found: Option<&AdoptedMaintainerKey>) {
match found {
Some(key) => {
println!(
"trusted: {} {}",
key.key_id,
prikk_hash::to_hex(&key.public_key)
);
println!("{NOT_REF_AUTHORITY_NOTE}");
}
None => println!("not trusted: {key_id}"),
}
}
pub(crate) fn print_trust_check_json(key_id: &str, found: Option<&AdoptedMaintainerKey>) {
let mut json = String::new();
json.push_str("{\n");
json.push_str(" \"schema_version\": \"trust-check-v1\",\n");
json.push_str(&format!(" \"key_id\": {},\n", escape_json_string(key_id)));
json.push_str(&format!(" \"trusted\": {},\n", found.is_some()));
match found {
Some(key) => json.push_str(&format!(
" \"public_key\": {}\n",
escape_json_string(&prikk_hash::to_hex(&key.public_key))
)),
None => json.push_str(" \"public_key\": null\n"),
}
json.push('}');
println!("{json}");
}