verifyProof method

  1. @override
Future<bool> verifyProof(
  1. dynamic proof,
  2. dynamic data,
  3. String did, {
  4. String? challenge,
  5. Map<String, dynamic>? jwk,
  6. Future<DidDocument> didResolver(
    1. String
    ) = resolveDidDocument,
})
override

Verifies a LinkedDataProof / DataIntegrityProof

Implementation

@override
Future<bool> verifyProof(proof, data, String did,
    {String? challenge,
    Map<String, dynamic>? jwk,
    Future<DidDocument> Function(String) didResolver =
        resolveDidDocument}) async {
  //compare challenge
  if (challenge != null) {
    var containingChallenge = proof['challenge'];
    if (containingChallenge == null) {
      throw Exception('Expected challenge in this credential');
    }
    if (containingChallenge != challenge) {
      throw Exception(
          'challenge in credential do not match expected challenge');
    }
  }
  var proofValue = proof.remove('proofValue');
  proof['@context'] = ed25519ContextIri;

  List<int> hash = await _dataToHash(data);

  var proofHash = sha256
      .convert(utf8.encode(await JsonLdProcessor.normalize(proof,
          options:
              JsonLdOptions(safeMode: true, documentLoader: loadDocument))))
      .bytes;
  var hashToSign = proofHash + hash;
  // print(hashToSign);

  proof.remove('@context');
  proof['proofValue'] = proofValue;

  var ddo = await didResolver(did);
  ddo = ddo.resolveKeyIds().convertAllKeysToJwk();

  var verificationMethod = proof['verificationMethod'];
  dynamic usedJwk;
  for (var k in ddo.verificationMethod!) {
    if (k.id == verificationMethod) {
      usedJwk = k.publicKeyJwk!;
      break;
    }
  }

  if (usedJwk == null) {
    throw Exception(
        'Can\'t find public key for id $verificationMethod in did document');
  }

  if (usedJwk['crv'] != 'Ed25519') {
    throw Exception(
        'Wrong crv value ${usedJwk['crv']} for this signature suite (ed25519 needed)');
  }

  var decodedKey = base64Decode(addPaddingToBase64(usedJwk['x']));

  return ed.verify(ed.PublicKey(decodedKey), Uint8List.fromList(hashToSign),
      base58BitcoinDecode(proofValue.substring(1)));
}