Mastering Bitcoin 2nd Edition - Programming the Open Blockchain

Overview

Code Examples: travis_ci

Mastering Bitcoin

Mastering Bitcoin is a book for developers, although the first two chapters cover bitcoin at a level that is also approachable to non-programmers. Anyone with a basic understanding of technology can read the first two chapters to get a great understanding of bitcoin.

This repository contains the complete first edition, second print, published in December 2014, and the complete second edition, third print, published in March 2018, as published by O'Reilly Media in paperback and ebook formats.

Issues, Errors, Comments, Contributions

If you know how to make a pull request to contribute a fix, please write the correction and use a pull request to submit it for consideration against the develop branch. If you are making several changes, please use a separate commit for each to make it easier to cherry-pick or resolve conflicts. Otherwise, please submit an issue, explaining the error or comment. If you would like to contribute extensive changes or new material, please coordinate with the author first; contact information can be found on his website: https://antonopoulos.com/

Reading this book

To read this book, see book.asciidoc. Click on each of the chapters to read in your browser. Other parties may choose to release PDFs of the book online.

Chapters

Published

"Mastering Bitcoin (Second Edition, Second Print): Programming the Open Blockchain" is now available in paperback and ebook formats by many booksellers worldwide:

Mastering Bitcoin (First Edition Second Print) is also published in Japanese, Korean, and Chinese (Simplified) by publishers in the respective countries.

Mastering Bitcoin (Open Edition), based on the First Edition, has been translated by volunteers into more than a dozen languages. Translations are available for free under CC-BY-SA license at: https://bitcoinbook.info

Source

The book's source code, found in this repository, is kept synchronized with the print and ebook editions.

Mastering Bitcoin - First Edition

The tags Edition1Print1, Edition1Print2 correspond to the two existing prints of Mastering Bitcoin (First Edition) as published by O'Reilly Media.

Creative Commons License
Mastering Bitcoin - First Edition by Andreas M. Antonopoulos LLC is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

This "Free Culture" compliant license was approved by my publisher O'Reilly Media (http://oreilly.com), who understands the value of open source. O'Reilly Media is not just the world's best publisher of technical books, but is also a strong supporter of this open culture and the sharing of knowledge.

Thank you O'Reilly Media!

Mastering Bitcoin - Second Edition

The tags, second_edition_print_1 and second_edition_print2, correspond to the first (June 8th, 2017) and second (July 20th, 2017) print of Mastering Bitcoin (Second Edition), as published by O'Reilly Media.

Creative Commons License
Mastering Bitcoin - Second Edition by Andreas M. Antonopoulos LLC is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Translations

If you are interested in translating this book, please join our team of volunteers at: https://www.transifex.com/aantonop/mastering-bitcoin

Free copies of "Mastering Bitcoin Open Edition," translated in many languages, can be downloaded from: https://bitcoinbook.info

