To be able to do one of the most common functions done in Bitcoin and blockchain technology –Hashing, you will need to use a cryptography library first. The crypto library that is needed is the crypto-js library.
Now you have a class “Block” which will allow you to easily create countless Block instances in your blockchain. It’s time to create a constructor to allow you to easily and dynamically create your Blocks.
The index will be a number that you assign each block, consecutively. You will see many block explorers call this “block height” when referencing a particular block. They’re referring to the index value.
The third step is the hypothetical value you are plugging in here – txHash. In Bitcoin, block headers hold the “Merkle Root” which is a hash of all the transactions in that block. So you will at least provide the hash to represent this later in the program.
The ‘previousHash’ parameter is the link that creates the “chain” to the block-chain. Each block holds a hash of the previous block. So whenever creating a new block, you want to create a pass in the hash of the last block as an argument. He can run the hash function in-line in the constructor, so therefore, you just need to pass in the block.
With your blocks properly defined, you can create a simple function that you can call on to generate the next block. Now you just have to initialize the blockchain array and create your very first block: The Genesis Block.
For the purposes of readability, you have to put some formatting in there. And there it is, the pocket sized blockchain that creates a sequence of blocks with bare bone structure in less than 30 lines of code!