How to create a blockchain with javascript.

in #blockchain6 years ago (edited)

How to create a blockchain with javascript.

Blockchain

A chain of blocks or also known as blockchain is a data structure in which the information is stored in sets (blocks), to which meta information is added relative to another block of the previous chain in a time line, which means that its content can only be edited by modifying all the previous blocks.

In this post we will learn how to create a blockchain with javascript and nodejs.

1.- We will create a folder with the name of our choice in this case we will call it "blockchain", inside it we will create a file that will be called main.js.

2.- Now we will open this folder with the code editor of our preference.

Well now let's start with the code !!

1.- First we will create a class called Block which will contain a constructor function, we will also create the functions calculateHash, mineBlock, the constructor function will create the instances of the objects of the block and will provide them with their properties.

class Block {
    constructor(data) {
        this.index = 0;
        this.date = new Date();
        this.data = data;
        this.previousHash = "0";
        this.hash = this.calculateHash();
        this.nonce = 0;
    }

    calculateHash() {
    
    }

    mineBlock(difficulty) {
    
}

2.- We start npm, open the terminal and we are located in our project, now we will only execute the following command and it only remains to configure the project to our liking.

$ npm init

3.- Now we are using a npm module called crypto-js, this can be installed with the following command.

$ npm install –save crypto-js

4.- Now that the library is installed we will create a constant called SHA265 that requires crypto-js / sha265, this we will use to create the calculation function.

const SHA256 = require('crypto-js/sha256');

class Block {
    constructor(data) {
        this.index = 0;
        this.date = new Date();
        this.data = data;
        this.previousHash = "0";
        this.hash = this.calculateHash();
        this.nonce = 0;
    }

    calculateHash() {
        return SHA256(this.index + this.previousHash + this.date + this.data + this.nonce).toString();
    }

    mineBlock(difficulty) {
        while(!this.hash.startsWith(difficulty)) {
            this.nonce++;
            this.hash = this.calculateHash();
        }
    }
}
  • The function calculateHash we will use to give each block its own hash, it takes each piece of the Block object, throws it in a SHA256 function and converts it to a string.

  • The second mineBlock function will be used to safely process transactions in blockchain networks adding difficulty to the process of generating the hash, in this case the difficulty will be the number of zeros with which the hash will begin.

5.- Now that we create the structure of our individual block, we need to create a structure an object of class Blockchain this will have the following functions, constructor, createGenesis, latestBlock, addBlock, checkValid.

  • Our Blockchain class needs to have the capacity to create instances, to initiate, access the most recent information of the block and expand by adding a new block.
class Blockchain {
    constructor(difficulty = '00') {
        this.chain = [this.createGenesis()];
        this.difficulty = difficulty;
    }

    createGenesis() {
        return new Block(0, {amount: 100});
    }

    latestBlock() {
        return this.chain[this.chain.length - 1];
    }

    addBlock(newBlock) {
        newBlock.index = this.latestBlock().index + 1;
        newBlock.previousHash = this.latestBlock().hash;
        newBlock.hash = newBlock.calculateHash();
        newBlock.mineBlock(this.difficulty);
        console.log('Mined! '+ newBlock.hash+' with nonce'+ newBlock.nonce);
        this.chain.push(newBlock);
    }

    checkValid() {
        for (let i = 1; i < this.chain.length; i++) {
            const currentBlock = this.chain[i];
            const previousBlock = this.chain[i - 1];

            if (currentBlock.hash !== currentBlock.calculateHash()) {
                return false;
            }

            if (currentBlock.previousHash !== previousBlock.hash) {
                return false;
            }
        }
        return true;
    }
}

6.- Well let's explain what we did in this code.

Constructor function.

This function creates an instance in our chain of blocks with a property in a list structure, that is why our Block object has indexes this property is used to find its position in our list of chains, it also defines the difficulty of initial mining.

CreateGenesis function.

Our block chain needs the createGenesis function since it creates the first block of our chain. Usually in blockchain the first block of any is known as "Genesis block", that is why the name of createGenesis, normally the information of the genesis block is entered manually. Since it is the first block of the chain, its index will be 0, the date and time will be the current ones, as far as the previous hash will be 0 since there is no other hash that does it.

LatestBlock function.

This function obtains the information of the most recent block and will also be necessary in the following function.

AddBlock function.

In order for our block to have the capacity to extend continuously, it needs this function, the block chains have this functionality, the best known is Bitcoin (BTC), which is responsible for creating new blocks every 10 minutes with the information of the transactions made in It is time frame.
Its implementation of the addBlock function is simple, it takes the Block object as an input which has a timestamp and data. Then, our addBlock function will use the latestBlock function to give it an index and a hash before our new block, after that we give it its own hash using the function calculateHash, we will also pass the difficulty of mining defined in the property difficulty, and finally we push this block in our chain, now we have a new block.

CheckValid function.

This function verifies the integrity of the block chain and detects if something has been tampered with.
You should know that the hashes are critical to detect changes in our chain since even the smallest change in an object will result in a completely different hash, so this function uses a for loop to traverse the string and try to match its hashes to detect changes.
There are 2 parts in our loop the first is to match currentBlock.hash with currentBlock.calculateHash and the second is to match currentBlock.previousHash with previousBlock.hash, the first one is used to check if the currentBlock information has been manipulated without updating currentBlock. hash The second is used to verify if a previous block has been modified.

7.- Finally we will add these lines at the end of our code that add two blocks with data, validate them and show them in the console.

