Skip to content

Instantly share code, notes, and snippets.

View cardeol's full-sized avatar

CARD30L cardeol

View GitHub Profile
@cardeol
cardeol / index.js
Created March 30, 2018 22:48
Main algorithm blockchain
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");
function BlockChain(difficulty) {
this.blocks = [];
this.difficulty = difficulty;
this.add = function(block) { // a new kid on the blockchain
this.blocks.push(block);
}
@cardeol
cardeol / block.js
Created March 30, 2018 22:31
A Javascript Block class for a blockchain
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
/*
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]);
};
}