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

Learn Blockchain Programming with JavaScript

Book Description

Explore the essentials of blockchain technology with JavaScript to develop highly secure bitcoin-like applications

Key Features

  • Develop bitcoin and blockchain-based cryptocurrencies using JavaScript
  • Create secure and high-performant blockchain networks
  • Build custom APIs and decentralized networks to host blockchain applications

Book Description

Learn Blockchain Programming with JavaScript begins by giving you a clear understanding of what blockchain technology is. You'll then set up an environment to build your very own blockchain and you'll add various functionalities to it. By adding functionalities to your blockchain such as the ability to mine new blocks, create transactions, and secure your blockchain through a proof-of-work you'll gain an in-depth understanding of how blockchain technology functions.

As you make your way through the chapters, you'll learn how to build an API server to interact with your blockchain and how to host your blockchain on a decentralized network. You'll also build a consensus algorithm and use it to verify data and keep the entire blockchain network synchronized. In the concluding chapters, you'll finish building your blockchain prototype and gain a thorough understanding of why blockchain technology is so secure and valuable.

By the end of this book, you'll understand how decentralized blockchain networks function and why decentralization is such an important feature for securing a blockchain.

What you will learn

  • Gain an in-depth understanding of blockchain and the environment setup
  • Create your very own decentralized blockchain network from scratch
  • Build and test the various endpoints necessary to create a decentralized network
  • Learn about proof-of-work and the hashing algorithm used to secure data
  • Mine new blocks, create new transactions, and store the transactions in blocks
  • Explore the consensus algorithm and use it to synchronize the blockchain network

Who this book is for

Learn Blockchain Programming with JavaScript is for JavaScript developers who wish to learn about blockchain programming or build their own blockchain using JavaScript frameworks.

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. Learn Blockchain Programming with JavaScript
  3. About Packt
    1. Why subscribe?
    2. Packt.com
  4. Contributors
    1. About the author
    2. 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. Conventions used
    4. Get in touch
      1. Reviews
  6. Setting up the Project
    1. What is a blockchain?
    2. What you will learn...
    3. Environment setup
      1. Project source code
    4. Summary
  7. Building a Blockchain
    1. Before we get building...
      1. An explanation of the JavaScript constructor function 
      2. Explanation of the prototype object
    2. Blockchain constructor function
    3. Building the createNewBlock method
      1. Testing the createNewBlock method
        1. Testing the Blockchain constructor function
        2. Testing the createNewBlock method
    4. Building the getLastBlock method
    5. Creating the createNewTransaction method
      1. Testing the createNewTransaction method
        1. Adding a pending transaction to our blockchain 
    6.  Hashing the data
      1. Understanding the SHA256 hashing function
      2. The hashBlock method
      3. Testing the hashBlock method
    7. What is a Proof of Work?
      1. Creating the proofOfWork method
      2. Testing the proofOfWork method
    8. Creating a genesis block
      1. Testing the genesis block
    9. Summary
  8. Accessing the Blockchain through an API
    1. Setting up Express.js
      1. Installing Express.js
      2. Using Express.js
    2. Building the API foundation
    3. Installing Postman and body-parser
      1. Installing Postman 
    4. Building the /blockchain endpoint
      1. Testing the /blockchain endpoint
    5. Building the /transaction endpoint
      1. Testing the /transaction endpoint
    6. Building the /mine endpoint
    7. Testing the endpoints
      1. /mine endpoint testing 
      2. Testing the / blockchain endpoint
      3. Testing the /transaction endpoint
    8. Summary
  9. Creating a Decentralized Blockchain Network
    1. Creating multiple nodes
      1. Running multiple instances of networkNode.js
      2. Testing the multiple nodes
    2. Adding the currentNodeUrl
    3. New endpoints outline
      1. Defining the /register-and-broadcast-node endpoint
      2. Creating the /register-node endpoint 
        1. The difference between register-and-broadcast-node and register-node endpoint
      3. Defining the /register-nodes-bulk endpoint 
      4. Understanding how all of the endpoints work together 
    4. Building  the /register-and-broadcast-node endpoint 
      1. Continuing to work on the /register-and-broadcast-node endpoint 
        1. A quick recap of how the register-and-broadcast-node endpoint functions 
    5. Building the  /register-node endpoint
      1. Testing the  /register-node endpoint
        1. Installing the request library
      2. Endpoint testing
    6. Building the /register-nodes-bulk endpoint
      1. Testing the /register-nodes-bulk endpoint
    7. Testing all of the network endpoints
    8. Summary
  10. Synchronizing the Network
    1. Understanding the need to synchronize the network 
    2. Refactoring the createNewTransaction method and the /transaction endpoint
      1. Modifications to the createNewTransaction method
      2. Building the addTransactionToPendingTransactions method 
    3. Building the /transaction/broadcast endpoint
    4. Refactoring the /transaction endpoint
    5. Testing the transaction endpoints
      1. Recapping how to create the network 
      2. Testing the transaction endpoints
    6. Updating the mining information
      1. The updated mining process
      2. Refactoring the /mine endpoint
    7. Building the /receive-new-block endpoint
    8. Testing the new and updated /mine endpoints
    9. Summary
  11. Consensus Algorithms
    1. What is the consensus algorithm ?
    2. Building the chainIsValid method
      1. Testing the chainIsValid method
        1. Verifying the output of the test
      2. A tiny modification to verify the results properly
    3. Building the /consensus endpoint
      1. Quick review of the build process
      2. Testing the /consensus endpoint
    4. Summary
  12. Block Explorer
    1. What is a block explorer? 
    2. Defining the block explorer endpoints 
    3. Building the getBlock method
    4. Building the /block/:blockHash endpoint
      1. Testing the /block/:blockHash endpoint 
    5. Defining the getTransaction method
    6. Building the /transaction/:transactionId endpoint
      1. Testing the /transaction/:transactionId endpoint
        1. Adding new transactions and blocks to the blockchain 
        2. Testing the endpoint 
    7. Building the getAddressData method
      1. Knowing the balance 
    8. Developing the /address/:address endpoint
      1. Testing the /address/:address endpoint
    9. Adding the block explorer file
      1. Building the /block-explorer endpoint
    10. Block explorer file explanation
    11. Testing our block explorer
    12. Summary 
  13. In conclusion...
    1. A quick review
    2. Areas of improvement
  14. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think