Search Inside Bitcoins

How Do Bitcoin Transactions Actually Work?

Don’t invest unless prepared to lose all the money you invest. This is a high-risk investment, you shouldn’t expect to be protected if something goes wrong.

Join Our Telegram channel to stay up to date on breaking news coverage

Whether you’re interested in becoming a developer for blockchain applications, or you’re just looking to understand what happens under the hood when you send bitcoin to a friend, it’s good to have a working knowledge of what happens when you create and broadcast Bitcoin transactions to the Bitcoin network. Why?

Because transactions are a basic entity on top of which the bitcoin blockchain is constructed. Transactions are the result of a brilliant collision of cryptography, data structures, and simple non-turing-complete scripting. They’re simple enough that common transaction types aren’t overly-complex, but flexible enough to allow developers to encode fairly customized transactions types as well. Today we’ll take a tour of the former.

As a developer, how does your bitcoin client post a new transaction to the network (and what happens when it’s received)?

What exactly is happening when you send some bitcoin to a friend?

This post will assume that the reader has a basic understanding of hashing, asymmetric cryptography, and P2P networking. It’s also a good idea to have a good sense for what exactly a blockchain is, even if you’re unfamiliar with any specific mechanics.

 

Bitcoin Transactions and their role in the bigger picture

 

Bitcoin is comprised of a few major pieces: nodes and a blockchain. The role of a typical node is to maintain its own blockchain version and update it once it hears of a “better” (longer) version. Simply put, the blockchain has blocks, and blocks have transactions.

With this simplified but accurate picture in mind, you might be wondering what exactly a transaction is made out of.

How will understanding transactions help me to become a better blockchain developer? How do transactions allow me to transfer some bitcoin to a friend?

 

It turns out that the answers to these questions vary based on many things. Even assuming that we’re talking only bitcoin, we can use transactions in a number of creative ways to accomplish a variety of personalized goals. Let’s start at the beginning, that is, let’s take a look a good old-fashioned pay-to-PK-hash transaction type. After all, this type of transaction accounts for over 99% of all transactions on the bitcoin blockchain.

 

First, let’s build a mental model. It’s tempting to think of bitcoin as an account-based system. After all, when I send bitcoin to somebody, that person receives money and I’m left with a remaining balance. In the real world though, things are represented a bit differently. Generally speaking, when I send money to somebody I am sending spending all of that money (minus transaction fees). Some of that money will be spent back to my own personal account if there exists a remaining balance. The point is that all of the money moves every single time. You can skip to section 3.1 of for an explanation of why this model is preferable.

 

With that in mind, we can generalize and say that a bitcoin transaction has some inputs and outputs. A graphical representation might look something like this:

 

 

This was somewhat confusing to me when I first saw it, so I’ll elaborate a bit. When I post a transaction, I’m essentially “claiming” an output and proving that I have permission to spend the amount of money at that output. So if I’m Bob and I want to pay Alice, those inputs are my proof that I have been given a certain amount of money (although this might just be a portion of my total balance), and the outputs will correspond to Alice’s account. In this simple case, there would be only a

Join Our Telegram channel to stay up to date on breaking news coverage

Read next