Programming Bitcoin
by Jimmy Song
Copyright 2019 Jimmy Song. All rights reserved.
Printed in the United States of America.
Published by OReilly Media, Inc. , 1005 Gravenstein Highway North, Sebastopol, CA 95472.
OReilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://oreilly.com). For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com .
- Editors: Mike Loukides and Michele Cronin
- Production Editor: Kristen Brown
- Copyeditor: James Fraleigh
- Proofreader: Rachel Head
- Indexer: Judy McConville
- Interior Designer: David Futato
- Cover Designer: Karen Montgomery
- Illustrator: Rebecca Demarest
- March 2019: First Edition
Revision History for the First Edition
- 2019-02-08: First Release
See http://oreilly.com/catalog/errata.csp?isbn=9781492031499 for release details.
The OReilly logo is a registered trademark of OReilly Media, Inc. Programming Bitcoin, the cover image, and related trade dress are trademarks of OReilly Media, Inc.
The views expressed in this work are those of the author, and do not represent the publishers views. While the publisher and the author have used good faith efforts to ensure that the information and instructions 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 intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights.
978-1-492-03149-9
[LSI]
Foreword
Its fun to be a science fiction writer. To build a society where wealth is no longer a mirage erected on the empty promises of governments and manipulations of central banks, where exchanges of value can be completed among the trustless without paying a tax to middlemen, where code can be law, where collective decision making is not subject to the distortions of centralizationall I have to do is to open up a text editor and start making up stuff.
But compelling stories require more than imagination. They require knowledge of the world. Worldbuilding isnt about literary verisimilitude or strings of technobabbleits about piercing through the superficial to ask what if questions that get at the heart of how the world works. The more a writer understands the mechanisms and codes that make up the world, the more interesting the questions they ask become.
Changing the real world is much, much harder than writing fiction, but it similarly requires knowledge. Beyond wisdom, idealism, grit, discipline, and single-minded determination in the face of doubt, a would-be world-changer needs understanding: of the available tools, their capabilities, and their limits.
The world of Bitcoin and blockchain today is still largely a world of fiction. Pundits selling hope and hype, with no understanding of the underlying reality, are far louder and more influential than those who are doing the hard work of bringing about change. Politically motivated screeds premised on fear and get-rich-quick schemes appealing to greed pass for knowledge with the help of a sprinkling of technobabble and trending hashtags.
But you can no more understand blockchain by reading whitepapers or thinkpieces than you can learn to build a company by going to business school and watching PowerPoints.
You have to code.
There is no better way to understand a technology than to build something useful to you in it. Until youve coded the fundamental building blocks of a blockchain application with your own hands, you will not be able to intuit the difference between empty hype and realizable possibility.
This book is the most efficient and comprehensive way to learn about Bitcoin and blockchain through coding. With great skill and insight, Jimmy Song has crafted a learning path that will take you from the basic math behind Bitcoin to the latest extensions and forks. Along the way, the exercisesrefined through extensive work with live studentswill not only teach you the mechanics of working with the blockchain, but also an intuition for the elegance and beauty of this technology.
The journey will not be easy. Even with a teacher as adept as Jimmy to guide you, this isnt a book to be flipped through when youre bored from bingeing on Netflix. It requires you to put in considerable work to get the most out of it. There is no shortcut, no CliffsNotes. But that is very much in line with the constitutive principle of Bitcoin: you must have skin in the game; you must demonstrate proof-of-work. Only then can you trust your knowledge.
Happy coding!
Ken Liu
A winner of the Nebula, Hugo, and World Fantasy awards, Ken Liu is the author of The Dandelion Dynasty, a silkpunk epic fantasy series in which the magic is engineering, and The Paper Menagerie and Other Stories, a collection. His SF story about blockchain, Byzantine Empathy, was originally published by the MIT Press.
Preface
This book will teach you the technology of Bitcoin at a fundamental level.It doesnt cover the monetary, economic, or social dynamics of Bitcoin, but knowing how Bitcoin works under the hood should give you greater insight into whats possible.Theres a tendency to hype Bitcoin and blockchain without really understanding whats going on; this book is meant to be an antidote to that tendency.
After all, there are lots of books about Bitcoin, covering the history and the economic aspects and giving technical descriptions.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.The material has been refined extensively, as Ive taught this course more than 20 times, to over 400 people as of this writing.
By the time youre done with the book, youll not only be able to create transactions, but also get all the data you need from peers and send the transactions over the network.It covers everything needed to accomplish this, including the math, parsing, network connectivity, and block validation.
What Do I Need to Know?
A prerequisite for this book is that you know programmingPython, 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 preferable, but even a beginning knowledge is probably sufficient to get a lot of the concepts.
Some knowledge of math is required, especially for Chapters .These chapters introduce mathematical concepts probably not familiar to those who didnt major in mathematics.Math knowledge around algebra level should suffice to understand the new concepts and to code the exercises covered in those chapters.