xdid_method_web/
lib.rs

1//! [xdid](https://github.com/unavi-xyz/xdid) implementation of [did:web](https://w3c-ccg.github.io/did-method-web/).
2
3use reqwest::{Client, ClientBuilder};
4use xdid_core::{Method, ResolutionError, did::Did, document::Document};
5
6pub use reqwest;
7
8mod parse;
9
10const NAME: &str = "web";
11
12pub struct MethodDidWeb {
13    pub client: Client,
14}
15
16impl MethodDidWeb {
17    /// Create a new did:web resolver.
18    ///
19    /// # Errors
20    ///
21    /// Returns an error if the HTTP client cannot be constructed.
22    pub fn new() -> Result<Self, reqwest::Error> {
23        let client = ClientBuilder::new().build()?;
24        Ok(Self { client })
25    }
26}
27
28impl Method for MethodDidWeb {
29    fn method_name(&self) -> &'static str {
30        NAME
31    }
32
33    fn resolve(
34        &self,
35        did: Did,
36    ) -> std::pin::Pin<
37        Box<
38            dyn std::future::Future<Output = Result<xdid_core::document::Document, ResolutionError>>
39                + Send
40                + Sync,
41        >,
42    > {
43        debug_assert_eq!(did.method_name.0, self.method_name());
44
45        let client = self.client.clone();
46        let url = parse::parse_url(&did);
47
48        Box::pin(async move {
49            let req = client
50                .get(url)
51                .build()
52                .map_err(|_| ResolutionError::InvalidDid)?;
53
54            let doc = client
55                .execute(req)
56                .await
57                .map_err(|e| ResolutionError::ResolutionFailed(e.to_string()))?
58                .json::<Document>()
59                .await
60                .map_err(|e| ResolutionError::ResolutionFailed(e.to_string()))?;
61
62            Ok(doc)
63        })
64    }
65}