In traditional banking systems, transferring funds amounts to changing entries in a balance table: one client’s amount decreases, another’s increases. Interbank transactions sometimes involve intermediaries, such as SWIFT. However, the business logic is still based on a centralized account register.
In contrast, Bitcoin operates on a fundamentally different foundation — a decentralized chain of blocks (blockchain). It lacks a single table of balances or a central authority that manages this data. In this article, we will look at the internal structure of a transaction in the Bitcoin network, analyze how it is formed, and also explain the role of the scripting language Bitcoin Script — a little-known but key component of the system.
Transaction Basics: Inputs and Outputs
A transaction in the Bitcoin network is a structure with many components, of which the most important are inputs and outputs.
Inputs are references to the results of previous transactions that become the source of funds for a new operation. For example, if the following transactions were previously sent to address X:
- TXN_ID: 123456, amount: 40 BTC
- TXN_ID: 6453795, amount: 10 BTC
- TXN_ID: 888888, amount: 100 BTC
A user wishing to spend 45 BTC can either reference a single transaction (e.g. 888888) or combine several (123456 and 6453795) to provide the required amount.
Outputs, in turn, indicate where the currency units will be sent after the transaction is completed. A single transaction can have multiple outputs with different amounts and recipients.
It is important to note that each output can only be used as an input once and only in its entirety. If you received 10 BTC as an input and want to spend 8 BTC, you create an output for 8 BTC to the recipient and a second output for the remaining 2 BTC back to your address. The difference between the input and output amounts is the transaction fee and is transferred to the miner who first included it in the block.
Commission (Transaction Fee) and its role
The difference between the sum of inputs and the sum of outputs is called the transaction fee. This fee is the key incentive for miners to include a transaction in a block. Miners typically sort incoming unverified transactions by fee size, favoring those with higher fees to maximize their income. The higher the fee, the faster the user will see confirmation of their transaction.
UTXO: Unspent Output Model
An important technical concept in the Bitcoin ecosystem is UTXO (Unspent Transaction Output). Since each output can only be used once, the balance of an address is actually the sum of all its UTXOs. Instead of traversing the entire blockchain to calculate the balance, you can effectively rely on the UTXO pool, which significantly improves performance.
Transaction structure
A standard transaction is described in the Bitcoin protocol specification and contains several key fields:
- The hash of the referenced transaction (previous output hash), written in little endian format.
- The output index of the previous transaction.
- Various scripts: scriptSig (unlocking script) and scriptPubKey (locking script).
- The block lock time field specifies the minimum block or timestamp at which a transaction can be active.
- Sequence is a deprecated parameter.
To generate a transaction hash, the SHA-256 function is applied twice to the byte sequence of the entire transaction.
Bitcoin Script Language
Bitcoin Script is a simple, stack-based, Turing-incomplete programming language built into the network to verify spending conditions. Its design eliminates loops and complex calculations, ensuring secure execution.
Each script consists of opcodes – instructions, of which there were initially about 80. Example of an instruction: OP_DUP OP_HASH160 5 OP_EQUAL. Scripts are processed from left to right, working with the stack.
Logic of blocking/unblocking funds
There are two related scripts in the transaction:
- Locking script (scriptPubKey) — sets the conditions for spending the severance funds (analogous to a will: who and under what conditions can spend them).
- Unlocking script (scriptSig) – contains evidence of the fulfillment of conditions (for example, signatures), confirming the right of the owner of the private key to use the funds.
To check the validity of scripts, they are combined and executed as a single program: if TRUE is at the top of the stack after the calculations are completed, the transaction is considered valid.
Pseudo example of a script with multiplication
Let’s look at a simple example of blocking funds based on the product of two numbers: the locking script contains OP_MUL 370 OP_EQUAL, that is, to unblock, you need to provide two numbers whose product is 370.
In the unlocking script, we enter, for example, 10 and 37. When the script is executed, the numbers are multiplied, and if the result matches 370, the funds are spent.
Standard Pay-to-PubKey-Hash (P2PKH) script
A widely used transaction type in Bitcoin is Pay-to-PubKey-Hash (P2PKH), which ensures that only the owner of the corresponding private key can spend the funds.
Operating principle:
- The sender specifies the hash of the public key (destination address) in the locking script.
- To spend the money, the recipient places the public part of the key and a digital signature confirming ownership of the private key in the unlocking script.
- The script checks the match of hashes and signatures, thus ensuring security.
Using Blockchain to Store Arbitrary Data
Since everything that is once entered into the blockchain cannot be deleted, some users began to write additional data there, turning Bitcoin into a P2P disk with some limitations.
Thus, the line “Make America great again” can be included in the locking script. The amount is minimal (for example, 0.0000001 BTC), since the funds are effectively “locked” forever.
To solve the problem of chain “pollution”, the opcode OP_RETURN was introduced – it allows legal storage of up to 40 bytes of arbitrary information and marks the output as “provably unspendable”, excluding it from the UTXO pool and saving space.
Conclusion
Bitcoin transactions are a much more complex and multi-layered mechanism than simply moving balances between accounts. The combination of the UTXO model, cryptographic signatures, and a proprietary scripting language create a robust and flexible structure that ensures the security and decentralization of the network.
Understanding the structure of inputs, outputs, and the internal logic of scripts opens up opportunities for deep cryptanalysis and the creation of innovative applications based on blockchain technology.
Literature and Resources
- Bitcoin Script language (part 1 and 2)
- Bitcoin Standard Scripts
- Explaining transactions with OP_RETURN
- Online sandbox for working with Bitcoin Script
- NPM package bitcoin-script