Every new Bitcoin transaction starts with an existing Bitcoin transaction.
For the purposes of this explanation, let’s assume that a Wallet means a single Public Private Key Pair. The Wallet will have previously scanned the blockchain and know that a particular transaction in a particular block has an output that contains an amount of Bitcoin that has been sent to the Wallet’s address (which is derived from the Wallet’s public key).
The Wallet can then being to construct a new transaction based on the output of the existing transaction.
The Wallet first creates a list of inputs. In our case, we will assume only 1 input, but there can be more than one.
This input refers to the ID of the existing transaction, and the sequence number of the output in that transaction (there can be multiple outputs in a transaction too).
The input also contains an unlocking script. This comprises a digital signature, created with the Wallet’s Private Key, and a copy of the Wallet’s Public Key (remember, the output represents Bitcoin sent to the Wallet creating the transaction).
When the transaction is later validated by a Bitcoin node, a test will be performed on the digital signature to verify if it matches a Private Key.
The Wallet now creates a list of outputs. In our case, we will assume only 1 output, but there can be more than one. In most cases, there are at least 2 outputs, where the 2nd output is created to return a balance of change to the Wallet that is sending the Bitcoin.
An output must contain the amount of Bitcoin that is being transferred ( this is the UTXO = Unspent Transaction Output )
An output will also include a locking script, which will include a hash of the Public Key of the Wallet to which the Bitcoin is being sent ( the “address” of the destination Wallet ).
Once the Wallet has completed the construction of the transaction, the transaction is passed to the nearest Bitcoin node, which forwards it to the Bitcoin node network. Each node that receives the transaction validates it.
The validation process can be summarised as follows:
- The Signature in the Input unlocking script is checked to ensure it is a valid signature
- The PublicKey in the Input unlocking script is converted into a PublicKeyHash
- This PublicKeyHash is compared to the PublicKeyHash in the Output locking script ( recipient address == Wallet address )
- The signature in the Input locking script is matched against the PublicKeyHash in the Output unlocking script to ensure both refer to the same PrivateKey
- The final output of all of the above should evaluate to TRUE at the end of execution of both scripts
Once a node validates the transaction in this way, it places it in the transaction pool. The transaction pool exists on every node, so every node that receives the transaction will have that transaction in their transaction pool.
Each mining node (which is also a standard node) will also have its own copy of the transaction pool. The miner will attempt to create a new block by bundling transactions from the transaction pool into a candidate block and attempting to solve the blockchain’s Proof of Work requirement for a new block.
6. Insertion in a new block
If the miner is successful in adding a new block to the blockchain, the transaction will be included in that block and be available for another Wallet to start the process of creating a new transaction.
A Wallet has a record of which blockchain transactions have sent it Bitcoin.
The Wallet makes a new transaction to spend these, providing proof that the previous transaction output belongs to it.
The new transaction indicates another Wallet which is entitled to the forwarded Bitcoin, and tells the destination Wallet what proof is required for it to use that output in a transaction of its own.