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:z6Mk') || did.startsWith('did:key:z6LS')) {
    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 client = await HttpClient()
          .getUrl(Uri.parse('$resolverAddress/1.0/identifiers/$did'))
          .timeout(Duration(seconds: 30));
      var res = await client.close();
      if (res.statusCode == 200) {
        var contents = StringBuffer();
        await for (var data in res.transform(utf8.decoder)) {
          contents.write(data);
        }
        var didResolution = jsonDecode(contents.toString());
        return DidDocument.fromJson(didResolution['didDocument']);
      } else {
        throw Exception('Bad status code ${res.statusCode}');
      }
    } catch (e) {
      throw Exception('Something went wrong during resolving: $e');
    }
  }
}