Assignment 15
by Ebony McCoy
HTML
<input type=textbox id="content" />
<input type="button" value="Add Node to Blockchain" onclick="addNode()" />
<p id="output1"></p>
JavaScript
var SHA256 = ""
var bList = new Blockchain();
function Block(data, prevHash) {
this.data = data;
this.prevHash = prevHash;
this.hash = calculateHash;
this.content = content;
this.previous = null;
this.next = null;
}
Blockchain.prototype.calculateHash = function() {
return SHA256(this.prevHash + JSON.stringify(this.data)).toString();
}
function blockChain() {
// var list = new Blockchain();
this.head = null;
this.tail = null; //should it be this.tail??
}
Blockchain.prototype.push = function(content) {
var node = new Node(content);
//node.content = _content;
}
Blockchain.prototype.createGenesisBlock = function() {
return new Block(0, "0");
}
Blockchain.prototype.getLatestBlock = function() {
return list(list.length - 1);
}
Blockchain.prototype.addBlock = function(newBlock) {
newBlock.prevHash = this.getLatestBlock.hash;
newBlock.hash = newBlock.calculateHash();
list.push(newBlock);
}
Blockchain.prototype.isChainValid = function() {
for (var i = 1; i < list.length; i++) {
var currentBlock = list(i);
var prevBlock = list(i - 1);
if (currentBlock.hash !== currentBlock.calculateHash) {
return false;
}
if (currentBlock.prevHash !== prevBlock.hash) {
return false;
}
}
return true;
}
var saveCoin = new Blockchain();
saveCoin.addBlock(new Block(1, "ebony", {
amount: 4
}));
saveCoin.addBlock(new Block(2, "nicole", {
amount: 10
}));
console.log("Is chain valid?" + saveCoin.isChainValid);
//document.getElementById("output1").innerHTML = JSON.stringify(saveCoin, null, 4);