resolveDidDocument function

Future<DidDocument> resolveDidDocument(
  1. String did, [
  2. String? resolverAddress
])

Resolves the Did-Document for did.

Resolving if did:key can be done internally, for all other did-methods an URL resolverAddress to an instance of a universal resolver is needed.

Implementation

Future<DidDocument> resolveDidDocument(String did,
    [String? resolverAddress]) async {
  if (did.startsWith('did:key')) {
    return resolveDidKey(did);
  } else if (did.startsWith('did:web')) {
    return resolveDidWeb(did);
  } else {
    if (resolverAddress == null) {
      throw Exception(
          'The did con only be resolved using universal resolver, therefore the resolver address is required');
    }
    try {
      var res = await http
          .get(Uri.parse('$resolverAddress/1.0/identifiers/$did'))
          .timeout(Duration(seconds: 30));
      if (res.statusCode == 200) {
        var didResolution = jsonDecode(res.body);
        return DidDocument.fromJson(didResolution['didDocument']);
      } else {
        throw Exception('Bad status code ${res.statusCode}');
      }
    } catch (e) {
      throw Exception('Something went wrong during resolving: $e');
    }
  }
}