Blockchain By Example

Book description

Dive into working mechanics of the decentralized blockchain applications to build your own scalable dapps

About This Book
  • Guide to help you learn Blockchain ecosystem and their terminologies
  • Implement smart contracts, wallet, consensus protocols, and other principles
  • Design and develop decentralized applications using Bitcoin, Ethereum, and Hyperledger
Who This Book Is For

If you are keen on learning how to build your own blockchain decentralized applications from scratch, then this book is what you need. Implement key building blocks of a blockchain ecosystem with this handy guide. It explains all the basic concepts required to develop intermediate projects

What You Will Learn
  • Grasp the decentralized technology fundamentals to master blockchain principles.
  • Build blockchain projects on Bitcoin, Ethereum, and Hyperledger.
  • Create your own currency and a payment application using Bitcoin
  • Implement Web3.js apps and supply chain system using Hyperledger.
  • Write smart contracts, run your ICO and build tontine decentralized app using Ethereum
  • Explore Blockchain to learn how to implement distributed file management.
  • Integrate blockchain into your existing systems in your organization
In Detail

The blockchain is a revolution promising a new world without middlemen. Technically, it is an immutable and tamper-proof distributed ledger of all transactions across a peer-to-peer network. With this book, you will set a foundation to get into the grips of blockchain ecosystem to build real-world projects.

Throughout the book, we will walk you through a detailed step-by-step process using solidity to build three blockchain projects with different complexity levels and hurdles. This book will provide you with all the relevant concepts related to cryptocurrencies, smart contracts, and blockchain in order to build your own decentralized applications. Each project will teach you just enough about blockchain to be productive right away. We will cover the different set of projects such as Bitcoin Payment System, Supply Chain on Hyperledger, and Tontine Bank Dapp using Ethereum. We will cover major challenges that are associated with Blockchain ecosystems such as scalability, integration, distributed file management and more.

By the end of this book, you will not only be able to tackle the common issues in the blockchain ecosystem but also design and build reliable, scalable distributed systems.

Publisher resources

