What happens when you transfer Bitcoin?

Cryptocurrencies are becoming more and more a part of our mainstream. Despite the ebbs and flows of the crypto market, more and more people are beginning to be a part of this industry. Yet, the common people still do not fully trust currencies like bitcoin. Now, part of the reason for this lack of trust could be that these currencies are still in their nascent phase, and the markets around them are supposed to be volatile. However, part of the reason could also be that people do not fully understand such currencies and the technology behind them. Unlike cash, which people can physically hold, or banks, which have a centralized authority, cryptocurrencies operate in a decentralized manner by the community as a whole, with the help of incredibly sophisticated technology. To fully trust these currencies, people need to have a basic understanding of the underlying technology. In a previous article, I covered how a blockchain agrees upon its state using different types of consensus mechanisms to avoid fraud and double-spending. In this article, I will cover one of the most basic features of any currency: how to transact with it.

fear.gif

If you have ever used your phones or PCs for regular bank transactions or PayPal/PayTM transactions, then initiating a transaction will be intuitive enough for you. You will set up a crypto wallet of your choosing based on simple instructions and then hold the currencies in your wallet. The process may differ a bit depending upon your wallet, but essentially, you are supposed to enter a valid public key or address(or scan the corresponding QR code that automatically fills the field up). This public key or address can be understood to be just the account number of a bank account; just like account numbers, a public key corresponds to one unique user and is sufficient to send the currency to that user. After filling this field, you will fill in the amount you want to transfer.

1_mdLK65venmiyUgtzf_H88g.png

Until this point, the process to send a bitcoin is very similar to the process to send money through a bank. However, this is where the first difference between the two processes appears. If you have ever made a transaction through credit/debit cards, you may be aware of the small transaction fees that banks charge to complete your transaction. This transaction fee is a fixed percentage of the total amount. However, since cryptocurrencies like bitcoins are decentralized, there is no central institution to complete the transactions. It’s the miners that verify the transaction and include it in the blockchain and it’s them, your transaction fees go to. The wallet gives you the option to enter any amount as the transaction fees. However, the important point to note is that this fee is a reward for the miner to prioritize verifying and completing your transaction. If your transaction fee is too small, it can cause a delay in the completion of your transaction, and sometimes, the transaction might even revert. Think of transaction fees as the tip you choose for the valets to complete your food delivery. If the valet can see all the tips, and your tip is too small, your food will take a much longer time to be delivered. After the inclusion of a reasonable transaction fee, you can send your transaction, at which point your transaction is propagated to the bitcoin network, where one of the many miners will verify your transaction and include them in the blockchain at which point, it becomes a confirmed transaction.

As you may realize, on the user’s end, the process to transfer a cryptocurrency is not that complicated. But to trust the process and the safety and integrity of your transactions, we must take a look under the hood to see what’s really going on. I will try to explain the key points in increasing order of complexity, all the way to the cryptography that supports the entirety of the transaction mechanisms.

Let’s start from the beginning, when you took for granted that wallets hold all your currencies, just like your cash wallets hold all your cash. In the case of cryptocurrency, the process is slightly more complicated. Digital currencies like bitcoins always remain on the blockchain. All you possess is the ownership of those currencies. This ownership is established through something known as a private key. Private keys are alphanumeric sequences that are the sole access to all the crypto assets associated with them. You can intuitively imagine private keys as the password to your bank account. Anyone possessing the private key has complete access to the currencies associated with that private key. Thus, it is imperative that you don’t share your private key with anyone. This private key is often accompanied by a public key. As the name suggests, this is a key that you can freely provide to people for them to send you money.

Now we get to the part where the magic starts to happen.

200.gif

