I have read that to sign a message we need the private key but in the below code snippet hasing is done in the 'getMessageHash' function and 'signing' is done in the 'getEthSignedMessageHash' function. But here we are not using any private in both the functions then how can it be possible to sign a message without the private key and I have seen the same structure in the 'MessageHashUtils' library and i'm aware that signature generation process occurs off-chain. Am I missing something ?https://medium.com/@solidity101/100daysofsolidity-verifying-signatures-on-the-blockchain-a-solidity-deep-dive-b3b964ad51f5

//Function to hash the message

    function getMessageHash(
        address _to,
        uint _amount,
        string memory _message,
        uint _nonce
    ) public pure returns (bytes32) {
        return keccak256(abi.encodePacked(_to, _amount, _message, _nonce));
//function to sign the message

    function getEthSignedMessageHash(
        bytes32 _messageHash
    ) public pure returns (bytes32) {
        Signature is produced by signing a keccak256 hash with the following format:
        "\x19Ethereum Signed Message\n" + len(msg) + msg
                abi.encodePacked("\x19Ethereum Signed Message:\n32", _messageHash)

1 Answer 1


This is not signing, it is hashing of a message, the hash is then used. "Signed" in function name means addition of "\x19Ethereum Signed Message:\n32" here.

I think these methods are in the contract for verification

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.