TCHBIEEALVTB67SZZT5RI7DPOCIXF25JXFXLTILFQ5K3ZB3PB7JQC
/// A [DID Document], only capturing the fields we're interested in.
///
/// Specifically, we only care about the [`DID`], the subset of verification
/// methods we support (as per [`VerificationMethod`]), and any `alsoKnownAs`
/// references.
///
/// [DID Document]: https://www.w3.org/TR/did-core/#dfn-did-documents
}
impl PartialDocument {
/// Validate the document according to the [`ValidDocument`] criteria.
///
/// Returns `None` if validation doesn't pass. Consumes `self`, as an
/// invalid document isn't of any further use.
pub fn validate(self) -> Option<ValidDocument> {
let verification_method = self
.verification_method
.into_iter()
.filter(|method| method.controller == self.id && method.public_key.has_ed25519())
.collect::<Vec<_>>();
not(verification_method.is_empty()).then(|| {
ValidDocument(Self {
verification_method,
..self
})
})
}
/// A [`PartialDocument`] passing certain validation criteria for use in
/// `yapma`.
///
/// Constructed by [`PartialDocument::validate`].
///
/// The validation criteria are essentially that at least one
/// [`VerificationMethod`] must be in the document for which:
///
/// * the `controller` is equal to the document `id`
/// * if the `public_key` is a PGP key, it must be an Ed25519 key or have at
/// least one Ed25519 subkey.
///
/// Any [`VerificationMethod`] which doesn't pass this is removed from the
/// `verification_method` list of the inner [`PartialDocument`].
pub struct ValidDocument(PartialDocument);
/// `true` if the given PGP key contains at least one Ed25519 signing key.
pub fn has_ed25519(key: &pgp::SignedPublicKey) -> bool {
use pgp::{
crypto::ecc_curve::ECCCurve,
types::public::PublicParams,
};
fn is_ed25519(params: &PublicParams) -> bool {
matches!(
params,
PublicParams::EdDSA {
curve: ECCCurve::Ed25519,
..
}
)
}
is_ed25519(key.primary_key.public_params())
|| key
.public_subkeys
.iter()
.map(|sub| sub.key.public_params())
.any(is_ed25519)
}