getDidFromSignature function
Extracts the did used for signing jws
.
If a detached jws is given the signed string must be given separately as toSign
.
toSign
could be a String or a json-object (Dart Map).
Implementation
Future<String> getDidFromSignature(String jws,
{String? toSign, Erc1056? erc1056}) async {
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 (toSign != null) {
payload = removePaddingFromBase64(base64UrlEncode(utf8.encode(toSign)));
} else {
throw Exception('No payload given');
}
var signingInput = '${splitted[0]}.$payload';
var hashToSign = sha256.convert(ascii.encode(signingInput)).bytes;
var pubKey = ecRecover(hashToSign as Uint8List, signature);
var did = 'did:ethr:${EthereumAddress.fromPublicKey(pubKey).hexEip55}';
if (erc1056 != null) {
if (erc1056.networkName != 'mainnet') {
did =
'did:ethr:${erc1056.networkName}:${EthereumAddress.fromPublicKey(pubKey).hexEip55}';
}
var expectedDid = await erc1056.identityOwner(did);
if (expectedDid != did) {
throw Exception('Did of Signature do not match with ERC1056 entry');
}
}
return did;
}