I'm trying to verify a signature using OpenZeppelin's SignatureChecker
library, but always returns false. This is the verify function from the lib:
function isValidSignatureNow(
address signer,
bytes32 hash,
bytes memory signature
) internal view returns (bool) {
(address recovered, ECDSA.RecoverError error) = ECDSA.tryRecover(hash, signature);
if (error == ECDSA.RecoverError.NoError && recovered == signer) {
return true;
}
(bool success, bytes memory result) = signer.staticcall(
abi.encodeWithSelector(IERC1271.isValidSignature.selector, hash, signature)
);
return (success && result.length == 32 && abi.decode(result, (bytes4)) == IERC1271.isValidSignature.selector);
}
And this is how I'm generating the signature using Ethers.js: https://docs.ethers.io/v5/api/signer/#Signer--signing-methods
const dataToSign = {"data1":"value1","data2":"value2"};
const dataHash = ethers.utils.keccak256(
ethers.utils.toUtf8Bytes(JSON.stringify(dataToSign))
);
const dataHashBin = ethers.utils.arrayify(dataHash);
const wallet = new ethers.Wallet(process.env.PK);
const signature = await wallet.signMessage(dataHashBin);
const isValid = await signTest.verify(wallet.address, dataHash, signature); //--> inherits OP's lib
//have passed 'dataHashBin' instead of 'dataHash', same issue
console.log(isValid); // ---> false every time
I think the problem is in how I'm generating the signature, but can't figure out why I'm doing wrong.
Thanks!