const date = require('date-utils'); var dt = new Date();
var Block = new function(){ this.index = 0; this.timestamp = dt.clone(); this.data = "data : Genesis Block" this.previous_hash = "0"; this.hash = crypto.createHash("sha256").update(this.index + this.timestamp + this.data + this.previous_hash).digest("hex"); };
console.log("index : ", Block.index, "timestamp : ", Block.timestamp, "data : ", Block.data, "previous_hash : ", Block.previous_hash, "hash", Block.hash);
const next_block = async () => { try{ Block.index = Block.index+1; Block.timestamp = dt.clone(); Block.data = "Hey! I'm block " + Block.index; Block.previous_hash = Block.hash; Block.hash = crypto.createHash("sha256").update(Block.index + Block.timestamp + Block.data + Block.previous_hash).digest("hex"); }catch (error) { console.log('Task Failure',error);} }
var num_of_blocks_to_add = 20;
for(var i=0; i<num_of_blocks_to_add; i++){ next_block(); console.log("index : ", Block.index, "timestamp : ", Block.timestamp, "data : ", Block.data, "previous_hash : ", Block.previous_hash, "hash", Block.hash); } |