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

Mastering Blockchain - Second Edition

Book Description

Learn about cryptography and cryptocurrencies, so you can build highly secure, decentralized applications and conduct trusted in-app transactions.

About This Book

  • Get to grips with the underlying technical principles and implementations of blockchain
  • Build powerful applications using Ethereum to secure transactions and create smart contracts
  • Explore cryptography, mine cryptocurrencies, and solve scalability issues with this comprehensive guide

Who This Book Is For

This book will appeal to those who wish to build fast, highly secure, transactional applications. It targets people who are familiar with the concept of blockchain and are comfortable with a programming language.

What You Will Learn

  • Master the theoretical and technical foundations of the blockchain technology
  • Understand the concept of decentralization, its impact, and its relationship with blockchain technology
  • Master how cryptography is used to secure data - with practical examples
  • Grasp the inner workings of blockchain and the mechanisms behind bitcoin and alternative cryptocurrencies
  • Understand the theoretical foundations of smart contracts
  • Learn how Ethereum blockchain works and how to develop decentralized applications using Solidity and relevant development frameworks
  • Identify and examine applications of the blockchain technology - beyond currencies
  • Investigate alternative blockchain solutions including Hyperledger, Corda, and many more
  • Explore research topics and the future scope of blockchain technology

In Detail

A blockchain is a distributed ledger that is replicated across multiple nodes and enables immutable, transparent and cryptographically secure record-keeping of transactions. The blockchain technology is the backbone of cryptocurrencies, and it has applications in finance, government, media and almost all other industries. Mastering Blockchain, Second Edition has been thoroughly updated and revised to provide a detailed description of this leading technology and its implementation in the real world.

This book begins with the technical foundations of blockchain technology, teaching you the fundamentals of distributed systems, cryptography and how it keeps data secure. You will learn about the mechanisms behind cryptocurrencies and how to develop applications using Ethereum, a decentralized virtual machine. You will also explore different other blockchain solutions and get an introduction to business blockchain frameworks under Hyperledger, a collaborative effort for the advancement of blockchain technologies hosted by the Linux Foundation. You will also be shown how to implement blockchain solutions beyond currencies, Internet of Things with blockchain, blockchain scalability, and the future scope of this fascinating and powerful technology.

Style and approach

This comprehensive guide allows you to build smart blockchain applications and explore the power of this database. The book will let you quickly brush up on the basics of the blockchain database, followed by advanced implementations of blockchain in currency, smart contracts, decentralization, and so on.