Comments
  • Differentiate between

    Differentiate between "Bitcoin" (protocol, network) and "bitcoin" (unit of currency)

    In this book, the word "bitcoin" is generally written in lowercase. This differs from the usage specified on bitcoin.org and the Bitcoin Wiki, both of which recommend capitalizing the word when referring to the protocol or network and writing it lowercase in reference to the unit of currency. Vice's Motherboard blog also has a post explaining that this usage aids readability.

    Is there a rationale for always writing "bitcoin" lowercase? If computer languages (Python, JavaScript) and protocols (Ethernet, File Transfer Protocol) are generally capitalized, why not write "Bitcoin" when referring to the protocol?

    formatting 
    opened by DePasqualeOrg 18
  • ch02: Incorporating additional corrections into mbc2_0209.png

    ch02: Incorporating additional corrections into mbc2_0209.png

    Corrections:

    1. Confirmations (Blocks deep) should be Confirmations (Block depth) see https://en.bitcoin.it/wiki/Confirmation
    2. Blocks (Blockchain) should be Blocks (Block height) see https://www.investopedia.com/terms/b/block-height.asp
    3. Block 277315 should be deleted and instead of it there should be "Mempool Alice's transaction" as it wrong that this block has the transaction with 0 confirmations - it is the mempool.
    4. The arrows connecting the blocks should go upwards not downwards as the block count goes up.

    Cc: @montvid @rating89us

    image issue 
    opened by wbnns 16
  • Rename this book to 'Bitcoin Core Book'.

    Rename this book to 'Bitcoin Core Book'.

    Hi Mr. Will Binns, thanks for you spending time on this book. I really appreciate it.

    However, let's keep honest and rename the new version of this book to 'Bitcoin Core Book'.

    Since you are one important person who is helping the Bitcoin Core chain to fight against the real Bitcoin chain. So it's immoral to intentionally cause confusion among newbies by pretending that you are still working for Bitcoin.

    You can do anything on your Bitcoin Core chain, Knots or not, censorship or not. We Bitcoiners don't care. But at least please don't maliciously mislead newbies out of evil will.

    I really appreciate it if you can be honest on this issue in the future. You will still be a good person, who works for the enemy of Bitcoin.

    opened by jli225 8
  • Third print of second edition was already released

    Third print of second edition was already released

    The third print of the second edition seems to have already been released (at least, that's what what I'm reading says: "2018-03-23: Third Release").

    1. This isn't mentioned in the releases, I guess the tag and release description should be updated?
    2. README.md still incorrectly mentions the second print as the last one.
    3. The link to Amazon says it links to the second print, I guess whoever orders the book will get the third print. I think it makes sense to just drop the "Second Print" in the preface to the Amazon link.
    out of date information 
    opened by danra 7
  • Issue with example 4-4 using libbitcoin with MacOSX High Sierra Unix

    Issue with example 4-4 using libbitcoin with MacOSX High Sierra Unix

    I am having issues trying to run the addr.cpp program

    I enter the command g++ -std=c++11 -o addr addr.cpp $(pkg-config --cflags --libs libbitcoin) and then get these errors

    addr.cpp:11:9: error: no type named 'ec_point' in namespace 'libbitcoin'
        bc::ec_point public_key = bc::secret_to_public_key(secret);
        ~~~~^
    addr.cpp:11:35: error: no member named 'secret_to_public_key' in namespace 'libbitcoin'
        bc::ec_point public_key = bc::secret_to_public_key(secret);
                                  ~~~~^
    addr.cpp:12:40: error: no member named 'encode_hex' in namespace 'libbitcoin'; did you
          mean 'encode_hash'?
        std::cout << "Public key: " << bc::encode_hex(public_key) << std::endl;
                                       ~~~~^~~~~~~~~~
                                           encode_hash
    /usr/local/include/bitcoin/bitcoin/formats/base_16.hpp:65:20: note: 'encode_hash'
          declared here
    BC_API std::string encode_hash(hash_digest hash);
                       ^
    3 errors generated.
    

    Can someone please help me?

    opened by ipaxos 7
  • Chapter 4: Link to pybitcointools is not relevent anymore

    Chapter 4: Link to pybitcointools is not relevent anymore

    Looks like Vitalik said bye bye to the code he wrote for bitcoin last year on Dec 15th. The referance of pybitcointools is no more valid.

    https://github.com/bitcoinbook/bitcoinbook/blob/develop/ch04.asciidoc#implementing-keys-and-addresses-in-python

    out of date information 
    opened by sindhoor7 6
  • Ch. 02 Bob's tx id to Gopesh can't be found - imaginary - lie?

    Ch. 02 Bob's tx id to Gopesh can't be found - imaginary - lie?

    I can't seem to find the tx in 2bbac8bb3a57a2363407ac8c16a67015ed2e88a4388af58cf90299e0744d3de4 in Block Explorer for the tx of 0.01 to Gopesh - can you? Why mix real and imaginary tx? Bob DID NOT PAY Gopesh - it is a lie! Bob did not pay to ANYONE! See: https://blockchain.info/address/1GdK9UzpHBzqzX2A9JFP3Di4weBwqgmoQA Or here - no inputs to Gopesh or anyone: https://blockchair.com/bitcoin/outputs?q=recipient(1GdK9UzpHBzqzX2A9JFP3Di4weBwqgmoQA)

    Could someone add links instead of pictures of tx id's as I had to type the tx id myself to check...

    opened by montvid 6
  • pybitcointools is no longer maintananced

    pybitcointools is no longer maintananced

    The text in ch03 and ch04 introduces pybitcointools. But, sadly that support has ended. 😢

    Example 4-5 and 4-6 uses pybitcointools, so, we may have to change or remove these examples.

    technical error suggested content 
    opened by nekonenene 6
  • Discerning Bitcoin from bitcoin

    Discerning Bitcoin from bitcoin

    It recently became clear to me that the holistic product/service/project “Bitcoin” is represented differently, in written communication online, in contrast to (actual rights to) a single “bitcoin” (or part there of). The difference is the capitalisation of b.

    opened by tpmccallum 6
  • having a hard time compiling cpp code examples

    having a hard time compiling cpp code examples

    I apologize if this is the wrong place to post my troubles. I've installed libbitcoin am now getting errors I cannot solve during compilation of addr.cpp adn other cpp files. There are a bunch of depreciation warnings which are followed by the error. Here's one warning with the errors I'm getting:

    In file included from /usr/include/boost/iostreams/detail/is_dereferenceable.hpp:13:0,
                     from /usr/include/boost/iostreams/detail/resolve.hpp:26,
                     from /usr/include/boost/iostreams/detail/push.hpp:24,
                     from /usr/include/boost/iostreams/detail/streambuf/indirect_streambuf.hpp:31,
                     from /usr/include/boost/iostreams/stream_buffer.hpp:22,
                     from /usr/include/boost/iostreams/stream.hpp:21,
                     from /usr/local/include/bitcoin/bitcoin/utility/container_sink.hpp:24,
                     from /usr/local/include/bitcoin/bitcoin/message/messages.hpp:57,
                     from /usr/local/include/bitcoin/bitcoin.hpp:114,
                     from addr.cpp:1:
    /usr/include/boost/type_traits/detail/template_arity_spec.hpp:13:84: note: #pragma message: NOTE: Use of this header (template_arity_spec.hpp) is deprecated
     # pragma message("NOTE: Use of this header (template_arity_spec.hpp) is deprecated")
                                                                                        ^
    addr.cpp: In function ‘int main()’:
    addr.cpp:12:5: error: ‘ec_point’ is not a member of ‘bc’
         bc::ec_point public_key = bc::secret_to_public_key(secret);
         ^~
    addr.cpp:13:36: error: ‘encode_hex’ is not a member of ‘bc’
         std::cout << "Public key: " << bc::encode_hex(public_key) << std::endl;
                                        ^~
    addr.cpp:13:51: error: ‘public_key’ was not declared in this scope
         std::cout << "Public key: " << bc::encode_hex(public_key) << std::endl;
                                                       ^~~~~~~~~~
    

    From the warning message, it looks like the compiler can see bitcoin.hpp file, but for some reason the namespace 'bc' doesn't have the methods ec_point etc? I'm not a pro in c++ so this is probably user error rather than a bug.

    Using Ubuntu 16.10 with g++ compiler version 6.2.0

    opened by jcramer 6
  • Chapter 5 seems to overly confuse addresses with keys

    Chapter 5 seems to overly confuse addresses with keys

    UTXOs aren't tied to addresses, but only to the script the address translates to (which may or may not be related to the user the address represents).

    wording pending 
    opened by luke-jr 6
  • Textpassage removed in Chapter 2

    Textpassage removed in Chapter 2

    Given TransactionID of the example is not availble at blockchain.com: https://blockexplorer.com/txs/0627052b6f28912f2703066a912ea577f2ce4da4caa5a5fbd8a57286c345c2f2

    opened by squeezer44 0
  • Example 4-5: wif_encoded_private_key should be calculated using the decimal private key

    Example 4-5: wif_encoded_private_key should be calculated using the decimal private key

    key-to-address-ecc-example.py (example 4-5) appears to use the incorrect representation of private key on line 45. It uses decoded_private_key (the decimal representation of primary key) rather than private_key (the hexadecimal representation).

    This is inconsistent with the calculation of wif_compressed_private_key, which uses private_key + '01' rather than decoded_private_key + '01'.

    Also, if I override the random private_key with the private key used in Table 4-4 (i.e., private_key = "1E99423A4ED27608A15A2616A2B0E9E52CED330AC530EDCC32C8FFC6A526AEDD"), I get an inconsistent result for the wif encoding.

    Per Table 4-4, the WIF value should be

    5J3mBbAH58CpQ3Y5RNJpUKPE62SQ5tfcvU2JpbnkeyhfsYB1Jcn.

    Instead, I receive a value of

    5JJrpphRQLdPJU1aNK3N43S5DjGbj8y9cQac2dxyGodtnk1SkfS.

    By changing the code on line 45 to wif_encoded_private_key = cryptos.encode_privkey(private_key, 'wif'), I receive a wif value of 5J3mBbAH58CpQ3Y5RNJpUKPE62SQ5tfcvU2JpbnkeyhfsYB1Jcn that is consistent with the example in table 4-4.

    The values for WIF-Compressed are consistent between Example 4-5, as-is, and Table 4-4.

    Finally, the source code for cryptos.encode_privkey seems to indicate the input should be in decimal - see line 223.

    Am I missing something? I wondered if the wif format works equally well for hexadecimal and decimal private keys, and tried this:

    $ bx wif-to-ec 5JJrpphRQLdPJU1aNK3N43S5DjGbj8y9cQac2dxyGodtnk1SkfS                                                                      
    40e0fe11b6a62375e206e33810a16cd942c5a94068b55eb3ceaf75a9642c4d11
    

    The result matched neither the decimal nor hexadecimal representation of private key.

    opened by kjayma 0
  • Ch01: Misleading description of

    Ch01: Misleading description of "mining" at the beginning.

    Currently the book says: .... "mining," which involves competing to find solutions to a mathematical problem while processing Bitcoin transactions. This could make the wrong impression that mining solves some general usage mathematical prblems or mathematical problems that has some utility other than hashing blocks and proof of work. I think it is important not to confuse the reader at the beginning, since the beginnng of the book is often used by journalists and (sloppy) researchers has inluence on wide public. I would recommend to change it to something like: .... "mining", which involves processing Bitcoin transactions, organising the into "blocks" and competing to solve a mathematical problem of encoding those blocks in certain way that will be explained further later in this book.

    p.s. (Also the comma should be after the closing paranthesis)

    opened by redocrepus 0
  • Update or remove Coinprism Coloured Coin example

    Update or remove Coinprism Coloured Coin example

    The coinprism.info example in chapter 12 needs to either be updated or removed given that coinprism.info is no longer online (currently points to a domain for sale page) and cpr.sm is not resolvable.

    opened by alfiedotwtf 0
  • Node mislabeling

    Node mislabeling

    Figure 10-3 and 10-4 are mislabeled - There are "Node X" and "Node Y" in the images but the text talks about "Node A and Node B", but then reading the description, "Node A" = "Node X", and "Node Y" is not "Node B", and that "Node B" is not labeled at all.

    opened by alfiedotwtf 0
