Bitcoin Transactions

in #bitcoin6 years ago

In this post we will explore what is a bitcoin transaction and how it works.

What is a Transaction

A transaction is the fundamental building block of the bitcoin blockchain. It’s the operation in which we transfer value from one party to another in a secure way. Here you can see a random transaction with one confirmation in the blockchain:


A transaction on the Bitcoin block explorer

This chart shows some of the information that a transaction has, but it doesn’t reflect how it really works. It’s just a way to put the information together in a readable user friendly way.
We will go over some concepts from this chart, and we’ll look at the fact that bitcoins, at a protocol level, aren’t “coins” at all. Nor is there any bitcoin balance or storage, the definition of bitcoin as “a distributed ledger” is very important, because it’s exactly that, a ledger that records transactions with inputs and outputs!

A note about bitcoin: One bitcoin can be divided down to eight decimal places, and each of the individual decimals are called “satoshis”. So in a transaction where I send 0.00000045 bitcoin I can say that I spent 45 statoshis.

A transaction is composed by inputs and outputs. These aren’t just addresses as the chart shows. From now on, we’re going to call the outputs the proper way: Unspent Transactions Outputs or UTXO’s. In the Bitcoin protocol, the transaction inputs are also UTXO’s from a previous transaction, therefore the UTXO is the fundamental building block of a bitcoin transaction.

A side note, for the sake of clarity, we say that inputs are UTXO’s from previously transactions, which is true, but later on this article, we’ll see that there’s also the unlocking script that lives in the UTXO acting as input.

Are you getting a basic idea on how the bitcoin blockchain works yet? Remember, it’s a distributed ledger that has a record of every UTXO made. Now it’s clearer why there aren’t any bitcoins or storage, it’s all in the transaction history!
For example, for a crypto wallet to show your “balance”, it has to look in the blockchain for the UTXO’s which your private key controls. Then, it adds up the values of the UTXO’s and shows you the final balance. Similarly when you want to spend one bitcoin, your wallet will check if you have enough UTXOs that adds up to one Bitcoin. If you have enough, the wallet will create another transaction with these UTXO’s as inputs.

Crypto wallets and light clients usually tracks the UTXO’s of the private keys they own. Full node clients track all UTXO’s of the network in their UTXO set.

The UTXO is a discrete and indivisible unit of value denominated in satoshis. The first question that comes to mind when we say that UTXO’s are indivisible: what happens when we try to spend less than the UTXO’s that we own? Let’s see an example:
I want to send Sarah, 0.5 Bitcoins, and in my Balance I have 1 bitcoin that came from only one UTXO, so I can’t divide the UTXO of one bitcoin, I must send the entire bitcoin in the transaction, and create another output as my change. Similar as when we pay with a five dollar bill something that is only two dollars, we can’t divide the five dollar bill, so we pay with the entire five dollar bill and receive three dollars as change. This is an important aspect about bitcoin transactions.

If we watch again the bitcoin transaction chart, we’ll see that there’s a total input of 4.32123876 BTC from a previously UTXO, in the estimated bitcoin transacted we see 0.1 BTC, and in the outputs we can see 4 different addresses, probably the one with 0.1BTC is the receiver of the transaction, and the other three are random generated address to give back the change. For security and anonymity reasons some wallets will generate random bitcoin addresses and divide the change payments in order to make it more difficult to track the spent funds, that’s also why we see an “estimated” bitcoin transacted.

A transaction on the Bitcoin block explorer

There’s one exception for transactions that don’t consume a previously generated UTXO, and these are called “coinbase” transactions. This is the first transaction in a block and are generated from the fees of the transactions in that block to be paid to the miner.

Transaction Fees

Did you notice the difference between the total input and the total output? If you look at the Fee value of the transaction, you will see that the fee is the difference between the total input and total output. So the formula for calculating fees is:

Total Input — Total Output = Fee

And this is important, because it is something that must be calculated “on the fly”, there isn’t any space in the transaction structure where the fees are saved. When the transaction is being constructed, we must create an output that is a bit less than the input in order to let the miners know the fees that the transaction carries.

Most of the transactions include a fee that is meant to compensate the miners for securing the network. It’s also beneficial to stop spammers for sending constant transactions. The fees are collected by the miner who includes the transactions on the next block of the blockchain. Another important aspect is that fees aren’t mandatory, however transactions with zero fees rarely get mined and sometimes will not even be propagated across the network.

How the size of the fee is calculated?

In order to choose the difference between total input and total output, we have to see how complex the transaction is. The size of the fee is calculated from the size of the transaction in kilobytes, not by the amount of bitcoin being transacted. A transaction of 10 BTC could have less fees that one with 2 BTC.

Nowadays, there are certain services that calculate the size of the fee dynamically, based on user input. The user can choose between high, medium and low fees. The size of the fee affects the processing priority of the transaction, this means that miners will look for transactions that have higher fees to be mined first because they will collect more money, therefore lower fees will result in more time processing.

How UTXOs can be spent or how transaction inputs are created?

In this section, we explore the mechanism that Bitcoin has in order to validate that an UTXO is spent by the it’s owner, and after being spent, the receiver has ownership of the UTXO created as output. For this, every transaction, has on a very basic level a cryptographic puzzle or locking script that serves as a validator of the transaction. For the locking script to be solved, there’s an unlocking script also. Let’s see them with more detail:

The Locking and Unlocking Scripts

The locking script specifies a condition placed on an UTXO. This condition is the one to be met in order to spend this output in the future. The unlocking script on the other hand, satisfies the conditions placed on an output by a locking script and allows the output to be spend. The unlocking script exist in the transaction input.


This is a chart showing the concept of a transaction

When creating an input, we have to provide an unlocking script that satisfies the locking script of a certain UTXO. Then the nodes that validate the transactions will have the input with the unlocking script, so they have to track the UTXO that is needed, and take the locking script from it. After that, the node executes the locking and unlocking scripts together. The input is valid if the unlocking script satisfies the locking script conditions.

The Scrypt Language

Bitcoin transaction language is called Scrypt. This is the language where the locking and unlocking scripts are written. It is a turing incomplete programming language by design, that means that it lacks certain control flows that other turing complete languages have — on purpose for security reasons. Specifically, Scrypt doesn’t have loops or complex flow control capabilities. This ensures that the language has limited complexity and predictable execution times, leaving less chances for hacking or introducing bugs by programmers.

It is important to understand that the locking and unlocking scripts are based on a programming language so they are binded to the logic inside the code, so it could be whatever the programmer wants it to be, allowing nearly an infinite variety of conditions to be expressed. In other words, the transaction validations are not based on static patterns.

With that being said, most of the transactions in the bitcoin network are locked with a Pay-to-Public-Key-Hash or “P2PKH” script. These outputs contain a locking script that locks the output to a bitcoin address. An output locked with a “P2PKH” script can only be spent by presenting the public key and digital signature created by the corresponding private key.

To summarize, in bitcoin almost all transactions use a P2PKH locking script for locking unspent transactions. This means we have to provide a digital signature and the corresponding public key as the unlocking script in the transaction input. Then, the validation nodes will take the corresponding UTXO and execute the associated locking script with our unlocking script and check if it’s valid. If it is, the transaction gets validated and added to a block in order to be propagated to the network.

That’s how transactions works in Bitcoin, thank you so much for having reach this point, and I hope you have learned something new about this amazing technology.

See you next time!

Coin Marketplace

STEEM 0.27
TRX 0.13
JST 0.032
BTC 63096.57
ETH 2954.05
USDT 1.00
SBD 3.55