Transactions

What is a bitcoin transaction?

A bitcoin transaction is just a bunch of data.

It contains information about the amount being sent, the account it is being sent from, and the account it is being sent to.

Diagram showing a transaction as moving an amount of bitcoins from one address to another.

This is just information, so it can be easily represented in a single line of data:

Diagram showing a transaction as a line of data.

And when you "make a transaction", you just send this transaction data in to the bitcoin network.

Diagram showing a transaction being sent in to the bitcoin network.

Eventually, one of the nodes on the network will mine your transaction in to a block, and this block (with your transaction in it) will be added to the permanent file of transactions (called the blockchain).

Diagram showing a transaction being mined in to a block on the blockchain.

And that's all a bitcoin transaction is – a simple line of data that gets sent in to the bitcoin network so that it can get mined on to the blockchain.

How does a bitcoin transaction work?

A bitcoin address is like an account number that holds bitcoins.

However, when you make a transaction, it's not like taking an exact amount of coins out of a pot and moving them in to another.

Diagram showing how a transaction doesn't move an exact amount of coins from one pot (address) to another.

Instead, an address keeps track of each individual payment it has received:

Diagram showing how an address holding individual payment amounts (outputs).

So when you want to send bitcoins to someone else, you grab whole amounts that you have already received, and use them to send a new amount to a new address:

Diagram showing how a transaction spends outputs from one address and sends new outputs to a different address.

And when that someone else wants to send bitcoins to another person, they will use up the whole amounts they have received in the same way:

Diagram showing a further transaction spending outputs and sending them to another address.

So in effect you receive bitcoins in batches, and you use those batches to create new batches to send to other people.

That's how transactions work.

What if the batches add up to more than the amount I want to send?

Good question Sir/Madam.

In this instance (which it often is), you just add another output to the transaction and send the difference back to yourself:

Diagram showing a change output in a transaction.

This may seem awkward at first, I know, but it's a precise way of doing it from a programming perspective.

Summary

  1. Your wallet gives you a bitcoin address. Bitcoins arrive at this address in batches, called outputs.
  2. A bitcoin transaction is the process of using these outputs (as inputs) to create new outputs that belong to someone else's address.
  3. All of this can be represented by a single line of data.
Diagram showing a complete bitcoin transaction represented as a single line of data.

For more details on how this system of outputs works, check out outputs.

What prevents other people from spending my bitcoins?

Or in other words…

Question: "If making a transaction is simply a case of feeding a line of data in to the bitcoin network, why can't someone construct a transaction that includes my address and uses it to send bitcoins to their address?"

Answer: Because each transaction output has a lock on it:

Diagram showing a lock on top of a transaction output.

And if you create a transaction without unlocking these outputs, nodes on the bitcoin network will reject the transaction:

Diagram showing a node rejecting a transaction where the inputs have not been unlocked.

But fortunately for you, your each address comes with a unique private key:

Diagram showing an address with a corresponding private key.

So if you want to send bitcoins in a transaction, you use this private key to create a one-time signature that can unlock the outputs located at your address.

Diagram showing a private key being used to unlock outputs that have been locked to an address.

After unlocking all of the outputs you want to use, the transaction will be accepted by nodes and propagated across the Bitcoin network.

Diagram showing a transaction with unlocked inputs being accepted by a node and propagated across the network.

And that's how bitcoin transactions work.