Bitcoin Transactions: How they work

Verify.as
verifyas
Published in
4 min readOct 5, 2017

This is the first in a series of posts where we discuss the core concepts behind the Blockchain, Bitcoin and Ethereum. At Verify, we’re building a reputation protocol on the Ethereum blockchain and are sharing these posts in an effort to share our knowledge with the wider crypto community.

You’ve surely heard of bitcoins and know a thing or two about them. One thing for certain is that it is changing the financial system as we know it; and especially the physical currency we know as money. The biggest change is that it gets rid of the centralized body that issues and controls the money. I know that begets many questions; in this post let us start with “how is a transaction executed in bitcoin?”

To answer the question, let us take the scenario where you send some money to a friend, let us call him Jay. In the normal case what happens is that the amount to be transferred is deducted from your account and is added to Jay’s account; otherwise an error is raised, be it for lack of proper address or even lack of funds.

Def transfer_by_bank(Me, Jay, 10$):
If Me not in accounts:
return False #verifies if I have an account in bank
If Jay not in accounts:
return False #verifies if Jay has an account
If 10$ in Me: #meaning I have sufficient balance
Me = Me -10
Jay = Jay+10
Return True #the transfer happened correctly

In bitcoins there isn’t a centralized entity or bank to process the transfer of money. What bitcoin has is what is called a public ledger. From the name it is public and is available and is duplicated all over the bitcoin network hence making it extremely hard to change it or mess with it. Any change made will be detected by the nodes in the network; this is a whole topic on its own, one that we will leave for another post. Now back to the public ledger and how a transfer or transaction happens in bitcoin.

So the ledger can be thought of as a state transition system. You have states and transitions that trigger changes between states. The state transition system takes in a state and a transaction and produces an output which is basically a state/result.

The state consists of ownerships and balances, essentially who owns what. Each ownership and balance is called an Unspent transaction output (UTXO). think of it as the unspent mined coins. It is worth noting here that the owner’s field in the UTXO is 20 Bytes address, and it is the public key of the owner. (in the diagram I am assuming xxxxxx is my/your address and jay’s address is Jay’s; of course everything is hashed in bitcoin; i used this notation for simplicity and easier demonstration)

Note here too that in bitcoins, balances are not added or summed together; meaning if you have 2 BTC in your account and you received 2 BTC from a friend, 6 BTC from your grandfather and 4 BTC from your grandmother, the total here is 14 BTC; in the state, they exist as 4 different UTXOs with you being the owner (your ID/Hash) of each one of them.

Now let us talk about the transaction, it can have a single input or more; however each input should contain a reference to an existing UTXO and a signature produced by the owner’s private key. the result is one or more outputs. Each output contains a new UTXO(s) to add to the state. Note the (s) because there can be more than one output/UTXO.

Before doing any transaction some validation need to take place, the first thing we need to do is to check if UTXO exists

def EXEC(ST, TX):
if UTXO not in ST:
return False # don’t send coins that don’t exist

After that we need to check if the signature matches the UTXO owner.

if SIG != UTXO Owner:
return False # don’t send someone else’s coins

When we have an output we need to do another validation to verify the number of coins entered = the number of coins in the output (Confusing? bear with me I will explain it in a bit)

if SUM(UTXO in ST) < SUM(UTXO in OUTPUT):
return False

Now let me explain why we need to verify that the number of input coins is the same as the output coins; Remember earlier I said that you had 4 UTXOs in the state (owner xxxxxx) of tol value of 14 BTC, 3 of which were sent to you by different people? Assuming now you decided to send Jay 9 BTC.

In this case you check the state and you realize that you have 4 UTXO one being 2 BTC, another 2 BTC, another 6 BTC and the last being 4 BTC; so you take UTXOs that equal or are bigger than 9; you take the closest combination to 9. in this case we can take 2,2 and 6 = 10 BTC. The transaction then is created with these 3 inputs and the result would be 2 outputs. one being Jay’s address with 9 BTC and another would be 1 BTC as change; basically what is left from the 10 BTC. If the 1 BTC is not claimed by you; the miner will claim it.

After the transaction is successful all input UTXOs will be removed and the output UTXOs would be added to the state.

And that is how a transaction takes place in bitcoins. The same process happens for every transaction; validate ownership and balance, process and add to state.

--

--