Course Content
Introduction to Blockchain
Introduction to Blockchain
Unspent Transaction Output
UTXO and Balance
The Unspent Transaction Output (UTXO) model is a fundamental concept in the way Bitcoin and several other cryptocurrencies track ownership of digital currency representing the amount of digital currency someone has available to spend.
Each UTXO represents a chunk of Bitcoin that can be used as an input in a new transaction. The Bitcoin network tracks all UTXOs in the blockchain, and the collection of all UTXOs is known as the UTXO set. This set is used to determine a wallet's balance and to verify new transactions.
Let's take a look at an example:
As you can see, Bob has a total of 4 UTXOs, and they all comprise his wallet. Summing up their amounts, we get the balance of Bob's wallet, which is equal to 4 BTC.
Spending UTXOs
Now, let's illustrate the spending of UTXOs with an example to make things clear. Suppose Bob wants to send 1.9 BTC to his friend Alice with the transaction fee equal to 0.01 BTC. Here is her wallet before the transaction:
As for now, Alice has 3 UTXOs making the total balance of 8.45 BTC.
We can now proceed to our transaction, however there is one detail to keep in my mind.
Since Bob doesn't have a single UTXO or a combination of UTXOs equal exactly to 1.91 BTC (1.9 BTC for Alice + 0.01 BTC as transaction fee), he'll have to use a UTXO or a combination of UTXOs with the total amount greater than this one. Let's say, the wallet chooses a UTXO with 2 BTC (however, using 1.5 BTC, 0.3 BTC and 0.2 BTC UTXOs is also possible).
Let's now take a look at our transaction:
After the transaction is confirmed, Alice's wallet balance increases by 1.9 BTC. Bob's wallet balance will now consist of the remaining UTXOs he did not spend (1.5 BTC, 0.3 BTC, and 0.2 BTC) plus the new change UTXO of 0.09 BTC (after the fee of 0.01 BTC is taken out). Bob's new total balance will be the sum of these UTXOs, which is 2.09 BTC.
As you can see, when Bob's wallet creates the transaction, it also creates locking scripts for the outputs. The output that sends 1.9 BTC to Alice will have a locking script that specifies the conditions under which those funds can be spent in the future. This script is known as the scriptPubKey (script public key).
Finally, when Bob broadcasts the transaction to the network, miners will validate it by checking that the unlocking script (scriptSig) for each input satisfies the locking script (scriptPubKey) from the output where the UTXOs were previously sent to Bob. If the scripts are valid and the transaction is confirmed, it becomes part of a block in the blockchain.
Thanks for your feedback!