  • Note: Blockchain class receives the difficulty of mining in this case the difficulty is 3 zeros, the more zeros more difficult will be the mining of the blocks.

Be careful not to add many zeros or your equipment could stop!

let myCoin = new Blockchain('000');
myCoin.addBlock(new Block({amount: 50}));
myCoin.addBlock(new Block({amount: 25}));

console.log("The block chain is valid?", myCoin.checkValid());

8.- Now our final code will look like this.

const SHA256 = require('crypto-js/sha256');

class Block {
    constructor(data) {
        this.index = 0;
        this.date = new Date();
        this.data = data;
        this.previousHash = "0";
        this.hash = this.calculateHash();
        this.nonce = 0;
    }

    calculateHash() {
        return SHA256(this.index + this.previousHash + this.date + this.data + this.nonce).toString();
    }

    mineBlock(difficulty) {
        while(!this.hash.startsWith(difficulty)) {
            this.nonce++;
            this.hash = this.calculateHash();
        }
    }
}

class Blockchain {
    constructor(difficulty = '00') {
        this.chain = [this.createGenesis()];
        this.difficulty = difficulty;
    }

    createGenesis() {
        return new Block(0, {amount: 100});
    }

    latestBlock() {
        return this.chain[this.chain.length - 1];
    }

    addBlock(newBlock) {
        newBlock.index = this.latestBlock().index + 1;
        newBlock.previousHash = this.latestBlock().hash;
        newBlock.hash = newBlock.calculateHash();
        newBlock.mineBlock(this.difficulty);
        console.log('Mined! '+ newBlock.hash+' with nonce '+ newBlock.nonce);
        this.chain.push(newBlock);
    }

    checkValid() {
        for (let i = 1; i < this.chain.length; i++) {
            const currentBlock = this.chain[i];
            const previousBlock = this.chain[i - 1];

            if (currentBlock.hash !== currentBlock.calculateHash()) {
                return false;
            }

            if (currentBlock.previousHash !== previousBlock.hash) {
                return false;
            }
        }
        return true;
    }
}

let myCoin = new Blockchain('0000');
myCoin.addBlock(new Block({amount: 50}));
myCoin.addBlock(new Block({amount: 25}));

console.log("The block chain is valid?", myCoin.checkValid());

9.- Now we just have to try our blockchain open the terminal, locate yourself at the root of your project and execute the following command.

$ node main.js

10.- You will see something like this.

Mined! 00000e0d4ead0b3bf31acc6af0b09ead1924442ae26b0ba7e7d6900176eec17e with nonce 24943
Mined! 0000d038ac586662b838055f1b6687b6d213fa1dd78fee947be5184b6853920e with nonce 44157
The block chain is valid? True

11.- To see the information contained in each block, add the following line of code at the end.

console.log(JSON.stringify(myCoin, null, 4));

12.- Now you should see something like this.

Mined! 0000f66f662e39a11b318ba7cf2667615a19fed11384c6a09c7d52a44f47a07d with nonce 89368
Mined! 0000be061e6026fff3471a18627d033cdd969c1f16e99f59ac6957e616d1905f with nonce 159427
The block chain is valid? true
{
    "chain": [
        {
            "index": 0,
            "date": "2018-10-18T23:26:40.639Z",
            "data": 0,
            "previousHash": "0",
            "hash": "660ea1d34ab625219001fcc5c698acf9d1208f73d1dba7272e89ec8ebcf3e019",
            "nonce": 0
        },
        {
            "index": 1,
            "date": "2018-10-18T23:26:40.643Z",
            "data": {
                "amount": 50
            },
            "previousHash": "660ea1d34ab625219001fcc5c698acf9d1208f73d1dba7272e89ec8ebcf3e019",
            "hash": "0000f66f662e39a11b318ba7cf2667615a19fed11384c6a09c7d52a44f47a07d",
            "nonce": 89368
        },
        {
            "index": 2,
            "date": "2018-10-18T23:26:49.377Z",
            "data": {
                "amount": 25
            },
            "previousHash": "0000f66f662e39a11b318ba7cf2667615a19fed11384c6a09c7d52a44f47a07d",
            "hash": "0000be061e6026fff3471a18627d033cdd969c1f16e99f59ac6957e616d1905f",
            "nonce": 159427
        }
    ],
    "difficulty": "0000"
}

Ready now you have already created a chain of blocks (Blockchain) in javascript and nodejs, in future post we will learn more about blockchain and javascript.

The code repository: GitHub

Sort:  

Congratulations @stricker! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

Award for the number of upvotes received

Click on the badge to view your Board of Honor.
If you no longer want to receive notifications, reply to this comment with the word STOP

Support SteemitBoard's project! Vote for its witness and get one more award!

@stricker, thank you for supporting @steemitboard as a witness.

Here is a small present to show our gratitude
Click on the badge to view your Board of Honor.

Once again, thanks for your support!

Do not miss the last post from @steemitboard:

SteemitBoard Ranking update - Resteem and Resteemed added

Congratulations @stricker! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You got your First payout

Click here to view your Board of Honor
If you no longer want to receive notifications, reply to this comment with the word STOP

Do not miss the last post from @steemitboard:

SteemitBoard notifications improved

You can upvote this notification to help all Steemit users. Learn why here!

Congratulations @stricker! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 1 year!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Do not miss the last post from @steemitboard:

The new SteemFest⁴ badge is ready

You can upvote this notification to help all Steem users. Learn how here!