verifyProof method
Future<bool>
verifyProof(
- dynamic proof,
- dynamic data,
- String did, {
- String? challenge,
- Map<
String, dynamic> ? jwk, - Future<
DidDocument> didResolver() = 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)));
}