BlockChain

You are going to create your own Blockchain (just like in the video) with only one little difference. He uses an Array to store his Blockchain, you are simply going to do this making the Blockchain a List. Differences will be; -Block object will need a pointer to next (and previous if implemented doubly which I recommend) -Your Block will NOT need an index -Your Block will NOT need a timestamp. -Blockchain Object will need a pointer to head and tail. -Blockchain will need to implement push (you should have this). -The key will be writing the isChainValid() function -You must use a 64 bit or greater Hash Function For the output – you will create a Blockchain that allows the user to input data and push that onto the end of the node. When the user does that the output should be the results of the isChainValid() function. You will then create a Button that will invalidate the Blockchain by directly changing the content of the head. It should then output – isChainValid() to show that isChainValid() works correctly. Note: You do not need to implement the Proof of Work capability.

by Neil Daley

HTML

<h1>
Blockchain
</h1>

<!-- Begin input div -->
<div class="divBG">

  <!-- Create user input content-->
  <table border="0" align="center" height="165px">
    <tr>
      <td>
        Name <input type="textbox" id="name"><br />
        Date <input type="date" id="date" value = "2018-01-01"><br />
        Transaction Amount <input type="number" id="amount" min="0" step=".01" value ="0"/><br />
        
      </td>
    </tr>
    <tr>
      <td valign="top">
        <input type="button" class="btnstyle" value="New Blockchain" id="btnNewBlockchain" onClick="newBlockchain();" />
        <input type="button" class="btnstyle" value="Add Block" id="btnAddBlock" onClick="newUserBlock();" />
      </td>
    </tr>
    <tr height="15%">
      <td>
        <!-- Begin div displays -->
        <div id="message">

        </div>
      </td>
    </tr>
  </table>
</div>
<!-- End input div -->
<br />
<!-- Begin array div -->
<div class="outputDiv">
  <big><strong>Blockchain Data</strong></big>
  <br />
  <div class="container" id="output">

  </div>
</div>
<!-- End array div -->
<!-- End div displays -->

JavaScript

var sha256 = function sha256(ascii) {
  function rightRotate(value, amount) {
    return (value >>> amount) | (value << (32 - amount));
  }

  var mathPow = Math.pow;
  var maxWord = mathPow(2, 32);
  var lengthProperty = 'length'
  var i, j; // Used as a counter across the whole file
  var result = ''

  var words = [];
  var asciiBitLength = ascii[lengthProperty] * 8;

  //* caching results is optional - remove/add slash from front of this line to toggle
  // Initial hash value: first 32 bits of the fractional parts of the square roots of the first 8 primes
  // (we actually calculate the first 64, but extra values are just ignored)
  var hash = sha256.h = sha256.h || [];
  // Round constants: first 32 bits of the fractional parts of the cube roots of the first 64 primes
  var k = sha256.k = sha256.k || [];
  var primeCounter = k[lengthProperty];
  /*/
  var hash = [], k = [];
  var primeCounter = 0;
  //*/

  var isComposite = {};
  for (var candidate = 2; primeCounter < 64; candidate++) {
    if (!isComposite[candidate]) {
      for (i = 0; i < 313; i += candidate) {
        isComposite[i] = candidate;
      }
      hash[primeCounter] = (mathPow(candidate, 0.5) * maxWord) | 0;
      k[primeCounter++] = (mathPow(candidate, 1 / 3) * maxWord) | 0;
    }
  }

  ascii += '\x80' // Append Ƈ' bit (plus zero padding)
  while (ascii[lengthProperty] % 64 - 56) ascii += '\x00' // More zero padding
  for (i = 0; i < ascii[lengthProperty]; i++) {
    j = ascii.charCodeAt(i);
    if (j >> 8) return; // ASCII check: only accept characters in range 0-255
    words[i >> 2] |= j << ((3 - i) % 4) * 8;
  }
  words[words[lengthProperty]] = ((asciiBitLength / maxWord) | 0);
  words[words[lengthProperty]] = (asciiBitLength)

  // process each chunk
  for (j = 0; j < words[lengthProperty];) {
    var w = words.slice(j, j += 16); // The message is expanded into 64 words as part of the iteration
    var oldHash = hash;
    // This is now the undefinedworking hash", often labelled as...