Is it hard to build a blockchain from scratch?

Sinai Nday
Level Up Coding
Published in
4 min readJan 16, 2021

--

Episode 2 :
In Javascript

Website : https://open-blockchain-project.com/

Improved Golang code (Frontend+backend) : https://github.com/ndaysinaiK/baby-blockchain

This article is the continuation of the previous one, make sure you read the first episode that can be found here.

In episode 1, we talked about blockchain concepts and built a baby prototype, in this episode We are going to implement the same concepts using a different programming language called javascript. I know it may be tough to code in Go, hope you are happy now😉.

So to refer to the figure We drew in episode 1, let’s just copy and paste it here, thanks, Larry Tesler.

by Sinai Nday

The same mechanism will be applied using javascript, yay! one of the top used programming languages.

To make it possible We will need a server that can run our javascript code, We can use a web browser, but let’s do things professionally.

Requirements:

  • Nodejs: Runtime environment that executes JavaScript code outside a web browser. install it and try to set up a project, you can follow the steps here.
  • Express: A nodejs middleware web application, we will use this later, but let’s install it first.
  • Nodemon: A tool that helps develop node.js based applications by automatically restarting the node application when the files are modified
  • Bcrypt: A library for fast encryption, you can also use any hash function you want.

let’s get started:

  • Create a folder called javascript and add a file called entry.js
  • In the folder type npm init to initialize the project, fill in all the requirements, for the entry point enter entry.js
  • Open the terminal and type npm i --save-dev nodemon to install the nodemon tool.
  • Run npm i express to install express JS too.
  • Install bcrypt npm i bcrypt

After all my package.json looks like this:

And the folder structure looks like this :

Open the terminal and head to the javascript folder, type npm run start don’t mind if you see an error it’s because there is nothing in theentry.js file.

Now We are ready to start coding our blockchain. Open the entry.js file in any IDE and write this code to understand it, skip the comment:

Done typing?

Here are some explanations:

In the code above We created a Block class that contains an id, timestamp, hash, previous hash, and data attributes. To use that class in the future We created a constructor and added a method for hash generation.

Since a blockchain is a set of blocks, We created another class called Blockchain to store all the blocks, it’s just a contractor with an array in javascript, then We added a method AddBlock that will add a block to our chain.

Finally, We initialized our chain and tested it by emitting 3 different transactions.

Result:

If you installed nodemon, just check the terminal where you ran it, you will see the whole blockchain information.

Final result: by Sinai Nday

Congratulations! it’s very simple in javascript, We made it just in a few lines of code.

Do you know you can click 50 times the medium like button? so go ahead and smash it if you liked this post.👍

The whole code:

Python version here

“A Blockchain Engineer is a professional who understands thoroughly the principles of blockchain, security, and software engineering for designing, developing, maintaining, testing and evaluating the blockchain core engines and software whereas Blockchain Developer is a professional who builds software on top of the blockchain called decentralized applications. Most blockchain developers use open blockchain platforms and frameworks like Ethereum, hyperledger fabric, EOS, etc…”

Which one do you want to be?

In case you find any typos or error when running the code, reach out to me:

LinkedIn: Sinai Nday

Some coffee will be much appreciated😉

With Paypal

--

--