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,040 points
65 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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
• 27,620 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 26, 2018 in Blockchain by Shashank
• 10,330 points
66 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
339 views
+3 votes
4 answers

How do I add a new node on a Hyperledger network?

You can do this by generating the crypto ...READ MORE

answered Apr 6, 2018 in Blockchain by Christine
• 15,790 points
1,164 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
61 views
0 votes
1 answer
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
• 27,620 points
142 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
• 27,620 points
38 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.