I think that I can verify a hash of a message using my signature within a Solidity contract, something like this:

contract ecrecover {
    function testRecovery(bytes32 hash, bytes signature) returns (address) {
    bytes32 r;
    bytes32 s;
    uint8 v;
    assembly {
      r := mload(add(signature, 32))
      s := mload(add(signature, 64))
      v := and(mload(add(signature, 65)), 255)
    if (v < 27) v += 27;
    address addr = ecrecover(hash, v, r, s);
    return addr;

I am unclear how to generate the hash or signature given my message and private key. For example, say my message is the string: I am Eric

My account data are:

address: 0x9858EfFD232B4033E47d90003D41EC34EcaEda94

Private Key: 0x1ab42cc412b618bdea3a599e3c9bae199ebf030895b039e9db1e30dafb12b727

How would I generate the hash and signature of my message?

1 Answer 1


The following should work:

let signature = web3.eth.accounts.sign (
  'I am Eric',
let hash = web3.eth.accounts.hashMessage('I am Eric');

See eth.accounts.sign, web3.eth.accounts.hashMessage documentation for details.

  • That is for javascript, I presume. Is there a way to generate the signature and hash within Solidity? Commented Nov 5, 2019 at 17:30
  • While this is possible in theory, this does not make much sense, because Solidity is commonly used to develop smart contracts, and smart contract usually cannot do anything in private, thus smart contract would not be able to preserve privacy of the private key used for signature generation. Commented Nov 5, 2019 at 17:35
  • So when Solidity contracts verify messages, this is always for confirming off-chain transactions? Commented Nov 5, 2019 at 18:44

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.