What is an ecrecover function in Solidity?

In Solidity, the ecrecover function is an inbuilt cryptographic method that enables the retrieval of the signer's address of a message that has been signed using their private key.

This function is frequently used in Ethereum smart contracts to verify the authenticity of user messages. The ecrecover function is also known as ecrecover.

Syntax of ecRecover function

Let's see ecRecover function syntax:

function NamofFunction(parameter list) scope returns() {
//statements
}

Parameters of ecrecover function

The ecRecover function takes four parameters as input. The datatypes, along with parameters and explanation, are as follows:

  1. bytes32 - The hash of the message that was signed.

  2. uint8 - The v value of the signature, where v the value represents the recovery identifier.

  3. bytes32 - The r value of the signature.

  4. bytes32 - The s value of the signature.

Return type of ecrecover function

This function returns the Ethereum address of the signer as address type.

Coding example

pragma solidity ^0.5.0;
contract SignatureVerifier {
function verify(bytes32 messageHash, uint8 v, bytes32 r, bytes32 s) public pure returns (address) {
bytes32 prefixedHash = keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", messageHash));
address signer = ecrecover(prefixedHash, v, r, s);
require(signer != address(0), "Invalid signature");
return signer;
}
}

Explanation

  • In line 1 we import the solidity package.

  • In line 3 we create the contract name SignatureVerifier.

  • In lines 4 to 9 we create a verify function that takes four parameters messageHash,v, r, and s as input and return the Ethereum address of the signer. In order to use this function, the signer must first sign the message with their private key and then supply the values for v, r, and s, and the values for the validate function. The verify method then uses the eRrecover code to retrieve the signer's Ethereum address and confirms that it is not the null address (0x0), which would suggest an invalid signature.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved