Nicholas Dalessandro COP3530 Assignment 15

by Ron Eaglin

HTML

Input a Number:
<input id="text"> <br/>
<button id="output0" onclick = "add2chain();">Add Blockchain</button>
<button id="output1" onclick = "makeinvalid();">Invalidate Blockchain</button><br/> 
<p id="value"></p>
<br/>

JavaScript

var Node = function(a) {
  this.next = null;
  this.prev = null;
  this.key = SHA256(a);
  return this;
}
var List = function(a) {
  this.head = null;
  this.tail = null;
  this.length = 0;
  return this;
}
List.prototype.list2string = function() {
  var str = "";
  var anode = this.head;
  while (anode != null) {
    str += anode.key + '<br/>';
    anode = anode.next;
  }
  return str;
}
List.prototype.push = function(a) {
  var node = new Node(a);
  node.c = a;
  if (this.head == null) {
    this.head = node;
    this.tail = node;
    this.length++;
    return node;
  }
  node.prev = this.tail;
  this.tail.next = node;
  this.tail = node;
  this.length++;
  return node;
}
List.prototype.pop = function() {
  var poppedNode = this.tail;
  if (this.head == null) {
    return null;
  }
  if (this.head == this.tail) {
    this.head = null;
    this.tail = null;
    this.length--;
    return poppedNode;
  }
  this.tail = this.tail.prev;
  this.tail.next = null;
  this.length--;
  return poppedNode;
}

var L = new List();
var allblocks = new List();

function pushNode(value) {
  value = document.getElementById('value').value;
  L.push(value);
  document.getElementById('output0').innerHTML = L.list2string();
}

function popNode() {
  L.pop();
  document.getElementById('output0').innerHTML = L.list2string();

}

function BlockchainIsValid() {
  var value = document.getElementById('text').value;
  var key = SHA256(value);
  var anode = L.head;
  var previoushash = L.tail.prev.key; 
  var result = 'SHA256';
  while (anode != null) {
    if (anode.key == key) {
      result = 'Block Chain Valid';
      document.getElementById('value').innerHTML += "<br/>" + result + "<br/> Unhashed Content: " + value +"<br/> SHA256 Hash: "+ key +" <br/> Previous Hash: "+ previoushash;
      return result;
    }
    if (anode.key != key) {
      result = "Block chain not valid";
      document.getElementById('value').innerHTML +="<br/>" + result + "<br/> Unhashed Content: " + value +"<br/>...