Skip to content

Latest commit

 

History

History
291 lines (199 loc) · 14.7 KB

preface.asciidoc

File metadata and controls

291 lines (199 loc) · 14.7 KB

Preface

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 to help you learn.

Who is this book for?

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.

What do I need to know?

The prerequisites for this book are that you know programming and that you know Python in particular. The library itself is written in Python 3 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 #chapter_finite_fields and #chapter_elliptic_curves. These chapters introduce mathematical concepts probably not familiar to those that didn’t major in mathematics. Math knowledge around algebra-level should suffice to understand the new concepts and to code the exercises covered in those chapters.

General Computer Science knowledge like hash functions will come in handy, but are not strictly necessary to complete the exercises in this book.

How is the book arranged?

This book is split into 14 chapters and built on top of one another. 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 #chapter_finite_fields-#chapter_serialization establish the mathematical tools that we need, Chapters #chapter_tx_parsing-#chapter_p2sh cover Transactions, which are the fundamental unit of Bitcoin and Chapters #chapter_blocks-#chapter_bloom_filters cover Blocks and Networking. The last two chapters cover some advanced topics but don’t actually require you to write code.

Chapters #chapter_finite_fields and #chapter_elliptic_curves cover some math that we need. Finite Fields and Elliptic Curves are needed in order for us to understand Elliptic Curve Cryptography in [chapter_elliptic_curve_cryptography]. After we’ve established the Public Key Cryptography at the end of [chapter_elliptic_curve_cryptography], we move towards implementing how the cryptographic primitives are stored and transmitted through parsing and serializing all the different classes that we established in [chapter_serialization].

[chapter_tx_parsing] covers the Transaction structure. [chapter_script] goes into the smart contract language behind Bitcoin called Script. [chapter_tx] combines all the previous chapters in validating and creating transactions based on the Elliptic Curve Cryptography from the first 4 chapters. [chapter_p2sh] establishes how p2sh works, which is a way to make more powerful smart contracts.

[chapter_blocks] covers Blocks, which are groups of ordered Transactions. [chapter_networking] covers network communication in Bitcoin. [chapter_spv] and [chapter_bloom_filters] go into how a light client, or software without access to the entire blockchain, might request and broadcast data from and to nodes that do have the entire blockchain.

[chapter_segwit] covers Segwit and [chapter_advanced] covers suggestions for further study. Again, these chapters are not strictly necessary, but are included as a way to give you a taste of what more there is to learn.

Chapters #chapter_finite_fields-#chapter_bloom_filters have exercises that require you to build up the library from scratch. The answers are in the Appendix and in the corresponding chapter directory in the GitHub repository (https://github.com/jimmysong/programmingbitcoin). 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_bloom_filters] 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.

Setting Up

In order to get the most out of this book, you’ll want to create an environment where you can run the example code and do the exercises. Here are the instructions for setting everything up.

Install Python 3.5 or above on your machine:

  • Windows:

  • Mac OS X:

  • Linux: see your distro docs (many Linux distributions like Ubuntu come with Python 3.5+ pre-installed)

Install pip:

Download this script:

Run this script using Python 3:

$ python3 get-pip.py

Install 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/programmingbitcoin
$ cd programmingbitcoin

Install virtualenv:

$ pip install virtualenv

Install the requirements:

Linux/OSX:

$ virtualenv -p python3 .venv
$ . .venv/bin/activate
(.venv) $ pip install -r requirements.txt

Windows:

C:\programmingbitcoin> virtualenv -p C:\PathToYourPythonInstallation\Python.exe .venv
C:\programmingbitcoin> .venv\Scripts\activate.bat
C:\programmingbitcoin> pip install -r requirements.txt

Run Jupyter notebook:

(.venv) $ jupyter notebook
[I 11:13:23.061 NotebookApp] Serving notebooks from local directory: /home/jimmy/programmingbitcoin
[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).
[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=f849627e4d9d07d2158e3fcde93590eff4a9a7a01f65a8e7

You should have a browser open up automatically:

Jupyter
Figure 1. Jupyter

You can navigate to chapter directories. To do the exercises from [chapter_finite_fields], you would navigate to code-ch01:

Chapter 1 Directory
Figure 2. Jupyter Directory View

From here you can open Chapter1.ipynb:

Chapter 1 Notebook
Figure 3. Jupyter Notebook

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. You can run each "cell" and see the results as if this were an interactive Python shell.

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 check whether your code is correct directly in Jupyter. You will need to edit the corresponding file by clicking through a link like the "this test" link in Figure P-3 . This will take you to a browser tab like this:

Chapter 1 ecc.py
Figure 4. ecc.py

You can edit the file here and save in order to make the test pass.

Answers

All the answers to the various exercises in this book are in the Appendix. They are also available in the code-chxx/answers.py file where xx is the chapter that you’re on.

Conventions Used in This Book

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.

Using Code Examples

Supplemental material (code examples, exercises, etc.) is available for download at https://github.com/jimmysong/programmingbitcoin.

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: “Programming Bitcoin by Jimmy Song (O’Reilly). Copyright 2019, 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 [email protected].

O’Reilly Safari

Note

Safari (formerly Safari Books Online) is a membership-based training and reference platform for enterprise, government, 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.

How to Contact Us

Please address comments and questions concerning 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 [email protected].

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

Acknowledgments