0

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!

1 Answer 1

0

SOLUTION:

const dataToSign = {"data1":"value1","data2":"value2"};

const dataHash = ethers.utils.keccak256(
   ethers.utils.toUtf8Bytes(dataToSign)
);
const dataHashBin = ethers.utils.arrayify(dataHash);

const ethHash = ethers.utils.hashMessage(dataHashBin);

const wallet = new ethers.Wallet(process.env.PK);
const signature = await wallet.signMessage(dataHashBin);

const isValid = await signTest.verify(wallet.address, ethHash, signature);

console.log(isValid); //true

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.