isValidEip191Signature static method

bool isValidEip191Signature(
  1. String address,
  2. String message,
  3. String sig
)

Implementation

static bool isValidEip191Signature(
  String address,
  String message,
  String sig,
) {
  // Get the sig bytes
  // print(sig);
  final sigBytes = Uint8List.fromList(
    hex.decode(sig.substring(2)),
  );

  // If the sig bytes aren't 65 bytes long, throw an error
  if (sigBytes.length != 65) {
    throw Exception('Invalid signature length');
  }

  // Get the r and s values from the sig bytes
  final r = BigInt.parse(
    hex.encode(sigBytes.sublist(0, 32)),
    radix: 16,
  );
  final s = BigInt.parse(
    hex.encode(sigBytes.sublist(32, 64)),
    radix: 16,
  );
  // print(sigBytes[64]);
  final v = getNormalizedV(sigBytes[64]);
  // print(r);
  // print(s);
  // print(v);

  // // 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) {
  //   print('Could not derive publicKey');
  //   return false;
  // }

  // Convert the public key to an address
  final publicKeyBytes = crypto.ecRecover(
    hashMessage(message),
    crypto.MsgSignature(r, s, v),
  );
  // print(hex.encode(publicKeyBytes));
  final hashedPubKeyBytes = keccak256(publicKeyBytes);
  final addressBytes = hashedPubKeyBytes.sublist(12, 32);
  final recoveredAddress = '0x${hex.encode(addressBytes)}';

  // final String recoveredAddress = EthSigUtil.recoverSignature(
  //   signature: sig,
  //   message: hashMessage(message),
  //   //  Uint8List.fromList(
  //   //   ascii.encode(message),
  //   // ),
  // );

  // print(recoveredAddress.toLowerCase());
  // print(address.toLowerCase());

  return recoveredAddress.toLowerCase() == address.toLowerCase();
}