Mastering Bitcoin: Programming the Open Blockchain [3 ed.] 9781098150099

Join the technological revolution that's taking the financial world by storm. Mastering Bitcoin is your guide throu

147 104

English Pages 400 Year 2023

Report DMCA / Copyright

DOWNLOAD FILE

Mastering Bitcoin: Programming the Open Blockchain [3 ed.]
 9781098150099

Table of contents :
Preface
Writing the Bitcoin Book
Intended Audience
Why Are There Bugs on the Cover?
Conventions Used in This Book
Code Examples
Using Code Examples
Changes Since the Previous Edition
Bitcoin Addresses and Transactions in This Book
O’Reilly Online Learning
How to Contact Us
Contacting the Authors
Acknowledgments for the First and Second Editions
Acknowledgments for the Third Edition
Early Release Draft (GitHub Contributions)
1. Introduction
History of Bitcoin
Getting Started
Choosing a Bitcoin Wallet
Types of Bitcoin wallets
Full node versus Lightweight
Who controls the keys
Quick Start
Recovery Codes
Bitcoin Addresses
Receiving Bitcoin
Getting Your First Bitcoin
Finding the Current Price of Bitcoin
Sending and Receiving Bitcoin
2. How Bitcoin Works
Bitcoin Overview
Buying from an Online Store
Bitcoin Transactions
Transaction Inputs and Outputs
Transaction Chains
Making Change
Coin Selection
Common Transaction Forms
Constructing a Transaction
Getting the Right Inputs
Creating the Outputs
Adding the Transaction to the Blockchain
Transmitting the transaction
How it propagates
Bob’s view
Bitcoin Mining
Spending the Transaction
3. Bitcoin Core: The Reference Implementation
From Bitcoin to Bitcoin Core
Bitcoin Development Environment
Compiling Bitcoin Core from the Source Code
Selecting a Bitcoin Core Release
Configuring the Bitcoin Core Build
Building the Bitcoin Core Executables
Running a Bitcoin Core Node
Configuring the Bitcoin Core Node
Bitcoin Core API
Getting Information on Bitcoin Core’s Status
Exploring and Decoding Transactions
Exploring Blocks
Using Bitcoin Core’s Programmatic Interface
Alternative Clients, Libraries, and Toolkits
C/C++
JavaScript
Java
Python
Go
Rust
Scala
C#
4. Keys and Addresses
Public Key Cryptography
Private Keys
Elliptic Curve Cryptography Explained
Public Keys
Output and Input Scripts
IP Addresses: The Original Address for Bitcoin (P2PK)
Legacy Addresses for P2PKH
Base58check Encoding
Compressed Public Keys
Legacy Pay to Script Hash (P2SH)
Bech32 Addresses
Problems with Bech32 Addresses
Bech32m
Private Key Formats
Compressed Private Keys
Advanced Keys and Addresses
Vanity Addresses
Generating vanity addresses
Vanity address security and privacy
Paper Wallets
5. Wallet Recovery
Independent Key Generation
Deterministic Key Generation
Public Child Key Derivation
Hierarchical Deterministic (HD) Key Generation (BIP32)
Seeds and Recovery Codes
Backing Up Nonkey Data
Backing Up Key Derivation Paths
A Wallet Technology Stack in Detail
BIP39 Recovery Codes
Generating a recovery code
From recovery code to seed
Optional passphrase in BIP39
Creating an HD Wallet from the Seed
Private child key derivation
Using derived child keys
Extended keys
Public child key derivation
Using an Extended Public Key on a Web Store
Hardened child key derivation
Index numbers for normal and hardened derivation
HD wallet key identifier (path)
Navigating the HD wallet tree structure
6. Transactions
A Serialized Bitcoin Transaction
Version
Extended Marker and Flag
Inputs
Length of Transaction Input List
Outpoint
Input Script
Sequence
Original sequence-based transaction replacement
Opt-in transaction replacement signaling
Sequence as a consensus-enforced relative timelock
Outputs
Outputs Count
Amount
Uneconomical outputs and disallowed dust
Output Scripts
Witness Structure
Circular Dependencies
Third-Party Transaction Malleability
Second-Party Transaction Malleability
Segregated Witness
Witness Structure Serialization
Lock Time
Coinbase Transactions
Weight and Vbytes
Legacy Serialization
7. Authorization and Authentication
Transaction Scripts and Script Language
Turing Incompleteness
Stateless Verification
Script Construction
The script execution stack
A simple script
Separate execution of output and input scripts
Pay to Public Key Hash
Scripted Multisignatures
An Oddity in CHECKMULTISIG Execution
Pay to Script Hash
P2SH Addresses
Benefits of P2SH
Redeem Script and Validation
Data Recording Output (OP_RETURN)
Transaction Lock Time Limitations
Check Lock Time Verify (OP_CLTV)
Relative Timelocks
Relative Timelocks with OP_CSV
Scripts with Flow Control (Conditional Clauses)
Conditional Clauses with VERIFY Opcodes
Using Flow Control in Scripts
Complex Script Example
Segregated Witness Output and Transaction Examples
Pay to witness public key hash (P2WPKH)
Wallet construction of P2WPKH
Pay to witness script hash (P2WSH)
Differentiating between P2WPKH and P2WSH
Upgrading to Segregated Witness
Embedding segregated witness inside P2SH
Nested pay to witness public key hash
Nested pay to witness script hash
Merklized Alternative Script Trees (MAST)
Pay to Contract (P2C)
Scriptless Multisignatures and Threshold Signatures
Taproot
Tapscript
8. Digital Signatures
How Digital Signatures Work
Creating a Digital Signature
Verifying the Signature
Signature Hash Types (SIGHASH)
Schnorr Signatures
Serialization of Schnorr Signatures
Schnorr-based Scriptless Multisignatures
Schnorr-based Scriptless Threshold Signatures
ECDSA Signatures
ECDSA Algorithm
Serialization of ECDSA Signatures (DER)
The Importance of Randomness in Signatures
Segregated Witness’s New Signing Algorithm
9. Transaction Fees
Who Pays the Transaction Fee?
Fees and Fee Rates
Estimating Appropriate Fee Rates
Replace By Fee (RBF) Fee Bumping
Child Pays for Parent (CPFP) Fee Bumping
Package Relay
Transaction Pinning
CPFP Carve Out and Anchor Outputs
Adding Fees to Transactions
Timelock Defense Against Fee Sniping
10. The Bitcoin Network
Node Types and Roles
The Network
Compact Block Relay
Private Block Relay Networks
Network Discovery
Full Nodes
Exchanging “Inventory”
Lightweight Clients
Bloom Filters
How Bloom Filters Work
How Lightweight Clients Use Bloom Filters
Compact Block Filters
Golomb-Rice Coded Sets (GCS)
What Data to Include in a Block Filter
Downloading Block Filters from Multiple Peers
Reducing Bandwidth with Lossy Encoding
Using Compact Block Filters
Lightweight Clients and Privacy
Encrypted and Authenticated Connections
Mempools and Orphan Pools
11. The Blockchain
Structure of a Block
Block Header
Block Identifiers: Block Header Hash and Block Height
The Genesis Block
Linking Blocks in the Blockchain
Merkle Trees
Merkle Trees and Lightweight Clients
Bitcoin’s Test Blockchains
Testnet: Bitcoin’s Testing Playground
Using testnet
Problems with testnet
Signet: The Proof of Authority Testnet
The default signet and custom signets
Regtest: The Local Blockchain
Using Test Blockchains for Development
12. Mining and Consensus
Bitcoin Economics and Currency Creation
Decentralized Consensus
Independent Verification of Transactions
Mining Nodes
The Coinbase Transaction
Coinbase Reward and Fees
Structure of the Coinbase Transaction
Coinbase Data
Constructing the Block Header
Mining the Block
Proof-of-Work Algorithm
Target Representation
Retargeting to Adjust Difficulty
Median Time Past (MTP)
Successfully Mining the Block
Validating a New Block
Assembling and Selecting Chains of Blocks
Mining and the Hash Lottery
The Extra Nonce Solution
Mining Pools
Managed pools
Peer-to-peer mining pool (P2Pool)
Hashrate Attacks
Changing the Consensus Rules
Hard Forks
Hard forks: Software, network, mining, and chain
Diverging miners and difficulty
Contentious hard forks
Soft Forks
Criticisms of soft forks
Soft fork signaling with block version
BIP34: Signaling and activation
BIP9: Signaling and activation
BIP8: Mandatory lock-in with early activation
Speedy trial: Fail fast or succeed eventually
Consensus Software Development
13. Bitcoin Security
Security Principles
Developing Bitcoin Systems Securely
The Root of Trust
User Security Best Practices
Physical Bitcoin Storage
Hardware Signing Devices
Ensuring Your Access
Diversifying Risk
Multisig and Governance
Survivability
14. Second-Layer Applications
Building Blocks (Primitives)
Applications from Building Blocks
Colored Coins
Single-Use Seals
Pay to Contract (P2C)
Client-Side Validation
RGB
Taproot Assets
Payment Channels and State Channels
State Channels—Basic Concepts and Terminology
Simple Payment Channel Example
Making Trustless Channels
Asymmetric Revocable Commitments
Hash Time Lock Contracts (HTLC)
Routed Payment Channels (Lightning Network)
Basic Lightning Network Example
Lightning Network Transport and Pathfinding
Lightning Network Benefits
A. The Bitcoin Whitepaper by Satoshi Nakamoto
Bitcoin - A Peer-to-Peer Electronic Cash System
Introduction
Transactions
Timestamp Server
Proof-of-Work
Network
Incentive
Reclaiming Disk Space
Simplified Payment Verification
Combining and Splitting Value
Privacy
Calculations
Conclusion
References
License
B. Errata to the Bitcoin Whitepaper
Abstract
Transactions
Proof of Work
Reclaiming Disk Space
Simplified Payment Verification
Privacy
Calculations
C. Bitcoin Improvement Proposals
Index

Polecaj historie