├── .gitignore ├── block.js ├── blockchain.js ├── index.js ├── package-lock.json └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /block.js: -------------------------------------------------------------------------------- 1 | const sha256 = require('crypto-js/sha256') 2 | 3 | class Block { 4 | constructor(index = 0, previousHash = null, data = 'Genesis block', difficulty = 1) { 5 | this.index = index 6 | this.previousHash = previousHash 7 | this.data = data 8 | this.timestamp = new Date() 9 | this.difficulty = difficulty 10 | this.nonce = 0 11 | 12 | this.mine() 13 | } 14 | 15 | generateHash() { 16 | return sha256(this.index + this.previousHash + JSON.stringify(this.data) + this.timestamp + this.nonce).toString() 17 | } 18 | 19 | mine() { 20 | this.hash = this.generateHash() 21 | 22 | while (!(/^0*$/.test(this.hash.substring(0, this.difficulty)))) { 23 | this.nonce++ 24 | this.hash = this.generateHash() 25 | } 26 | } 27 | } 28 | 29 | module.exports = Block -------------------------------------------------------------------------------- /blockchain.js: -------------------------------------------------------------------------------- 1 | const Block = require('./block') 2 | 3 | class Blockchain { 4 | constructor(difficulty = 1) { 5 | this.blocks = [new Block()] 6 | this.index = 1 7 | this.difficulty = difficulty 8 | } 9 | 10 | getLastBlock() { 11 | return this.blocks[this.blocks.length - 1] 12 | } 13 | 14 | addBlock(data) { 15 | const index = this.index 16 | const difficulty = this.difficulty 17 | const previousHash = this.getLastBlock().hash 18 | 19 | const block = new Block(index, previousHash, data, difficulty) 20 | 21 | this.index++ 22 | this.blocks.push(block) 23 | } 24 | 25 | isValid() { 26 | for (let i = 1; i < this.blocks.length; i++) { 27 | const currentBlock = this.blocks[i] 28 | const previousBlock = this.blocks[i - 1] 29 | 30 | if (currentBlock.hash !== currentBlock.generateHash()) { 31 | return false 32 | } 33 | 34 | if (currentBlock.index !== previousBlock.index + 1) { 35 | return false 36 | } 37 | 38 | if (currentBlock.previousHash !== previousBlock.hash) { 39 | return false 40 | } 41 | } 42 | return true 43 | } 44 | } 45 | 46 | module.exports = Blockchain -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const Blockchain = require('./blockchain') 2 | 3 | const blockchain = new Blockchain() 4 | blockchain.addBlock({ amount: 4 }) 5 | blockchain.addBlock({ amount: 50 }) 6 | 7 | console.log(blockchain) 8 | 9 | console.log(blockchain.isValid()) 10 | blockchain.blocks[1].data.amount = 30000 11 | console.log(blockchain.isValid()) -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "blockchain-javascript", 3 | "version": "1.0.0", 4 | "lockfileVersion": 1, 5 | "requires": true, 6 | "dependencies": { 7 | "crypto-js": { 8 | "version": "3.1.9-1", 9 | "resolved": "https://registry.npmjs.org/crypto-js/-/crypto-js-3.1.9-1.tgz", 10 | "integrity": "sha1-/aGedh/Ad+Af+/3G6f38WeiAbNg=" 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "blockchain-javascript", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "dependencies": { 12 | "crypto-js": "^3.1.9-1" 13 | } 14 | } 15 | --------------------------------------------------------------------------------