Complete Basic Blockchain Development in Javascript

complete basic blockchain development in javascript


Complete Basic Blockchain Development in Javascript

We’re going to write a very basic application for Blockchain development in JavaScript, it won’t be anything too fancy but it will be just enough so you can understand how a blockchain actually works. We called this Blockchain a “devprofessorCoin”.

Install node.js  and open bash terminal in your project directory and write the following command to generate crypto-js library, we will use this because we need SHA256 as a hash function and JavaScript doesn’t provide it by default.


Now create a file called main.js  and add the following code in it. (Read comments for explanation)

Now write this below code to create blocks

Now go to your bash command prompt and write the following command to run you code.


Our block chain is the object, it contains a property “chain” which is an array and contains all of the blocks on our chain and you can see that each block references the previous block.

Blockchains are great because once a block is added it cannot be changed without invalidating the rest of the chain. We have created a function to validate our blocks called isChainValid()

Now let’s try to check if our blocks are valid.


Now of course if you detect that a new block broke your chain or if something is wrong with your chain then you should have a mechanism that rolls back the changes and then puts your block chain back in a correct state.

It’s also lacks many features such as proof of work or appear to be network to communicate with other miners and it even doesn’t check if you had enough funds to make a transaction so there are lots of limitations to my little Blockchain right here but it perfectly demonstrates how a Blockchain works behind the scenes.

Read More: What is Blockchain Technology?

Read More: Building Blockchain using PHP

Proof of work

Right now we can create new blocks really quickly. All we have to do is create a transaction, compute its hash and add it to an array. Now modern computers can do that incredibly quickly but we don’t want people to create hundreds of thousands of blocks per second and spam our blockchain.

There’s also a security issue, you can change the contents of a block and then simply recalculate the hashes for all the blocks after that and you will end up with a valid chain – even though you tampered with it and obviously that’s not what we want.

To solve these issues block chains have something that is called PROOF OF WORK. With this mechanism you have to prove that you’ve put a lot of computing power into making a block. This process is also called MINING so how can we implement something like this? While Bitcoin for example requires the hash of a block to begin with a certain amount of zeros and because you cannot influence the output of a hash function, you simply have to try a lot of combinations and hope you get lucky with the hash that has a sufficient number of zeroes in front of it, this requires a lot of computing power, this is also called the difficulty and it is sent so that there is a steady amount of new blocks. In Bitcoin’s case the aim is to create one new block every ten minutes, as computers get faster over time they will require less time to mine a new block, to compensate for that the difficulty will simply be increased.

Lets change our code (Follow comments for details)


As you can see the hashes of our blocks now start with two zeros and that is because we set the difficulty to 2. However you also saw that it happened really quickly, a spammer could still easily generate many fake blocks or someone could tamper with our entire chain so to counteract this we will increase the difficulty to 4 and run it again.


You can see that it already takes a lot longer to mine our blocks and our blocks now start with four zeros instead of two so using this mechanism we can control how fast new blocks can be added to our block chain.

Continue Reading
You may also like...

I am a Sr Android App, Web Developer & Blockchain Project Manager having 5 plus years of extensive hands-on experience in Object Oriented Programming, Database Design and Implementation using Agile & UML. Pro in Yii2 PHP Framework, Android App Development, MYSQL, EOS & Etherium Blockchain & Smart Contract development.

Click to comment

Leave a Reply

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

More in Blockchain

Recent Posts


To Top