fi_ethr_resolver/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use ethr::build_did_doc_from_logs;
use fi_common::{did::DidDocument, error::Error};
use regex::Regex;
use util::strip0x;

mod did;
mod ethr;
mod events;
mod util;
mod verification;

pub async fn resolve(did: &str, provider: &str, accept: &str) -> Result<DidDocument, Error> {
    let context: Vec<String> = match accept {
        "application/did+json" => Vec::new(),
        "application/did+ld+json" => Vec::from([
            String::from("https://www.w3.org/ns/did/v1"),
            String::from("https://w3id.org/security/suites/secp256k1recovery-2020/v2"),
            String::from("https://w3id.org/security/v3-unstable"),
        ]),
        _ => {
            return Err(Error::new(
                format!(
                    "The DID resolver does not support the requested 'accept' format: {}",
                    accept
                )
                .as_str(),
            ))
        }
    };

    let regex = match Regex::new("^(.*)?(0x[0-9a-fA-F]{40}|0x[0-9a-fA-F]{66})$") {
        Ok(val) => val,
        Err(error) => return Err(Error::new(error.to_string().as_str())),
    };

    if !regex.is_match(did) {
        return Err(Error::new(
            format!("Not a valid did:ethr: {}", did).as_str(),
        ));
    }

    let did_components = did.split(":").collect::<Vec<&str>>();
    /*
    let network = match did_components.len() >= 4 {
        true => did_components[2..did_components.len() - 1].join(":"),
        false => String::from(""),
    };
    */

    let contract_address = strip0x(String::from(*did_components.last().unwrap()));

    let mut did_doc = DidDocument {
        context,
        id: String::from(did),
        verification_method: None,
        authentication: None,
        assertion_method: None,
        capability_delegation: None,
        capability_invocation: None,
        key_agreement: None,
        services: None,
    };

    let (created_did_doc, _deactivated, _version_id) =
        match build_did_doc_from_logs(provider, contract_address.as_str(), &mut did_doc).await {
            Ok(val) => val,
            Err(error) => return Err(error),
        };

    Ok(created_did_doc)
}