When you log in to your crypto wallet, you will see the total amount of crypto you hold, associated with that private key. You may think that this is just like logging in to your online net banking and seeing the total amount you have in your bank account. However, while both the amounts represent the same thing, how they are calculated is widely different. The thing is, while your online net banking just asks your bank servers how much amount you possess in a certain account, such a thing is impossible to achieve in the case of cryptocurrencies like bitcoins. While you already may know that there are no centralized institutions like banks in decentralized crypto, as it turns out, there are no such things as accounts either. To understand how bitcoin is stored on the blockchain without accounts, let’s consider an example where Alice transfers Bob 1 BTC. When the transfer of this 1 BTC is completed to Bob, it is stored as Bob’s unspent transaction unit in a publicly accessible database called the chain-state. Now let’s assume that Bob has to transfer 1 BTC to John. When Bob puts 1 BTC amount in his wallet to transfer to John, Bob’s wallet will take the unspent transaction unit that he received from Alice, and pass its ownership to John. That same 1 BTC will now be stored in the database as John’s 1 BTC. It is this passing of ownership of unspent transaction units from one owner to another that is referred to as a Transaction. This model of transacting, used in popular cryptocurrencies like Bitcoin, is known as the Unspent Transaction Output model (UTXO). Every time you receive some bitcoin, it is stored as a different unspent transaction unit. This is the reason that crypto wallets do not need accounts to display your total balance. All your wallet does is that it scans the entire blockchain and aggregates all the unspent transaction units belonging to you.

pubkey.PNG

To get a sense of how ownership of UTXO is transferred, we now have reached a point where we cannot go any further without delving into the underlying cryptography. The first essential piece of knowledge we need to understand is that your public key and private key are part of a key pair. To further nail this point home, let’s take a look at how both the keys are generated. Simply put, the private key is a 256-bit number, picked at random by your wallet or another key generator software. For bitcoin, the private key can be any random number between 1 and 1.158 * 10⁷⁷. The way this is practically achieved is to input a large string of bits(from a truly random source) into an SHA-256 algorithm. SHA-256 is a hashing function, that takes an input of any size and procures a result that is always 256 bits long. Thus by using it, we generate a truly unique private key to be used for your digital currency. Now the interesting part is that your public key is also generated from your private key using a one-way cryptographic function, known as the elliptic curve multiplication. The elliptic curve used in bitcoin is known as “secp256k1”, which is represented by the function y² mod p = (x³ + 7) mod p. Without going into much more mathematical detail about this elliptic curve, the important point to remember here is that this function is asymmetric in nature, i.e. while the private key can be converted to find the public key, due to the asymmetry of point arithmetic in elliptic curves, the public key cannot be converted to find the private key. This feature of elliptic curve cryptography is especially important because we need to remember that the public key is readily available to anyone that wants to send you the money. If that public key could be reverse engineered to figure out the private key in a feasible manner, everyone that has your private key would then be able to access all your cryptocurrency since they will have your private key. [An important side note: earlier I used the terms public key and address interchangeably since both of them can be used to transfer crypto to another person. However, they are not the same. In fact, addresses are derived by double hashing a public key: first hashing a public key by SHA-256 and then by RIPEMD 160. This public key hash then goes through a BASE58 encoding to procure an address]. But the use of elliptic curves in Bitcoin does not end here. The Bitcoin protocol uses the Elliptic Curve Digital Signature Algorithm. Signatures have a very important role to play in computer science. A person can use their private key to sign any piece of data. And once again, because of the asymmetric nature of elliptic curves, while your secret private key is required to create the signature, anyone can verify the validity of your signature by your publicly available public key.

prv.PNG

The derivation of a public key from a private key has a very special meaning in Bitcoin. The fact that there are no accounts in Bitcoin, only UTXO, poses questions of its own. For example, when one transfers money from their bank account to another bank account, the bank can simply check the sender’s account to see if they possess the money that they are sending. But how does a miner verifies the same when they verify your bitcoin transaction? To understand this, we need to understand how a transaction is structured when you transfer some bitcoin to another address. Every transaction has two parts: a transaction input, which are the details provided by the sender, and a transaction output, the resulting data produced after completion of the transaction. Now, remember the Alice and Bob example, where we realized that anytime Bob receives 1 bitcoin from Alice, the bitcoin received by Bob is stored in a central database as Bob’s unspent transaction. So, how can a miner verify that it is in fact Bob who owns this unspent transaction unit? As it turns out, when Alice sends Bob some bitcoin on his public key, she is locking that unspent transaction unit with Bob’s public key. Now whenever Bob needs to use this unspent transaction unit to transfer bitcoin to John, he can claim its ownership by providing his digital signature, which he creates by his private key. The transaction input, that Bob will provide whenever he wants to use that specific unspent transaction unit contains, among other things, a reference to the specific unspent UTXO that has the cryptocurrency he is going to use and an unlocking script, which contains his digital signature. A miner can then use the reference provided by Bob to check the central database, which contains all the blockchain’s UTXO, whether such an unspent transaction unit exists. In the central database, alongside Bob’s unspent transaction unit that he received from Alice, will be a locking script. This locking script was generated when Alice sent the 1 BTC to Bob by locking it with his public key. The miner now has all the information they need to complete the transaction. They have Bob’s public key from the locking script and they have Bob’s signature has been signed by Bob’s private key. Returning full circle, since the public key is derived from the private key, the locking and unlocking scripts can be matched without revealing Bob’s private key and the transaction can be completed for John. Bob will now no longer be in possession of that specific UTXO, as he would have passed its ownership to John, by locking that UTXO with John’s public key.

utxo.PNG

For this last part, I want to quickly go over the mystery of why Bitcoin transactions do not have a fixed transaction fee and how these transaction fees vary. The key point to understand is that while bank transaction fees depend on the total amount that is being transacted, the bitcoin transaction fees depend on the amount of data that needs to be verified and included in the blockchain to complete the transaction. To visualize this process, I want you to imagine the unspent transaction units as ‘notes’ that we use while transacting with cash. Imagine that Alice has received 5 BTC from a mining reward. The reason I am choosing a mining reward as the source of Alice’s bitcoin is that in the case of the UTXO model, all the bitcoin generated from one source can be considered a single note. Now Alice has an unspent transaction unit or note of 5 BTC. If she wants to send 1 BTC to Bob from this note, then this transaction will be represented as Alice sending a note of 5 BTC to Bob, and Bob returning a change of 4 BTC back to Alice. This is exactly like the way we transact with cash. If we want to buy something worth 2 dollars, but all we possess is a 5-dollar note, then we do not cut the note into pieces to procure a 2-dollar note. Instead, we pay the 5-dollar note and receive 3 dollars in change. The only difference is that while in the cash system, the units of currencies are pre-defined(1$, 5$, 10$, etc. You cannot have a note of 7.25$), in UTXO, an unspent transaction unit or ‘note’ can be worth any amount as long it is generated from the same source. Now, while Alice sending 1 BTC to Bob by transferring 5 BTC and receiving 4 BTC is actually a large amount of currency, the transaction fee for this transaction will be relatively small. The reason for this is that the data involved in the transaction will be small. After all, all a miner needs to verify is whether Alice is in possession of this one note of 5 BTC. On the other hand, let’s say Alice has 1 BTC on her. But this 1 BTC is a result of her receiving bitcoin from many different sources. For example, let’s assume that Alice received 0.5 BTC from Person A, 0.3 BTC from Person B, and 0.2 BTC from Person C. Now if Alice wants to transfer this 1 BTC to Bob, in the UTXO model, it will be represented as Alice sending Bob three separate notes or unspent transaction units worth 0.5 BTC, 0.3 BTC, and 0.2 BTC. As a result, the amount of data to verify and include the transaction in a block has increased. As a result, this transaction, worth the same amount as the previous one, will have a significantly higher transaction fee.

Bitcoin-fees.jpg

The decision whether or not to invest in crypto and use it as a method of transacting should not be based on hype or fear, but on a rational understanding of the technology underneath it. I hope this article could help you in making some of those decisions. See you next time.

bye.gif

BYE!!!

#THW Web3