Book Image

Learn Blockchain Programming with JavaScript

By : Eric Traub
Book Image

Learn Blockchain Programming with JavaScript

By: Eric Traub

Overview of this book

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.
Table of Contents (10 chapters)

Environment setup

Let's get started with building our blockchain project. The first thing we're going to do is open our terminal and create our blockchain directory by typing commands into the terminal, as seen in the following screenshot:

Let's begin by creating a folder called programs. Inside this folder, let's create a directory called blockchain. This directory is currently empty. Inside of this blockchain directory is where we're going to be doing all of our programming. We are going to be building our entire blockchain inside of this blockchain directory.

Now our blockchain directory is ready, and the first thing that we need to do is to add some folders and files into it. The first folder that we want to put into the directory will be called dev, so we want to make sure that we are inside of the blockchain directory, and then let's type the following command into the terminal:

mkdir dev

Inside this dev directory is where we are going to be doing most of our coding. This is where we're going to build our blockchain data structure and create our API to interact with our blockchain, test it, and fulfill other similar tasks. Next, inside this dev folder, let's create two files: blockchain.js and test.js. To do this, enter the following command:

cd dev
touch blockchain.js test.js

The touch term in the preceding command line will help us in creating the mentioned files. The blockchain.js file is where we will type our code to create the blockchain and the test.js file is where we will write code to test our blockchain.

Next, let's return back to our blockchain directory by typing the following command in the terminal:

cd .. 

In the blockchain directory, let's run the following command to create the npm project:

npm init 

After running the preceding command, you will get some options on your terminal. To set up the project, you can just press Enter through those options.

So, this is pretty much all we need to do in order to set up our project folder structure. Now, if you go to our blockchain directory and open it with a text editor such as Sublime or Atom (or whatever you would like), you will get to see the file structure, as seen in the following screenshot:

The blockchain directory consists of the dev folder that we just created. Inside the dev folder, we can observe our blockchain.js and test.js files. Also, when we run the npm init command, it creates the package.json file for us. This .json file will keep track of our project and any dependencies that we need, allowing us to run scripts. We'll be working more inside of this package.json file in further chapters, so you'll become more familiar with it as we progress through the book.

Project source code

Before we start coding our blockchain, it is worth noting that the entire source code for this book can be found on GitHub at the following link: In this repository, you'll find the completed code for the entire project, and you will also be able to explore all of the files that we will be building in further chapters. Therefore, this may be a good resource for you to use as you make your way through the book.