Building a Blockchain using JavaScript
--
A Blockchain on its basic concept refers to a distributed database that maintains a continuously growing list of ordered records that anyone can read but that are immutable.
Once a block has been added to the chain, it cannot be changed anymore without invalidating the rest of the chain.
Building a Block
The structure of the blocks must be defined, the images below shows an example of how the structure should look like. It is important to ensure the data integrity for each block, this is why each block contains a hash that is computed based on its contents. It also contains the hash of the previous block.
In JavaScript this is how our block class will look like:
Block Hash
Remember that to keep the integrity of the data, the block needs to be hashed and included in a particular field on the block. We are using the crypto-js library and this is the “calculateHash” function:
Generating a Block
In order to generate a block, the hash of the previous block must be know and the data should be provided. The addBlock() is responsible for adding a new block to our chain. To do that we add the hash of the previous block to our new block. That way we preserve the integrity of the chain. Because we changed the contents of our new block, we need to recalculate it’s hash. When that’s done, I push the block onto the chain (array).
getLatestBlock() method
This is to return the latest block on our blockchain
The Genesis Block
To initialize our chain array with values for the genesis block:
Validate the Chain
To make sure that nobody altered the blockchain. It loops over all the blocks and checks if the hash of each block is correct. It also checks if each block points to the correct previous block by comparing the previousHash value. If everything checks out it returns true and if something is wrong it returns false.
Building the chain
Now we can start chaining blocks together in a Blockchain class! This class will include the mentioned functions and the constructor.
In the constructor we initialize the chain by creating an array that contains the genesis block. The first block is special because it cannot point to a previous block.
Running the application
First you can add some block to the chain with the following code after initializing the app:
To see the Blockchain data you can execute this:
*note that is being JSON styled for easy reading:
from the terminal run:
node <javascript_file.js>
You can check if the chain is valid by calling the isChainValid() function. Also, you can change the data like in the next 2 code lines, where we are changing the block data value and even recalculating the blockchain. Check the results when you run the app again
Conclusion
While this implementation is far from complete. It does demonstrate how a blockchain works and clearly explain the basic concepts of a blockchain and how those are easy to understand and implement.
The complete project code can be found in the following Git repository:
https://github.com/juliomacr/blockchain-demo
If you want to learn more about Blockchain development, the SalsaMobi education efforts on Blockchain related technologies may be a good start point, also you can subscribe here and we will notified you when we create more articles or tutorials.
Julio Marin is an advocate for the research and implementation of new technologies and their use on day to day life. Research areas include multimedia development and delivery, cryptocurrencies, mobile development and marketing automation. Currently works in SalsaMobi as Senior Curriculum Developer and Blockchain Development Team Lead.