Last active
August 13, 2017 23:43
-
-
Save ChrisCates/0fb649a2dc5db4d9db9f11c1d2be41e2 to your computer and use it in GitHub Desktop.
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
| 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