question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ECDSA.sol recover() returns different address than ecrecover()

See original GitHub issue

I may be going crazy, but it seems like ECDSA.recover() and ecrecover() return different addresses.

šŸ”¢ Code to reproduce bug Here’s what I get from web3.accounts.sign():

{ message: 'This is a signed message',
  messageHash:
   '0x7aa10675d70f58a5e7427fdc02dfd8207ec1c637163aa974382c683fea3df843',
  v: '0x1b',
  r:
   '0xf0150782e269d731548e9fe92eadd90e6178bcd7d2c948b2583e132020f8b44d',
  s:
   '0x23c27ecea23c7d9579807b2f574430fb5be4266ab50945c1c509bf04212d1294',
  signature:
   '0xf0150782e269d731548e9fe92eadd90e6178bcd7d2c948b2583e132020f8b44d23c27ecea23c7d9579807b2f574430fb5be4266ab50945c1c509bf04212d12941b' }

Here’s the solidity code:

function verify(bytes32 hash, bytes memory signature) public view returns (address){
        return ECDSA.recover(hash, signature);
    }
    
function testRecovery(bytes32 h, uint8 v, bytes32 r, bytes32 s) public view returns (address) {
      bytes32 prefixedHash = keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", h));
      address addr = ecrecover(prefixedHash, v, r, s);

      return addr;
  }

šŸ“ Details Screenshot from 2019-04-07 16-25-23 Is this even possible? I thought keccak was collision-resistant!

šŸ’» Environment

Latest OpenZeppelin, solidity 0.5.0, local Ethereum (pre-Constantinople, not mainnet, if that matters)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jdowning100commented, Apr 8, 2019

Hello @jdowning100! I believe the issue is that you’re recovering two different hashed messages: in the ECDSA test you provide the same hash value as you do for h in the ecrecover test, but this second one doesn’t expect a hash like ECDSA does, but a message instead (which it then hashes).

That’s actually not true - ecrecover() expects a hash of the message: https://github.com/ethereum/solidity/blob/develop/docs/units-and-global-variables.rst#mathematical-and-cryptographic-functions But anyways, I discovered what I did wrong. The ECDSA contract has a function toEthSignedMessageHash() for prefixing the message hash with the proper ā€œ\x19Ethereum Signed Message:\n32ā€ prefix but the function is unused. When I call toEthSignedMessageHash(hash) in recover() it works. (I should also note that web3.eth.accounts.sign adds the prefix, while web3.eth.sign does not, which is totally ridiculous)

0reactions
frangiocommented, Jan 13, 2022

@maksimjeet20566 Please create a topic in our forum and share the code that you’re using to generate and verify the signature.

Read more comments on GitHub >

github_iconTop Results From Across the Web

solidity - Getting wrong address from ECDSA.recover()
I stumbled upon this question and went through the Ethers.js code as well as the draft-EIP712.sol code, but kept having the same result...
Read more >
Utilities - OpenZeppelin Docs
The data signer can be recovered with ECDSA.recover , and its address compared to verify the signature. Most wallets will hash the data...
Read more >
Ethereum's ecrecover(), OpenZeppelin's ECDSA, and web3's ...
This article talks about some of the tricks of dealing with Ethereum's ecrecover() function. This is a changeup from the more businessĀ ...
Read more >
What is ecrecover in Solidity? - Solidity developer
In some cases ecrecover can return a random address instead of 0 for an invalid signature. This is prevented above by the owner...
Read more >
Address 0x41997744c0bf7eee41e3903dc8b3df0f7ba8e847
function owner() public view virtual returns (address) { return _owner; } /** * @dev Throws if called by any account other than the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found