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:
bytes32- The hash of the message that was signed.uint8- Thevvalue of the signature, wherevthe value represents the recovery identifier.bytes32- Thervalue of the signature.bytes32- Thesvalue 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
verifyfunction that takes four parametersmessageHash,v,r, andsas input and return the Ethereumaddressof the signer. In order to use this function, the signer must first sign the message with their private key and then supply the values forv,r, ands, and the values for the validate function. Theverifymethod then uses theeRrecovercode to retrieve the signer's Ethereum address and confirms that it is not thenull address (0x0),which would suggest an invalid signature.
Free Resources