{"id":2033,"date":"2021-09-04T11:14:28","date_gmt":"2021-09-04T11:14:28","guid":{"rendered":"https:\/\/codeinsightacademy.com\/blog\/?p=2033"},"modified":"2021-09-04T11:14:32","modified_gmt":"2021-09-04T11:14:32","slug":"blockchain-demo","status":"publish","type":"post","link":"https:\/\/codeinsightacademy.com\/blog\/javascript\/blockchain-demo\/","title":{"rendered":"Blockchain Demo"},"content":{"rendered":"\n<p>main.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const SHA256 = require('crypto-js\/sha256');\r\n\r\nclass Block {\r\n\t\r\n\tconstructor(index, timestamp, data, prevHash = '') {\r\n\t\tthis.index = index;\r\n\t\tthis.timestamp = timestamp;\r\n\t\tthis.data = data;\r\n\t\tthis.prevHash = prevHash;\r\n\t\tthis.hash = this.calculateHash();\r\n\t}\r\n\t\r\n\tcalculateHash() {\r\n\t\treturn SHA256(this.index + this.prevHash + this.timestamp + JSON.stringify(this.data)).toString();\r\n\t}\r\n\t\r\n}\r\n\r\nclass Blockchain {\r\n\tconstructor() {\r\n\t\tthis.chain = &#91;this.createGenesisBlock()];\r\n\t}\r\n\t\r\n\tcreateGenesisBlock() {\r\n\t\treturn new Block(0, \"01\/01\/2021\", \"Genesis block\", \"0\");\r\n\t}\r\n\t\r\n\tgetLatestBlock() {\r\n\t\treturn this.chain&#91;this.chain.length - 1];\r\n\t}\r\n\t\r\n\taddBlock(newBlock) {\r\n\t\t\tnewBlock.prevHash = this.getLatestBlock().hash;\r\n\t\t\tnewBlock.hash = newBlock.calculateHash();\r\n\t\t\tthis.chain.push(newBlock);\r\n\t}\r\n\t\r\n\tisChainValid() {\r\n\t\tfor(let i=1; i &lt; this.chain.length; i++) {\r\n\t\t\tconst currentBlock = this.chain&#91;i];\r\n\t\t\tconst prevBlock = this.chain&#91;i-1];\r\n\t\t\t\r\n\t\t\tif(currentBlock.hash !== currentBlock.calculateHash()) {\r\n\t\t\t\treturn false;\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\tif(currentBlock.prevHash != prevBlock.hash) {\r\n\t\t\t\treturn false;\r\n\t\t\t}\r\n\t\t}\r\n\t\t\r\n\t\treturn true;\r\n\t}\r\n\t\r\n}\r\n\r\nlet tcoin = new Blockchain();\r\ntcoin.addBlock(new Block(1, \"03\/09\/2021\", {amt: 100}));\r\ntcoin.addBlock(new Block(1, \"04\/09\/2021\", {amt: 200}));\r\n\r\nconsole.log(tcoin.isChainValid());\r\nconsole.log(JSON.stringify(tcoin, null, 4));\r\n\r\n\/\/tempering data\r\ntcoin.chain&#91;2].amt = 2000;\r\nconsole.log(JSON.stringify(tcoin, null, 4));\r\nconsole.log(tcoin.isChainValid());<\/code><\/pre>\n\n\n\n<p>Proof of Work<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const SHA256 = require('crypto-js\/sha256');\r\n\r\nclass Block {\r\n\t\r\n\tconstructor(index, timestamp, data, prevHash = '') {\r\n\t\tthis.index = index;\r\n\t\tthis.timestamp = timestamp;\r\n\t\tthis.data = data;\r\n\t\tthis.prevHash = prevHash;\r\n\t\tthis.hash = this.calculateHash();\r\n\t\tthis.nonce = 0;\r\n\t}\r\n\t\r\n\tcalculateHash() {\r\n\t\treturn SHA256(this.index + this.prevHash + this.timestamp + JSON.stringify(this.data) + this.nonce).toString();\r\n\t}\r\n\t\r\n\tmineBlock(complexity) {\r\n\t\twhile(this.hash.substring(0, complexity) !== Array(complexity + 1).join(\"0\")) {\r\n\t\t\tthis.nonce++;\r\n\t\t\t\/\/console.log(this.nonce);\r\n\t\t\tthis.hash = this.calculateHash();\r\n\t\t}\r\n\t\t\r\n\t\tconsole.log(`Block mined: ${this.hash}`);\r\n\t}\r\n\t\r\n}\r\n\r\nclass Blockchain {\r\n\tconstructor(complexity = 0) {\r\n\t\tthis.chain = &#91;this.createGenesisBlock()];\r\n\t\tthis.complexity = complexity;\r\n\t}\r\n\t\r\n\tcreateGenesisBlock() {\r\n\t\treturn new Block(0, \"01\/01\/2021\", \"Genesis block\", \"0\");\r\n\t}\r\n\t\r\n\tgetLatestBlock() {\r\n\t\treturn this.chain&#91;this.chain.length - 1];\r\n\t}\r\n\t\r\n\taddBlock(newBlock) {\r\n\t\t\tnewBlock.prevHash = this.getLatestBlock().hash;\r\n\t\t\t\/\/newBlock.hash = newBlock.calculateHash();\r\n\t\t\tnewBlock.mineBlock(this.complexity);\r\n\t\t\tthis.chain.push(newBlock);\r\n\t}\r\n\t\r\n\tisChainValid() {\r\n\t\tfor(let i=1; i &lt; this.chain.length; i++) {\r\n\t\t\tconst currentBlock = this.chain&#91;i];\r\n\t\t\tconst prevBlock = this.chain&#91;i-1];\r\n\t\t\t\r\n\t\t\tif(currentBlock.hash !== currentBlock.calculateHash()) {\r\n\t\t\t\treturn false;\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\tif(currentBlock.prevHash != prevBlock.hash) {\r\n\t\t\t\treturn false;\r\n\t\t\t}\r\n\t\t}\r\n\t\t\r\n\t\treturn true;\r\n\t}\r\n\t\r\n}\r\n\r\nlet tcoin = new Blockchain(5);\r\n\r\nconsole.log(\"Mining Block 1...\");\r\ntcoin.addBlock(new Block(1, \"03\/09\/2021\", {amt: 100}));\r\n\r\nconsole.log(\"Mining Block 2...\");\r\ntcoin.addBlock(new Block(1, \"04\/09\/2021\", {amt: 200}));\r\n\r\nconsole.log(JSON.stringify(tcoin, null, 4));<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>main.js Proof of Work<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[18],"tags":[],"_links":{"self":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/2033"}],"collection":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/comments?post=2033"}],"version-history":[{"count":1,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/2033\/revisions"}],"predecessor-version":[{"id":2034,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/2033\/revisions\/2034"}],"wp:attachment":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/media?parent=2033"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/categories?post=2033"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/tags?post=2033"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}