Downloading the example code for this book You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Table of Contents

  1. Title Page
  2. Copyright and Credits
    1. Mastering Blockchain Second Edition
  3. Packt Upsell
    1. Why subscribe?
    2. PacktPub.com
  4. Contributors
    1. About the author
    2. About the reviewer
    3. Packt is searching for authors like you
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  6. Blockchain 101
    1. The growth of blockchain technology
    2. Distributed systems
    3. The history of blockchain and Bitcoin
      1. Electronic cash
      2. Blockchain
        1. Blockchain defined
          1. Peer-to-peer
          2. Distributed ledger
          3. Cryptographically-secure
          4. Append-only
          5. Updateable via consensus
      3. Generic elements of a blockchain
        1. How blockchain works
        2. How blockchain accumulates blocks
      4. Benefits and limitations of blockchain
      5. Tiers of blockchain technology
      6. Features of a blockchain
    4. Types of blockchain
      1. Distributed ledgers
      2. Distributed Ledger Technology
      3. Public blockchains
      4. Private blockchains
        1. Semiprivate blockchains
        2. Sidechains
        3. Permissioned ledger
      5. Shared ledger
      6. Fully private and proprietary blockchains
      7. Tokenized blockchains
      8. Tokenless blockchains
    5. Consensus
      1. Consensus mechanism
      2. Types of consensus mechanisms
      3. Consensus in blockchain
    6. CAP theorem and blockchain
    7. Summary
  7. Decentralization
    1. Decentralization using blockchain
    2. Methods of decentralization
      1. Disintermediation
      2. Contest-driven decentralization
    3. Routes to decentralization
      1. How to decentralize
      2. The decentralization framework example
    4. Blockchain and full ecosystem decentralization
      1. Storage
      2. Communication
      3. Computing power and decentralization
    5. Smart contracts
    6. Decentralized Organizations
      1. Decentralized Autonomous Organizations
      2. Decentralized Autonomous Corporations
      3. Decentralized Autonomous Societies
      4. Decentralized Applications (DApps)
      5. Requirements of a Decentralized Application
      6. Operations of a DApp
        1. DApp examples
          1. KYC-Chain
          2. OpenBazaar
          3. Lazooz
    7. Platforms for decentralization
      1. Ethereum
      2. MaidSafe
      3. Lisk
    8. Summary
  8. Symmetric Cryptography
    1. Working with the OpenSSL command line
    2. Introduction
      1. Mathematics
        1. Set
        2. Group
        3. Field
        4. A finite field
        5. Order
        6. An abelian group
        7. Prime fields
        8. Ring
        9. A cyclic group
        10. Modular arithmetic
      2. Cryptography
      3. Confidentiality
      4. Integrity
      5. Authentication
        1. Entity authentication
        2. Data origin authentication
      6. Non-repudiation
      7. Accountability
    3. Cryptographic primitives
      1. Symmetric cryptography
        1. Stream ciphers
        2. Block ciphers
          1. Block encryption mode
          2. Electronic Code Book
          3. Cipher Block Chaining
          4. Counter mode
          5. Keystream generation mode
          6. Message authentication mode
          7. Cryptographic hash mode
      2. Data Encryption Standard
      3. Advanced Encryption Standard
        1. How AES works
    4. Summary
  9. Public Key Cryptography
    1. Asymmetric cryptography
      1. Integer factorization
      2. Discrete logarithm
      3. Elliptic curves
    2. Public and private keys
      1. RSA
        1. Encryption and decryption using RSA
        2. Elliptic Curve Cryptography
          1. Mathematics behind ECC
          2. Point addition
          3. Point doubling
      2. Discrete logarithm problem in ECC
        1. RSA using OpenSSL
        2. RSA public and private key pair
          1. Private key
          2. Public key
          3. Exploring the public key
        3. Encryption and decryption
          1. Encryption
          2. Decryption
        4. ECC using OpenSSL
          1. ECC private and public key pair
          2. Private key
          3. Private key generation
      3. Hash functions
        1. Compression of arbitrary messages into fixed-length digest
        2. Easy to compute
        3. Preimage resistance
        4. Second preimage resistance
        5. Collision resistance
        6. Message Digest
        7. Secure Hash Algorithms
          1. Design of Secure Hash Algorithms
          2. Design of SHA-256
          3. Design of SHA-3 (Keccak)
          4. OpenSSL example of hash functions
          5. Message Authentication Codes
          6. MACs using block ciphers
          7. Hash-based MACs
        8. Merkle trees
        9. Patricia trees
        10. Distributed Hash Tables
        11. Digital signatures
      4. RSA digital signature algorithm
        1. Sign then encrypt
        2. Encrypt then sign
      5. Elliptic Curve Digital Signature Algorithm
        1. How to generate a digital signature using OpenSSL
        2. ECDSA using OpenSSL
        3. Homomorphic encryption
        4. Signcryption
        5. Zero-Knowledge Proofs
        6. Blind signatures
        7. Encoding schemes
    3. Financial markets and trading
      1. Trading
      2. Exchanges
        1. Orders and order properties
        2. Order management and routing systems
        3. Components of a trade
        4. The underlying instrument
        5. General attributes
        6. Economics
        7. Sales
        8. Counterparty
      3. Trade life cycle
      4. Order anticipators
      5. Market manipulation
    4. Summary
  10. Introducing Bitcoin
    1. Bitcoin
      1. Bitcoin definition
      2. Bitcoin – a bird's-eye view
        1. Sending a payment to someone
    2. Digital keys and addresses
      1. Private keys in Bitcoin
      2. Public keys in Bitcoin
      3. Addresses in Bitcoin
        1. Base58Check encoding
        2. Vanity addresses
          1. Multisignature addresses
    3. Transactions
      1. The transaction life cycle
        1. Transaction fee
        2. Transaction pools
      2. The transaction data structure
        1. Metadata
        2. Inputs
        3. Outputs
        4. Verification
        5. The script language
        6. Commonly used opcodes
      3. Types of transactions
        1. Coinbase transactions
        2. Contracts
      4. Transaction verification
        1. Transaction malleability
    4. Blockchain
      1. The structure of a block
      2. The structure of a block header
      3. The genesis block
    5. Mining
      1. Tasks of the miners
      2. Mining rewards
      3. Proof of Work (PoW)
      4. The mining algorithm
      5. The hash rate
      6. Mining systems
        1. CPU
        2. GPU
        3. FPGA
        4. ASICs
      7. Mining pools
    6. Summary
  11. Bitcoin Network and Payments
    1. The Bitcoin network
    2. Wallets
      1. Non-deterministic wallets
      2. Deterministic wallets
      3. Hierarchical Deterministic wallets
      4. Brain wallets
      5. Paper wallets
      6. Hardware wallets
      7. Online wallets
      8. Mobile wallets
    3. Bitcoin payments
    4. Innovation in Bitcoin
      1. Bitcoin Improvement Proposals (BIPs)
      2. Advanced protocols
      3. Segregated Witness (SegWit)
      4. Bitcoin Cash
      5. Bitcoin Unlimited
      6. Bitcoin Gold
      7. Bitcoin investment and buying and selling bitcoins
    5. Summary
  12. Bitcoin Clients and APIs
    1. Bitcoin installation
      1. Types of Bitcoin Core clients
        1. Bitcoind
        2. Bitcoin-cli
        3. Bitcoin-qt
      2. Setting up a Bitcoin node
      3. Setting up the source code
      4. Setting up bitcoin.conf
      5. Starting up a node in testnet
      6. Starting up a node in regtest
      7. Experimenting with Bitcoin-cli
      8. Bitcoin programming and the command-line interface
    2. Summary
  13. Alternative Coins
    1. Theoretical foundations
      1. Alternatives to Proof of Work
        1. Proof of Storage
        2. Proof of Stake (PoS)
      2. Various stake types
        1. Proof of coinage
        2. Proof of Deposit (PoD)
        3. Proof of Burn
        4. Proof of Activity (PoA)
        5. Nonoutsourceable puzzles
      3. Difficulty adjustment and retargeting algorithms
        1. Kimoto Gravity Well
        2. Dark Gravity Wave
        3. DigiShield
        4. MIDAS
    2. Bitcoin limitations
      1. Privacy and anonymity
        1. Mixing protocols
        2. Third-party mixing protocols
        3. Inherent anonymity
      2. Extended protocols on top of Bitcoin
        1. Colored coins
        2. Counterparty
      3. Development of altcoins
        1. Consensus algorithms
        2. Hashing algorithms
        3. Difficulty adjustment algorithms
        4. Inter-block time
        5. Block rewards
        6. Reward halving rate
        7. Block size and transaction size
        8. Interest rate
        9. Coinage
        10. Total supply of coins
    3. Namecoin
      1. Trading Namecoins
      2. Obtaining Namecoins
      3. Generating Namecoin records
    4. Litecoin
    5. Primecoin
      1. Trading Primecoin
      2. Mining guide
    6. Zcash
      1. Trading Zcash
      2. Mining guide
        1. Address generation
        2. GPU mining
          1. Downloading and compiling nheqminer
      3. Initial Coin Offerings (ICOs)
      4. ERC20 tokens
    7. Summary
  14. Smart Contracts
    1. History
    2. Definition
    3. Ricardian contracts
      1. Smart contract templates
      2. Oracles
      3. Smart Oracles
      4. Deploying smart contracts on a blockchain
      5. The DAO
    4. Summary
  15. Ethereum 101
    1. Introduction
      1. The yellow paper
        1. Useful mathematical symbols
      2. Ethereum blockchain
    2. Ethereum – bird's eye view
    3. The Ethereum network
      1. Mainnet
      2. Testnet
      3. Private net
    4. Components of the Ethereum ecosystem
      1. Keys and addresses
      2. Accounts
        1. Types of accounts
      3. Transactions and messages
        1. Contract creation transaction
        2. Message call transaction
        3. Messages
        4. Calls
        5. Transaction validation and execution
        6. The transaction substate
        7. State storage in the Ethereum blockchain
        8. The world state
        9. The account state
        10. Transaction receipts
      4. Ether cryptocurrency / tokens (ETC and ETH)
      5. The Ethereum Virtual Machine (EVM)
        1. Execution environment
        2. Machine state
        3. The iterator function
      6. Smart contracts
        1. Native contracts
    5. Summary
  16. Further Ethereum
    1. Programming languages
      1. Runtime bytecode
        1. Opcodes and their meaning
        2. Arithmetic operations
        3. Logical operations
        4. Cryptographic operations
        5. Environmental information
        6. Block information
        7. Stack, memory, storage, and flow operations
        8. Push operations
        9. Duplication operations
        10. Exchange operations
        11. Logging operations
        12. System operations
      2. Blocks and blockchain
        1. The genesis block
        2. The block validation mechanism
          1. Block finalization
        3. Block difficulty
        4. Gas
      3. Fee schedule
        1. Forks in the blockchain
        2. Nodes and miners
          1. The consensus mechanism
        3. Ethash
          1. CPU mining
          2. GPU mining
          3. Benchmarking
          4. Mining rigs
          5. Mining pools
        4. Wallets and client software
          1. Geth
          2. Eth
          3. Pyethapp
          4. Parity
          5. Light clients
          6. Installation
          7. Eth installation
          8. Mist browser
          9. Geth
          10. The geth console
          11. Funding the account with bitcoin
          12. Parity installation
          13. Creating accounts using the parity command line
        5. APIs, tools, and DApps
          1. Applications (DApps and DAOs) developed on Ethereum
          2. Tools
      4. Supporting protocols
        1. Whisper
        2. Swarm
      5. Scalability, security, and other challenges
      6. Trading and investment
    2. Summary
  17. Ethereum Development Environment
    1. Test networks
    2. Setting up a private net
      1. Network ID
      2. The genesis file
      3. Data directory
        1. Flags and their meaning
        2. Static nodes
    3. Starting up the private network
      1. Running Mist on private net
      2. Deploying contracts using Mist
      3. Block explorer for private net / local Ethereum block explorer
    4. Summary
  18. Development Tools and Frameworks
    1. Languages
      1. Compilers
        1. Solidity compiler (solc)
          1. Installation on Linux
          2. Installation on macOS
        2. Integrated Development Environments (IDEs)
          1. Remix
        3. Tools and libraries
          1. Node version 7
          2. EthereumJS
        4. Ganache
        5. MetaMask
        6. Truffle
          1. Installation
        7. Contract development and deployment
          1. Writing
          2. Testing
    2. Solidity language
      1. Types
        1. Value types
          1. Boolean
          2. Integers
          3. Address
        2. Literals
          1. Integer literals
          2. String literals
          3. Hexadecimal literals
        3. Enums
        4. Function types
          1. Internal functions
          2. External functions
        5. Reference types
          1. Arrays
          2. Structs
          3. Data location
          4. Mappings
        6. Global variables
        7. Control structures
          1. Events 
          2. Inheritance
          3. Libraries
          4. Functions
        8. Layout of a Solidity source code file
          1. Version pragma
          2. Import
          3. Comments
    3. Summary
  19. Introducing Web3
    1. Web3
      1. Contract deployment
      2. POST requests
      3. The HTML and JavaScript frontend
        1. Installing web3.js
          1. Example
          2. Creating a web3 object
          3. Checking availability by calling any web3 method
          4. Contract functions
      4. Development frameworks
        1. Truffle
          1. Initializing Truffle
          2. Interaction with the contract
          3. Another example
          4. An example project – Proof of Idea
        2. Oracles
        3. Deployment on decentralized storage using IPFS
          1. Installing IPFS
        4. Distributed ledgers
    2. Summary
  20. Hyperledger
    1. Projects under Hyperledger
      1. Fabric
      2. Sawtooth Lake
      3. Iroha
      4. Burrow
      5. Indy
      6. Explorer
      7. Cello
      8. Composer
      9. Quilt
    2. Hyperledger as a protocol
    3. The reference architecture
      1. Requirements and design goals of Hyperledger Fabric
        1. The modular approach
        2. Privacy and confidentiality
        3. Scalability
        4. Deterministic transactions
        5. Identity
        6. Auditability
        7. Interoperability
        8. Portability
        9. Rich data queries
    4. Fabric
      1. Hyperledger Fabric
        1. Membership services
        2. Blockchain services
        3. Consensus services
        4. Distributed ledger
          1. The peer to peer protocol
          2. Ledger storage
          3. Chaincode services
          4. Components of the fabric
          5. Peers
          6. Orderer nodes
          7. Clients
          8. Channels
          9. World state database
          10. Transactions
          11. Membership Service Provider (MSP)
          12. Smart contracts
          13. Crypto service provider
          14. Applications on blockchain
          15. Chaincode implementation
          16. The application model
          17. Consensus in Hyperledger Fabric
          18. The transaction life cycle in Hyperledger Fabric
      2. Sawtooth Lake
        1. PoET
        2. Transaction families
        3. Consensus in Sawtooth
        4. The development environment – Sawtooth Lake
      3. Corda
        1. Architecture
          1. State objects
          2. Transactions
          3. Consensus
          4. Flows
        2. Components
          1. Nodes
          2. The permissioning service
          3. Network map service
          4. Notary service
          5. Oracle service
          6. Transactions
          7. Vaults
          8. CorDapp
        3. The development environment – Corda
    5. Summary
  21. Alternative Blockchains
    1. Blockchains
      1. Kadena
      2. Ripple
        1. Transactions
          1. Payments related
          2. Order related
          3. Account and security-related
        2. Interledger
          1. Application layer
          2. Transport layer
          3. Interledger layer
          4. Ledger layer
      3. Stellar
      4. Rootstock
        1. Sidechain
        2. Drivechain
      5. Quorum
        1. Transaction manager
        2. Crypto Enclave
        3. QuorumChain
        4. Network manager
      6. Tezos
      7. Storj
      8. MaidSafe
      9. BigchainDB
      10. MultiChain
      11. Tendermint
        1. Tendermint Core
        2. Tendermint Socket Protocol (TMSP)
    2. Platforms and frameworks
      1. Eris
    3. Summary
  22. Blockchain – Outside of Currencies
    1. Internet of Things
      1. Physical object layer
      2. Device layer
      3. Network layer
      4. Management layer
      5. Application layer
      6. IoT blockchain experiment
        1. First node setup
        2. Raspberry Pi node setup
          1. Installing Node.js
        3. Circuit
      7. Government
        1. Border control
        2. Voting
        3. Citizen identification (ID cards)
        4. Miscellaneous
      8. Health
      9. Finance
        1. Insurance
        2. Post-trade settlement
        3. Financial crime prevention
      10. Media
    2. Summary
  23. Scalability and Other Challenges
    1. Scalability
      1. Network plane
      2. Consensus plane
      3. Storage plane
      4. View plane
      5. Block size increase
      6. Block interval reduction
      7. Invertible Bloom Lookup Tables
      8. Sharding
      9. State channels
      10. Private blockchain
      11. Proof of Stake
      12. Sidechains
        1. Subchains
        2. Tree chains (trees)
        3. Block propagation
        4. Bitcoin-NG
        5. Plasma
    2. Privacy
      1. Indistinguishability Obfuscation
      2. Homomorphic encryption
      3. Zero-Knowledge Proofs
      4. State channels
      5. Secure multiparty computation
      6. Usage of hardware to provide confidentiality
      7. CoinJoin
      8. Confidential transactions
      9. MimbleWimble
      10. Security
        1. Smart contract security
          1. Formal verification and analysis
          2. Oyente tool
    3. Summary
  24. Current Landscape and What's Next
    1. Emerging trends
      1. Application-specific blockchains (ASBCs)
      2. Enterprise-grade blockchains
      3. Private blockchains
      4. Start-ups
      5. Strong research interest
      6. Standardization
      7. Enhancements
      8. Real-world implementations
      9. Consortia
      10. Answers to technical challenges
      11. Convergence
      12. Education of blockchain technology
      13. Employment
      14. Cryptoeconomics
      15. Research in cryptography
      16. New programming languages
      17. Hardware research and development
      18. Research in formal methods and security
      19. Alternatives to blockchains
      20. Interoperability efforts
      21. Blockchain as a Service
      22. Efforts to reduce electricity consumption
    2. Other challenges
      1. Regulation
      2. Dark side
    3. Blockchain research
      1. Smart contracts
      2. Centralization issues
      3. Limitations in cryptographic functions
      4. Consensus algorithms
      5. Scalability
      6. Code obfuscation
    4. Notable projects
      1. Zcash on Ethereum
      2. CollCo
      3. Cello
      4. Qtum
      5. Bitcoin-NG
      6. Solidus
      7. Hawk
      8. Town-Crier
      9. SETLCoin
      10. TEEChan
      11. Falcon
      12. Bletchley
      13. Casper
    5. Miscellaneous tools
      1. Solidity extension for Microsoft Visual Studio
      2. MetaMask
      3. Stratis
      4. Embark
      5. DAPPLE
      6. Meteor
      7. uPort
      8. INFURA
    6. Convergence with other industries
    7. Future
    8. Summary
  25. Another Book You May Enjoy
    1. Leave a review – let other readers know what you think