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?

2 Answers 2

2

Hello Team Not sure if this was ever resolved but i faced the same issue but have resolved this using the latest

@openzeppelin/contracts:^5.0.2

making use of the following

import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";

import "@openzeppelin/contracts/utils/cryptography/MessageHashUtils.sol";

and the utilizing this in my contract like this

 address recovered = ECDSA.recover(
        MessageHashUtils.toEthSignedMessageHash(bytes32HashHere),
        bytesSignatureHere
    );

I Hope This answer can help the community Thank You Team.

1
  • Yes, when it comes to the time that I put this comment, there is no "toEthSignedMessageHash" function in ECDSA contract. That function has been moved to MessageHashUtils contract.
    – Sapthaka
    Commented Oct 20 at 7:30
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 you have read our privacy policy.

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