This file contains hidden or 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
var Blockchain = require("./blockchain.js"); | |
var Block = require("./block.js"); | |
var difficulty = 7; // number of zeroes in the hash key | |
var blockchain = new Blockchain(difficulty); | |
var currentBlock, prevBlock, genesisBlock; | |
(function Main() { | |
var i; // counter | |
genesisBlock = new Block("Hello Blockchain"); |
This file contains hidden or 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
function BlockChain(difficulty) { | |
this.blocks = []; | |
this.difficulty = difficulty; | |
this.add = function(block) { // a new kid on the blockchain | |
this.blocks.push(block); | |
} |
This file contains hidden or 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
var sha256 = require("sha256"); | |
var method = Block.prototype; | |
function Block(data, previousHash) { | |
if(typeof previousHash === "undefined") previousHash = "genesis_block"; | |
this._previousHash = previousHash; | |
this._data = JSON.stringify({ data: data }); | |
this._nonce = 0; | |
this._timeStamp = new Date().getTime(); | |
this._hash = this.computeHash(); // constructor |
This file contains hidden or 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
/* | |
Cross-browser hasOwnProperty solution, based on answers from: | |
http://stackoverflow.com/questions/135448/how-do-i-check-to-see-if-an-object-has-an-attribute-in-javascript | |
*/ | |
if ( !Object.prototype.hasOwnProperty ) { | |
Object.prototype.hasOwnProperty = function(prop) { | |
var proto = obj.__proto__ || obj.constructor.prototype; | |
return (prop in this) && (!(prop in proto) || proto[prop] !== this[prop]); | |
}; | |
} |