I am trying to use the OpenZeppelin ECRecovery library for a project I am working on.

Unfortunately I don't fully understand how it works and there's no documentation for it.

Let's say I want to sign the message 'hello'. First I hash it:

msg = web3.sha3('hello');

Then I sign it by using web3.eth.sign and metamask.

web3.eth.sign(web3.eth.accounts[0], msg, (err, res) => {
    sig = res;

The returned signature is:


In the library code it says:

// Check the signature length
if (sig.length != 65) {
  return (address(0));

So if I call the function with the hash and the above signature it will fail because this signature has a length of 132.

I also thought about using the r or the s value of the signature only by splitting it in JavaScript: (even though I don't even know what these values represent, as there is absolutely no information in the solidity documentation)

var r = sig.substr(0,66);
var s = "0x" + sig.substr(66,64);

But both have a length of 66 and fail as well.

Has anyone ever used their library and can help me out?


1 Answer 1


Its length is 132 but you have 2 for the prefix '0x' then you have 130 hexadecimal characters that represent 130/2 = 65 bytes as expected. You have to make sure you are sending them as an array of bytes.

  • I don't understand. Which one of the values (r or s) should I send? Could you please write an example code in js?
    – Chris
    Commented May 20, 2018 at 7:01
  • I tried sending this: "ebb853cf9818c590e428bfa2b793b23e975d978bfefbb8aff164a282786c3eda0054113161b1a72f29f371a35b9a378413d689e49addcea34a8a617270c2bc951c" but it does not work.
    – Chris
    Commented May 20, 2018 at 7:08
  • I got it... I manually wrote the input parameters and set the signature as bytes32. That was the problem. Thanks.
    – Chris
    Commented May 20, 2018 at 7:14

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.