Releases(second_edition_print3_rc1)
  • second_edition_print3_rc1(Mar 3, 2018)

    Major changes:

    • Moved Segwit from Appendix to Chapter 7 (Advanced Transactions)
    • Added a small section on BIP-176 native segwit (Bech32) addresses

    Minor Changes:

    • 153 bug fixes from the Issue List and Pull Requests https://github.com/bitcoinbook/bitcoinbook/issues?utf8=%E2%9C%93&q=created%3A%3E2017-07-20+is%3Aclosed+

    O'Reilly Editor:

    • Rachel Roumeliotis

    Release and Github Editor:

    • Will Binns (wbnns)

    Contributors (in alphabetic order):

    • Akira Chiku (achiku)
    • Alex Waters (alexwaters)
    • Andrew Donald Kennedy (grkvlt)
    • bitcoinctf
    • Bryan Gmyrek (physicsdude)
    • Casey Flynn (cflynn07)
    • cclauss
    • Chapman Shoop (belovachap)
    • Christie D'Anna (avocadobreath)
    • Cody Scott (Siecje)
    • coinradar
    • Cragin Godley (cgodley)
    • Craig Dodd (cdodd)
    • dallyshalla
    • Darius Kramer (dkrmr)
    • David Huie (DavidHuie)
    • Diego Viola (diegoviola)
    • Dirk Jäckel (biafra23)
    • Dimitris Tsapakidis (dimitris-t)
    • Dmitry Marakasov (AMDmi3)
    • drstrangeM
    • Ed Eykholt (edeykholt)
    • Ed Leafe (EdLeafe)
    • Edward Posnak (edposnak)
    • Elias Rodrigues (elias19r)
    • Eric Voskuil (evoskuil)
    • Eric Winchell (winchell)
    • Erik Wahlström (erikwam)
    • effectsToCause (vericoin)
    • Esteban Ordano (eordano)
    • ethers
    • fabienhinault
    • Frank Höger (francyi)
    • Gaurav Rana (bitcoinsSG)
    • genjix
    • halseth
    • Holger Schinzel (schinzelh)
    • Ioannis Cherouvim (cherouvim)
    • Ish Ot Jr. (ishotjr)
    • ivangreene
    • James Addison (jayaddison)
    • Jameson Lopp (jlopp)
    • Jason Bisterfeldt (jbisterfeldt)
    • Javier Rojas (fjrojasgarcia)
    • Jeremy Bokobza (bokobza)
    • JerJohn15
    • Joe Bauers (joebauers)
    • joflynn
    • Johnson Lau (jl2012)
    • Jonathan Cross (jonathancross)
    • Jorgeminator
    • Kai Bakker (kaibakker)
    • Lucas Betschart (lclc)
    • Magomed Aliev (30mb1)
    • Mai-Hsuan Chia (mhchia)
    • marcofalke
    • Marzig (marzig76)
    • Matt McGivney (mattmcgiv)
    • Maximilian Reichel (phramz)
    • Michalis Kargakis (kargakis)
    • Michael C. Ippolito (michaelcippolito)
    • Mihail Russu (MihailRussu)
    • Minh T. Nguyen (enderminh)
    • Nagaraj Hubli (nagarajhubli)
    • Nekomata (nekomata-3)
    • Philipp Gille (philippgille)
    • Robert Furse (Rfurse)
    • Richard Kiss (richardkiss)
    • Ruben Alexander (hizzvizz)
    • Sam Ritchie (sritchie)
    • Sebastian Falbesoner (theStack)
    • Sergej Kotliar (ziggamon)
    • Seiichi Uchida (topecongiro)
    • Simon de la Rouviere (simondlr)
    • Stephan Oeste (Emzy)
    • takaya-imai
    • Thiago Arrais (thiagoarrais)
    • Thomas Kerin (afk11)
    • venzen
    • Will Binns (wbnns)
    • wintercooled
    • wjx
    • Wojciech Langiewicz (wlk)
    • Yancy Ribbens (yancyribbens)
    • yurigeorgiev4
    Source code(tar.gz)
    Source code(zip)
  • second_edition_print2(Feb 2, 2018)

  • second_edition_print_1(Jul 3, 2017)

