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

With Early Release ebooks, you get books in their earliest form—the author's raw and unedited content as he or she writes—so you can take advantage of these technologies long before the official release of these titles. You'll also receive updates when significant changes are made, new chapters are available, and the final ebook bundle is released.

Learn how to program a Bitcoin library with this hands-on guide from one of the leading teachers on bitcoin and bitcoin programming. Author Jimmy Song shows you the basics, including the math, blocks, network, and transactions behind this popular cryptocurrency and its blockchain payment system. You’ll also learn how simplified payment verification and how proof-of-work works.

This book is ideal for programmers looking to change the course of their careers, enterprises exploring Bitcoin applications, and students taking a college-level class. You can use this book as training tool to help you learn Bitcoin concepts and then as a handy reference guide once you start to build your Bitcoin library.

You’ll learn how to:

  • Parse, validate, and create Bitcoin transactions
  • Use the Script smart contract language
  • Verify proof-of-work to secure the blockchain
  • Use Python libraries to program Bitcoin
  • Create your own testnet transaction
  • Understand simplified payment verification and learn how light wallets work
  • Explore public key cryptography at a fundamental level

Table of Contents

  1. 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 Safari
    9. How to Contact Us
    10. Acknowledgments
  2. 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
  3. 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
  4. 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
  5. 4. Serialization
    1. Uncompressed SEC format
      1. Exercise 1
    2. Compressed SEC Format
      1. Exercise 2
    3. DER Signatures
      1. Exercise 3
    4. Base58
    5. Transmitting your Public Key
      1. Exercise 4
    6. Address Format
      1. Exercise 5
    7. WIF Format
      1. Exercise 6
    8. Big and Little Endian Redux
      1. Exercise 7
      2. Exercise 8
      3. Exercise 9
    9. Conclusion
  6. 5. Transactions
    1. Transaction Components
    2. Version
      1. Exercise 1
    3. Inputs
      1. Parsing Script
      2. Exercise 2
    4. Outputs
    5. Locktime
      1. Exercise 5
    6. Coding Transactions
    7. Transaction Fee
      1. Calculating the fee
      2. Exercise 6
    8. Conclusion
  7. 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. Stardard 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
    10. p2pkh
    11. Scripts can be Arbitrarily Constructed
      1. Exercise 3
      2. Utilty of Scripts
      3. Exercise 4
      4. Sha1 Piñata
    12. Conclusion
  8. 7. Transaction Creation and Validation
    1. Validating Transactions
    2. Checking the spentness of inputs
    3. Checking the sum of the inputs vs the sum of the outputs
    4. Checking the Signature
      1. Step 1: Empty all the ScriptSigs
      2. Step 2: Replace the ScriptSig of the input being signed with the previous ScriptPubKey
      3. Step 3: Append the hash type
      4. Exercise 1
      5. Exercise 2
    5. Verifying the entire transaction
    6. Creating transactions
    7. Creating a transaction
    8. Combining to make a transaction
    9. Signing a transaction
      1. Exercise 3
      2. Creating your own transactions on testnet
      3. Exercise 4
      4. Exercise 5
    10. Conclusion
  9. 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. Step 1: Empty all the ScriptSigs
      7. Step 2: Replace the ScriptSig of the p2sh input being signed with the RedeemScript
      8. Step 3: Append the hash type
      9. Exercise 4
      10. Exercise 5
    6. Conclusion