Skip to content

Instantly share code, notes, and snippets.

@ChrisCates
Last active August 13, 2017 23:43
Show Gist options
  • Save ChrisCates/0fb649a2dc5db4d9db9f11c1d2be41e2 to your computer and use it in GitHub Desktop.
Save ChrisCates/0fb649a2dc5db4d9db9f11c1d2be41e2 to your computer and use it in GitHub Desktop.
const fs = require('fs');
const shaEmoji = require('./sha-emoji');
var BlockChain = require('./blockchain.json');
if (BlockChain.length == 0) {
GenesisBlock(
"๐Ÿ‘บ๐Ÿ˜ฌ๐Ÿ‘†๐Ÿ˜ฑ๐Ÿ˜ง๐Ÿ‘‰๐Ÿค•๐Ÿ˜บ๐Ÿ‘๐Ÿ˜ฎ๐Ÿ˜ท๐Ÿ˜ณโ˜๏ธ๐Ÿค๐Ÿคง๐Ÿค’๐Ÿ˜ป๐Ÿค๐Ÿ‘Œ๐Ÿค›๐Ÿ’ฉโ˜๏ธ๐Ÿค•๐ŸŽƒ๐Ÿ‘‰๐Ÿ‘‰๐Ÿ‘ป๐Ÿ‘Š๐Ÿ‘‡๐Ÿ‘พ๐Ÿค’๐Ÿ˜ป๐Ÿ˜ณ๐Ÿ˜ฎ๐Ÿคง๐Ÿ˜จ๐Ÿคž๐Ÿคœ๐Ÿค๐Ÿ–๐Ÿค–๐Ÿคข๐Ÿคš๐Ÿ˜ช",
Number(new Date()),
"๐Ÿ˜‚"
)
}
function GenesisBlock(genesisHash, date, data) {
WriteBlock({
'index': 0,
'phash': genesisHash,
'hash': HashBlock(genesisHash, date, data),
'date': date,
'data': data
});
}
function AddBlock(phash, index, date, data) {
var verifiedBlockHash = BlockChain[BlockChain.length - 1]['hash'];
if (verifiedBlockHash == phash && index == BlockChain.length) {
WriteBlock({
'index': index,
'phash': phash,
'hash': HashBlock(phash, date, data),
'date': date,
'data': data
});
} else {
console.log("BlockChain out of sync... resyncing.");
}
}
function WriteBlock(data) {
BlockChain.push(data);
fs.writeFileSync(__dirname + "/blockchain.json", JSON.stringify(BlockChain, null, 2), "UTF-8");
console.log("Wrote block", data.hash);
}
function HashBlock(phash, date, data) {
return shaEmoji(phash + date + data);
}
function CreateBlock(data) {
AddBlock(BlockChain[BlockChain.length - 1]['hash'], BlockChain.length, Number(new Date()), data);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment