Understanding Bitcoin Transactions

Seven months ago I joined BTCJam as Lead Backend Engineer, and I must confess at the time my knowledge about bitcoin was very limited. All I knew was that it was a digital money people use to buy funny stuff, right?

For the past few months I have been reading and learning a lot, and although I am no expert yet, many people including my friends consider me to be a “bitcoin master.” Some of the question they always ask me are, “‘what are all that bitcoin transactions we see on blockchain.info?’ ‘Why do I see a lot of addresses on the left side (inputs) and several others on the right side (outputs)?’ ‘Why is there a seemingly random transfer to a weird address along with the amount that I actually transferred?’ and  ‘What about the transactions where I see hundreds of addresses on the right side?'”

If you ever thought of these things and you are not quite sure what is happening, then you are in the right place! In this blogpost I will try to explain as smoothly as possible what my “google-fu” has enabled me to learn about the magical world of bitcoin transactions.

To begin, let’s say you are selling products using bitcoins. You give your receiving address (address-A1) to the buyer, and he sends you 0.1BTC for a funny hat he just bought. Later, he wants a funny dinosaur costume and sends you 0.2BTC for it. Then another customer buys a water gun, but since you want to keep track of who paid what, you create another address in your wallet (address-A2) and your customer sends 0.4BTC to this address.

By the end of the day, your wallet has a balance of 0.7BTC. That actually means you have three “unspent outputs” in your wallet: 0.1 + 0.2 BTC in address-A1 + 0.4 BTC in address-A2. Although all the bitcoins are yours, they are associated with different addresses.

w1

So what’s this “unspent output” you are seeing? It means from the three transactions that were made, your addresses were the output addresses of the transactions and you have not used those bitcoins (or outputs) in other transactions.
Now let’s see what happens when you decide to transfer 0.45 BTC to your friend’s address-F1 :

t001

Whoa! What was that? Why are there two inputs and two outputs in this transaction?
You always spend the “unspent outputs” – you can not simply subtract 0.45 from your wallet because the bitcoins are tied to the address they were sent to. Futhermore, you always spend the outputs as a whole – you can not break an unspent output into smaller pieces. What actually happens is that you send two of your unspent outputs (0.4 BTC that was in address-A2 and 0.1 BTC from address-A1) and then get back 0.05 BTC in form of “change” in a new automatically generated address (address-Ach). The outputs are, therefore, 0.45 to your friend’s address (address-F1) and 0.05 to yourself in the address-Ach.

Your balance is now 0.25 BTC (or you have a sum of 0.25 unspent output):

w02

The thing is that most bitcoin clients, like bitcoin-qt, do not show the change addresses in their standard interface, which is somewhat misleading to the user. Trust me! They are there.

Sometimes you can get a nice, round one-address-to-one transaction if the amount you are sending happens to match any of your unspent outputs. In this case, if you happened to send 0.2 or 0.05 BTC to another address, you would spend a whole and single “unspent output” and no change would be needed.

This is what your transaction would look like:

t002

Some of my friends often confused the “unspent outputs” with “unconfirmed transactions.” Remember that the unspent output is just BTC that is still sitting in one of you bitcoin addresses waiting to be used in the form of input in another transaction, while unconfirmed transactions are transactions that are not yet confirmed by the bitcoin network. On BTCJam, we use a minimum of 3 confirmations to make your bitcoins available for investment; this avoids exploits like transaction malleability.

Now, sometimes you are going to see things like this:

trans
You might ask the same question I asked when I first saw it:

“Oh my! what is that!?”

It is now clear why there can only be a few inputs for a transaction: those addresses just happened to hold a sum of unspent outputs that are now inputs . But what about hundreds of outputs?

What happens is that transaction fees are incurred in the transaction. Let’s use a mining pool as an example that makes daily payments to its members and they can not afford to make one transaction to each member every day. You would spend a lot of bitcoin in transactions fees. In order to avoid this, they make use of a not very well know feature of the bitcoin protocol, which is sending different amounts of bitcoins to different addresses in a single transaction. With this you can minimize the transaction fees – and that is the reason you see sometimes those transactions with lots and lots of output addresses (remember that some of them might also be “change” addresses).

Some of you might ask “I got the whole ‘change address’ thing… but why? Why can’t they just send the change back to the input address? Wouldn’t that make this simpler to understand?”
Well… yes, it would. And that’s the point – Maybe you don’t want it to be simple. Look at this transaction.

Can you tell if the sender is spending 1.32492124 BTC and receiving 0.001 BTC back or if he is spending 0.001 BTC and receiving 1.32492124 BTC back as change? You can’t! And you can’t tell which address is the receiving address and which is the change address, improving, thus, one of the main bitcoin features: anonymity. You cannot just dig through all bitcoin transactions of address “abcdab” and see how much it has spent and how much the users balance is; after a few bitcoin transactions the balance is dispersed between several receive addresses and change addresses.

Well, that’s all folks. I hope I have helped some of you figure out this often confusing aspect of bitcoin. If you have more questions or comments, please don’t hesitate to write in our comments section! I’ll do my best to answer your questions right away – or figure out the answer ASAP!

Demiam

One thought on “Understanding Bitcoin Transactions

Comments are closed.