Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learn Blockchain Programming with JavaScript

You're reading from   Learn Blockchain Programming with JavaScript Build your very own Blockchain and decentralized network with JavaScript and Node.js

Arrow left icon
Product type Paperback
Published in Nov 2018
Publisher Packt
ISBN-13 9781789618822
Length 252 pages
Edition 1st Edition
Languages
Concepts
Arrow right icon
Author (1):
Arrow left icon
Eric Traub Eric Traub
Author Profile Icon Eric Traub
Eric Traub
Arrow right icon
View More author details
Toc

Table of Contents (10) Chapters Close

Preface 1. Setting up the Project 2. Building a Blockchain FREE CHAPTER 3. Accessing the Blockchain through an API 4. Creating a Decentralized Blockchain Network 5. Synchronizing the Network 6. Consensus Algorithms 7. Block Explorer 8. In conclusion... 9. Other Books You May Enjoy

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: https://github.com/PacktPublishing/Learn-Blockchain-Programming-with-JavaScript. 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.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime