verify method

  1. @override
Future<bool> verify(
  1. dynamic proof,
  2. dynamic data,
  3. String did, {
  4. String? challenge,
})
override

Implementation

@override
Future<bool> verify(proof, data, String did, {String? challenge}) async {
  //compare challenge
  if (challenge != null) {
    var containedChallenge = proof['challenge'];
    if (containedChallenge == null) {
      throw Exception('Expected challenge in this credential');
    }
    if (containedChallenge != challenge) {
      throw Exception('a challenge do not match expected challenge');
    }
  }

  //verify signature
  var signature = _getSignatureFromJws(proof['jws']);

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

  var jws = proof.remove('jws');
  proof['@context'] = ecdsaRecoveryContextIri;
  var proofHash = sha256
      .convert(utf8.encode(await JsonLdProcessor.normalize(proof,
          options:
              JsonLdOptions(safeMode: true, documentLoader: loadDocument))))
      .bytes;
  var hashToSign = sha256.convert(proofHash + hash).bytes;

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

  var pubKey = web3_crypto.ecRecover(hashToSign as Uint8List, signature);

  var givenAddress = EthereumAddress.fromHex(did.split(':').last);

  return EthereumAddress.fromPublicKey(pubKey).hexEip55 ==
      givenAddress.hexEip55;
}