Owner
Mastering Bitcoin
Mastering Bitcoin
Mastering Ethereum, by Andreas M. Antonopoulos, Gavin Wood

Mastering Ethereum Mastering Ethereum is a book for developers, offering a guide to the operation and use of the Ethereum, Ethereum Classic, RootStock

Ethereum Book 17k Jan 9, 2023
Blockchain-go - A repository that houses a blockchain implemented in Go

blockchain-go This is a repository that houses a blockchain implemented in Go. F

Onyeka 1 May 1, 2022
Open Source Etho Mining Pool - tuned for 8000000 block hardfork on EthoProtocol blockchain.

Open Source Etho Mining Pool - tuned for 8000000 block hardfork on EthoProtocol blockchain. image to be updated soon! Features This pool is being furt

Don Kingdon 8 Aug 13, 2022
A full node Bitcoin (BSV) implementation written in Go

bsvd bsvd is a full node Bitcoin (BSV) implementation written in Go (golang). This project is a port of the bchd codebase to Bitcoin (BSV). It provide

null 42 Dec 25, 2022
A db for bitcoin-sv & BTC

Welcome to go-svdb Project =========== Boquan Team The Boquan is a team dedicated to promoting and developing true bitcoin. The team has successfully

mempool.com team 31 Sep 3, 2021
Moeing chain is an EVM&Web3 compatible sidechain for Bitcoin Cash

