How to build a Blockchain in Javascript

In this tutorial, we’re gonna look at Blockchain concept and way to build a Blockchain using Javascript.

Next Post: How to create Blockchain API in Javascript

Blockchain Overview

In simplest term, a Blockchain is an immutable distributed ledger.

What is a ledger?
>> A ledger is simply a collection of financial accounts or transactions which records transactions that people have made.

How about ‘immutable’?
>> Immutable means that this ledger cannot be changed anyway. Once the transactions are recorded, they can never be undone or altered. Nobody can ever change the amount of money that was sent or which people took part in the transaction.

Last, ‘distributed’ means that this ledger is not controlled by a single entity.
>> Instead it is independently constructed and held by many nodes. For example, Bitcoin are supported and hosted by thousands of everyday computer across the whole world.
This is a great benefit of Blockchain technology because we don’t have to trust any single company with our data. Our data is persisted by the entire network of thousands of different entities which are all acting independently, and each entity has the exact same copy of the ledger which is hosted and synchronized across the entire network.

There are many ways that Blockchain technology can be applied to our world today to make certain industries more secure efficient and trustworthy such as financial services, healthcare, credit, energy…

Build a Blockchain

Setup environment
Install Nodejs

– install LTS version of Node from NodeJs Installer.
– verify you have everything installed correctly by command: npm --version and node --version.

Setup Project folder tree

Under Blockchain project folder, create src and test folder with blockchain.js file inside:

build-blockchain-javascript-setup-folder-tree

Initialize Nodejs runtime

Point command prompt to project folfer, run cmd:
npm init

Install SHA256 module

Run cmd:
npm install sha256

Now our Blockchain project structure is like this:

build-blockchain-javascript-project-folder-tree

Block data structure

Block is the important part of our Blockchain. This is data structure of the item which will be connected in a chain inside our Blockchain later. All data that we need is stored inside Block objects.

Block object has:
index property and this will be basically the block number in our chain.
timestamp that indicates when this block was created.
transactions: when we create a new block, we want to store all of the new transactions that have just been created. These transactions will be inside of block in a chain so that they can never be changed.
nonce: this is like a tiny info added to the contents of a block that makes hash output of the contents of the block will change. Miners must find a nonce value that, when put into the hashing algorithm, generates an output that meets the requirements, in this case, a certain number of leading zeros (for example, out must be start with 5-zeros: ‘00000fc87…e0’).
hash: a single string that all of our transactions and some info are going to be basically compressed into.
prevBlockHash: the hash data from previous block.

Blockchain Object

Blockchain class has ability to be instantiated, create new Block, access the latest Block information, make new Transaction, hash Block, run Proof Of Work, and has chain of Blocks along with pending Transactions that should be stored when new Block is created.

Constructor

chain: this array is where all Blocks that we mine will be stored in as a chain.
transactions: where we will hold all of the new transactions that are created before they are placed into a new Block.
– We also create a Genesis Block in the constructor function.

So what is a Genesis Block?
>> Genesis Block is quite simply the first Block in a Blockchain. Every Blockchain needs to start with one block and we want this to happen right when the Blockchain is created.

The createNewBlock() method has a nonce, a prevBlockHash and a hash as parameters.
We don’t have any prevBlockHash and we haven’t done any Proof Of Work. So to create this Block, we simply pass in some arbitrary parameters like below (or anything we like):
+ nonce: 100
+ prevBlockHash: ‘0’
+ hash: ‘Genesis block’

Create new Block

This method takes 3 parameters: nonce, prevBlockHash and hash.
– First, we create a new Block object.
– Next, we set pendingTransactions equal to an empty array. This is because once we create a new Block, we are putting all of the new transactions into this Block. Clearing the entire transactions array helps us start over for the next Block.
– Then we push new Block into our chain.
– Finally we return the new Block.

Get latest Block

Make new Transaction

This method takes 3 parameters:
+ amount: indicates how much is being sent in this transaction.
+ sender: sender’s address.
+ recipient: recipient’s address.

– First, we create a new transaction object from these parameters.
– Next, we push this transaction into our pendingtransactions array.
Our Blockchain has a lot of people who are making a lot of different transactions.
Every time a new transaction is created, it’s going to be pushed into our new transactions array.
But all of the transactions in this array are not really set in the ledger. They’re not really recorded into our Blockchain yet, it just happens when a new Block is created.
>> So all of these new transactions are just pending transactions and they have not been validated.

– Finally, we return the number of the Block that this transaction will be added to.

Hash Block

Inside of this method, we use SHA256 hashing to hash our Block data.

Proof of Work

Proof of Work is a very important method essential to Blockchain technology. It is one of the reasons that Bitcoin and many other Blockchains are so secured.

So, what is Proof of Work?
If we take a look at our Blockchain, we can see that every Blockchain is pretty much a list of Blocks.
Every single Block has to be created and added to the chain. But it is not enough.

We want to make sure that every Block that is added to the chain is legitimate, it must have the correct transactions and the correct data inside. People could fake how much Bitcoin they have and essentially cause fraud and steal money from other people.

>> Proof of Work helps us do this.
Everytime we create a new Block, we first have to make sure that it is a legitimate block by mining it through Proof of Work.

proofOfWork() method will take in the prevBlockHash and the currentBlockData.
From this data, it is going to try to generate a specific hash.

This hash, in our case, is going to be a hash that starts with 2 zeros ('00').
The hash that is generated from SHA256 is random. So how to make this posible?
>> The only way we can do is by running our hashBlock() method many many times until we get a hash that starts with 2 zeros.

Now, how does this proof of work method actually secure the black chain?
>> The reason that this proofOfWork() method will secure our Blockchain is because in order to generate the correct hash, we have to run hashBlock() method many many times, and it is going to use a lot of computing power and energy.

On top of that, not only our hashBlock() method take in the currentBlockData, but it also takes in the prevBlockHash. This means that all of the Blocks in our Blockchain are linked together by their data.
>> So if someone tries to go back and remine or recreate a Block that already exists, he will also have to remind and recreates every single Block that comes after the first one they recreated. This would take an incredible amount of calculations and energy. It is not feasible for a well-developed Blockchain.

Even if he can change all Blocks, it would require massive amounts of computing power to access every instance (or at least a 51% majority) of a the Blockchain and alter them all at the same time. The bigger your network is, the more secure Blockchain will be.

Run Blockchain

test.blockchain.js

Run cmd: node test/blockchain.js.
Result in Console:

Source Code

Blockchain

By grokonez | May 30, 2018.



Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*