1

We have a function:

function _validSignature(bytes memory signature, bytes32 msgHash) internal view returns (bool) {
        return msgHash.toEthSignedMessageHash().recover(signature) == _signerAddress;
    }

As i know toEthSignedMessageHash() takes messageHash as argument and recover() takes messageHash and signature as argument. But i did not understand how writing code like this:msgHash.toEthSignedMessageHash().recover(signature) working. Can you explain me this? When i tried same thing like this:

function recoverAddress(bytes32 _hash) public returns(address recovered){
        bytes32 signature = ECDSA.toEthSignedMessageHash(_hash);
        recovered = ECDSA.recover(_hash, signature);
        return recovered;
    }

its giving error for ECDSA.recover().

Error:

Member "recover" not found or not visible after argument-dependent lookup in type(library ECDSA).

Why? Why my approach giving error?

1 Answer 1

0

msgHash.toEthSignedMessageHash().recover(signature) works because you're attaching the ECDSA library's functions to bytes32.

This line:

using ECDSA for bytes32

will make it so everytime you have bytes32var.someFunction(otherArgs), the compiler will interpret it like ECDSA.someFunction(bytes32var, otherArgs).


This function:

    function recoverAddress(bytes32 _hash) public returns(address recovered){
        bytes32 signature = ECDSA.toEthSignedMessageHash(_hash);
        recovered = ECDSA.recover(_hash, signature);
        return recovered;
    }

is giving you error cause there's no recover function that takes (bytes32, bytes32) as arguments. You probably meant to write something like:

    function recoverAddress(bytes memory signature, bytes32 msgHash) public returns(address recovered){
        bytes32 _message = ECDSA.toEthSignedMessageHash(msgHash);
        recovered = ECDSA.recover(_message, signature);
        return recovered;
    }
2

Your Answer

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

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