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.
ecRecover
functionLet's see ecRecover
function syntax:
function NamofFunction(parameter list) scope returns() {//statements}
ecrecover
functionThe 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
- The v
value of the signature, where v
the value represents the recovery identifier.
bytes32
- The r
value of the signature.
bytes32
- The s
value of the signature.
ecrecover
functionThis function returns the Ethereum address of the signer as address
type.
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;}}
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