checkForRevocation function

Future<bool> checkForRevocation(
  1. dynamic credential, {
  2. Erc1056? erc1056,
  3. RevocationRegistry? revocationRegistry,
})

Implementation

Future<bool> checkForRevocation(dynamic credential,
    {Erc1056? erc1056, RevocationRegistry? revocationRegistry}) async {
  Map<String, dynamic> credMap;
  if (credential is VerifiableCredential) {
    credMap = credential.toJson();
  } else {
    credMap = credentialToMap(credential);
  }

  // check for Revocation
  if (credMap.containsKey('credentialStatus')) {
    var credStatus = credMap['credentialStatus'];

    if (credStatus['type'] == 'EthereumRevocationList') {
      if (revocationRegistry != null) {
        revocationRegistry.setContract(credStatus['id']);
        var revoked = await revocationRegistry
            .isRevoked(getHolderDidFromCredential(credMap));
        if (revoked) throw RevokedException('Credential was revoked', 'rev');
      } else {
        throw RevokedException('Revocation contract needed', 'revErr');
      }
    } else if (credStatus['type'] == 'RevocationList2020Status') {
      var status = RevocationList2020Status.fromJson(credStatus);
      var res = await get(Uri.parse(status.revocationListCredential),
              headers: {'Accept': 'application/json'})
          .timeout(Duration(seconds: 30), onTimeout: () {
        return Response('Timeout', 408);
      });

      if (res.statusCode == 200) {
        var revCred = RevocationList2020Credential.fromJson(res.body);
        try {
          await verifyCredential(revCred);
        } on SignatureException catch (_) {
          throw RevokedException(
              'could not verify RevocationListCredential', 'revErr');
        }

        var revoked = revCred.isRevoked(int.parse(status.revocationListIndex));
        if (revoked) {
          throw RevokedException('Credential is revoked', 'rev');
        }
      } else {
        throw RevokedException(
            'Error loading status list from ${status.revocationListCredential}',
            'revErr');
      }
    } else if (credStatus['type'] == 'StatusList2021Entry') {
      var status = StatusList2021Entry.fromJson(credStatus);
      var res = await get(Uri.parse(status.statusListCredential),
              headers: {'Accept': 'application/json'})
          .timeout(Duration(seconds: 30), onTimeout: () {
        return Response('Timeout', 408);
      });

      if (res.statusCode == 200) {
        var revCred = StatusList2021Credential.fromJson(res.body);

        if (revCred.statusPurpose != status.statusPurpose) {
          throw RevokedException(
              'StatusPurpose of StatusListEntry and StatusListCredential do not match',
              'revErr');
        }
        try {
          await verifyCredential(revCred);
        } on SignatureException catch (_) {
          throw RevokedException(
              'could not verify RevocationListCredential', 'revErr');
        }

        var revoked = revCred.isRevoked(int.parse(status.statusListIndex));
        if (revoked) {
          throw RevokedException(
              'Credential is ${status.statusPurpose == CredentialStatus2021Purpose.revocation ? 'revoked' : 'suspended'}',
              status.statusPurpose == CredentialStatus2021Purpose.revocation
                  ? 'rev'
                  : 'sus');
        }
      } else {
        throw RevokedException(
            'Error loading status list from ${status.statusListCredential}',
            'revErr');
      }
    } else {
      throw RevokedException(
          'Unknown Status-method : ${credStatus['type']}', 'revErr');
    }
  }

  return false;
}