Download Example Code

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Blockchain By Example
  3. About Packt
    1. Why subscribe?
    2. Packt.com
  4. Foreword
  5. Contributors
    1. About the authors
    2. About the reviewers
    3. Packt is searching for authors like you
  6. 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
  7. Say Hello to Blockchain
    1. The emergence of blockchain and cryptocurrency
      1. From virtual currencies to cryptocurrency
      2. The invention of bitcoin
      3. What is blockchain?
        1. Blocks
        2. Transactions
        3. Smart contracts
    2. Interact with the blockchain
      1. Getting started
        1. Running a bitcoin client for the first time 
        2. Synchronizing the blockchain
          1. Running Bitcoin Core
          2. Running Electrum
      2. Method 1 – Building a raw transaction using Bitcoin Core
        1. Funding our address
        2. Unspent transaction output
        3. Creating the transaction
          1. Transaction structure
        4. Signing the transaction 
        5. Sending the transaction
        6. Retrieving your message online from the blockchain
        7. Using the local blockchain
      3. Method 2 – build a raw bitcoin transaction in JavaScript
        1. Preparation
        2. Let's code
    3. Types of blockchains
      1. Classification of blockchains
    4. Summary
  8. Building a Bitcoin Payment System
    1. What is Bitcoin?
      1. Why choose Bitcoin as a platform?
    2. Getting started with Bitcoin
      1. Setting up a Regtest environment
        1. First commands
      2. Transactions in bitcoin 
        1. Mining 
        2. Bitcoin scripting
          1. Building a P2PKH script using JavaScript
          2. Building a custom script
    3. Building a payment gateway
      1. Project description
        1. Bitcoin payment protocol overview
      2. Prerequisites
      3. Project setup
        1. Certificate management
      4. Merchant side
        1. Building a payment request URI
        2. Routing
          1. Checkout view
          2. Proceeding with payment
          3. Requesting payment details
          4. Receiving and acknowledging payment
          5. Invoicing
      5. Client side
      6. Previewing your application
    4. Payment protocol using BitcoinJ
      1. Prerequisites
        1. BitcoinJ installation
      2. BitcoinJ client
        1. Bitcoin wallet configuration
        2. Requesting payment
        3. Sending a payment
        4. Testing the code
    5. Bitcoin smart contracts
      1. What is Rootstock?
        1. Rootstock setup
        2. Interactions with RSK
        3. Account settings
      2. Writing your first bitcoin smart contract
        1. Deploying the contract
        2. Interacting  with the contract
    6. Summary
  9. Building Your Own Cryptocurrency
    1. Compiling Bitcoin from source
      1. Preparing your build system
        1. Installing dependencies
          1. Bitcoin-qt - Qt5 GUI for Bitcoin
        2. Cloning the Bitcoin source code
      2. Building Bitcoin Core
        1. Checking your build worked
    2. New cryptocurrency – Readercoin
      1. Cloning Bitcoin
      2. Readercoin rebranding
      3. Changing the ports
      4. Changing pchMessageStart
      5. Genesis block
        1. New pszTimestamp
        2. New nonce, epoch time, and nbits
        3. New genesis hash
        4. Editing the Merkle root
      6. Removing the seed nodes
      7. Checkpoints
      8. ChainTxData
      9. Rewarding
      10. Halving
      11. Total supply
      12. POW parameters – target
        1. New block time
          1. Difficulty adjustment time interval
          2. Difficulty adjustment rate
      13. Time maturity
      14. Block size
      15. BIPs: Bitcoin Improvement Proposals
      16. Compiling and testing
      17. Design and graphics (Qt wallet)
      18. Redesign of the Bitcoin-Qt wallet
        1. Graphical interface addition
      19. Building a Readercoin network
      20. Readercoin mining
      21. Sending transactions
      22. Building a transaction and block explorer
        1. Iquidus setup requirements
        2. Creating Iquidus's database
        3. Installing Iquidus
          1. Iquidus configuration
        4. Syncing databases with the blockchain
        5. Wallet preparation
        6. Starting the explorer
      23. Building an exchange market
    3. Summary
  10. Peer-to-Peer Auctions in Ethereum
    1. Introduction to Ethereum
      1. Ethereum virtual machine and smart contracts
      2. Ether and gas
      3. Your first smart contract
      4. What's a DApp?
        1. DApp architecture
    2. Building an auction DApp 
      1. Auction description
      2. Auction contract in Solidity
      3. Contract code analysis
        1. State variables
          1. Variable state types
          2. Visibility and access modifiers
        2. Enumerations
        3. Arrays
        4. Mappings
        5. Structures
        6. Functions
        7. Modifiers
        8. Condition and error verification
        9. Events
      4. Inheritance
        1. Constructors
        2. Time in Solidity
        3. Special variables and functions
        4. The fallback function
        5. Function overloading
          1. The bidding function
          2. Canceling an auction
          3. Withdrawing bids
        6. Contract destruction
      5. Remix IDE
      6. Bidding form in web3.js
      7. Introduction to the web3.js API
        1. Step 1 – talking to the blockchain
        2. Step 2 – interaction with the smart contract
          1. The ABI
        3. Call versus send transactions
          1. Invoking contract methods via a call
          2. Invoking contract methods via a transaction
        4. Callbacks
        5. Reading state variables
        6. Watching events
          1. Indexing events and filtering
        7. Numbers in Ethereum and floating point
        8. Transaction status receipt
    3. Deployment environment – multiple ways to enjoy!
      1. Option 1 – Ganache
      2. Option 2 – Testnet
        1. Connecting MetaMask to Testnet
      3. Option 3 – private network
        1. Geth installation
        2.  Creating new accounts
        3. Genesis file
        4. Node initialization
        5. Connecting Ethereum nodes
        6. RPC tests
        7. Mining process
        8. Contract deployment
      4. Compiling and deploying contracts using solc
      5. Proof of authority (POA) and difficulty adjustment
        1. Option 1 – editing the Ethereum client code
        2. Option 2 – POW
    4. Running the auction DApp
      1. Contract and transaction cost
        1. How cost is calculated
        2. Deployment cost
        3. Function execution cost
        4. Contract destruction cost
        5. Potential optimization
      2.  Solidity tips and tricks
    5. Summary
  11. Tontine Game with Truffle and Drizzle
    1. Background
      1. Prerequisites
    2. Truffle quick start
      1. Installing Truffle
      2. Saying hello to Truffle
      3. Running Truffle for the first time
        1. Preparing the migration
        2. Configuring Truffle
        3. Compiling the contract
        4. Migrating the contract
        5. Setting up Ganache
    3. The Tontine contract
      1. General structure
        1. UML model
        2. Preparing the contracts
    4. Cplayer as a CRUD contract
      1. Smart contract data location
        1. The CRUD data repository
      2. CRUD – Create
      3. CRUD – Read
        1. Mapping iterations
      4. CRUD – Update
        1. Function overloading
      5. CRUD – Delete
    5. Tontine interfaces – Itontine
    6. Interface implementation – Ctontine contract
      1. Smart contract interaction
        1. Contract instantiation
        2. Contract creation
        3. Reading the storage of a remote contract
        4. Editing the storage of a remote contract
      2. Joining the game
        1. Calling remote functions
          1. Option one – using the ABI
          2. Option two – using low-level calls
          3. Using call to send ether
      3. The ping() method
      4. The eliminate() method
      5. The share_pension() method
        1. Standard integer division
      6. The claimReward method
        1. The this keyword
    7. Truffle unit tests
      1. Preparing the testing environment
      2. Migrating the contracts
        1. Running Ganache
      3. Preparing the tests
      4. Testing addPlayer()
      5. Testing findPlayer()
      6. Testing removePlayer()
      7. Testing Ctontine
      8. Testing a payable function
      9. Testing events
      10. Testing claimReward()
        1. Time manipulation
      11. Running the tests
      12. Testing with Solidity
      13. Debugging with Truffle and Remix
        1. Debugging with Truffle
          1. Spotting the error
          2. Breakpoints and watching values
        2. Debugging with Remix
          1. Attaching the debugger to the code
          2. Watching values
          3. Setting breakpoints
    8. Frontend with Drizzle
      1. Prerequisites
      2. What is the Drizzle box?
      3. Getting started with the Drizzle box
        1. Running the demo
        2. Starting the web UI
      4. Hacking the Drizzle box
      5. The game’s homepage
    9. Trying the DApp
      1. Connecting Ganache to MetaMask
      2. Running the Tontine DApp
      3. Showtime – ready to play?
      4. Getting help
    10. Summary
  12. Blockchain-Based Futures System
    1. Project presentation
    2. Futures smart contract
      1. Blockchain oracles
    3. Web3j
      1. Prerequisites
      2. Setting up the Web3J Maven project
      3. Installing web3j
      4. Wallet creation
    4. Java client 
      1. The wrapper generator
      2. Initializing web3j
      3. Setting up Ethereum accounts
      4. Deploying the contract
      5. Interacting with smart contracts
      6. Calling a contract function
      7.  Calling view methods
      8. Web3j events
      9. Enhancement
    5. Summary
  13. Blockchains in Business
    1. Public versus private and permissioned versus permissionless blockchains
    2. Privacy and anonymity in Ethereum
    3. Why are privacy and anonymity important?
    4. The Ethereum Enterprise Alliance
      1. Ethereum's licensing
    5. Blockchain-as-a-Service
    6. Quorum
      1. Privacy
      2. Higher performance and throughput
      3. Permission and governance
      4. The Quorum client
        1. Quorum Node
        2. Constellation
      5. Our project
        1. Prerequisites
        2. Bringing up the network
        3. Interacting with the network
        4. Testing the network
          1. Deploying the public contract
          2. Deploying the private contract
        5. Permissioning the network
    7. Summary
  14. Creating an ICO
    1. What is an ICO?
    2. Project setup
    3. Token contracts
      1. ERC-20 token standard
        1. Name and symbol (optional)
        2. totalSupply (required)
        3. decimals (optional)
        4. Transfer event (required)
        5. balanceOf (required)
        6. transfer() (required)
        7. The constructor
        8. Delegated transfer
        9. allowance (required)
        10. approve() (required)
        11. Approval event (required)
        12. transferFrom() (required)
        13. The complete token contract
      2. ERC-223 token standard
      3. ERC-721 non-fungible token standard
    4. Token sale contracts
      1. Hard cap
      2. Soft cap
      3. Uncapped
      4. Dutch auction
      5. Reverse dutch auction
      6. Considerations
    5. Implementing our token sale contract
      1. Constructor
      2. Token supply
      3. Buying tokens
      4. Ending the sale
      5. The complete token sale contract
    6. Contract security
      1. Known attack vectors
        1. Integer overflow/underflow
        2. Reentrancy
      2. OpenZeppelin
    7. Testing the code
      1. The public test networks
        1. Ropsten
        2. Rinkeby
        3. Kovan
      2. Migrating the code
      3. Testing our token contract
      4. Testing our token sale contract
    8. Deploying to a test network
      1. Running Geth on Rinkeby
      2. Configuring Truffle to work with Geth
      3. The Truffle console
      4. Provisioning the token sale contract
    9. Verifying our contract code on Etherscan
    10. Creating a frontend website
      1. Setting up the frontend development
        1. Frontend directory structure
        2. index.html
        3. app.js
      2. Running the frontend code
      3. Interacting with the frontend
    11. Summary
    12. Suggestions for further work
  15. Distributed Storage IPFS and Swarm
    1. Background
    2. Swarm and IPFS
      1. Installing IPFS
      2. Installing Swarm
    3. Hosting our frontend
      1. Serving your frontend using IFPS
        1. Using IPNS
      2. Serving your frontend using Swarm
        1. ENS 
    4. IPFS file uploader project
      1. Project setup
      2. The web page
        1. index.html
        2. main.js
    5. Summary
  16. Supply Chain on Hyperledger
    1. Food industry supply chain
    2. Quick Hyperledger overview
      1. Hyperledger Fabric 
        1. End-to-end transaction flow
      2. Hyperledger Composer
    3. Setting up the development environment
      1. Prerequisites 
      2. Installing Hyperledger Fabric
    4. Fabric's Chaincode
      1. Writing Chaincode
      2. The Init function
      3. The Invoke function
        1. Hyperledger data storage
      4. The Query function
        1. Error handling
    5. Building a business network
      1. Privacy in Hyperledger 
      2. Define services in a compose file
      3. Resources and profiles
      4. Fabric channel and Genesis block 
      5. Generate peers and orderer certificates
      6. Start the Docker containers
      7. Join the channel and install Chaincode
    6. Chaincode compilation and deployment
      1. Interacting with Chaincode
    7. Running the project
      1. Interacting over REST API/JSON-RPC
        1. Setting up the development environment
        2. Register users
        3. Querying the Chaincode  from UI
      2. Hyperledger Composer
        1. Get Hyperledger Composer and Playground
        2. Composer Playground
    8. Summary
  17. Letter of Credit (LC) Hyperledger
    1. LC concepts and design
    2. Development environment
      1. Setting up the IDE
      2. Getting Hyperledger Fabric running
    3. Creating a composer Fabric application
      1. Creating our first business network using Hyperledger Composer
        1. Models definition
        2. JavaScript transaction logic
        3. Access control definition
    4. LC business network
      1. Initial LC model
      2. Participant onboarding
      3. Initial agreement
      4. LC request
      5. LC approval
      6. LC advising
      7. Goods shipping
      8. Present document
      9. Document delivery
      10. Debit payment
      11. Payment transfer
      12. Pay the seller
      13. LC closure
    5. Deploying the LC
      1. Deploying business network
      2. Generating a REST server
      3. Testing LC project
        1. Participant onboarding
        2. Initial agreement
        3. LC request
        4. LC approval
        5. LC advising
        6. Goods shipping
        7. Present document
        8. Deliver document
        9. Debit payment
        10. Payment transfer
        11. Pay the seller
        12. LC closure
    6. Hyperledger Fabric on IBM Cloud
    7. Summary
  18. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Blockchain By Example
  • Author(s): Bellaj Badr, Richard Horrocks, Xun Wu
  • Release date: November 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781788475686