This article is targeted to the users who wish to know the basics of the Blockchain technology. It does also address basic concepts of Crypto-Currencies. Blockchain will be one of the most powerful technologies in the future as people nowadays are more interested in investing in digital currencies.
Blockchain has gain its popularity from crypto currency transactions. Every one or the other person is investing in crypto currency but there are still many of them who haven’t heard of the term Blockchain. So, what exactly Blockchain and Blockchain development is?
What is a Blockchain?
A chain of blocks is termed as Blockchain, which contains particular information. Each block contains a data, hash and hash of previous block. The data stored in the block depends on the type of the Blockchain. For example, if there is a Blockchain of cryptocurrency then data will contain the information of the sender, receiver and the amount of the coins. It is a distributed ledger of all the transactions that records all the exchanges of data in a decentralized manner. Now what distributed ledger is?
A distributed ledger is a ledger of any transactions supported by a decentralized network across different locations and people, eliminating the need of a central authority. Blockchain is a distributed ledger that is completely open to anyone. Once data has been recorded, it becomes very difficult to change it. So, how does that work? Let’s take an example of 3 blocks:
Here we have a chain of 3 blocks, each block having hash and hash of previous block. The first block’s previous hash is 0000 that means it is not pointing to any previous block and this block is called as Genesis block. If we want to add any block, we will add it after the third block and the chain gets updated periodically after every 10 minutes. The access to Blockchain data is public i.e. accessible to anyone on the internet.
Principles of Blockchain:
- Decentralization – Blockchain uses peer to peer network, where single user doesn’t control the transaction but power is distributed among all the users.
- Cryptography – Blockchain uses the power of cryptography to ensure its users a high level of security and authenticity.
Types of Blockchain :
There are 3 types of Blockchain :
- Public Blockchain: Anyone can participate in this type of Blockchain i.e. these types of Blockchain are open and transparent; anyone can do anything at a given point of time. Example : Bitcoin, Litecoin, etc.
- Private Blockchain: In this type of Blockchain, the access to the Blockchain is only for those who has got the permission. In public type, there is no in-charge so anyone can access or edit it. But, in private, there is an in-charge who looks after every transaction and gives the access to particular users only.
- Consortium or Federated Blockchain: In this types of Blockchain, instead of one in-charge, it has multiple in-charge. Basically, it has a group of companies or representative individuals coming together and making decisions for the best benefit of the whole network. Such groups are also called consortiums or a federation that’s why the name of that Blockchain is consortium or federated Blockchain.
Let’s start it with creating a Block first. In Block class, you need to add constructor and pass timestamp, data and the hash of the last block :
The timestamp states when the block was created, data can be any data you want to add in the block and previousHash is string that states the hash of the previous block.
The blocks are connected to each other through hashes so we need to calculate hash code of each block. The hash code of a block can be calculated by taking all of its contents and passing it through a hash function :
In the above code, I have used SHA256 to calculate the hash of the block. This function is not available initially so we need to install it using command prompt:
npm install crypto-js
Then we need to import it in our main.js using following :
Next we need to use calculateHash() function in the constructor of the Block
After defining the Block class, we need to define Blockchain:
Blockchain is a simple object that only contains chain as a property. First of all, we need to create a Genesis block which is the first block of the Blockchain. We need a different function to create a genesis block:
Now we need to add createGenesisBlock() in our Blockchain constructor :
Now we need to add more methods that can allow us to do the basic functionality of the Blockchain, for example adding new block and accessing the latest block.
The above function returns the last block.
The addBlock function is used to add new block in the block chain.
Now, let’s implement the above code blocks and how can we use the above functions: