xdid_method_key/
lib.rs

1//! [xdid](https://github.com/unavi-xyz/xdid) implementation of [did:key](https://w3c-ccg.github.io/did-method-key/).
2
3use parser::DidKeyParser;
4use xdid_core::{
5    Method, ResolutionError,
6    did::Did,
7    did_url::DidUrl,
8    document::{Document, VerificationMethod, VerificationMethodMap},
9};
10
11mod keys;
12mod parser;
13
14pub use keys::*;
15
16const NAME: &str = "key";
17
18pub struct MethodDidKey;
19
20impl Method for MethodDidKey {
21    fn method_name(&self) -> &'static str {
22        NAME
23    }
24
25    fn resolve(
26        &self,
27        did: Did,
28    ) -> std::pin::Pin<
29        Box<
30            dyn std::future::Future<Output = Result<xdid_core::document::Document, ResolutionError>>
31                + Send
32                + Sync,
33        >,
34    > {
35        debug_assert_eq!(did.method_name.0, self.method_name());
36
37        Box::pin(async move {
38            let parser = DidKeyParser::default();
39            let did_key = parser
40                .parse(&did)
41                .map_err(|_| ResolutionError::InvalidDid)?;
42
43            let did_url = DidUrl {
44                did: did.clone(),
45                path_abempty: String::new(),
46                query: None,
47                fragment: Some(did.method_id.0.clone()),
48            };
49
50            Ok(Document {
51                id: did.clone(),
52                also_known_as: None,
53                controller: None,
54                verification_method: Some(vec![VerificationMethodMap {
55                    id: did_url.clone(),
56                    typ: "JsonWebKey2020".to_string(),
57                    controller: did.clone(),
58                    public_key_jwk: Some(did_key.to_jwk()),
59                    public_key_multibase: None,
60                }]),
61                authentication: Some(vec![VerificationMethod::Url(did_url.clone())]),
62                assertion_method: Some(vec![VerificationMethod::Url(did_url.clone())]),
63                capability_invocation: Some(vec![VerificationMethod::Url(did_url.clone())]),
64                capability_delegation: Some(vec![VerificationMethod::Url(did_url)]),
65                service: None,
66                key_agreement: None,
67            })
68        })
69    }
70}