This is going to be a small post. I am going to go into the details of signing a transaction in a blockchain network. Have you ever wondered how it's done? I mean 'signing' a transaction - it sounds a bit strange, isn't it. If you had imagined an old man sitting infront of a computer clicking on a 'sign now' button in order to sign a transaction, then you are not alone. Let's now understand how it is really done.
What is a Blockchain Transaction?
Before we go into the nitty gritty details, we must understand three key points -
A blockchain node is required to interact with the blockchain network
Blockchain is the single source of truth, i.e. if someone claims that their transaction is not processed, one can always verify it on the blockchain using the blockchain explorer in order to settle disputes
If you cannot setup a blockchain but still want to interact with blockchain, then you'll need to use services like 'Infura' which itself is connected to a blockchain node in it's backend
You might not have heard of infura but you might have heard about the Metamask wallet. Metamask interacts with blockchain using infura service in it's backend. That's how metamask can 'sign' Ethereum transactions. But what is a transaction? A transaction can be considered as a record of one account attempting to send some money (ETH) or tokens to another account. In every Ethereum transaction, there is a 'transaction object' that is pushed to the blockchain. This transaction object is the 'main ingredient' of a block in a blockchain.
There are two main types of transactions - transactions from a contract account (smart contract transactions) and transactions from an external account (user to user transactions). Each transaction object has got the following properties (we will discuss them in details in the coming sections):
- From: 'from' account which sends the transaction, who is also responsible for all the payments
- To: 'to' account, in case of crypto transfers (this is an optional field; smart contracts doesn't have this field)
- value: amount of Ether to be send to the target address
- gasPrice: amount of Ether the sender is willing to pay per unit gas to get this transaction processed
- startGas/gasLimit: unit of gas that this transaction can consume
- V,R,S: cryptographic hash value generated from the private key of an account that is used to sign the transaction
- nonce: junk value that is used to mine a block
- data: this field is only present in smart contract transactions and it has only the compiled bytecode of smart contracts to be executed in the blockchain
Smart contracts are written in Solidity language which is though not exactly but is somewhat similar to TypeScript lanugage. Anyways, as the topic of our discussion is transaction signing, I will write about it in another article.
Public Key, Private Key and (Ethereum) Account
We should understand these terms first in order to grasp the concept of transaction signature. Take Metamask for example. Within it, there is an option to add account. What is an account? Think of it like the email address which you can share with anyone. You can open your email account using it's password but no one else can.
In this case, this password exists in the forms of private key & public. Private key is the most important part of the three which one needs to safe guard it at any cost because if bad actors gets a hold of it, you will permanently lose your account. Public key is generated as a one way function from the private key itself using the ECDSA (Elliptical Curve Digital Signature Algorithm) algorithm. One way function in the sense that public key is generated from the private key but vice versa is not possible. And the account is created by taking the last 20 bytes of the public key and hashing it through the KECCAK algorithm. That means, one can generate the public key and account using the private key alone.
Signing a Transaction
How does the blockchain know whether it can transfer X ETH from account A to account B? I mean the balance can be found out by looking the send/receive transactions in the blockchain explorer but how does the network know that account owner of X IS making the transaction? Well that is accomplished by the signature of the transaction. How is it done you ask? Simple. Just by including the V,R, and S value within the transaction object.
As mentioned before, the private key of an account is passed through a cryptographic algorithm which will spit out three equal hash values. What is the significance of this step? Well, the blockchain network will take the 'R' and 'S' part ('V' part is used for something else which we will not discuss here) of the signature and hash it through the ECRECOVER algorithm to generate the account. Now if this account is same as that of the 'From' account (and it should have transactional balance too) then the blockchain will know that the account owner is making the transaction.
Conclusion
Transaction signatures ensure authenticity of transactions. Signatures that is used to sign transactions are generated from the account's private key, where the account itself and the public key is generated from the private key. Due to this, the private key needs to be safe guarded at all cost and it is better kept somewhere offline out of reach of most of the people.
Posted Using LeoFinance Beta
Congratulations @mishalalexander! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s) :
Your next target is to reach 50 upvotes.
Your next target is to reach 50 upvotes.
You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word
STOP
To support your work, I also upvoted your post!