use serde::{Deserialize, Serialize};
use crate::credentials::CredentialBundle;
use crate::did_secrets::SecretEntry;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContextProvisionBundle {
pub context_id: String,
pub context_name: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub vta_url: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub vta_did: Option<String>,
pub credential: CredentialBundle,
pub admin_did: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub did: Option<ProvisionedDid>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProvisionedDid {
pub id: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub did_document: Option<serde_json::Value>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub log_entry: Option<String>,
pub secrets: Vec<SecretEntry>,
}
#[cfg(test)]
mod tests {
use super::*;
fn sample_credential() -> CredentialBundle {
CredentialBundle::new("did:key:z6Mk123", "z1234567890", "did:key:z6MkVTA")
}
#[test]
fn test_serde_json_roundtrip_without_did() {
let bundle = ContextProvisionBundle {
context_id: "my-app".to_string(),
context_name: "My Application".to_string(),
vta_url: Some("https://vta.example.com".to_string()),
vta_did: Some("did:webvh:abc:example.com".to_string()),
credential: sample_credential(),
admin_did: "did:key:z6Mk123".to_string(),
did: None,
};
let json = serde_json::to_string(&bundle).unwrap();
let decoded: ContextProvisionBundle = serde_json::from_str(&json).unwrap();
assert_eq!(decoded.context_id, "my-app");
assert_eq!(decoded.context_name, "My Application");
assert_eq!(decoded.admin_did, "did:key:z6Mk123");
assert_eq!(decoded.credential.did, "did:key:z6Mk123");
assert!(decoded.did.is_none());
}
#[test]
fn test_serde_json_roundtrip_with_did() {
let bundle = ContextProvisionBundle {
context_id: "my-app".to_string(),
context_name: "My Application".to_string(),
vta_url: None,
vta_did: None,
credential: sample_credential(),
admin_did: "did:key:z6Mk123".to_string(),
did: Some(ProvisionedDid {
id: "did:webvh:abc:example.com".to_string(),
did_document: Some(serde_json::json!({"id": "did:webvh:abc:example.com"})),
log_entry: Some("{\"log\": \"entry\"}".to_string()),
secrets: vec![SecretEntry {
key_id: "did:webvh:abc:example.com#key-0".to_string(),
key_type: crate::keys::KeyType::Ed25519,
private_key_multibase: "z6Mk...signing".to_string(),
}],
}),
};
let json = serde_json::to_string(&bundle).unwrap();
let decoded: ContextProvisionBundle = serde_json::from_str(&json).unwrap();
assert_eq!(decoded.context_id, "my-app");
let did = decoded.did.unwrap();
assert_eq!(did.id, "did:webvh:abc:example.com");
assert!(did.did_document.is_some());
assert!(did.log_entry.is_some());
assert_eq!(did.secrets.len(), 1);
}
}