did_ion/sidetree/
resolver.rs

1use std::marker::PhantomData;
2
3use iref::Uri;
4use ssi_dids_core::{
5    resolution::{self, DIDMethodResolver, Error, HTTPDIDResolver, Output},
6    DIDBuf, DIDMethod, DIDResolver,
7};
8
9use super::Sidetree;
10
11/// DID Resolver using ION/Sidetree REST API
12#[derive(Debug, Clone)]
13pub struct HTTPSidetreeDIDResolver<S: Sidetree> {
14    pub http_did_resolver: HTTPDIDResolver,
15    pub _marker: PhantomData<S>,
16}
17
18impl<S: Sidetree> HTTPSidetreeDIDResolver<S> {
19    pub fn new(sidetree_api_url: &Uri) -> Self {
20        let mut identifiers_url = sidetree_api_url.to_owned();
21        identifiers_url
22            .path_mut()
23            .push(iref::uri::Segment::new("identifiers").unwrap());
24        // let identifiers_url = format!("{}identifiers/", sidetree_api_url);
25        Self {
26            http_did_resolver: HTTPDIDResolver::new(&identifiers_url),
27            _marker: PhantomData,
28        }
29    }
30}
31
32impl<S: Sidetree> DIDMethod for HTTPSidetreeDIDResolver<S> {
33    const DID_METHOD_NAME: &'static str = S::METHOD;
34}
35
36impl<S: Sidetree> DIDMethodResolver for HTTPSidetreeDIDResolver<S> {
37    async fn resolve_method_representation<'a>(
38        &'a self,
39        method_specific_id: &'a str,
40        options: resolution::Options,
41    ) -> Result<Output<Vec<u8>>, Error> {
42        let did = DIDBuf::from_string(format!("did:{}:{method_specific_id}", S::METHOD)).unwrap();
43        self.http_did_resolver
44            .resolve_representation(&did, options)
45            .await
46    }
47}