Transactions in Bitcoin

In one of my previous posts, I had given an overview of how transactions occur. Now I’ll explain how transactions are actually accounted and performed.

Parallels with real currency

In Bitcoin and other cryptocurrencies forked/derived from it, transactions are accounted in a way similar to how physical currency is accounted for when exchanging for goods and services. Let me give an example.

Assume that Alice received 6.25 Bitcoins from a coinbase transaction. Before I continue, let me first explain about coinbase transactions.

Coinbase Transactions

A coinbase trasaction is a special type of transaction that introduces new coins to the Bitcoin economy and is also the only way new currency can be introduced into the system. A miner receives a coinbase transaction when the block created by him/her is accepted by other nodes. For those of you who need a refresher on mining, you check my post about mining.

This means that Alice mined her Bitcoin and now is able to spend it. If Alice wishes to send 3 Bitcoins to Bob, she has to send all of the 6.25 Bitcoins to Bob. This may seem strange to a lot of people since in traditional online transactions, we can specify the exact amount to be transferred. However in the Bitcoin system, extra money that is sent is returned as change to the sender, in this case, Alice. This is similar to how in the real world, we can buy something worth 4 units of money with a currency note having a value of 10 units and the extra amount is returned back.

But how does it work?

Spending An Output
Source: developer.bitcoin.org

In the Bitcoin economy, addresses are used to send and receive transactions. These addresses may seem similar to bank accounts, but they are not. Addresses can easily be generated by wallet programs and ideally aren’t reused. This means that if Alice uses an address 1FxroD4o1v4GMazE5yNMzuqhsidVd9r2CB to send Bitcoins to an address given by Bob, say 1Ndp5nkTdLQ7xm9efAeuZNtKxHy1rXcNi, then the next time, she may use an address like 1KjqzSumGodFiuJNExysaQx6ivDsXfE5g3 to send some Bitcoins to Charlie. Addresses are disposable unlike accounts in traditional online transactions.

Now, if Alice decides to send 6.25 Bitcoins to an address associated with Bob, then the input to the address is a single UTXO (Unspent Transaction Output). In most cases, there will be at least 1 input and 1 output in a transaction. As the name suggests, UTXOs are outputs of a transaction which haven’t been spent before. The 6.25 Bitcoins constitute a UTXO since they represent minted coins. Since Alice only intends to send 3 Bitcoins to Bob, the remaining amount of 3.25 Bitcoins is sent back to an address owned by Alice in the form of a new UTXO. This new UTXO is an element in the set of output UTXOs albeit sent from Alice to herself.

Hello !. This is Bob.

Before this transaction takes place though, Bob will send the cryptographic hash of his public key, encoded in the form of a Bitcoin address. These addresses are base-58 encoded strings. This is unlike strings in English, which are base-26 encoded. In simpler terms, while English language strings (or words) can use any character from the 26 letters of English, Bitcoin addresses use 58 different characters to form strings. As a word may not contain all characters of English in it, a Bitcoin address, may not contain all the 58 characters. An address also contains a hash and a checksum. These addresses may be encoded into a QR code for convenience.

Alice will then decode Bob’s address to obtain the hash of his public key. She will then create a standard P2PKH (Pay-to-Public-Key-Hash) transaction output, which containing instructions which allow anyone to spend that output if they can prove that he/she controls the private key associated with the public key hash. These instructions are called the pubkey script. Alice will then broadcast this transaction to the network and Bob’s wallet will show the 3 Bitcoins as a UTXO.

Creating A P2PKH Public Key Hash To Receive Payment
Source: developer.bitcoin.org

If sometime later, Bob decides to spend the amount given by Alice, he must first reference the transaction created by Alice. This reference is called a transaction identifier or txid. As I mentioned, there may be multiple inputs and outputs to/from a transaction. Each of the outputs has an index associated with it, called the output index. The first output has the index 0. For Bob to be able to spend the amount given by Alice, he will create an input using the txid and the specific output (identified by its index number). Then he will create a signature script which will satisfy the conditions placed by Alice. As mentioned in the above paragraph, Alice’s pubkey script from the previous transaction (of 3 Bitcoins to Bob) contains conditions provided by her.

Unlocking A P2PKH Output For Spending
Source: developer.bitcoin.org

Bob’s signature script will contain two pieces of data:

  • His public key so that Alice’s pubkey script can verify if it is indeed Bob. This is done by hashing it to generate a hashed public key.
  • A digital signature to verify that Bob owns the private key which created the public key. This will be verified by the pubkey script.

If this is becoming a little complicated, I would suggest you to read my post about cryptography in the blockchain before continuing. If in the second transaction, Bob wants to send 1 Bitcoin to Charlie, then he will create another pubkey script just like Alice did. This script will contain conditions which Charlie must satisfy before spending the money later on. Bob’s digital signature uses the txid and the output index of the previous transaction, the previous output’s pubkey script and the new pubkey script

Note that Alice, Bob and Charlie are not people, but wallet programs used by people to send and receive funds. There are various types of wallets. You can find a list of their types and some popular ones in this post.

Some pitfalls

Other than P2PKH addresses, there are two other types of Bitcoin addresses in use, called P2SH (Pay-to-Script-Hash) and Bech32. The difference between the three is that the P2PKH addresses start with a 1, P2SH addresses start with a 3 and bech32 addresses start with “bc1”. The aboe explanation is for transactions conducted using P2PKH addresses only. Another important thing to note is that if the sum of values of input UTXOs is greater than the output UTXOs, then miners may charge fees for the transaction to occur. This is a source of income for miners and ensures that a block contains your transaction. Coinbase transactions are different from ordinary transactions, as they don’t follow a lot of rules associated with normal transactions. Moreover, coinbase transactions also have another additional limitation. The Bitcoins received from a coinbase transaction cannot be spent before 100 blocks have been added to the blockchain. This is to prevent miners from spending amounts which may have been obtained by mining stale blocks.

Enfin

Bitcoin transactions are usually of the form mentioned above, but cryptocurrencies such as Ethereum use the traditional accounts model. This means that transactions need to keep track of adding and subtracting amounts from the receivers and senders. I’ll explain this some other day, but till then stay tuned for more blockchain related posts.

References

Leave a comment