This tutorial will explain you to build a blockchain which returns mining rewards. How To Code a Bitcoin “like” Blockchain In JavaScript
As we like simplicity, we will only use three different files:
- app.js: contains the Express API for interacting with our blockchain.
- blockchain.js: contains the classes for constructing our blockchain, blocks, and transactions.
- package.json : contains our installed npm packages.
The most important package we will use is crypto-js. This package contains crypto standards that can be used in JavaScript, like calculating our hash. In my opinion, a hash is the glue between two blocks. A lot of other packages can be found in this package.json file, mainly for compiling JavaScript ES7.
Let’s take a look at the different classes.
Block Structure
A block consists of several properties. First, we pass a timestamp to indicate the moment the block got created. Next parameter is an array of transactions ready to be mined. At last, we define a field that contains the link to the previous block using a hash. This field is optional as the genesis block doesn’t have a predecessor.
class Block { constructor(timestamp, transactions, previousHash = '') { this.previousHash = previousHash; this.timestamp = timestamp; this.transactions = transactions; this.hash = this.calculateHash(); this.nonce = 0; } … }
As you can see, the block contains a hash parameter which will be crucial for our proof-of-work algorithm. A hash can be best described as a function that creates a unique output for an input by performing mathematical operations on the input. Unlike an encryption method, you cannot derive the input based on the output of the hash function. Whenever we find the correct hash for a particular block, it will be stored inside the block as it forms our chain.
The nonce parameter is very important for our proof-of-work algorithm. The chance of finding the correct hash is very low. Each time an incorrect hash has been calculated, the nonce gets increased. Why? If we do not increase this nonce, we will be calculating the same hash over and over again. As we are altering the nonce whenever we find an incorrect hash, we will calculate a different hash each time. This is an intensive calculation (mining) process, and that’s why it’s called proof-of-work.
Block Hash
The hash is calculated using the sha256() functionality offered by the crypto-js package. We simply take the previous hash, timestamp, pending transactions and nonce as input for the creation of a new hash.
calculateHash() { return SHA256(this.previousHash + this.timestamp + JSON.stringify(this.transactions) + this.nonce).toString(); }
Build Blockchain
We are ready to start building our blockchain. Our blockchain is created using an empty constructor. This constructor sets some important parameters like our genesis block, the mining difficulty, any pending transactions and the mining reward for mining a new block. We will explain the exact use of the mining difficulty later in this article.
class Blockchain{ constructor(genesisNode) { this.chain = [this.createGenesisBlock()]; this.nodes = [+genesisNode] this.difficulty = 4; this.pendingTransactions = []; this.miningReward = 100; } … }
Source/More: How To Code a Bitcoin “like” Blockchain In JavaScript – Blockgeeks