O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Programming Bitcoin

Book Description

Dive into Bitcoin technology with this hands-on guide from one of the leading teachers on Bitcoin and Bitcoin programming. Author Jimmy Song shows Python programmers and developers how to program a Bitcoin library from scratch. You’ll learn how to work with the basics, including the math, blocks, network, and transactions behind this popular cryptocurrency and its blockchain payment system.

By the end of the book, you'll understand how this cryptocurrency works under the hood by coding all the components necessary for a Bitcoin library. Learn how to create transactions, get the data you need from peers, and send transactions over the network. Whether you’re exploring Bitcoin applications for your company or considering a new career path, this practical book will get you started.

  • Parse, validate, and create bitcoin transactions
  • Learn how to use the Bitcoin Script smart contract language
  • Work with exercises in each chapter to build a Bitcoin library from scratch
  • Understand how to secure a blockchain by verifying proof of work
  • Program Bitcoin using Python 3 and libraries including pycoin
  • Understand how simplified payment verification and light wallets work
  • Work with public-key cryptography and cryptographic primitives

Table of Contents

  1. Foreword
  2. Preface
    1. Who Is This Book For?
    2. What Do I Need to Know?
    3. How Is the Book Arranged?
    4. Setting Up
    5. Answers
    6. Conventions Used in This Book
    7. Using Code Examples
    8. O’Reilly Online Learning
    9. How to Contact Us
    10. Acknowledgments
  3. 1. Finite Fields
    1. Learning Higher-Level Math
    2. Finite Field Definition
    3. Defining Finite Sets
      1. Constructing a Finite Field in Python
      2. Exercise 1
    4. Modulo Arithmetic
      1. Modulo Arithmetic in Python
    5. Finite Field Addition and Subtraction
      1. Exercise 2
      2. Coding Addition and Subtraction in Python
      3. Exercise 3
    6. Finite Field Multiplication and Exponentiation
      1. Exercise 4
      2. Exercise 5
      3. Coding Multiplication in Python
      4. Exercise 6
      5. Coding Exponentiation in Python
      6. Exercise 7
    7. Finite Field Division
      1. Exercise 8
      2. Exercise 9
    8. Redefining Exponentiation
    9. Conclusion
  4. 2. Elliptic Curves
    1. Definition
    2. Coding Elliptic Curves in Python
      1. Exercise 1
      2. Exercise 2
    3. Point Addition
    4. Math of Point Addition
    5. Coding Point Addition
      1. Exercise 3
    6. Point Addition for When x1≠x2
      1. Exercise 4
    7. Coding Point Addition for When x1≠x2
      1. Exercise 5
    8. Point Addition for When P1 = P2
      1. Exercise 6
    9. Coding Point Addition for When P1 = P2
      1. Exercise 7
    10. Coding One More Exception
    11. Conclusion
  5. 3. Elliptic Curve Cryptography
    1. Elliptic Curves over Reals
    2. Elliptic Curves over Finite Fields
      1. Exercise 1
    3. Coding Elliptic Curves over Finite Fields
    4. Point Addition over Finite Fields
    5. Coding Point Addition over Finite Fields
      1. Exercise 2
      2. Exercise 3
    6. Scalar Multiplication for Elliptic Curves
      1. Exercise 4
    7. Scalar Multiplication Redux
    8. Mathematical Groups
      1. Identity
      2. Closure
      3. Invertibility
      4. Commutativity
      5. Associativity
      6. Exercise 5
    9. Coding Scalar Multiplication
    10. Defining the Curve for Bitcoin
      1. Working with secp256k1
    11. Public Key Cryptography
    12. Signing and Verification
      1. Inscribing the Target
      2. Verification in Depth
      3. Verifying a Signature
      4. Exercise 6
      5. Programming Signature Verification
      6. Signing in Depth
      7. Creating a Signature
      8. Exercise 7
      9. Programming Message Signing
    13. Conclusion
  6. 4. Serialization
    1. Uncompressed SEC Format
      1. Exercise 1
    2. Compressed SEC Format
      1. Exercise 2
    3. DER Signatures
      1. Exercise 3
    4. Base58
      1. Transmitting Your Public Key
      2. Exercise 4
      3. Address Format
      4. Exercise 5
      5. WIF Format
      6. Exercise 6
    5. Big- and Little-Endian Redux
      1. Exercise 7
      2. Exercise 8
      3. Exercise 9
    6. Conclusion
  7. 5. Transactions
    1. Transaction Components
    2. Version
      1. Exercise 1
    3. Inputs
      1. Parsing Script
      2. Exercise 2
    4. Outputs
      1. Exercise 3
    5. Locktime
      1. Exercise 4
      2. Exercise 5
    6. Coding Transactions
    7. Transaction Fee
      1. Calculating the Fee
      2. Exercise 6
    8. Conclusion
  8. 6. Script
    1. Mechanics of Script
    2. How Script Works
    3. Example Operations
      1. Coding Opcodes
      2. Exercise 1
    4. Parsing the Script Fields
      1. Coding a Script Parser and Serializer
    5. Combining the Script Fields
      1. Coding the Combined Instruction Set
    6. Standard Scripts
    7. p2pk
      1. Coding Script Evaluation
      2. Stack Elements Under the Hood
      3. Exercise 2
    8. Problems with p2pk
    9. Solving the Problems with p2pkh
      1. p2pkh
    10. Scripts Can Be Arbitrarily Constructed
      1. Exercise 3
      2. Utility of Scripts
      3. Exercise 4
      4. SHA-1 Piñata
    11. Conclusion
  9. 7. Transaction Creation and Validation
    1. Validating Transactions
      1. Checking the Spentness of Inputs
      2. Checking the Sum of the Inputs Versus the Sum of the Outputs
      3. Checking the Signature
      4. Exercise 1
      5. Exercise 2
      6. Verifying the Entire Transaction
    2. Creating Transactions
      1. Constructing the Transaction
      2. Making the Transaction
      3. Signing the Transaction
      4. Exercise 3
    3. Creating Your Own Transactions on testnet
      1. Exercise 4
      2. Exercise 5
    4. Conclusion
  10. 8. Pay-to-Script Hash
    1. Bare Multisig
    2. Coding OP_CHECKMULTISIG
      1. Exercise 1
    3. Problems with Bare Multisig
    4. Pay-to-Script-Hash (p2sh)
    5. Coding p2sh
      1. More Complicated Scripts
      2. Addresses
      3. Exercise 2
      4. Exercise 3
      5. p2sh Signature Verification
      6. Exercise 4
      7. Exercise 5
    6. Conclusion
  11. 9. Blocks
    1. Coinbase Transactions
      1. Exercise 1
      2. ScriptSig
      3. BIP0034
      4. Exercise 2
    2. Block Headers
      1. Exercise 3
      2. Exercise 4
      3. Exercise 5
      4. Version
      5. Exercise 6
      6. Exercise 7
      7. Exercise 8
      8. Previous Block
      9. Merkle Root
      10. Timestamp
      11. Bits
      12. Nonce
    3. Proof-of-Work
      1. How a Miner Generates New Hashes
      2. The Target
      3. Exercise 9
      4. Difficulty
      5. Exercise 10
      6. Checking That the Proof-of-Work Is Sufficient
      7. Exercise 11
      8. Difficulty Adjustment
      9. Exercise 12
      10. Exercise 13
    4. Conclusion
  12. 10. Networking
    1. Network Messages
      1. Exercise 1
      2. Exercise 2
      3. Exercise 3
    2. Parsing the Payload
      1. Exercise 4
    3. Network Handshake
    4. Connecting to the Network
      1. Exercise 5
    5. Getting Block Headers
      1. Exercise 6
    6. Headers Response
    7. Conclusion
  13. 11. Simplified Payment Verification
    1. Motivation
    2. Merkle Tree
    3. Merkle Parent
      1. Exercise 1
    4. Merkle Parent Level
      1. Exercise 2
    5. Merkle Root
      1. Exercise 3
    6. Merkle Root in Blocks
      1. Exercise 4
    7. Using a Merkle Tree
    8. Merkle Block
      1. Merkle Tree Structure
      2. Exercise 5
      3. Coding a Merkle Tree
      4. The merkleblock Command
      5. Exercise 6
      6. Using Flag Bits and Hashes
      7. Exercise 7
    9. Conclusion
  14. 12. Bloom Filters
    1. What Is a Bloom Filter?
      1. Exercise 1
      2. Going a Step Further
    2. BIP0037 Bloom Filters
      1. Exercise 2
      2. Exercise 3
    3. Loading a Bloom Filter
      1. Exercise 4
    4. Getting Merkle Blocks
      1. Exercise 5
    5. Getting Transactions of Interest
      1. Exercise 6
    6. Conclusion
  15. 13. Segwit
    1. Pay-to-Witness-Pubkey-Hash (p2wpkh)
      1. Transaction Malleability
      2. Fixing Malleability
    2. p2wpkh Transactions
    3. p2sh-p2wpkh
    4. Coding p2wpkh and p2sh-p2wpkh
    5. Pay-to-Witness-Script-Hash (p2wsh)
    6. p2sh-p2wsh
    7. Coding p2wsh and p2sh-p2wsh
    8. Other Improvements
    9. Conclusion
  16. 14. Advanced Topics and Next Steps
    1. Suggested Topics to Study Next
      1. Wallets
      2. Payment Channels and Lightning Network
    2. Contributing
    3. Suggested Next Projects
      1. Testnet Wallet
      2. Block Explorer
      3. Web Shop
      4. Utility Library
      5. Finding a Job
    4. Conclusion
  17. A. Solutions
    1. Chapter 1: Finite Fields
      1. Exercise 1
      2. Exercise 2
      3. Exercise 3
      4. Exercise 4
      5. Exercise 5
      6. Exercise 6
      7. Exercise 7
      8. Exercise 8
      9. Exercise 9
    2. Chapter 2: Elliptic Curves
      1. Exercise 1
      2. Exercise 2
      3. Exercise 3
      4. Exercise 4
      5. Exercise 5
      6. Exercise 6
      7. Exercise 7
    3. Chapter 3: Elliptic Curve Cryptography
      1. Exercise 1
      2. Exercise 2
      3. Exercise 3
      4. Exercise 4
      5. Exercise 5
      6. Exercise 6
      7. Exercise 7
    4. Chapter 4: Serialization
      1. Exercise 1
      2. Exercise 2
      3. Exercise 3
      4. Exercise 4
      5. Exercise 5
      6. Exercise 6
      7. Exercise 7
      8. Exercise 8
      9. Exercise 9
    5. Chapter 5: Transactions
      1. Exercise 1
      2. Exercise 2
      3. Exercise 3
      4. Exercise 4
      5. Exercise 5
      6. Exercise 6
    6. Chapter 6: Script
      1. Exercise 1
      2. Exercise 2
      3. Exercise 3
      4. Exercise 4
    7. Chapter 7: Transaction Creation and Validation
      1. Exercise 1
      2. Exercise 2
      3. Exercise 3
      4. Exercise 4
      5. Exercise 5
    8. Chapter 8: Pay to Script Hash
      1. Exercise 1
      2. Exercise 2
      3. Exercise 3
      4. Exercise 4
      5. Exercise 5
    9. Chapter 9: Blocks
      1. Exercise 1
      2. Exercise 2
      3. Exercise 3
      4. Exercise 4
      5. Exercise 5
      6. Exercise 6
      7. Exercise 7
      8. Exercise 8
      9. Exercise 9
      10. Exercise 10
      11. Exercise 11
      12. Exercise 12
      13. Exercise 13
    10. Chapter 10: Networking
      1. Exercise 1
      2. Exercise 2
      3. Exercise 3
      4. Exercise 4
      5. Exercise 5
      6. Exercise 6
    11. Chapter 11: Simplified Payment Verification
      1. Exercise 1
      2. Exercise 2
      3. Exercise 3
      4. Exercise 4
      5. Exercise 5
      6. Exercise 6
      7. Exercise 7
    12. Chapter 12: Bloom Filters
      1. Exercise 1
      2. Exercise 2
      3. Exercise 3
      4. Exercise 4
      5. Exercise 5
      6. Exercise 6
  18. Index