Mastering Bitcoin
Mastering Bitcoin
Mastering Bitcoin
“When
Mastering Bitcoin
Want to join the technological revolution that’s taking the world of finance I talk about
by storm? Mastering Bitcoin is your guide through the seemingly complex
Bitcoin, I am sometimes
world of bitcoin, providing the requisite knowledge to help you participate in
the internet of money. Whether you’re building the next killer app, investing asked 'but how does
in a startup, or simply curious about the technology, this practical book is it really work?' Now I
essential reading. have a great answer
Bitcoin, the first successful decentralized digital currency, is still in its infancy for that question,
and it’s already spawned a multi-billion dollar global economy. This economy
Mastering
because anybody
is open to anyone with the knowledge and passion to participate. Mastering
Bitcoin provides you with the knowledge you need (passion not included). who reads Mastering
Bitcoin will have a deep
Bitcoin
This book includes:
understanding of how
■■ A broad introduction to bitcoin—ideal for non-tech users, it works and will be
investors, and business executives well-equipped to write
■■ Technical foundations of bitcoin and cryptographic currencies the next generation of
for developers, engineers, and software and systems architects
amazing cryptocurrency
■■ Details of the bitcoin decentralized network, peer-to-peer
architecture, transaction lifecycle, and security principles
applications. ”
—Gavin Andresen
■■ Offshoots of the bitcoin and blockchain inventions, including Chief Scientist, Bitcoin Foundation
alternative chains, currencies, and applications
■■ User stories, elegant analogies, examples, and code snippets
illustrating key technical concepts
“Andreas' book will help
you join the software UNLOCKING DIGITAL CRYPTOCURRENCIES
revolution in the world of
Andreas M. Antonopoulos is a noted technologist and serial entrepreneur who
has become one of the most well-known and well-respected figures in bitcoin.
finance. ” —Naval Ravikant
An engaging public speaker, teacher, and writer, Andreas makes complex subjects cofounder, AngelList
accessible and easy to understand. Andreas advises multiple technology startups
and speaks regularly at conferences and community events around the world.
Antonopoulos
E-COMMERCE
Twitter: @oreillymedia
facebook.com/oreilly
US $34.99 CAN $36.99
ISBN: 978-1-449-37404-4
Andreas M. Antonopoulos
Mastering Bitcoin
“When
Mastering Bitcoin
Want to join the technological revolution that’s taking the world of finance I talk about
by storm? Mastering Bitcoin is your guide through the seemingly complex
Bitcoin, I am sometimes
world of bitcoin, providing the requisite knowledge to help you participate in
the internet of money. Whether you’re building the next killer app, investing asked 'but how does
in a startup, or simply curious about the technology, this practical book is it really work?' Now I
essential reading. have a great answer
Bitcoin, the first successful decentralized digital currency, is still in its infancy for that question,
and it’s already spawned a multi-billion dollar global economy. This economy
Mastering
because anybody
is open to anyone with the knowledge and passion to participate. Mastering
Bitcoin provides you with the knowledge you need (passion not included). who reads Mastering
Bitcoin will have a deep
Bitcoin
This book includes:
understanding of how
■■ A broad introduction to bitcoin—ideal for non-tech users, it works and will be
investors, and business executives well-equipped to write
■■ Technical foundations of bitcoin and cryptographic currencies the next generation of
for developers, engineers, and software and systems architects
amazing cryptocurrency
■■ Details of the bitcoin decentralized network, peer-to-peer
architecture, transaction lifecycle, and security principles
applications. ”
—Gavin Andresen
■■ Offshoots of the bitcoin and blockchain inventions, including Chief Scientist, Bitcoin Foundation
alternative chains, currencies, and applications
■■ User stories, elegant analogies, examples, and code snippets
illustrating key technical concepts
“Andreas' book will help
you join the software UNLOCKING DIGITAL CRYPTOCURRENCIES
revolution in the world of
Andreas M. Antonopoulos is a noted technologist and serial entrepreneur who
has become one of the most well-known and well-respected figures in bitcoin.
finance. ” —Naval Ravikant
An engaging public speaker, teacher, and writer, Andreas makes complex subjects cofounder, AngelList
accessible and easy to understand. Andreas advises multiple technology startups
and speaks regularly at conferences and community events around the world.
Antonopoulos
E-COMMERCE
Twitter: @oreillymedia
facebook.com/oreilly
US $34.99 CAN $36.99
ISBN: 978-1-449-37404-4
Andreas M. Antonopoulos
O’Reilly Ebooks—Your bookshelf on your devices!
When you buy an ebook through oreilly.com you get lifetime access to the book, and
whenever possible we provide it to you in five, DRM-free file formats—PDF, .epub,
Kindle-compatible .mobi, Android .apk, and DAISY—that you can use on the devices of
your choice. Our ebook files are fully searchable, and you can cut-and-paste and print
them. We also alert you when we’ve updated the files with corrections and additions.
The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Mastering Bitcoin, the cover image, and
related trade dress are trademarks of O’Reilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a trademark
claim, the designations have been printed in caps or initial caps.
While the publisher and the author have used good faith efforts to ensure that the information and instruc‐
tions contained in this work are accurate, the publisher and the author disclaim all responsibility for errors
or omissions, including without limitation responsibility for damages resulting from the use of or reliance
on this work. Use of the information and instructions contained in this work is at your own risk. If any code
samples or other technology this work contains or describes is subject to open source licenses or the intel‐
lectual property rights of others, it is your responsibility to ensure that your use thereof complies with such
licenses and/or rights.
ISBN: 978-1-449-37404-4
[LSI]
Table of Contents
Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi
Quick Glossary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
1. Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
What Is Bitcoin? 1
History of Bitcoin 3
Bitcoin Uses, Users, and Their Stories 4
Getting Started 6
Quick Start 7
Getting Your First Bitcoins 9
Sending and Receiving Bitcoins 10
v
Compiling Bitcoin Core from the Source Code 33
Using Bitcoin Core’s JSON-RPC API from the Command Line 38
Getting Information on the Bitcoin Core Client Status 40
Wallet Setup and Encryption 41
Wallet Backup, Plain-text Dump, and Restore 42
Wallet Addresses and Receiving Transactions 42
Exploring and Decoding Transactions 44
Exploring Blocks 48
Creating, Signing, and Submitting Transactions Based on Unspent Outputs 49
Alternative Clients, Libraries, and Toolkits 56
Libbitcoin and sx Tools 56
pycoin 57
btcd 58
5. Transactions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Introduction 109
Transaction Lifecycle 109
Creating Transactions 110
Broadcasting Transactions to the Bitcoin Network 110
vi | Table of Contents
Propagating Transactions on the Bitcoin Network 111
Transaction Structure 111
Transaction Outputs and Inputs 112
Transaction Outputs 113
Transaction Inputs 115
Transaction Fees 118
Adding Fees to Transactions 119
Transaction Chaining and Orphan Transactions 120
Transaction Scripts and Script Language 121
Script Construction (Lock + Unlock) 122
Scripting Language 123
Turing Incompleteness 126
Stateless Verification 126
Standard Transactions 126
Pay-to-Public-Key-Hash (P2PKH) 127
Pay-to-Public-Key 128
Multi-Signature 129
Data Output (OP_RETURN) 130
Pay-to-Script-Hash (P2SH) 132
Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
Table of Contents | ix
CHAPTER 1
Introduction
What Is Bitcoin?
Bitcoin is a collection of concepts and technologies that form the basis of a digital money
ecosystem. Units of currency called bitcoins are used to store and transmit value among
participants in the bitcoin network. Bitcoin users communicate with each other using
the bitcoin protocol primarily via the Internet, although other transport networks can
also be used. The bitcoin protocol stack, available as open source software, can be run
on a wide range of computing devices, including laptops and smartphones, making the
technology easily accessible.
Users can transfer bitcoins over the network to do just about anything that can be done
with conventional currencies, including buy and sell goods, send money to people or
organizations, or extend credit. Bitcoins can be purchased, sold, and exchanged for
other currencies at specialized currency exchanges. Bitcoin in a sense is the perfect form
of money for the Internet because it is fast, secure, and borderless.
Unlike traditional currencies, bitcoins are entirely virtual. There are no physical coins
or even digital coins per se. The coins are implied in transactions that transfer value
from sender to recipient. Users of bitcoin own keys that allow them to prove ownership
of transactions in the bitcoin network, unlocking the value to spend it and transfer it to
a new recipient. Those keys are often stored in a digital wallet on each user’s computer.
Possession of the key that unlocks a transaction is the only prerequisite to spending
bitcoins, putting the control entirely in the hands of each user.
Bitcoin is a distributed, peer-to-peer system. As such there is no “central” server or point
of control. Bitcoins are created through a process called “mining,” which involves com‐
peting to find solutions to a mathematical problem while processing bitcoin transac‐
tions. Any participant in the bitcoin network (i.e., anyone using a device running the
full bitcoin protocol stack) may operate as a miner, using their computer’s processing
power to verify and record transactions. Every 10 minutes on average, someone is able
1
to validate the transactions of the past 10 minutes and is rewarded with brand new
bitcoins. Essentially, bitcoin mining decentralizes the currency-issuance and clearing
functions of a central bank and replaces the need for any central bank with this global
competition.
The bitcoin protocol includes built-in algorithms that regulate the mining function
across the network. The difficulty of the processing task that miners must perform—to
successfully record a block of transactions for the bitcoin network—is adjusted dy‐
namically so that, on average, someone succeeds every 10 minutes regardless of how
many miners (and CPUs) are working on the task at any moment. The protocol also
halves the rate at which new bitcoins are created every four years, and limits the total
number of bitcoins that will be created to a fixed total of 21 million coins. The result is
that the number of bitcoins in circulation closely follows an easily predictable curve that
reaches 21 million by the year 2140. Due to bitcoin’s diminishing rate of issuance, over
the long term, the bitcoin currency is deflationary. Furthermore, bitcoin cannot be in‐
flated by “printing” new money above and beyond the expected issuance rate.
Behind the scenes, bitcoin is also the name of the protocol, a network, and a distributed
computing innovation. The bitcoin currency is really only the first application of this
invention. As a developer, I see bitcoin as akin to the Internet of money, a network for
propagating value and securing the ownership of digital assets via distributed compu‐
tation. There’s a lot more to bitcoin than first meets the eye.
In this chapter we’ll get started by explaining some of the main concepts and terms,
getting the necessary software, and using bitcoin for simple transactions. In following
chapters we’ll start unwrapping the layers of technology that make bitcoin possible and
examine the inner workings of the bitcoin network and protocol.
Issuers of paper money are constantly battling the counterfeiting problem by using
increasingly sophisticated papers and printing technology. Physical money addresses
the double-spend issue easily because the same paper note cannot be in two places at
once. Of course, conventional money is also often stored and transmitted digitally. In
these cases, the counterfeiting and double-spend issues are handled by clearing all elec‐
2 | Chapter 1: Introduction
tronic transactions through central authorities that have a global view of the currency
in circulation. For digital money, which cannot take advantage of esoteric inks or holo‐
graphic strips, cryptography provides the basis for trusting the legitimacy of a user’s
claim to value. Specifically, cryptographic digital signatures enable a user to sign a digital
asset or transaction proving the ownership of that asset. With the appropriate archi‐
tecture, digital signatures also can be used to address the double-spend issue.
When cryptography started becoming more broadly available and understood in the
late 1980s, many researchers began trying to use cryptography to build digital curren‐
cies. These early digital currency projects issued digital money, usually backed by a
national currency or precious metal such as gold.
Although these earlier digital currencies worked, they were centralized and, as a result,
they were easy to attack by governments and hackers. Early digital currencies used a
central clearinghouse to settle all transactions at regular intervals, just like a traditional
banking system. Unfortunately, in most cases these nascent digital currencies were tar‐
geted by worried governments and eventually litigated out of existence. Some failed in
spectacular crashes when the parent company liquidated abruptly. To be robust against
intervention by antagonists, whether legitimate governments or criminal elements, a
decentralized digital currency was needed to avoid a single point of attack. Bitcoin is
such a system, completely decentralized by design, and free of any central authority or
point of control that can be attacked or corrupted.
Bitcoin represents the culmination of decades of research in cryptography and dis‐
tributed systems and includes four key innovations brought together in a unique and
powerful combination. Bitcoin consists of:
History of Bitcoin
Bitcoin was invented in 2008 with the publication of a paper titled “Bitcoin: A Peer-to-
Peer Electronic Cash System,” written under the alias of Satoshi Nakamoto. Nakamoto
combined several prior inventions such as b-money and HashCash to create a com‐
pletely decentralized electronic cash system that does not rely on a central authority for
currency issuance or settlement and validation of transactions. The key innovation was
to use a distributed computation system (called a “proof-of-work” algorithm) to conduct
a global “election” every 10 minutes, allowing the decentralized network to arrive at
consensus about the state of transactions. This elegantly solves the issue of double-spend
History of Bitcoin | 3
where a single currency unit can be spent twice. Previously, the double-spend problem
was a weakness of digital currency and was addressed by clearing all transactions
through a central clearinghouse.
The bitcoin network started in 2009, based on a reference implementation published
by Nakamoto and since revised by many other programmers. The distributed compu‐
tation that provides security and resilience for bitcoin has increased exponentially, and
now exceeds that combined processing capacity of the world’s top super-computers.
Bitcoin’s total market value is estimated at between 5 billion and 10 billion US dollars,
depending on the bitcoin-to-dollar exchange rate. The largest transaction processed so
far by the network was 150 million US dollars, transmitted instantly and processed
without any fees.
Satoshi Nakamoto withdrew from the public in April of 2011, leaving the responsibility
of developing the code and network to a thriving group of volunteers. The identity of
the person or people behind bitcoin is still unknown. However, neither Satoshi Naka‐
moto nor anyone else exerts control over the bitcoin system, which operates based on
fully transparent mathematical principles. The invention itself is groundbreaking and
has already spawned new science in the fields of distributed computing, economics, and
econometrics.
4 | Chapter 1: Introduction
North American low-value retail
Alice lives in Northern California’s Bay Area. She has heard about bitcoin from her
techie friends and wants to start using it. We will follow her story as she learns about
bitcoin, acquires some, and then spends some of her bitcoin to buy a cup of coffee
at Bob’s Cafe in Palo Alto. This story will introduce us to the software, the exchanges,
and basic transactions from the perspective of a retail consumer.
North American high-value retail
Carol is an art gallery owner in San Francisco. She sells expensive paintings for
bitcoin. This story will introduce the risks of a “51%” consensus attack for retailers
of high-value items.
Offshore contract services
Bob, the cafe owner in Palo Alto, is building a new website. He has contracted with
an Indian web developer, Gopesh, who lives in Bangalore, India. Gopesh has agreed
to be paid in bitcoin. This story will examine the use of bitcoin for outsourcing,
contract services, and international wire transfers.
Charitable donations
Eugenia is the director of a children’s charity in the Philippines. Recently she has
discovered bitcoin and wants to use it to reach a whole new group of foreign and
domestic donors to fundraise for her charity. She’s also investigating ways to use
bitcoin to distribute funds quickly to areas of need. This story will show the use of
bitcoin for global fundraising across currencies and borders and the use of an open
ledger for transparency in charitable organizations.
Import/export
Mohammed is an electronics importer in Dubai. He’s trying to use bitcoin to buy
electronics from the US and China for import into the UAE to accelerate the process
of payments for imports. This story will show how bitcoin can be used for large
business-to-business international payments tied to physical goods.
Mining for bitcoin
Jing is a computer engineering student in Shanghai. He has built a “mining” rig to
mine for bitcoins, using his engineering skills to supplement his income. This story
will examine the “industrial” base of bitcoin: the specialized equipment used to
secure the bitcoin network and issue new currency.
Each of these stories is based on real people and real industries that are currently using
bitcoin to create new markets, new industries, and innovative solutions to global eco‐
nomic issues.
Mobile Bitcoin
Mobile clients for smartphones, such as those based on the Android system, can either
operate as full clients, lightweight clients, or web clients. Some mobile clients are
synchronized with a web or desktop client, providing a multiplatform wallet across
multiple devices but with a common source of funds.
The choice of bitcoin client depends on how much control the user wants over funds.
A full client will offer the highest level of control and independence for the user, but in
turn puts the burden of backups and security on the user. On the other end of the range
of choices, a web client is the easiest to set up and use, but the trade-off with a web client
is that counterparty risk is introduced because security and control is shared with the
user and the owner of the web service. If a web-wallet service is compromised, as many
6 | Chapter 1: Introduction
have been, the users can lose all their funds. Conversely, if users have a full client without
adequate backups, they might lose their funds through a computer mishap.
For the purposes of this book, we will be demonstrating the use of a variety of down‐
loadable bitcoin clients, from the reference implementation (the Satoshi client) to web
wallets. Some of the examples will require the use of the reference client, which, in
addition to being a full client, also exposes APIs to the wallet, network, and transaction
services. If you are planning to explore the programmatic interfaces into the bitcoin
system, you will need the reference client.
Quick Start
Alice, who we introduced in “Bitcoin Uses, Users, and Their Stories” on page 4, is not
a technical user and only recently heard about bitcoin from a friend. She starts her
journey by visiting the official website bitcoin.org, where she finds a broad selection of
bitcoin clients. Following the advice on the bitcoin.org site, she chooses the lightweight
bitcoin client Multibit.
Alice follows a link from the bitcoin.org site to download and install Multibit on her
desktop. Multibit is available for Windows, Mac OS, and Linux desktops.
Once Alice has downloaded and installed the Multibit application, she runs it and is
greeted by a Welcome screen, as shown in Figure 1-1.
Getting Started | 7
Figure 1-1. The Multibit bitcoin client Welcome screen
Multibit automatically creates a wallet and a new bitcoin address for Alice, which Alice
can see by clicking the Request tab shown in Figure 1-2.
Figure 1-2. Alice’s new bitcoin address, in the Request tab of the Multibit client
The most important part of this screen is Alice’s bitcoin address. Like an email address,
Alice can share this address and anyone can use it to send money directly to her new
wallet. On the screen it appears as a long string of letters and numbers:
1Cdid9KFAaatwczBwBttQcwXYCpvK8h7FK. Next to the wallet’s bitcoin address is a QR
8 | Chapter 1: Introduction
code, a form of barcode that contains the same information in a format that can be
scanned by a smartphone camera. The QR code is the black-and-white square on the
right side of the window. Alice can copy the bitcoin address or the QR code onto her
clipboard by clicking the copy button adjacent to each of them. Clicking the QR code
itself will magnify it, so that it can be easily scanned by a smartphone camera.
Alice can also print the QR code as a way to easily give her address to others without
them having to type the long string of letters and numbers.
Bitcoin addresses start with the digit 1 or 3. Like email addresses, they
can be shared with other bitcoin users who can use them to send
bitcoin directly to your wallet. Unlike email addresses, you can cre‐
ate new addresses as often as you like, all of which will direct funds
to your wallet. A wallet is simply a collection of addresses and the keys
that unlock the funds within. You can increase your privacy by us‐
ing a different address for every transaction. There is practically no
limit to the number of addresses a user can create.
Getting Started | 9
exchange, you can then buy or sell bitcoins quickly just as you could with foreign cur‐
rency with a brokerage account.
You can find a more complete list at bitcoin charts, a site that offers price quotes and
other market data across many dozens of currency exchanges.
There are four other methods for getting bitcoins as a new user:
• Find a friend who has bitcoins and buy some from him directly. Many bitcoin users
start this way.
• Use a classified service such as localbitcoins.com to find a seller in your area to buy
bitcoins for cash in an in-person transaction.
• Sell a product or service for bitcoin. If you’re a programmer, sell your programming
skills.
• Use a bitcoin ATM in your city. Find a bitcoin ATM close to you using an online
map from CoinDesk.
Alice was introduced to bitcoin by a friend and so she has an easy way of getting her
first bitcoins while she waits for her account on a California currency market to be
verified and activated.
10 | Chapter 1: Introduction
Next, Joe has to figure out the exchange rate so that he can give the correct amount of
bitcoin to Alice. There are hundreds of applications and websites that can provide the
current market rate. Here are some of the most popular:
Bitcoin Charts
A market data listing service that shows the market rate of bitcoin across many
exchanges around the globe, denominated in different local currencies
Bitcoin Average
A site that provides a simple view of the volume-weighted-average for each currency
ZeroBlock
A free Android and iOS application that can display a bitcoin price from different
exchanges (see Figure 1-3)
Bitcoin Wisdom
Another market data listing service
Figure 1-3. ZeroBlock, a bitcoin market-rate application for Android and iOS
Using one of the applications or websites just listed, Joe determines the price of bitcoin
to be approximately 100 US dollars per bitcoin. At that rate he should give Alice 0.10
bitcoin, also known as 100 millibits, in return for the 10 US dollars she gave him.
Once Joe has established a fair exchange price, he opens his mobile wallet application
and selects to “send” bitcoin. For example, if using the Blockchain mobile wallet on an
Android phone, he would see a screen requesting two inputs, as shown in Figure 1-4.
In the input field for the bitcoin address, there is a small icon that looks like a QR code.
This allows Joe to scan the barcode with his smartphone camera so that he doesn’t have
to type in Alice’s bitcoin address (1Cdid9KFAaatwczBwBttQcwXYCpvK8h7FK), which is
quite long and difficult to type. Joe taps the QR code icon and activates the smartphone
camera, scanning the QR code from Alice’s printed wallet that she brought with her.
The mobile wallet application fills in the bitcoin address and Joe can check that it scan‐
Getting Started | 11
ned correctly by comparing a few digits from the address with the address printed by
Alice.
Joe then enters the bitcoin value for the transaction, 0.10 bitcoin. He carefully checks
to make sure he has entered the correct amount, because he is about to transmit money
and any mistake could be costly. Finally, he presses Send to transmit the transaction.
Joe’s mobile bitcoin wallet constructs a transaction that assigns 0.10 bitcoin to the ad‐
dress provided by Alice, sourcing the funds from Joe’s wallet and signing the transaction
with Joe’s private keys. This tells the bitcoin network that Joe has authorized a transfer
of value from one of his addresses to Alice’s new address. As the transaction is trans‐
mitted via the peer-to-peer protocol, it quickly propagates across the bitcoin network.
In less than a second, most of the well-connected nodes in the network receive the
transaction and see Alice’s address for the first time.
If Alice has a smartphone or laptop with her, she will also be able to see the transaction.
The bitcoin ledger—a constantly growing file that records every bitcoin transaction that
has ever occurred—is public, meaning that all she has to do is look up her own address
and see if any funds have been sent to it. She can do this quite easily at the blockchain.info
website by entering her address in the search box. The website will show her a page
listing all the transactions to and from that address. If Alice is watching that page, it will
update to show a new transaction transferring 0.10 bitcoin to her balance soon after Joe
hits Send.
12 | Chapter 1: Introduction
Confirmations
At first, Alice’s address will show the transaction from Joe as “Unconfirmed.” This means
that the transaction has been propagated to the network but has not yet been included
in the bitcoin transaction ledger, known as the blockchain. To be included, the trans‐
action must be “picked up” by a miner and included in a block of transactions. Once a
new block is created, in approximately 10 minutes, the transactions within the block
will be accepted as “confirmed” by the network and can be spent. The transaction is seen
by all instantly, but it is only “trusted” by all when it is included in a newly mined block.
Alice is now the proud owner of 0.10 bitcoin that she can spend. In the next chapter we
will look at her first purchase with bitcoin, and examine the underlying transaction and
propagation technologies in more detail.
Getting Started | 13
Want to read more?
You can buy this book at oreilly.com
in print and ebook format.
Buy 2 books, get the 3rd FREE!
Use discount code: OPC10
All orders over $29.95 qualify for free shipping within the US.