├── .gitignore ├── app.js ├── models.js ├── models ├── block.js ├── blockchain.js └── transaction.js ├── package-lock.json └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (https://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # Typescript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | # next.js build output 61 | .next 62 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | 2 | let Block = require('./models/block') 3 | let Transaction = require('./models/transaction') 4 | let Blockchain = require('./models/blockchain') 5 | 6 | // create genesis block 7 | let genesisBlock = new Block() 8 | let blockchain = new Blockchain(genesisBlock) 9 | 10 | // create a transaction 11 | let transaction = new Transaction('Mary','John',100) 12 | let block = blockchain.getNextBlock([transaction]) 13 | blockchain.addBlock(block) 14 | 15 | let anotherTransaction = new Transaction("Azam","Jerry",10) 16 | let block1 = blockchain.getNextBlock([anotherTransaction,transaction]) 17 | blockchain.addBlock(block1) 18 | 19 | console.log(blockchain) 20 | -------------------------------------------------------------------------------- /models.js: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | module.exports = {Transaction,Block} 7 | -------------------------------------------------------------------------------- /models/block.js: -------------------------------------------------------------------------------- 1 | 2 | class Block { 3 | 4 | constructor() { 5 | 6 | this.index = 0 7 | this.previousHash = "" 8 | this.hash = "" 9 | this.nonce = 0 10 | this.transactions = [] 11 | 12 | } 13 | 14 | get key() { 15 | return JSON.stringify(this.transactions) + this.index + this.previousHash + this.nonce 16 | } 17 | 18 | addTransaction(transaction) { 19 | this.transactions.push(transaction) 20 | } 21 | } 22 | 23 | module.exports = Block 24 | -------------------------------------------------------------------------------- /models/blockchain.js: -------------------------------------------------------------------------------- 1 | 2 | let Block = require('./block') 3 | let sha256 = require('js-sha256') 4 | 5 | class Blockchain { 6 | 7 | constructor(genesisBlock) { 8 | 9 | this.blocks = [] 10 | this.addBlock(genesisBlock) 11 | } 12 | 13 | addBlock(block) { 14 | 15 | if(this.blocks.length == 0) { 16 | block.previousHash = "0000000000000000" 17 | block.hash = this.generateHash(block) 18 | } 19 | 20 | this.blocks.push(block) 21 | } 22 | 23 | getNextBlock(transactions) { 24 | 25 | let block = new Block() 26 | 27 | transactions.forEach(function(transaction){ 28 | block.addTransaction(transaction) 29 | }) 30 | 31 | let previousBlock = this.getPreviousBlock() 32 | block.index = this.blocks.length 33 | block.previousHash = previousBlock.hash 34 | block.hash = this.generateHash(block) 35 | return block 36 | } 37 | 38 | generateHash(block) { 39 | 40 | let hash = sha256(block.key) 41 | 42 | while(!hash.startsWith("0000")) { 43 | block.nonce += 1 44 | hash = sha256(block.key) 45 | console.log(hash) 46 | } 47 | 48 | return hash 49 | 50 | } 51 | 52 | getPreviousBlock() { 53 | return this.blocks[this.blocks.length - 1] 54 | } 55 | 56 | } 57 | 58 | module.exports = Blockchain 59 | -------------------------------------------------------------------------------- /models/transaction.js: -------------------------------------------------------------------------------- 1 | 2 | class Transaction { 3 | 4 | constructor(from,to,amount) { 5 | this.from = from 6 | this.to = to 7 | this.amount = amount 8 | } 9 | 10 | } 11 | 12 | module.exports = Transaction 13 | -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "blockchain-core-engine", 3 | "version": "1.0.0", 4 | "lockfileVersion": 1, 5 | "requires": true, 6 | "dependencies": { 7 | "js-sha256": { 8 | "version": "0.9.0", 9 | "resolved": "https://registry.npmjs.org/js-sha256/-/js-sha256-0.9.0.tgz", 10 | "integrity": "sha512-sga3MHh9sgQN2+pJ9VYZ+1LPwXOxuBJBA5nrR5/ofPfuiJBE2hnjsaN8se8JznOmGLN2p49Pe5U/ttafcs/apA==" 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "blockchain-core-engine", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "app.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "dependencies": { 12 | "js-sha256": "^0.9.0" 13 | } 14 | } 15 | --------------------------------------------------------------------------------