use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DIDDoc {
pub id: String,
pub key_agreement: Vec<String>,
pub authentication: Vec<String>,
pub verification_method: Vec<VerificationMethod>,
pub service: Vec<Service>,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct VerificationMethod {
pub id: String,
#[serde(rename = "type")]
pub type_: VerificationMethodType,
pub controller: String,
#[serde(flatten)]
pub verification_material: VerificationMaterial,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub enum VerificationMethodType {
JsonWebKey2020,
X25519KeyAgreementKey2019,
Ed25519VerificationKey2018,
EcdsaSecp256k1VerificationKey2019,
X25519KeyAgreementKey2020,
Ed25519VerificationKey2020,
Other,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(untagged)]
pub enum VerificationMaterial {
#[serde(rename_all = "camelCase")]
JWK { public_key_jwk: Value },
#[serde(rename_all = "camelCase")]
Multibase { public_key_multibase: String },
#[serde(rename_all = "camelCase")]
Base58 { public_key_base58: String },
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Service {
pub id: String,
#[serde(flatten)]
pub service_endpoint: ServiceKind,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(tag = "type", content = "serviceEndpoint")]
pub enum ServiceKind {
DIDCommMessaging {
#[serde(flatten)]
value: DIDCommMessagingService,
},
Other {
#[serde(flatten)]
value: Value,
},
}
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DIDCommMessagingService {
pub uri: String,
#[serde(default)]
#[serde(skip_serializing_if = "Option::is_none")]
pub accept: Option<Vec<String>>,
#[serde(default)]
pub routing_keys: Vec<String>,
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
const SERVICE_URI: &str = "https://example.com/path";
#[test]
fn parsing_minimal_didcomm_messaging_service_works() {
let service: DIDCommMessagingService =
serde_json::from_value(json!({ "uri": SERVICE_URI })).unwrap();
assert_eq!(service.uri, SERVICE_URI);
assert!(service.routing_keys.is_empty());
assert!(service.accept.is_none());
}
}