Signature verification in Solidity involves a four-step process: (1) hash the message using keccak256, (2) sign the message off-chain using a wallet, (3) compute the eip_sign_message_hash by prefixing the message hash with '0x1901' and 'ethereum Signed Message' followed by the message length, then hashing again, and (4) verify the signature inside the smart contract by calling ec_recover with the eip_sign_message_hash and signature, then comparing the recovered signer with the expected signer. The signature is split into three components (R, S, and U8B) using assembly, where R and S are 32 bytes each and U8B is 1 byte, totaling 65 bytes.
Verify Signatures in Solidity 0.8: A Step-by-Step Guide
Added:the process of verifying a signature using solidity is in four steps first you have the message to sign and then you hast a message next you would sign the message this will be done off chain this will not be done inside the smart contract so this will be done off-chain using your wallet in the last step you can verify the signature inside the smart contract by calling the function ec recover passing in the hash of the message and the signature this function will return a signer and you can check that the signer that was returned from this function ec recover is actually equal to the true signer that signed the message i'm going to show you how to verify a signature inside solidity the first thing that we'll do is create a function called verify which will take in a message signature and assigner and verify that the signature is valid along the way we'll need to create some internal functions so we'll do that after we create the main function verify so let's get started i'll create the function by typing function verify it's going to take in several inputs address of the signer this will be the address that we expect ec recover to return next will be the message that we're signing for this example we'll keep it simple and we'll sign a string message so i'll say string memory message the last input will be the signature so it'll be bytes memory sig we'll make this function external appear and it will return a boolean if the signature is valid and the signer that was returned by ec recover is actually equal to the signer from the input then it will return true otherwise we'll return false so as outline in the first step the first thing that we will do is hash the message for hashing the message we will use catch up 256 and the hash returned by catch act 256 is bytes32 and we'll name this variable message hash is equal to get message hash this will be a function that we will write out later basically it will do a catch act 256 of the input the input will be the string message that we pass as an input to the function verify now when we actually sign the message off chain the message that is signed is not message hash this hash that is actually signed is also 32 and we'll name it if sign message hash is equal to get if sign message hash and then we'll pass in the message hash from step one we have not defined this function yet but we will do it later for the last step we'll take this eve signed message hash verify it with the signature this will recover the signer so we'll check that the signer that was returned is equal to the signer from the input we'll define this function later but for now we'll say recover passing in the eve sign message hash and also the signature and then we'll compare with the signer from the input and then we'll return this this completes the function verify and this will be the main function that we're going to be calling to verify a signature of the message we will now write the code for get message hash gets signed message hash and the function recover let's start with get message hash the function get message hash will take in a single input a string memory message we'll make this function public this function will be pure and it will return bytes 32 of the hash of the message inside this function will simply return the cat check 256 of api dot encode packed message next we'll write the function for get eve sign message hash i'll copy this function paste it here and then rename this function to get if sign message hash the input to this function will be bytes 32 of message hash so also change the input to message hash earlier i said that when we actually sign the message hash this hash will be actually prefixed with some strings and then hashed again and that will be the actual message that is signed the string that is prefixed to message hash is some shame with the message ethereum signed message followed by the length of the message bytes32 so the length of the message is 32. we append the actual message hash and then take the catch act 256 of this whole string so this is the actual message that is signed when you sign a message off chain once we have the actual message that is signed that you've signed message we will now write the function called recover that will take the eve sine message and signature and recover the signer from these two inputs the function we cover will take two inputs bytes 32 of e sign message hash and bytes memory of the signature for this example we'll make this function public this function will be pier and it will return the signer recover from the signature and the e sign message hash so we'll type returns address from the signature the first thing that we need to do is split the signature into three parts so we'll say byte 32 r bytes 32 s and u and 8 b is equal to the internal function called split which we will define later passing in the signature the r and s over here are cryptographic parameters used for digital signatures and the parameter b is something unique to ethereum we won't need to worry what all of these parameters mean all we need to do with these parameters is pass it to the function called ec recover passing in the message that was signed which will be e sign message hash and then passing in b r and s this is a function available in solidity so we will now have to define this function this function returns the address of the signer given the signed message and these parameters so we'll return the signer by typing return and the last function that we'll write is split how do we split the signature into these three parameters so i'll name the function split it's going to take in bytes memory sig for the input we'll make this function internal pure and it's going to return three parameters so you'll type returns bytes r bytes 32 s and unit 8 b we'll do a quick check on the signature and make sure that the signature length is equal to 65. why is it 65 well it is because bytes 32 is 32 length the next byte is 32 is another 32 length and unit 8 is 1 byte which is one length 32 plus 32 plus 1 is equal to 65 so we'll type require sig dot main is equal to 65 and if it is not we'll throw a message saying invalid signature length once we know that the signature length is equal to 65 we'll get the parameters for rsb from the signature sig and to do that we'll use assembly so i'll type assembly now sig is a dynamic data this is because it has a variable length and for dynamic data type the first 32 bytes stores the length of the data that's one thing to remember here and the other important thing to remember here is that this variable sig is not the actual signature it is a pointer to where the signature is stored in memory so with that in mind we can get the value of r by typing r assign to m load this will go to memory 32 bytes from the pointer that we provide into this input the first 32 bytes of sig is the length of the sig so you'll need to skip it by typing add to the pointer of sig 32 again here we're saying that from the pointer of sig skip the first 32 byte because it holds the length of the array after we skip the first 32 bytes the value for r is stored in the next 32 bytes next we'll get the value for s by saying s assigned to and load so load from memory add to the pointer upside skip the first 32 because it stores the length and also skip the next 32 because it holds the value for r 32 plus 32 is 64.
we'll do something similar for the value of b as well so we'll say b assign to load from memory add to the pointer of sig we want to skip the first 64 plus another 32 which stores the value of s so we'll say 96 and then for the value of b we don't need 32 bytes we only need the first byte so we'll say byte get the first byte from the 32 bytes after 96. now notice that i don't have to type return r s and b this is because the return is implicit we named our return values here and then we assigned it here so solidity will implicitly return these values we're now done with this function and this completes this contract let's now deploy this contract and actually sign some message and verify it i'll compile the contract by hitting compile cig then deploy the contract scroll down and then open the contract for the message that we're signing we'll say it is secret message and then we'll get the message hash so this will be the message that we're going to be signing using metamask i'll open my browser console by typing f12 and then we'll enable metamask by typing ethereum dot enable this will return a promise open the promise and make sure that it is fulfilled meaning that there was no error opening meta mask i'll assign an account that i'm going to be using to a variable named account this is the account that i'm going to be using to sign the message the message that i'm going to be signing is a hash equal to it will be this hash over here and sign the message inside the browser console i'll type ethereum dot request the method that i'm gonna call is personal sign and the parameters that i'm gonna pass is the account and the hash then hit enter i'll sign the message and this will return a promise again if i open it i can see here that this is the signature once we have the signature we can now call recover and verify to call recover we'll need to pass in the e sign message hash so we'll take this hash copy it paste it here and then call ethe sign message so this will be the message hash that we'll need to pass to the function we cover to recover the signer the function recover takes in two parameters e sign message hash and the signature so copy the e sign message hash from here paste it here and for the signature we'll copy it from the browser console signature copy it paste it here and then call scroll down and the function recover return this address over here and if you look at the browser console over here you can see that the account matches the signer that was recovered so now if we call verify for the signer we'll again paste the address of the signer the message that we signed was secret message and the signature you'll get it from the browser console paste it call scroll down and the signature check cell now just to show you that this function is working correctly i'm going to change the message a little bit then call and it returns false likewise if i fix the message and maybe change the signature a little bit for example change the first five to one and then call again it returns false to give you a better idea why it is returning false we will call the function recover when we change the message for example say secret messages and then call get message hash this will change the hash and if i were to provide this hash into esign message and then call each sign message this will change the eve sign message hash and if i were to put this inside here then the recovered address will be not equal to the actual signer and just to show you this i'm going to change a valid eve signed message hash to something else by changing the first nine to a one and then if i call notice that the address changed the signer that was recovered is not equal to the actual signer i'll fix the e sign message hash the other case where the verification fails is if the signature is invalid here we have a valid signature and i'm gonna invalidate this signature by changing some of the input for example changing the first five to a one again then calling call again notice that the signer that was returned is not equal to the actual signer
Up Next

Understanding Cross-Chain Bridges: Blockchain Interoperability Explained
@NextGen_V
2.5K views•2025-01-14

Torrent File Format & Bencoding: A Technical Deep Dive
@AsliEngineering
12.5K views•2022-08-08

Arbitrage Algorithm for Uniswap V2 and V3 Pools
@smartcontractprogrammer
166 views•2026-02-18

Understanding Ethereum: A Comprehensive Beginner's Overview
@99Bitcoins
3.1M views•2018-06-26
Related Study Plans & Knowledge Roadmaps
Structured learning paths in Blockchain & Crypto









![Learn Solidity: The COMPLETE Beginner’s Guide [Full Course] | Solidity Tutorial](https://i.ytimg.com/vi/9BZ0zjqwCPs/maxresdefault.jpg)





























