isValidEip191Signature static method
Implementation
static bool isValidEip191Signature(
String address,
String message,
String sig,
) {
// Get the sig bytes
final sigBytes = Uint8List.fromList(
hex.decode(
sig.substring(2),
),
);
// Get the r and s values from the sig bytes
final r = BigInt.parse(
hex.encode(
sigBytes.sublist(0, 32),
),
radix: 16,
);
late BigInt s;
late int v;
// Depending on the length of the sig bytes, we can determine the v value
if (sigBytes.length == 64) {
Uint8List sBytes = sigBytes.sublist(32, 64);
sBytes[0] &= 0x7f;
v = (sBytes[0] & 0x80 == 0) ? 27 : 28;
s = BigInt.parse(hex.encode(sBytes), radix: 16);
} else {
Uint8List sBytes = sigBytes.sublist(32, 64);
v = getNormalizedV(sigBytes[64]);
s = BigInt.parse(hex.encode(sBytes), radix: 16);
}
// Recover the public key from the signature
Uint8List? publicKeyBytes = AuthSecp256k1.recoverPublicKeyFromSignature(
v - 27,
r,
s,
hashMessage(message),
);
// If the public key is null, return false
if (publicKeyBytes == null) {
return false;
}
// Convert the public key to an address
final hashedPubKeyBytes = keccak256(publicKeyBytes);
final addressBytes = hashedPubKeyBytes.sublist(12, 32);
final recoveredAddress = '0x${hex.encode(addressBytes)}';
return recoveredAddress.toLowerCase() == address.toLowerCase();
}