Apr 28, 2024 1:13:57 AM

Bitcoin’s record-keeping system is based on individual coins, not accounts. In each transaction, coins are destroyed, and new ones are created, including some given to the miner when a block is mined. This is how the protocol increases its overall supply. Bitcoin functions like a coin manager.

These new coins are called Unspent Transaction Outputs (UTXOs). The ledger records all UTXOs to prevent double-spending. Once a UTXO is spent, it cannot be reused. The rules for using a UTXO are defined by a script - a piece of programming code. Only those who can prove they have the private key to sign the script can spend the UTXO.

Transactions require UTXOs as input and generate UTXOs as output. For example, Alice has 3 UTXOs (2, 7, and 10 bitcoins) and wants to send 11 bitcoins to Bob. Alice can use two of her UTXOs (10 and 7 bitcoins) as input. These UTXOs are no longer valid after the transaction is completed. Two new UTXOs are generated: one with 11 bitcoins for Bob and another with one bitcoin for Alice.

The transaction writer decides how many UTXOs will be used as input and how many will be generated. The only rule is that the total input value must match the output value, ensuring no bitcoins are created or destroyed. Only one UTXO is generated in mining new blocks, and none are destroyed.

We can consider Bitcoin as programmable money. The most common transaction type is P2PKH (pay-to-public-key-hash), in which bitcoins are sent to a single user. P2SH (pay-to-script-hash) is another transaction type where bitcoins are paid to a script that executes on the recipient’s end. The core idea of Bitcoin is straightforward: all coins are registered, and transactions turn old coins into new ones.

UTXOs have the advantage of not needing to store account information. However, wallets require extra work to calculate user balances. The only way a wallet can determine the number of bitcoins in an account is by looking up all the UTXOs, verifying which ones can be spent using the private key, and adding them up.

For blockchains that support smart contracts, there are better alternatives than UTXOs. Neo, for example, abandoned UTXOs in favor of an account-based system because of the added complexity of storing transactions and UTXO data. In the next lesson, we will explore account-based mechanisms.