verify method
Verifies a (detached) JWS
Implementation
@override
FutureOr<bool> verify(String jws,
{String? did, Map<String, dynamic>? jwk, dynamic data}) {
var splitted = jws.split('.');
if (splitted.length != 3) throw Exception('Malformed JWS');
var signature = _getSignatureFromJws(jws);
String payload;
if (splitted[1] != '') {
payload = splitted[1];
} else if (data != null) {
String signable = '';
if (data is String) {
signable = data;
} else if (data is Map<String, dynamic>) {
signable = jsonEncode(data);
} else {
throw Exception('Unexpected Datatype ${data.runtimeType} for toSign');
}
payload = removePaddingFromBase64(base64UrlEncode(utf8.encode(signable)));
} else {
throw Exception('No payload given');
}
var signingInput = '${splitted[0]}.$payload';
var hashToSign = sha256.convert(ascii.encode(signingInput)).bytes;
var pubKey = web3_crypto.ecRecover(hashToSign as Uint8List, signature);
if (did != null) {
return EthereumAddress.fromPublicKey(pubKey).hexEip55 ==
did.split(':').last;
} else if (jwk != null) {
// TODO: Check if it works
return EthereumAddress.fromPublicKey(pubKey).hexEip55 ==
EthereumAddress.fromPublicKey(Uint8List.fromList(
base64Decode(addPaddingToBase64(jwk['x']))))
.hexEip55;
} else {
throw Exception('Either did or jwk must be given');
}
}