This article is targeted to the users who wish to know the basics of the Blockchain technology. It does also address the 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 gained its popularity from cryptocurrency transactions. Every one or the other person is investing in cryptocurrency but there are still many of them who haven’t heard of the term Blockchain. So, what exactly Blockchain and Blockchain development are?
What is Blockchain?
A chain of blocks is termed as Blockchain, which contains particular information. Each block contains data, hash, and hash of the previous block. The data stored in the block depends on the type of 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 record 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 for 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 the previous block. The first block’s previous hash is 0000 which means it is not pointing to any previous block and this block is called a 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.
Also Read: How to integrate D-Apps with Blockchain Ethereum?
Principles of Blockchain:
- Decentralization – Blockchain uses peer to peer networks, where a single user doesn’t control the transaction but power is distributed among all the users. You can also create a Decentralized Application for Secure and High Performing Transactions.
- 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, access to the Blockchain is only for those who have got 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 access to particular users only.
- Consortium or Federated Blockchain: In these 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 a 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 previous hash is a string that states the hash of the previous block.
The blocks are connected to each other through hashes so we need to calculate the 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 a 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 a new blocks and accessing the latest block.
The above function returns the last block.
The addBlock function is used to add a new block in the blockchain.
Now, let’s implement the above code blocks and how can we use the above functions:
QSS has a proven track executing enterprise level web and mobile applications for its esteemedcustomers. To Know More...