Photo by olieman.eth on Unsplash
Recap from Week 18
To recap, last week we explained payments in fiat currency through banks & Financial Institutions. We also introduced certain paradigms in crypto such as :
- Wallets
- Private & Public Keys
- Addresses &
- Digital Signatures
Wallet
A wallet:
a. Controls access to a user's cryptocurrency
b. Manages keys and addresses
c. Signs transactions
d. Tracks balances of cryptocurrency
The below image is that of a wallet:
Photo by olieman.eth on Unsplash
Let us explain how a wallet does all of the above and more!!
A wallet has a public & private key and interacts with the blockchain. Now, let us assume that the "Sender" wants to transfer crypto currency (e.g., Bitcoin) to "Beneficiary". For that the Sender needs to "own" some crypto already. Let us assume that at 100 BTC.
We know that wallets control access to a user's cryptocurrency & the only way users can manage their cryptocurrency is through a wallet. This means the user's wallet accesses the "balance" of cryptocurrency in the blockchain. In traditional banking, when we say we have "balance" in our account , what does that mean?
Assume we are opening an account for the first time and we deposit cash into that account. The entry that is passed in the bank's books along with the customer ledger account is as below:
Thus the customer's account has a credit balance of USD 100.
If USD 50 is withdrawn from an ATM, the below entry will be passed in the bank's book
Now, the net balance in the customer account is
USD 100 - USD 50 = USD 50.
This balance is available for the customer to use, spend or transfer for any purpose (Keep this in mind as we read forward!!).
So, a balance in an account arises out of transactions and if
Inflows into the account is > outflows from the account, balance is positive
Inflows into the account = outflows from the account, balance is zero &
Outflows from the account > inflows into the account, the account is said to be overdrawn (if allowed by the bank).
Transactions (Input & Output)
Similarly, in a blockchain, transactions are the foundation and most important part of the system and the system is designed to ensure that transactions are initiated, validated and added to the blockchain.
When we say Sender owns 100 BTC, it means there were transactions - inflows and outflows which caused the net balance to be 100 BTC.
The inflow (credit) transaction is called a TRANSACTION OUTPUT.
Transaction Outputs are indivisible amounts of cryptocurrency recorded on a blockchain .
The outflow (debit) transactions is called a TRANSACTION INPUT.
Transaction Inputs are transactions which spend the cryptocurrency paid to a previous output
(Source of definitions : here)
The below diagram makes it more clear. We assume the balance of 100 BTC came from two transactions - an inflow of 300 BTC & outflow of 200 BTC meaning
Transaction Output = 300 BTC
Transaction Input = 200 BTC
UTXO (Unspent Transaction Output)
The closing balance of 100 BTC is called an unspent transaction output or UTXO. That means this balance is unspent and available for spending in the future (just like closing balance in your bank account).
Here, we assumed there was only one transaction output & input transaction. However, in reality, there will be multiple transactions linked to one address at any point in time just like there are multiple transactions in our accounts. But in a bank account, all the transactions are in one account and there is one closing balance in that account. In a blockchain, each transaction creates a separate UTXO which are spread across different blocks. (We will explain blocks later!!).
Thus, there will be multiple UTXOs attached to one address. When we refer to Sender's bitcoin "balance", it means the sum of all UTXOs that a user's wallet can access and spend and which may be scattered across multiple transactions and different blocks. The below diagram makes it more clear.
Digital Signature
But, there is one question - In a bank account, it is clear that there is one account and we "connect" or sign into that account using a password. In crypto world, how does the crypto "owner" connect to his balance (UTXO) or balances (multiple UTXOs) which is/are in the blockchain? We answered this.. through a wallet.
Now, how does the wallet connect to the UTXO from where the funds can be used later? This is where digital signature comes in handy.
To recap, a digital signature consists of two algorithms :
A SIGNING ALGORITHM that, given a private key & a transaction, produces a signature &
A SIGNATURE VERIFYING ALGORITHM, that allows anyone to verify the signature, given the message & a public key (corresponding to the private key used in the signing algorithm)
In simple terms, Private Key + Digital imprint of Transaction = Digital Signature
which means a private key can be applied to the transaction to produce a digital signature. This signature can only be produced by someone with the knowledge of the private key.
Thus, Sender's wallet is "locked" to the UTXO of 100 BTC through digital signature from his / her wallet. The private key plus the digital imprint of the transaction can be accessed only from Sender's wallet. Thus this is irrefutable proof that the Sender "owns" the funds.
Now, these funds are "locked" to the wallet and if these funds are to be released, they need to be "unlocked". Now, how does the funds get unlocked & released to be spent? In the case of online banking, funds can be sent only through
Input of correct password or OTP &
Input of valid beneficiary.
Scripts (Locking & Unlocking)
In crypto world, there are "scripts" which does this activity. In the case of bitcoin, each bitcoin transaction involves at least one
a. Locking Script &
b. Unlocking Script
A Locking Script is a code which says what conditions must be satisfied to access the UTXO for future spending. (E.g., a condition can be - only owner with a particular private key can "unlock" this transaction)
An unlocking Script is a code which satisfies those conditions and allows the funds to be "unlocked" for spending.
So, to transfer funds, the transaction inputs point to the relevant UTXO and then proves ownership through the unlocking script.
Now, it needs to be understood that a UTXO can only be "spent" or "consumed" in its entirety and not piecemeal. For example, if you have 100 USD and you need to spend 80 USD, you will give 100 USD and receive 20 USD change back. It is the same in bitcoin blockchain.
Let us assume Sender wants to send 50 BTC to Beneficiary. Sender's wallet will scan the blockchain for UTXOs linked to that wallet which are available for spending. It then comes across UTXO for 100 BTC. It will create two transaction inputs from the UTXO -
a. One transaction of 50 BTC which will go to Beneficiary wallet
b. Second transaction of 50 BTC which will go back to Sender wallet
Thus, the UTXO of 100 BTC is extinguished and two other transactions take its place where 50 BTC "moves forward " to Beneficiary wallet as a UTXO which is linked to Beneficiary wallet and 50 BTC moves back to Sender's wallet as a new UTXO meaning they each have " new balance" of 50 BTC to spend in the future.
When the BTC moves from sender to beneficiary, the beneficiary's wallet "ties" the 50 BTC UTXO to itself through it's digital signature and then the UTXO is linked to this wallet.
This is how at a very high level how value "moves" from one address to another in the bitcoin blockchain.
What happens next? Who verifies these transactions and how are these transactions verified?