Ddecades before it was created Bitcoin, the obsession with creating a purely digital money haunted many people's heads. But one of the main problems that everyone ran into head on was the famous double spending.
It was Satoshi Nakamoto who, thanks to a set of techniques, solved this great problem, being able to create the first purely digital decentralized money. In this article we will explain what UTXOs are and the great role they play in creating transactions so that your bitcoins can travel safely within the protocol blockchain.
UTXO, a concept to avoid double spending
As we have already explained in the article aimed at How do Bitcoin transactions work?, a transaction is made up of inputs and outputs. The set of inputs and outputs, together with currencies to be sent and cryptographic signatures, result in a transaction hash, usually called a HASH ID.
The entries are HASH ID of a transaction that received the purse and that have not been used previously, that is, they are UTXO, while the output is the destination address, to which UTXO will be created that you can later use in a transaction. The same address can have infinite UTXOs. This is why UTXOs are defined as a set of transactions.
When a person needs to send a transaction, it has to be nourished by UTXO. That is, of transactions that you have received and that have not been spent. This means that a person can use one or more UTXOs for the same transaction. In fact, those UTXOs can be part of one or more addresses in your wallet. And we go further, even a transaction could be created with UTXO from addresses of different wallets, as long as each one is signed with its corresponding clear private key.
All of this leads to one place: a UTXO can only be used once. And this is fundamental within the operation of blockchain technology, as it is part of the set of tools that guarantees that currencies are not used more than once (the famous double spending).
Such is the reliability of using this mechanism to identify unspent coins that this is the way in which in Bitcoin you can count how many coins there are in circulation. What you do is add up all the coins that are in the UTXO, that is, in the transactions that have not been spent.
Here is a simple example so that you understand the flow:
Imagine that Maria wants to pay Pedro a total of € 100 in bitcoins for a job. María hopes to receive payment from some people who owe her bitcoins and from that money she will pay for Pedro's work.
When Maria receives these payments, she has had some "Inputs" of money, and from those inputs she will make an "Output" to pay Pedro. Maria was paid her debts, which were a payment of € 75 and another of € 50.
When she pays Pedro, Maria has to use both entrances, since neither of them has enough, and she will put Pedro as the exit.
After that, Pedro will have a UTXO entry (he has received the payment).
Where do the remaining coins go?
As you can see, María had a total of € 125 divided into two UTXOs, but she only needed to send € 100. You have had to use both UTXOs, setting Pedro's address as Output or output, but your wallet will have done something else transparently to Maria. Your purse will have put an address for Maria as an outlet, to which you will have assigned the remaining € 25. It is what is known as direction of change.
By the way, there are some transactions that become a UTXO but are not generated by a previous UTXO. Read on to find out what they are.
With what has been described above you can understand how this model of "Tickets" y "Departures", can help us to establish relations of possession and granting of money. A relationships that in blockchain are marked by the use of cryptography and that provide us with absolute security that the money has reached its destination and that it is effectively under the control of the recipient.
UTXO and its importance in Bitcoin
In Bitcoin all transactions have this structure of inputs and outputs. In the "Entries" of a Bitcoin transaction, we can see the origins of the balance that we are managing with our wallet or purse. While in the "Departures", we can see where we send our money. In addition to this, we can also see the remaining money that is returned. All of this is visible and traceable by anyone, since the Bitcoin blockchain is public and transparent.
If you look closely, this model allows you to have total control over how money can be used and under what conditions it can be used. First of all, in order to use a balance in Bitcoin, that balance must be considered as a UTXO within your wallet. That is, someone must make a payment (an exit or UTXO) so that this payment becomes your entry and thus you have available balance to spend.
This is a recursive process. That is, it repeats from the moment the coin is generated as a result of a mined block. For example, a coinbase transaction it is actually a UTXO created by the miner to send that balance to an address under his control. Thus, this exit transaction becomes an inflow of money for the miner that can later be spent. The same process is repeated for the rest of Bitcoin users. With this we can see that UTXOs are an essential part of Bitcoin transactions and without them, their operation would be impossible.
Expanding the example of how a UTXO works on Bitcoin
We can see the operation of a UTXO in Bitcoin in the following way:
Daniel wants to pay for Luis's car, valued at 1 BTC, and a total of 1,5 BTC is available in his wallet. Daniel's balance is divided into two directions, A with 0,8 BTC and B with 0,7 BTC.
Thus, Daniel goes to Luis and makes the payment for 1 BTC. At this point, Daniel's wallet cannot send 1 BTC directly because the balance is split in two directions. So it takes both balances and converts them into the inputs of the payment transaction. He then takes the address of Luis and assigns to it the sending of 1 BTC, additionally assigns a total of 0,4995 BTC to the exchange address, and the rest remains as the mining commission.
Once Daniel submits his transaction, it will be processed and confirmed by the network, beginning its path to irreversibility. And at this point, it will be easy to see where the transaction UTXOs are.
The first UTXO that we can detect in the example are Daniel's balances. Those addresses with 0,8 BTC and 0,7 BTC of balance, are two UTXOs that are under their control and are the ones that allow them to make the payment to Luis. Since Daniel is in control of those balances, he can transform his UTXOs into "Tickets" for a new payment as he does in this example.
Immediately after the network confirms Daniel's transaction, the situation changes. Now, Daniel no longer has under his control the balances he had originally, and instead, his transaction has generated new UTXOs that overwrite the previous ones. The new UTXOs in question are represented first, by Luis's address and the 1 BTC he has received in it from Daniel. And second, for the exchange address and the 0,4995 BTC that Daniel has received from the network, because it is what remains of the payment he made. As we have said, the rest to complete the 1,5 BTC that Daniel had in principle remain as a commission payment for the miner.
Bitcoin Scripts and its relationship with UTXOs
The entire operation of the UTXO in Bitcoin is guaranteed by the Bitcoin Script, the programming language used to write all operations in Bitcoin. Each transaction has an associated script that allows us to:
- Validate that the balance used is really ours.
- Ensure that balances sent can only be spent by the person to whom we send them.
But how are these operations carried out? This is what we explain below:
Validating that the balance is ours
The first task to be able to use a balance in Bitcoin is to demonstrate that this balance is indeed ours. To do this, the first thing to keep in mind is that each UTXO that we transform into an entry (balance that we are going to spend) is actually the exit of a previous transaction that gave us access to those BTC. That is, all Bitcoin balances have a UTXO associated with it, and said UTXO has a blocking script associated with it.
This blocking script is a digital padlock that we must open in order to then be able to make use of the balance of said UTXO. The blocking scripts in Bitcoin are varied, but the most common is the P2PKH (Pay to Public Key Hash). Although there are also the P2SH (multisig), the P2PK (the most primitive of all). This blocking script can be opened with the private key that results in the address indicated in the UTXO.
So in order to unlock said script, what we must do is take the Bitcoin address, take our private key and generate the public key of said address. At the end we stamp our digital signature and with all this verified data, the balance of said address is unlocked so that we can use it. That is, the nodes will accept the transaction and put it in the mempool waiting for the miners.
On the contrary, if we fail to do that, the transaction is simply rejected by the nodes and we will not be able to use the balance. This process is called an unlock script. Of course, this cryptographic process is automatic and transparent for the users of a wallet, the whole process is carried out by the wallet itself, but now you know what happens inside it. In this scheme, this part of the operation is described in the scriptSig of each Bitcoin Script.
Creating the lock script
Proving that we can spend our UTXOs, we continue with the transaction creation process, creating a lock script for the new destination-directed UTXO. In this case, what the wallet does is create a script similar to the one that was created for us previously, which indicates that the only person who can spend this new UTXO is whoever has the private keys in their possession that control the addresses to where we have sent.
In this way, what is created is a validation chain, where bitcoins when passing from one address to another, are blocked to be used by the last person who takes possession of them.
As you can see, in Bitcoin everything has a well-defined why, and that allows all its parts together to allow the correct functioning of the cryptocurrencies. Being an open protocol, all nodes that want to be part of the protocol interpret the rules in the same way.
All the transactions are reviewed by all the nodes, being accepted those that accept the majority and confirmed those that the majority of the hash power (generated by the miners) accept. These controls democratically guarantee compliance with the protocol.