Assignment 15

Blockchain

by Ron Eaglin

HTML

Input a Number:
<input id="text">
<button id="output0">Add Blockchain</button>
<button id="output1">Validate Blockchain</button>
<div id="value"></div>
<br>

JavaScript

// linked list
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;
}

// converts list to string
List.prototype.listToString = function() {
var str = ''; //insert string here
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) {
alert(''); //alerts.
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();

//push and pop output to HTML document
function pushNode(value) {
value = document.getElementById('value').value;
L.push(value);
document.getElementById('output0').innerHTML = L.listToString();
}

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

// function to check if block chain is valid in list
function blockchainIsValid(value) {
value = document.getElementById('value').value;
var key = SHA256(value);
var anode = L.head;
var result = 'SHA256';
while (anode != null) {

if (anode.key == key) {
result = 'Block Chain is valid';
document.getElementById('output1').innerHTML = result;
return result;
}

else (anode.key != key); {
result = "Block Chain isn't valid";
document.getElementById(result).innerHTML = result;
return result;
}
anode = anode.next;
}
}

//
class Block {
constructor(data, previousHash = ''){
this.data = data;
this.previousHash = previousHash;
this.hash =...