Table of Contents for
Programming Bitcoin
Close
Version ebook
/
Retour
Programming Bitcoin
by Jimmy Song
Published by O'Reilly Media, Inc., 2019
Cover
nav
Programming Bitcoin
Programming Bitcoin
Preface
1. Finite Fields
2. Elliptic Curves
3. Elliptic Curve Cryptography
4. Serialization
5. Transactions
6. Script
7. Transaction Creation and Validation
8. Pay To Script Hash
9. Blocks
10. Networking
11. Simplified Payment Verification
12. Bloom Filters
13. Segwit
14. Advanced Topics and Next Steps
A. Solutions
Index
About the Author
Colophon
Preface
Who is this book for?
What do I need to know?
How is the book arranged?
Setting Up
Answers
Conventions Used in This Book
Using Code Examples
O’Reilly Safari
How to Contact Us
Acknowledgments
1. Finite Fields
Learning Higher Level Math
Finite Field Definition
Defining Finite Sets
Constructing a Finite Field in Python
Exercise 1
Modulo Arithmetic
Modulo Arithmetic in Python
Finite Field Addition and Subtraction
Exercise 2
Coding Addition and Subtraction in Python
Exercise 3
Finite Field Multiplication and Exponentiation
Exercise 4
Exercise 5
Coding Multiplication in Python
Exercise 6
Coding Exponentiation in Python
Exercise 7
Finite Field Division
Exercise 8
Exercise 9
Redefining Exponentiation
Conclusion
2. Elliptic Curves
Definition
Coding Elliptic Curves in Python
Exercise 1
Exercise 2
Point Addition
Math of Point Addition
Coding Point Addition
Exercise 3
Point Addition for when x1≠x2
Exercise 4
Coding Point Addition for when x1≠x2
Exercise 5
Point Addition for when P1=P2
Exercise 6
Coding Point Addition for when P1=P2
Exercise 7
Coding One More Exception
Conclusion
3. Elliptic Curve Cryptography
Elliptic Curves over Reals
Elliptic Curves over Finite Fields
Exercise 1
Coding Elliptic Curves over Finite Fields
Point Addition over Finite Fields
Coding Point Addition over Finite Fields
Exercise 2
Exercise 3
Scalar multiplication for Elliptic Curves
Exercise 4
Scalar Multiplication Redux
Mathematical Groups
Identity
Closure
Invertibility
Commutativity
Associativity
Exercise 5
Coding Scalar Multiplication
Defining the curve for Bitcoin
Working with secp256k1
Public Key Cryptography
Signing and Verification
Inscribing the Target
Verification in-depth
Verifying a Signature
Exercise 6
Programming Signature Verification
Signing In-depth
Creating a Signature
Exercise 7
Programming Message Signing
Conclusion
4. Serialization
Uncompressed SEC format
Exercise 1
Compressed SEC Format
Exercise 2
DER Signatures
Exercise 3
Base58
Transmitting your Public Key
Exercise 4
Address Format
Exercise 5
WIF Format
Exercise 6
Big and Little Endian Redux
Exercise 7
Exercise 8
Exercise 9
Conclusion
5. Transactions
Transaction Components
Version
Exercise 1
Inputs
Parsing Script
Exercise 2
Outputs
Exercise 3
Locktime
Exercise 4
Exercise 5
Coding Transactions
Transaction Fee
Calculating the fee
Exercise 6
Conclusion
6. Script
Mechanics of Script
How Script works
Example Operations
Coding opcodes
Exercise 1
Parsing the Script fields
Coding a Script parser and serializer
Combining the Script fields
Coding the combined instruction set
Standard Scripts
p2pk
Coding Script Evaluation
Stack elements under the hood
Exercise 2
Problems with p2pk
Solving the problems with p2pkh
p2pkh
Scripts can be Arbitrarily Constructed
Exercise 3
Utility of Scripts
Exercise 4
Sha1 Piñata
Conclusion
7. Transaction Creation and Validation
Validating Transactions
Checking the spentness of inputs
Checking the sum of the inputs vs the sum of the outputs
Checking the Signature
Step 1: Empty all the ScriptSigs
Step 2: Replace the ScriptSig of the input being signed with the previous ScriptPubKey
Step 3: Append the hash type
Exercise 1
Exercise 2
Verifying the entire transaction
Creating transactions
Creating a transaction
Combining to make a transaction
Signing a transaction
Exercise 3
Creating your own transactions on testnet
Exercise 4
Exercise 5
Conclusion
8. Pay To Script Hash
Bare Multisig
Coding OP_CHECKMULTISIG
Exercise 1
Problems with Bare Multisig
Pay-to-Script-Hash (p2sh)
Coding p2sh
More complicated scripts
Addresses
Exercise 2
Exercise 3
p2sh Signature Verification
Step 1: Empty all the ScriptSigs
Step 2: Replace the ScriptSig of the p2sh input being signed with the RedeemScript
Step 3: Append the hash type
Exercise 4
Exercise 5
Conclusion
9. Blocks
Coinbase Transactions
Exercise 1
ScriptSig
BIP0034
Exercise 2
Headers vs Full Blocks
Exercise 3
Exercise 4
Exercise 5
Version
BIP9
Exercise 6
Exercise 7
Exercise 8
Previous Block
Merkle Root
Timestamp
Bits
Nonce
Proof-of-work
How a miner generates new hashes
Target
Exercise 9
Difficulty
Exercise 10
Checking that the Proof-of-Work is Sufficient
Exercise 11
Difficulty Adjustment
Exercise 12
Exercise 13
Conclusion
10. Networking
Network Messages
Exercise 1
Exercise 2
Exercise 3
Parsing the payload
Exercise 4
Network handshake
Connecting to the network
Exercise 5
Getting Block Headers
Exercise 6
Headers response
Conclusion
11. Simplified Payment Verification
Motivation
Merkle Tree
Merkle Parent
Exercise 1
Merkle Parent Level
Exercise 2
Merkle Root
Exercise 3
Merkle Root in Blocks
Exercise 4
Using a Merkle Tree
Merkle Block
Merkle Tree Structure
Exercise 5
Coding a Merkle Tree
Merkle Block Command
Exercise 6
Using Flag bits and Hashes
Exercise 7
Conclusion
12. Bloom Filters
What is a Bloom Filter?
Exercise 1
Going a step further
BIP0037 Bloom Filters
Exercise 2
Exercise 3
Loading a Bloom Filter
Exercise 4
Getting Merkle Blocks
Exercise 5
Getting Transactions of Interest
Exercise 6
Conclusion
13. Segwit
Pay to Witness Pubkey Hash
Transaction Malleability
Fixing malleability
Pay-to-Witness-Pubkey-Hash (p2wpkh)
P2SH-P2WPKH
Coding p2wpkh and p2sh-p2wpkh
Pay-to-witness-script-hash (p2wsh)
P2SH-P2WSH
Coding p2wsh and p2sh-p2wsh
Other improvements
Conclusion
14. Advanced Topics and Next Steps
Wallets
Hierarchical Deterministic Wallets
Mnemonic Seeds
Payment Channels and Lightning Network
Contributing
Suggested Next Projects
Testnet Wallet
Block Explorer
Web Shop
Utility Library
Finding a Job
Conclusion
A. Solutions
Chapter 1: Finite Fields
Exercise 1
Exercise 2
Exercise 3
Exercise 4
Exercise 5
Exercise 6
Exercise 7
Exercise 8
Exercise 9
Chapter 2: Elliptic Curves
Exercise 1
Exercise 2
Exercise 3
Exercise 4
Exercise 5
Exercise 6
Exercise 7
Chapter 3: Elliptic Curve Cryptography
Exercise 1
Exercise 2
Exercise 3
Exercise 4
Exercise 5
Exercise 6
Exercise 7
Chapter 4: Serialization
Exercise 1
Exercise 2
Exercise 3
Exercise 4
Exercise 5
Exercise 6
Exercise 7
Exercise 8
Exercise 9
Chapter 5: Transactions
Exercise 1
Exercise 2
Exercise 3
Exercise 4
Exercise 5
Exercise 6
Chapter 6: Script
Exercise 1
Exercise 2
Exercise 3
Exercise 4
Chapter 7: Transaction Creation and Validation
Exercise 1
Exercise 2
Exercise 3
Exercise 4
Exercise 5
Chapter 8: Pay To Script Hash
Exercise 1
Exercise 2
Exercise 3
Exercise 4
Exercise 5
Chapter 9: Blocks
Exercise 1
Exercise 2
Exercise 3
Exercise 4
Exercise 5
Exercise 6
Exercise 7
Exercise 8
Exercise 9
Exercise 10
Exercise 11
Exercise 12
Exercise 13
Chapter 10: Networking
Exercise 1
Exercise 2
Exercise 3
Exercise 4
Exercise 5
Exercise 6
Chapter 11: SPV
Exercise 1
Exercise 2
Exercise 3
Exercise 4
Exercise 5
Exercise 6
Exercise 7
Chapter 12: Bloom Filters
Exercise 1
Exercise 2
Exercise 3
Exercise 4
Exercise 5
Exercise 6
Index