Solidity Smart Contract To Implement Message Signature And Verification: Let's code series
When we talk about signatures, the first thing that comes into our mind is the attestation and validation of a document or an asset from an authorized person. The concept of signature dates back to 3000 BC, it was originated from the Egyptian cultures in the form of small symbols, seals or complex patterns. Still in the modern days, the transaction of paper money is accomplished via cheques which is only valid if it has the signature of the owner of that bank account.
WHAT IS A DIGITAL SIGNATURE:
Prerequisites:
Setting up Ganache with remix:
NOW TIME TO CODE:
I'll break down the smart contract functions into small headings to explain what is happening.
i) Hashing the Message:
This function encrypts the message using Keccak256 algorithm which returns a 32 byte code hash value. The arguments of this function includes: i) The address of the message receiver account
ii) Amount of transaction
iii) Message in string notation
iv) Nonce (Number only used once) a number added in the hash string which strict the use of that hash again to ensure preventing the bypass.
Pure modifier is used in this function to ensure that the state of this function can not be modified.
ii) Signing the hash of the message:
This function expects the hash of the message we just created and and signs that hash using keccak256. You'll get clearer picture when i'll explain the usage of this function in the verification.
iii) Verification: Now the verification is the most interesting part of this whole journey, we verify a message by comparing the signer extracted from the signed hash we created in the previous step with the signer recovered from the signature produced from ECDSA cryptographic algorithm using the built in global solidity function ecrecover.
Here the first argument is the signed hash we made in step 2 while the second argument is the signature which we'll produce using ECDSA algorithm.
Now the elephant in the room is the presence of these 3 variables r,s,v, for that we need to understand the working of splitSignature function and why are we splitting the signature in these three components.
According to the documentation of web3, this ecrecover function works over the principle of Elliptic curve cryptography and this function requires total of 4 arguments which includes the signed hash and the three splitted component. The ecrecover function essentially recovers the signer of the signature.
iv) Splitting the signature:
The splitting of the bytes is done using the assembly language which is again a standard protocol proposed to this date and to my knowledge. Here mload is used to load the bytes from the memory to the variable, the add keyword here signifies that we are skipping first 32 bytes because dynamic arrays store the length of the array in the first 32 bytes, similarly for "s" we skip first 64 bytes in order to avoid the length of array and the bytes assigned to "r" and similarly goes for the variable "v".
Our final piece of the puzzle is the function verify itself, so its arguments includes the address of the original signer from the beginning, original amount, message, nonce value and finally the signature produced from ECDSA algorithm, let's talk about generating this signature from the browser console.
If you can see the 100 ethers in your account 2 then it indicates that you have successfully connected a ganache account to metamask


















Comments
Post a Comment