How Transactions Work
Inspect Raw Transaction
In this chapter we will examine the raw transaction data to understand how payment transactions work.
In particular:
How the sender construct a transaction to express how much money transfer and to whom
How the sender proves ownership of an UTXO
How the sender creates a new UTXO as payment, and make sure that only the intended receiver can cliam it
Money Transfer
Let's generate a new address to receive fund
For testing, let's send some money from our own wallet to this address:
The transaction id is returned:
Once the transaction is confirmed, we can see that the receiving address has money:
Look Up The Send Transaction
Use the transaction id to look up information about it:
Deciphering The Raw Transaction
The gettransaction
command presents the transaction data in a more human friendly form. We can see the underlying bitcoin data structure by decoding the raw data of a transaction.
This data structure is key to understanding how the bitcoin ledger works.
The hex
property returned previously is the raw transaction data. Or we could also look it up with getrawtransaction
:
Let's now decode the raw tx data:
This transaction has one vins (input value) and two vouts (output values).
Transaction Vins
When constructing a transaction, the wallet finds an UTXO that satifies the amount requested.
The input refers to an UTXO, which is the second output of the transaction 39b9...5aa6
. This particular UTXO is worth 20,000.
Since we are only sending 10 Hydras, this input is more than enough pay for the transfer itself, as well as the fees.
Transaction Vouts
The UTXO used is worth 20,000, but we actually only want to spend a smart part of it. Unlike a bank account, it is not possible to partially spend the value of the UTXO.
So what we do is that we spend 10 Hydras by sending it to the receiving address, and "spend" the rest of the amount by sending it back to ourself.
The result is that are two outputs in this transaction, creating two new UTXOs.
19989.99923200 is the change returning to sender.
10 is the amount sent to receiver.
Transaction Fee
The sum of the two outputs is less than the input.
This difference is given to the miner as the fee.
Cryptographic Proof of Ownership
By decoding the raw transaction, we can see that Vins and Vouts specifies where the money come from, and where it should go.
But how does the network know who has the authority to spend what money?
In the old financial world, the bank acts as the authority that decides whether a transaction can go through. Bitcoin, however, uses cryptogrpahy to distribute the authority to its end users.
The "authority" to spend money is like a uniquely shaped lock. If a person can present the key that has the correct shape to unlock it, the network then allows the money to be spent.
The sender must have the key to unlock the UTXOs used as inputs.
The sender create new locks as outputs, that only the receiver can unlock.
BTC Standard Pay Scripts
The cryptographic "lock" alluded to are just simple fragments of BTC scripts. The sender needs to provide the correct input, so the script evaluates to true.
Pay To Public Key
The UTXO that's used as input is the second output of the transaction 39b9bcabd6101251d4d728754a4d4b7083c00baa0614eeb2705e5179ad0d5aa6
. Let's decode that transaction:
The data for this UTXO is:
The "lock" is the script:
This script says that whoever that can produce a cryptographic signature attributable to the public key "03795cd550a584caf7818cbe2d71cbc05bbf59fdb6bacb876969b319c4d07a0211" may spend this output.
To unlock the script, the sender creates a cryptographic signature that signs the outputs (i.e. how much money and to whom).
The script is the lock, and signature is the key. Combine the two, we get a script that evaluates to "true":
The OP_CHECKSIG operation does two things:
It verifies that the signature is attributable to the public key
03795cd550a584caf7818cbe2d71cbc05bbf59fdb6bacb876969b319c4d07a0211
Verifies the transaction outputs against the signature. If a miner tries to tamper with the outputs in anyway (adjusting the amount of recipient), the check would fail.
A Pay-To-Public-Key UTXOs are typically created as mining/staking rewards and gas refunds. For payment to addresses, another type of script is more frequently seen.
Pay To Public Hash
When sending money to an address, the output created is usually a "Pay To Public Hash" (P2PKH) script.
In the raw transaction above we see that an output has a script like this:
This script is essentially saying "whoever that can create a signature attributable to a public key that hashes to 35dbe99b7be1e43463e0fec2d431b67bcc6ef67e can spend this output".
This script is the same as Pay To Public Key
, except for the extra indirection introduced by the public key hashing.
See pay to public hash validation for details on how this type of script can be validated.
Last updated