Full node client of smartBCH This repository contains the code of the full node client of smartBCH, an EVM&Web3 compatible sidechain for Bitcoin Cash.

null 121 Nov 29, 2022
Store data on Bitcoin for 350 sats/KB up to 185 KB by using P2SH-P2WSH witness scripts

Bitcandle Store data on Bitcoin for 350 sats/KB up to 185 kB by using P2SH-P2WSH witness scripts. 225ed8bc432d37cf434f80717286fd5671f676f12b573294db72

Aurèle Oulès 11 Aug 12, 2022
A curated Golang toolkit for creating Bitcoin SV powered apps

bsv A curated Golang toolkit for creating Bitcoin SV powered apps Table of Contents Installation Maintainers License Installation bsv requires a suppo

null 21 May 10, 2022
A work-in-progress Bitcoin wallet based on Output Descriptors

go-wallet A work-in-progress Bitcoin wallet Descriptors go-wallet is designed around Bitcoin Descriptors. It implements a Output Script Descriptors la

Gustavo Chaín 5 May 4, 2022
The go-to Bitcoin Node (BN) Go library.

go-bitcoin Go wrapper for bitcoin RPC RPC services Start by creating a connection to a bitcoin node b, err := New("rcp host", rpc port, "rpc usernam

null 3 Feb 13, 2022
Bitcoin CPU miner written in Go.

CPU Miner Bitcoin CPU miner written in Go. Introduction This is a CPU miner written in Go. It is a proof of concept and is not intended for production

Yaroslav Gaponov 1 Dec 29, 2022
A fully validating Bitcoin node with Utreexo support

btcd btcd is an alternative full node bitcoin implementation written in Go (golang). This project is currently under active development and is in a Be

Utreexo 14 Dec 21, 2022
Bitcoin futures curve from Deribit as a JSON webservice

Curve Bitcoin futures curve from Deribit as a JSON webservice Building go build . Running ./curve Expiration date and annualised yield of each contr

Steven Wilkin 0 Dec 13, 2021
Bitcoin Core integration/staging tree

Bitcoin Core integration/staging tree https://bitcoincore.org For an immediately usable, binary version of the Bitcoin Core software, see https://bitc

Bitcoin 67.6k Dec 30, 2022
Bitcoin Improvement Proposals

People wishing to submit BIPs, first should propose their idea or document to the bitc[email protected] mailing list (do not assign a

Bitcoin 7.6k Jan 2, 2023
Full bitcoin solution written in Go (golang)

About Gocoin Gocoin is a full Bitcoin solution written in Go language (golang). The software architecture is focused on maximum performance of the nod

Piotr Narewski 908 Dec 20, 2022
A simple, concurrent bitcoin miner framework implemented in Go.

Bitcoin Miner A simple, concurrent bitcoin miner framework implemented in Go. Disclaimer: this is not a product intended to be used for real mining, s

null 6 Dec 29, 2022
Bitcoin address balance checker on steroids.

BTCSteroids Bitcoin address balance checker on steroids. Table of contents Quick start What's included Use Cases Thanks Copyright and license Quick st

null 10 Dec 12, 2022
Btc-globe - Visualize Bitcoin node locations using golang

btc-globe Visualize Bitcoin nodes by location using Golang

null 0 Jan 19, 2022