Created
March 13, 2018 21:29
-
-
Save marklovett/acab5e5408c7b200acdd768553b5a0c2 to your computer and use it in GitHub Desktop.
savjee Blockchain javascript tutorial
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Description: My Blockchain tut- savjee Blockchain javascript tutorial, https://github.com/SavjeeTutorials/SavjeeCoin/blob/master/src/main.js,https://www.youtube.com/watch?v=zVqczFZr124 | |
* Version: 1.0 | |
* Author: Mark Lovett | |
* Author URI: https://marklovettdesign.com | |
*/ | |
const SHA256 = require("crypto-js/sha256"); | |
class Block { | |
constructor(index, timestamp, data, previousHash = '') { | |
this.index = index; | |
this.previousHash = previousHash; | |
this.timestamp = timestamp; | |
this.data = data; | |
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/2017", "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); | |
} | |
/* | |
isChainValid() { | |
for (let i = 1; i < this.chain.length; i++){ | |
const currentBlock = this.chain[i]; | |
const previousBlock = this.chain[i - 1]; | |
if (currentBlock.hash !== currentBlock.calculateHash()) { | |
return false; | |
} | |
if (currentBlock.previousHash !== previousBlock.hash) { | |
return false; | |
} | |
} | |
return true; | |
} | |
} | |
*/ | |
let savjeeCoin = new Blockchain(); | |
savjeeCoin.addBlock(new Block(1, "20/07/2017", { amount: 4 })); | |
savjeeCoin.addBlock(new Block(2, "20/07/2017", { amount: 8 })); | |
console.log('Blockchain valid? ' + savjeeCoin.isChainValid()); | |
/* | |
console.log('Changing a block...'); | |
savjeeCoin.chain[1].data = { amount: 100 }; | |
// savjeeCoin.chain[1].hash = savjeeCoin.chain[1].calculateHash(); | |
console.log("Blockchain valid? " + savjeeCoin.isChainValid()); | |
// console.log(JSON.stringify(savjeeCoin, null, 4)); | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: My Blockchain | |
* Plugin URI: https://marklovettdesign.com | |
* Description: savjee Blockchain javascript tutorial, https://github.com/SavjeeTutorials/SavjeeCoin/blob/master/src/main.js,https://www.youtube.com/watch?v=zVqczFZr124 | |
* Version: 1.0 | |
* Author: Mark Lovett | |
* Author URI: https://marklovettdesign.com | |
*/ | |
define( 'MY_BLOCKCHAIN_DIR', plugin_dir_path( __FILE__ ) ); | |
define( 'MY_BLOCKCHAIN_URL', plugins_url( '/', __FILE__ ) ); | |
require 'js/main.js'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment