assign15

by zazagalaxy

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.min.js"></script>

JavaScript

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

class Block {
  constructor(index, timestamp, data, previousHash = '') {
    this.index = index;
    this.timestamp = timestamp;
    this.data = data;
    this.previousHash = previousHash;
    this.hash = this.calculateHash;
  }

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

class Blockchain{
	constructor(){
  		this.chain = [this.createGenesisBlock()];
  }
  createGenesisBlock(){
  	return new Block(0, "01/01/2018", "Genesis block", "0");
  }
	getLatestBlock(){
  	return this.chain[this.chain.length - 1];
  }
  
  addBlock(newBlock){
  	newBlock.previousHash = this.getLatestBlock().hash;
    newBlock.hash = newBlock.calculateHash();
    this.chain.push(newBlock);
  }
  
}
let zazaCOin = new Blockchain();
zazaCoin.addBlock(new Block(1, "01/22/2018", { amount: 4  }));
zazaCoin.addBlock(new Block(2, "03/22/2018", { amount: 10  }));
console.log(JSON.stringif)