This book will teach you the technology of Bitcoin at a fundamental level. This book doesn’t cover monetary, economic or social dynamics of Bitcoin, but knowing how Bitcoin works under the hood should give you greater insight into what’s possible. There’s a tendency to hype Bitcoin and blockchain without really understanding what’s going on and this book is meant to be an antidote to that tendency.
After all, there are lots of books about Bitcoin, everything from the history of Bitcoin, the economic aspects to a technical description of Bitcoin. The aim of this book is to get you to understand Bitcoin by coding all of the components necessary for a Bitcoin library. The library is not meant to be exhaustive or efficient. The aim of the library is for you to learn.
This book is for programmers who want to learn how Bitcoin works by coding it themselves. You will learn Bitcoin by coding the "bare metal" stuff in a Bitcoin library you will create from scratch. This is not a reference book where you can look up the specification for a particular feature.
The material from this book has been largely taken from my two day seminar where I teach developers all about Bitcoin (see programmingblockchain.com). The material has been refined many times as I’ve taught this material 20+ times and have taught 400+ people as of this writing.
By the time you’re done with the book, you’ll not only be able to create transactions, but be able to get all the data you need from peers and send the transaction over the network. Everything to accomplish this is covered, including the math, the parsing, the network connectivity and block validation.
The prerequisites for this book are that you know programming and that you know Python in particular. The library itself is written in Python and a lot of the exercises can be done in a controlled environment like a Jupyter notebook. An intermediate knowledge of Python is preferred, but even a beginner knowledge of Python is probably sufficient to get a lot of the concepts.
Some knowledge of math is required, especially for Chapters 1 and 2. These chapters introduce mathematical concepts probably not familiar to those that didn’t major in mathematics. A high-school level knowledge of algebra should be sufficient to be able to understand the new concepts and code the material covered there.
General CS knowledge like hash functions will come in handy, but are not strictly necessary to complete the exercises in this book.
This book is split into 14 chapters and build one on top of the other. Each chapter is meant to build on the previous one as the Bitcoin library gets built from scratch all the way to the end.
Roughly speaking, Chapters 1-4 establish the mathematical tools that we need, Chapters 5-8 cover Transactions, which are the fundamental unit of Bitcoin and Chapters 9-12 cover Blocks and Networking. The last two chapters cover some advanced topics but don’t actually require you to write code.
Chapters 1 and 2 cover some math that we need. Finite Fields and Elliptic Curves are needed in order for us to understand Elliptic Curve Cryptography in Chapter 3. After we’ve established the Public Key Cryptography at the end of Chapter 3, we move towards actually implementing how they appear to a computer through parsing and serializing all the different classes that we established in Chapter 4.
Chapter 5 covers the Transaction structure. Chapter 6 goes into the smart contract language behind Bitcoin called Script. Chapter 7 combines all the previous chapters in validating and creating transactions based on the Elliptic Curve Cryptography from the first 4 chapters. Chapter 8 establishes how p2sh works, which is a way to make more powerful smart contracts.
Chapter 9 covers Blocks, which are groups of Transactions. Chapter 10 covers how the network communication is done in Bitcoin. Chapter 11 and 12 go into how a light node, or software without access to the entire blockchain might request and broadcast data from and to nodes that do have the entire blockchain.
Chapter 13 covers Segwit and Chapter 14 covers suggestions for further study. Again, these chapters are not strictly necessary, but are included as a way to give you additional things to learn.
Chapters 1-12 have lots of exercises that require you to build up the library from scratch. You will be writing many Python classes and will be building towards being able to not just validate transactions/blocks, but also creating your own transactions and broadcasting them on the network.
The last exercise in Chapter 12 is specifically asking you to connect to another node on the testnet network, calculate what you can spend, construct and sign a transaction of your devising and broadcast that on the network. Essentially, the first 11 chapters are setting up toward being able to do this exercise.
There will be a lot of unit tests that you will have to get to pass. The book has been designed this way so you can do the "fun" part of coding. We will be looking at a lot of code and diagrams throughout.
In order to get the most out of this book, you’ll want to create an environment where you can run through the exercises. Here are the instructions for setting everything up.
Install python 3.5 or above on your machine:
-
Windows:
https://www.python.org/ftp/python/3.6.2/python-3.6.2-amd64.exe -
Mac OS X:
https://www.python.org/ftp/python/3.6.2/python-3.6.2-macosx10.6.pkg -
Linux: see your distro docs (many Linux distributions like Ubuntu come with Python 3.5+ pre-installed)
Install pip:
Download this script https://bootstrap.pypa.io/get-pip.py and run it using python3:
$ python3 get-pip.pyInstall git:
The instructions for downloading and installing are at https://git-scm.com/downloads.
Download the source code for this book:
$ git clone https://github.com/jimmysong/programming-bitcoin
$ cd programming-bitcoinInstall virtualenv:
$ pip install virtualenvInstall the requirements:
Linux/OSX:
$ virtualenv -p python3 .venv
$ . .venv/bin/activate
(.venv) $ pip install -r requirements.txtWindows:
C:\programming-bitcoin> virtualenv -p C:\\PathToYourPythonInstallation\\Python.exe .venv
C:\programming-bitcoin> .venv\Scripts\activate.bat
C:\programming-bitcoin> pip install -r requirements.txt(.venv) $ jupyter notebook
[I 11:13:23.061 NotebookApp] Serving notebooks from local directory: /home/jimmy/programming-bitcoin
[I 11:13:23.061 NotebookApp] The Jupyter Notebook is running at:
[I 11:13:23.061 NotebookApp] http://localhost:8888/?token=f849627e4d9d07d2158e3fcde93590eff4a9a7a01f65a8e7
[I 11:13:23.061 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[W 11:13:23.065 NotebookApp] No web browser found: could not locate runnable browser.
[C 11:13:23.065 NotebookApp]
Copy/paste this URL into your browser when you connect for the first time,
to login with a token:
http://localhost:8888/?token=f849627e4d9d07d2158e3fcde93590eff4a9a7a01f65a8e7You should have a browser open up automatically to something that looks like this:
You can navigate to various directories and if you wanted to do the exercises from Chapter 1, you would need to go navigate to the code from Chapter 1 in the code-ch01 directory:
From here you can open Programming Bitcoin Chapter 1.ipynb:
You may want to familiarize yourself with this interface if you haven’t seen it before, but the gist of Jupyter is that it can run Python code from the browser in a way to make experimenting easy.
A large portion of the exercises will be coding concepts introduced in the book. The unit tests are written for you and you will need to write the Python code to make the tests pass. You can edit the file by clicking through a screen like the "this test" linke in Figure xxx. This will take you to a browser tab like this:
You can edit the file here and save in order to make the test pass.
The following typographical conventions are used in this book:
- Italic
-
Indicates new terms, URLs, email addresses, filenames, and file extensions.
- Constant width
-
Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords.
Constant width bold-
Shows commands or other text that should be typed literally by the user.
- Constant width italic
-
Shows text that should be replaced with user-supplied values or by values determined by context.
|
Tip
|
This element signifies a tip or suggestion. |
|
Note
|
This element signifies a general note. |
|
Warning
|
This element indicates a waning or caution. |
Supplemental material (code examples, exercises, etc.) is available for download at https://github.com/oreillymedia/title_title.
This book is here to help you get your job done. In general, if example code is offered with this book, you may use it in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission.
We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “Book Title by Some Author (O’Reilly). Copyright 2012 Some Copyright Holder, 978-0-596-xxxx-x.”
If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at permissions@oreilly.com.
|
Note
|
Safari (formerly Safari Books Online) is a membership-based training and reference platform for enterprise, govenment, educators, and individuals. |
Members have access to thousands of books, training videos, Leaning Paths, interactive tutorials, and curated playlists from over 250 publishers, including O’Reilly Media, Harvard Business Review, Prentice Hall Professional, Addison-Wesley Professional, Microsoft Press, Sams, Que, Peachpit Press, Adobe, Focal Press, Cisco Press, John Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe Press, FT Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, and Course Technology, among others.
For more information, please visit http://oreilly.com/safari.
Please address comments and questions concening this book to the publisher:
- O’Reilly Media, Inc.
- 1005 Gravenstein Highway North
- Sebastopol, CA 95472
- 800-998-9938 (in the United States or Canada)
- 707-829-0515 (intenational or local)
- 707-829-0104 (fax)
We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at http://www.oreilly.com/catalog/<catalog page>.
To comment or ask technical questions about this book, send email to bookquestions@oreilly.com.
For more information about our books, courses, conferences, and news, see our website at http://www.oreilly.com.
Find us on Facebook: http://facebook.com/oreilly
Follow us on Twitter: http://twitter.com/oreillymedia
Watch us on YouTube: http://www.youtube.com/oreillymedia



