Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Blockchain by (12.8k points)

I am trying to learn more about blockchain by building a small project using JavaScript. Can anyone explain to me why the previous hash does not show when I console.log the test blockchain?

(npm install --save crypto-js if you need SHA256)

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

        class Block{

            //Index: (Optional) tells us where the block is in the chain

            //Timestamp: tells us when the block was created 

            //Data: Can include any kind of data; for a currency you could store details of the transaction(transfer amount, sender id, receiver id)

         //previousHash: String which contains the hash of the block which came before it (Ensures data integrity)

           constructor(index, timestamp, data, previousHash = ''){

                this.index = index;

                this.timestamp = timestamp;

                this.data = data;

                this.previousHash = previousHash;

                //Hash of this block

                this.hash = this.calculateHash();

            }

           //Runs values from block through a hashing function to create a hash

            calculateHash(){

                return SHA256(this.index + this.previousHash + this.timestamp + JSON.stringify(this.data)).toString();

            }

        }

        //8.44

        class Blockchain{

            //Initializes blockchain

            constructor(){

                this.chain = [this.createGenesisBlock];

            }

           // first block needs to be created manually 

            //index, timeStamp, data, previousHash(there is none in this case)

            createGenesisBlock(){

                return new Block(0, "01/01/2017", "Genesis block", "0");

            }

            //Returns most recently added block

            getLatestBlock(){

            return this.chain[this.chain.length -1];

            }

            //adds a new block to the chain

            addBlock(newBlock){

                newBlock.previousHash = this.getLatestBlock().hash;

                newBlock.hash = newBlock.calculateHash

                //The chain is just an array so you can use regular array methods

                this.chain.push(newBlock);

            }

        }

       //create instance of blockchhain

        let Coin = new Blockchain();

        Coin.addBlock(new Block(1, "10/07/2017", {amount: 4}));

        Coin.addBlock(new Block(2, "12/07/2017", {amount: 10}));

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

I expect the console.logged JSON file to contain the previousHash as well as the index, timestamp and data. It contains all except previousHash.

Thanks for the help have been scratching my head for a while trying to figure this out

1 Answer

0 votes
by (29.8k points)

I think this is not working because you forgot the parenthesis in the line with this.chain = [this.createGenesisBlock()]; line, so what's happening is that the first element of the chain will be a reference to a function, instead of an instance of a Block.

Add the parenthesis, and it should work.

Related questions

Browse Categories

...