What do I import for SHA encryption in Node js

0 votes

I'm trying to recreate some code from a website I found online. The code is supposed to create a blockchain in Node.js. However, when I run it, it does the recognize the command:

return sha(JSON.stringify(this.data) + this.prevHash + this.index + this.timestamp);

It returns the error:

return sha(JSON.stringify(this.data) + this.prevHash + this.index + this.timestamp);
                ^

ReferenceError: sha is not defined
    at Block.getHash (C:\Users\winst\OneDrive\Documents\Blockchain\main.js:12:3)
    at new Block (C:\Users\winst\OneDrive\Documents\Blockchain\main.js:8:19)
    at BlockChain.addBlock (C:\Users\winst\OneDrive\Documents\Blockchain\main.js:22:14)
    at Object.<anonymous> (C:\Users\winst\OneDrive\Documents\Blockchain\main.js:42:9)
    at Module._compile (internal/modules/cjs/loader.js:702:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
    at Module.load (internal/modules/cjs/loader.js:612:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
    at Function.Module._load (internal/modules/cjs/loader.js:543:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)

What should I import in order to sha to be defined? The whole code:

class Block{

    constructor(index, data, prevHash){
    this.index = index;
    this.timestamp = Math.floor(Date.now() / 1000);
    this.data = data;
    this.prevHash = prevHash;
    this.hash = this.getHash();
    }

    getHash(){
        return sha(JSON.stringify(this.data) + this.prevHash + this.index + this.timestamp);
    }
}
class BlockChain{
    constructor(){
        this.chain = [];
    }
    addBlock(data){
    let index = this.chain.length;
    let prevHash = this.chain.length !== 0 ? this.chain[this.chain.length - 1].hash : 0;
    let block = new Block(index, data, prevHash);

    this.chain.push(block);
    }
    chainIsValid(){

    for(var i=0;i<this.chain.length;i++){

        if(this.chain[i].hash !== this.chain[i].getHash()) 
            return false;

        if(i > 0 && this.chain[i].prevHash !== this.chain[i-1].hash)
            return false;
    }

    return true;
}   
}
const CILCoin = new BlockChain();

CILCoin.addBlock({sender: "Bruce wayne", reciver: "Tony stark", amount: 100});
CILCoin.addBlock({sender: "Harrison wells", reciver: "Han solo", amount: 50});
CILCoin.addBlock({sender: "Tony stark", reciver: "Ned stark", amount: 75});

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

Sep 21, 2018 in Blockchain by slayer
• 29,370 points
822 views

1 answer to this question.

0 votes
var sha = require('sha.js');
//...
getHash() {
    return sha('sha256').update(JSON.stringify(this.data) + this.preHash + this.index + this.timestamp).digest('hex');
}
answered Sep 21, 2018 by digger
• 26,740 points

Related Questions In Blockchain

0 votes
1 answer

What path should I give for credentials in web3j?

WalletUtils.loadCredentials can be buggy, I would recommend ...READ MORE

answered Jun 27, 2018 in Blockchain by Shashank
• 10,400 points
873 views
+4 votes
2 answers

How do I create a new block in Hyperledger Fabric?

This link might help you: https://github.com/hyperledger/fabric-sample ...READ MORE

answered Oct 11, 2018 in Blockchain by Sahu
2,565 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,882 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,399 views
0 votes
1 answer

What is transientMap in node.js and getTransient in chaincode?

TransientMap: TransientMap  contains data that might be used to ...READ MORE

answered Jul 13, 2018 in Blockchain by digger
• 26,740 points
1,314 views
0 votes
1 answer

What is the peer node start in hyperledger?

Nodes are the fundamental element of the ...READ MORE

answered Jul 9, 2018 in Blockchain by digger
• 26,740 points
1,012 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP