use crate::error::AppError;
use super::ProvisionIntegrationDeps;
pub(super) async fn resolve_admin_template(
state: &ProvisionIntegrationDeps,
context: &str,
name: &str,
) -> Result<vta_sdk::did_templates::DidTemplate, AppError> {
resolve_template_by_name(state, context, name)
.await
.map_err(|e| match e {
AppError::NotFound(_) => AppError::Validation(format!(
"admin template '{name}' is not registered on this VTA. Register it via \
'pnm did-templates create {name} --file <path>' then retry, or use \
the built-in 'vta-admin' template."
)),
other => other,
})
}
pub(super) async fn resolve_template_by_name(
state: &ProvisionIntegrationDeps,
context: &str,
name: &str,
) -> Result<vta_sdk::did_templates::DidTemplate, AppError> {
if let Some(rec) =
crate::did_templates::get_context_template(&state.did_templates_ks, context, name).await?
{
return Ok(rec.template);
}
if let Some(rec) =
crate::did_templates::get_global_template(&state.did_templates_ks, name).await?
{
return Ok(rec.template);
}
if let Ok(tpl) = vta_sdk::did_templates::load_embedded(name) {
return Ok(tpl);
}
Err(AppError::NotFound(format!("template '{name}' not found")))
}
pub(super) fn template_targets_did_key_only(
template: &vta_sdk::did_templates::DidTemplate,
) -> bool {
!template.methods.is_empty() && template.methods.iter().all(|m| m == "key")
}
pub(super) fn template_targets_did_peer(template: &vta_sdk::did_templates::DidTemplate) -> bool {
template.methods.iter().any(|m| m == "peer")
}
pub(super) fn require_bundle_shape(
name: &str,
template: &vta_sdk::did_templates::DidTemplate,
) -> Result<(), AppError> {
use vta_sdk::did_templates::{SLOT_KA, SLOT_SIGNING};
let slots = template.key_slots();
let why = match (slots.get(SLOT_SIGNING), slots.get(SLOT_KA)) {
(Some(signing), _) if !signing.publish => "its signing key is kept out of the DID document",
(_, None) => "it has no key-agreement key",
_ => return Ok(()),
};
let next = if template.kind == "oid4vp-verifier" {
"\nThen, for a VTC verifier: `pnm keys bundle <context>` → `vtc oid4vp install`, or keep \
the key in the VTA with `vtc oid4vp attach`."
} else {
"\nThen hand its keys over with `pnm keys bundle <context>`."
};
Err(AppError::Validation(format!(
"template '{name}' cannot be provisioned as an integration: {why}, and an integration's \
sealed bundle carries both its signing and its key-agreement key as its DID document \
publishes them. Mint the DID directly instead:\n pnm did-mgmt dids create --context \
<context> --template {name} …{next}"
)))
}
pub(super) async fn resolve_template_kind(
templates_ks: &crate::store::KeyspaceHandle,
name: &str,
context: &str,
) -> Result<String, AppError> {
if let Some(rec) =
crate::did_templates::get_context_template(templates_ks, context, name).await?
{
return Ok(rec.template.kind);
}
if let Some(rec) = crate::did_templates::get_global_template(templates_ks, name).await? {
return Ok(rec.template.kind);
}
if let Ok(tpl) = vta_sdk::did_templates::load_embedded(name) {
return Ok(tpl.kind);
}
Err(AppError::NotFound(format!("template '{name}' not found")))
}