In this video tutorial, I will be setting up the mining part of the blockchain with the hashing algorithm SHA256 which is also used by BitCoin.
What Will I Learn?
- Make use of js-sha256 library
- Create a basic mining with SHA256 library
Requirements
- Basic understanding of BlockChain
- Understanding of Typescript
- Understanding basic of Object Oriented Programming and Data Structure
Difficulty
Advanced
Description
The project started with the branch: blockchain-1 and end with branch: blockchain-2
1: Install js-sha256 library
First, install js-sha256
library with npm install --save js-sha256
or yarn add js-sha256
Using js-sha256 library
You can try out js-sha256
library in Node REPL.
> node
> let sha256 = require('js-sha256');
> console.log(sha256('abc'));
Create typings
in types.d.ts
, declare a module for js-sha256, since this library is not in typings
// js-sha256
declare module 'js-sha256' {
function sha256(data: string): string;
export default sha256;
}
2: Add in more function for blockchain class
get previous block
This function will get the previous block
public getPreviousBlock(): BlockData {
return this.blocks[this.blocks.length - 1];
}
get next block
This function will get the next block. However, this step is a bit tricky because in order to get next block, it involves of mining.
Before implementing this method, we need a generateHash function.
public generateHash(block: BlockData): string {
let hash = sha256(block.key);
// mining
while(!hash.startsWith('7a7')) {
block.nonce += 1;
hash = sha256(block.key);
console.log(hash);
}
return hash;
}
This function expect to pass in a BlockData and return a string. The line while(!hash.startsWith('7a7'))
stated that if the hash starts with '7a7', then the mining is complete.
The generated hash will be started with '7a7'.
The hash generated is always the same, therefore a small change in the data or transaction will make different hashes, hence making that transaction to be invalid.
Then, we implement generateHash
into getNextBlock
public getNextBlock(transactions: TransactionData[]) {
let block = new Block();
transactions.map(function(t: TransactionData) {
block.addTransaction(t);
})
let previousBlock = this.getPreviousBlock();
block.index = this.blocks.length;
block.previousHash = previousBlock.hash;
block.hash = this.generateHash(block);
return block;
}
In order to get the next block, we add in transaction, index and previous hash to the block, and then generateHash
will generate hash for the current block.
The next step after generate the block, is to save the block with addBlock
, which I will explain later.
3: Testing out the code
In app.ts
file, we will test out the code we had written. The current setting for this blockchain is 1 transaction per block.
First, import all the necessary classes.
import Blockchain from './blockchain';
import Block from './block';
import Transaction from './transaction';
Then, create a genesis block (first block) by declaring new Block class.
// create a genesis block
let b = new Block();
Next, Initialize Blockchain class by passing in the block.
// initialize blockchain with genesis block
let bc = new Blockchain(b);
Now, create a transaction by passing in 'from', 'to', 'amount' parameters into Transaction class.
// create a transaction
let t = new Transaction('me', 'you', 7);
Last, generate new block with the Blockchain.
// mining part
let newB = bc.getNextBlock([t]);
bc.addBlock(newB);
You can check the transaction of the blockchain in the second block.
console.log(bc.blocks[1].transactions);
Full source code
The full source code is located at my github with the branch blockchain-2
Next tutorial
In the next tutorial, I will make all the blockchain feature into an API with Expreess.js.
Video Tutorial
Curriculum
Posted on Utopian.io - Rewarding Open Source Contributors