UNPKG

aiascs-blockchain

Version:

Sample Module for blockchain status of entities relate to each other

42 lines (35 loc) 892 B
const SHA256 = require("crypto-js/sha256"); class Block { constructor(timeStamp, data, previousHash = "") { /** * * @data is transaction information in the chain in * our case is the product id/token * */ this.timeStamp = timeStamp; this.previousHash = previousHash; this.data = data; this.hash = this.calculateHash(); this.nonce = 0; } calculateHash(){ return SHA256( this.previousHash + this.timeStamp + this.nonce + JSON.stringify(this.data) ).toString(); } mineBlock(difficult){ //make hash of a block begin with certain amount of zero, // so we will continue to loop until we get that, while ( this.hash.substring(0, difficult) != Array(difficult + 1).join("0") ) { this.nonce++; this.hash = this.calculateHash(); } } } module.exports = Block;