From ea97f0fea96368a0938c04900064beb953ee4f0b Mon Sep 17 00:00:00 2001 From: Chaitanya Gambali Date: Tue, 6 Jun 2023 21:22:00 +0530 Subject: [PATCH] first commit --- blockchain.json | 5 ++++- jest-html-reporters-attach/index/result.js | 2 +- package-lock.json | 11 +++++++++++ package.json | 1 + src/add-block.js | 14 +++++++++++++- src/add-transaction.js | 6 +++++- src/init-blockchain.js | 11 ++++++++++- transactions.json | 8 +++++++- 8 files changed, 52 insertions(+), 6 deletions(-) diff --git a/blockchain.json b/blockchain.json index 64d7ef3..155a024 100644 --- a/blockchain.json +++ b/blockchain.json @@ -1,6 +1,9 @@ [ { + "index": 0, + "timestamp": 1686066693618, "hash": "0", - "previousHash": null + "previousHash": null, + "transactions": [] } ] \ No newline at end of file diff --git a/jest-html-reporters-attach/index/result.js b/jest-html-reporters-attach/index/result.js index dafe634..0dad501 100644 --- a/jest-html-reporters-attach/index/result.js +++ b/jest-html-reporters-attach/index/result.js @@ -1 +1 @@ -window.jest_html_reporters_callback__({"numFailedTestSuites":3,"numFailedTests":3,"numPassedTestSuites":0,"numPassedTests":0,"numPendingTestSuites":0,"numPendingTests":0,"numRuntimeErrorTestSuites":0,"numTodoTests":0,"numTotalTestSuites":3,"numTotalTests":3,"startTime":1685453786212,"success":false,"testResults":[{"numFailingTests":1,"numPassingTests":0,"numPendingTests":0,"numTodoTests":0,"perfStats":{"end":1685453786803,"runtime":422,"slow":false,"start":1685453786381},"testFilePath":"/home/eshaan/Desktop/week-0-web3/tests/init-blockchain.test.js","failureMessage":"\u001b[1m\u001b[31m \u001b[1m● \u001b[22m\u001b[1mcorrectly initialised a blockchain\u001b[39m\u001b[22m\n\n ReferenceError: blockchain is not defined\n\u001b[2m\u001b[22m\n\u001b[2m \u001b[0m \u001b[90m 8 |\u001b[39m \t\u001b[90m// Create a blockchain constant containing genesisBlock\u001b[39m\u001b[0m\u001b[22m\n\u001b[2m \u001b[0m \u001b[90m 9 |\u001b[39m \t\u001b[90m// Refer writeBlockchain function from blockchain-helpers\u001b[39m\u001b[0m\u001b[22m\n\u001b[2m \u001b[0m\u001b[31m\u001b[1m>\u001b[22m\u001b[2m\u001b[39m\u001b[90m 10 |\u001b[39m \twriteBlockchain(blockchain)\u001b[33m;\u001b[39m\u001b[0m\u001b[22m\n\u001b[2m \u001b[0m \u001b[90m |\u001b[39m \t \u001b[31m\u001b[1m^\u001b[22m\u001b[2m\u001b[39m\u001b[0m\u001b[22m\n\u001b[2m \u001b[0m \u001b[90m 11 |\u001b[39m }\u001b[33m;\u001b[39m\u001b[0m\u001b[22m\n\u001b[2m \u001b[0m \u001b[90m 12 |\u001b[39m\u001b[0m\u001b[22m\n\u001b[2m \u001b[0m \u001b[90m 13 |\u001b[39m module\u001b[33m.\u001b[39mexports \u001b[33m=\u001b[39m { initBlockchain }\u001b[33m;\u001b[39m\u001b[0m\u001b[22m\n\u001b[2m\u001b[22m\n\u001b[2m \u001b[2mat blockchain (\u001b[22m\u001b[2msrc/init-blockchain.js\u001b[2m:10:18)\u001b[22m\u001b[2m\u001b[22m\n\u001b[2m \u001b[2mat Object.initBlockchain (\u001b[22m\u001b[2m\u001b[0m\u001b[36mtests/init-blockchain.test.js\u001b[39m\u001b[0m\u001b[2m:5:2)\u001b[22m\u001b[2m\u001b[22m\n","testResults":[{"ancestorTitles":[],"duration":1,"failureMessages":["ReferenceError: blockchain is not defined\n at blockchain (/home/eshaan/Desktop/week-0-web3/src/init-blockchain.js:10:18)\n at Object.initBlockchain (/home/eshaan/Desktop/week-0-web3/tests/init-blockchain.test.js:5:2)\n at Promise.then.completed (/home/eshaan/Desktop/week-0-web3/node_modules/jest-circus/build/utils.js:293:28)\n at new Promise ()\n at callAsyncCircusFn (/home/eshaan/Desktop/week-0-web3/node_modules/jest-circus/build/utils.js:226:10)\n at _callCircusTest (/home/eshaan/Desktop/week-0-web3/node_modules/jest-circus/build/run.js:297:40)\n at processTicksAndRejections (node:internal/process/task_queues:95:5)\n at _runTest (/home/eshaan/Desktop/week-0-web3/node_modules/jest-circus/build/run.js:233:3)\n at _runTestsForDescribeBlock (/home/eshaan/Desktop/week-0-web3/node_modules/jest-circus/build/run.js:135:9)\n at run (/home/eshaan/Desktop/week-0-web3/node_modules/jest-circus/build/run.js:68:3)\n at runAndTransformResultsToJestFormat (/home/eshaan/Desktop/week-0-web3/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapterInit.js:122:21)\n at jestAdapter (/home/eshaan/Desktop/week-0-web3/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapter.js:79:19)\n at runTestInternal (/home/eshaan/Desktop/week-0-web3/node_modules/jest-runner/build/runTest.js:367:16)\n at runTest (/home/eshaan/Desktop/week-0-web3/node_modules/jest-runner/build/runTest.js:444:34)\n at Object.worker (/home/eshaan/Desktop/week-0-web3/node_modules/jest-runner/build/testWorker.js:106:12)"],"fullName":"correctly initialised a blockchain","status":"failed","title":"correctly initialised a blockchain"}]},{"numFailingTests":1,"numPassingTests":0,"numPendingTests":0,"numTodoTests":0,"perfStats":{"end":1685453786807,"runtime":428,"slow":false,"start":1685453786379},"testFilePath":"/home/eshaan/Desktop/week-0-web3/tests/add-block.test.js","failureMessage":"\u001b[1m\u001b[31m \u001b[1m● \u001b[22m\u001b[1msuccessfuly added a block\u001b[39m\u001b[22m\n\n TypeError: Cannot read properties of undefined (reading 'hash')\n\u001b[2m\u001b[22m\n\u001b[2m \u001b[0m \u001b[90m 9 |\u001b[39m\u001b[0m\u001b[22m\n\u001b[2m \u001b[0m \u001b[90m 10 |\u001b[39m \texpect(addedBlock\u001b[33m.\u001b[39mhash)\u001b[33m.\u001b[39mnot\u001b[33m.\u001b[39mtoBe()\u001b[33m;\u001b[39m\u001b[0m\u001b[22m\n\u001b[2m \u001b[0m\u001b[31m\u001b[1m>\u001b[22m\u001b[2m\u001b[39m\u001b[90m 11 |\u001b[39m \texpect(addedBlock\u001b[33m.\u001b[39mpreviousHash)\u001b[33m.\u001b[39mtoBe(previousBlock\u001b[33m.\u001b[39mhash)\u001b[33m;\u001b[39m\u001b[0m\u001b[22m\n\u001b[2m \u001b[0m \u001b[90m |\u001b[39m \t \u001b[31m\u001b[1m^\u001b[22m\u001b[2m\u001b[39m\u001b[0m\u001b[22m\n\u001b[2m \u001b[0m \u001b[90m 12 |\u001b[39m \texpect(addedBlock\u001b[33m.\u001b[39mtransactions)\u001b[33m.\u001b[39mtoMatchObject([\u001b[0m\u001b[22m\n\u001b[2m \u001b[0m \u001b[90m 13 |\u001b[39m \t\t{\u001b[0m\u001b[22m\n\u001b[2m \u001b[0m \u001b[90m 14 |\u001b[39m \t\t\tfromAddress\u001b[33m:\u001b[39m \u001b[32m\"123\"\u001b[39m\u001b[33m,\u001b[39m\u001b[0m\u001b[22m\n\u001b[2m\u001b[22m\n\u001b[2m \u001b[2mat Object.hash (\u001b[22m\u001b[2m\u001b[0m\u001b[36mtests/add-block.test.js\u001b[39m\u001b[0m\u001b[2m:11:53)\u001b[22m\u001b[2m\u001b[22m\n","testResults":[{"ancestorTitles":[],"duration":2,"failureMessages":["TypeError: Cannot read properties of undefined (reading 'hash')\n at Object.hash (/home/eshaan/Desktop/week-0-web3/tests/add-block.test.js:11:53)\n at Promise.then.completed (/home/eshaan/Desktop/week-0-web3/node_modules/jest-circus/build/utils.js:293:28)\n at new Promise ()\n at callAsyncCircusFn (/home/eshaan/Desktop/week-0-web3/node_modules/jest-circus/build/utils.js:226:10)\n at _callCircusTest (/home/eshaan/Desktop/week-0-web3/node_modules/jest-circus/build/run.js:297:40)\n at processTicksAndRejections (node:internal/process/task_queues:95:5)\n at _runTest (/home/eshaan/Desktop/week-0-web3/node_modules/jest-circus/build/run.js:233:3)\n at _runTestsForDescribeBlock (/home/eshaan/Desktop/week-0-web3/node_modules/jest-circus/build/run.js:135:9)\n at run (/home/eshaan/Desktop/week-0-web3/node_modules/jest-circus/build/run.js:68:3)\n at runAndTransformResultsToJestFormat (/home/eshaan/Desktop/week-0-web3/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapterInit.js:122:21)\n at jestAdapter (/home/eshaan/Desktop/week-0-web3/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapter.js:79:19)\n at runTestInternal (/home/eshaan/Desktop/week-0-web3/node_modules/jest-runner/build/runTest.js:367:16)\n at runTest (/home/eshaan/Desktop/week-0-web3/node_modules/jest-runner/build/runTest.js:444:34)\n at Object.worker (/home/eshaan/Desktop/week-0-web3/node_modules/jest-runner/build/testWorker.js:106:12)"],"fullName":"successfuly added a block","status":"failed","title":"successfuly added a block"}]},{"numFailingTests":1,"numPassingTests":0,"numPendingTests":0,"numTodoTests":0,"perfStats":{"end":1685453786811,"runtime":431,"slow":false,"start":1685453786380},"testFilePath":"/home/eshaan/Desktop/week-0-web3/tests/add-transaction.test.js","failureMessage":"\u001b[1m\u001b[31m \u001b[1m● \u001b[22m\u001b[1mtesting add-transaction\u001b[39m\u001b[22m\n\n \u001b[2mexpect(\u001b[22m\u001b[31mreceived\u001b[39m\u001b[2m).\u001b[22mtoMatchObject\u001b[2m(\u001b[22m\u001b[32mexpected\u001b[39m\u001b[2m)\u001b[22m\n\n \u001b[1mMatcher error\u001b[22m: \u001b[31mreceived\u001b[39m value must be a non-null object\n\n Received has value: \u001b[31mundefined\u001b[39m\n\u001b[2m\u001b[22m\n\u001b[2m \u001b[0m \u001b[90m 8 |\u001b[39m \t\u001b[36mconst\u001b[39m addedTransaction \u001b[33m=\u001b[39m transactions[transactions\u001b[33m.\u001b[39mlength \u001b[33m-\u001b[39m \u001b[35m1\u001b[39m]\u001b[33m;\u001b[39m\u001b[0m\u001b[22m\n\u001b[2m \u001b[0m \u001b[90m 9 |\u001b[39m\u001b[0m\u001b[22m\n\u001b[2m \u001b[0m\u001b[31m\u001b[1m>\u001b[22m\u001b[2m\u001b[39m\u001b[90m 10 |\u001b[39m \texpect(addedTransaction)\u001b[33m.\u001b[39mtoMatchObject({\u001b[0m\u001b[22m\n\u001b[2m \u001b[0m \u001b[90m |\u001b[39m \t \u001b[31m\u001b[1m^\u001b[22m\u001b[2m\u001b[39m\u001b[0m\u001b[22m\n\u001b[2m \u001b[0m \u001b[90m 11 |\u001b[39m \t\tfromAddress\u001b[33m:\u001b[39m \u001b[32m\"123\"\u001b[39m\u001b[33m,\u001b[39m\u001b[0m\u001b[22m\n\u001b[2m \u001b[0m \u001b[90m 12 |\u001b[39m \t\ttoAddress\u001b[33m:\u001b[39m \u001b[32m\"456\"\u001b[39m\u001b[33m,\u001b[39m\u001b[0m\u001b[22m\n\u001b[2m \u001b[0m \u001b[90m 13 |\u001b[39m \t\tamount\u001b[33m:\u001b[39m \u001b[35m789\u001b[39m\u001b[33m,\u001b[39m\u001b[0m\u001b[22m\n\u001b[2m\u001b[22m\n\u001b[2m \u001b[2mat Object.toMatchObject (\u001b[22m\u001b[2m\u001b[0m\u001b[36mtests/add-transaction.test.js\u001b[39m\u001b[0m\u001b[2m:10:27)\u001b[22m\u001b[2m\u001b[22m\n","testResults":[{"ancestorTitles":[],"duration":4,"failureMessages":["Error: \u001b[2mexpect(\u001b[22m\u001b[31mreceived\u001b[39m\u001b[2m).\u001b[22mtoMatchObject\u001b[2m(\u001b[22m\u001b[32mexpected\u001b[39m\u001b[2m)\u001b[22m\n\n\u001b[1mMatcher error\u001b[22m: \u001b[31mreceived\u001b[39m value must be a non-null object\n\nReceived has value: \u001b[31mundefined\u001b[39m\n at Object.toMatchObject (/home/eshaan/Desktop/week-0-web3/tests/add-transaction.test.js:10:27)\n at Promise.then.completed (/home/eshaan/Desktop/week-0-web3/node_modules/jest-circus/build/utils.js:293:28)\n at new Promise ()\n at callAsyncCircusFn (/home/eshaan/Desktop/week-0-web3/node_modules/jest-circus/build/utils.js:226:10)\n at _callCircusTest (/home/eshaan/Desktop/week-0-web3/node_modules/jest-circus/build/run.js:297:40)\n at processTicksAndRejections (node:internal/process/task_queues:95:5)\n at _runTest (/home/eshaan/Desktop/week-0-web3/node_modules/jest-circus/build/run.js:233:3)\n at _runTestsForDescribeBlock (/home/eshaan/Desktop/week-0-web3/node_modules/jest-circus/build/run.js:135:9)\n at run (/home/eshaan/Desktop/week-0-web3/node_modules/jest-circus/build/run.js:68:3)\n at runAndTransformResultsToJestFormat (/home/eshaan/Desktop/week-0-web3/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapterInit.js:122:21)\n at jestAdapter (/home/eshaan/Desktop/week-0-web3/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapter.js:79:19)\n at runTestInternal (/home/eshaan/Desktop/week-0-web3/node_modules/jest-runner/build/runTest.js:367:16)\n at runTest (/home/eshaan/Desktop/week-0-web3/node_modules/jest-runner/build/runTest.js:444:34)\n at Object.worker (/home/eshaan/Desktop/week-0-web3/node_modules/jest-runner/build/testWorker.js:106:12)"],"fullName":"testing add-transaction","status":"failed","title":"testing add-transaction"}]}],"config":{"bail":0,"changedFilesWithAncestor":false,"ci":false,"collectCoverage":false,"collectCoverageFrom":[],"coverageDirectory":"/home/eshaan/Desktop/week-0-web3/coverage","coverageProvider":"babel","coverageReporters":["json","text","lcov","clover"],"detectLeaks":false,"detectOpenHandles":false,"errorOnDeprecated":false,"expand":false,"findRelatedTests":false,"forceExit":false,"json":false,"lastCommit":false,"listTests":false,"logHeapUsage":false,"maxConcurrency":5,"maxWorkers":7,"noStackTrace":false,"nonFlagArgs":[],"notify":false,"notifyMode":"failure-change","onlyChanged":false,"onlyFailures":false,"openHandlesTimeout":1000,"passWithNoTests":false,"projects":[],"reporters":[["default",{}],["/home/eshaan/Desktop/week-0-web3/node_modules/jest-html-reporters/index.js",{"filename":"index.html","pageTitle":"Web 3 - Week 0","openReport":true,"hideIcon":true}]],"rootDir":"/home/eshaan/Desktop/week-0-web3","runTestsByPath":false,"seed":-1168676141,"skipFilter":false,"snapshotFormat":{"escapeString":false,"printBasicPrototype":false},"testFailureExitCode":1,"testPathPattern":"","testSequencer":"/home/eshaan/Desktop/week-0-web3/node_modules/@jest/test-sequencer/build/index.js","updateSnapshot":"new","useStderr":false,"watch":false,"watchAll":false,"watchman":true,"workerThreads":false},"endTime":1685453786832,"_reporterOptions":{"publicPath":"/home/eshaan/Desktop/week-0-web3","filename":"index.html","expand":false,"pageTitle":"Web 3 - Week 0","hideIcon":true,"testCommand":"","openReport":true,"failureMessageOnly":0,"enableMergeData":false,"dataMergeLevel":1,"inlineSource":false,"urlForTestFiles":"","darkTheme":false,"includeConsoleLog":false},"logInfoMapping":{},"attachInfos":{}}) \ No newline at end of file +window.jest_html_reporters_callback__({"numFailedTestSuites":0,"numFailedTests":0,"numPassedTestSuites":3,"numPassedTests":3,"numPendingTestSuites":0,"numPendingTests":0,"numRuntimeErrorTestSuites":0,"numTodoTests":0,"numTotalTestSuites":3,"numTotalTests":3,"startTime":1686066692909,"success":false,"testResults":[{"numFailingTests":0,"numPassingTests":1,"numPendingTests":0,"numTodoTests":0,"perfStats":{"end":1686066693329,"runtime":360,"slow":false,"start":1686066692969},"testFilePath":"C:\\Users\\gscha\\OneDrive\\Documents\\CSOC\\web3\\week0\\week-0-web3\\tests\\add-block.test.js","failureMessage":null,"testResults":[{"ancestorTitles":[],"duration":5,"failureMessages":[],"fullName":"successfuly added a block","status":"passed","title":"successfuly added a block"}]},{"numFailingTests":0,"numPassingTests":1,"numPendingTests":0,"numTodoTests":0,"perfStats":{"end":1686066693454,"runtime":106,"slow":false,"start":1686066693348},"testFilePath":"C:\\Users\\gscha\\OneDrive\\Documents\\CSOC\\web3\\week0\\week-0-web3\\tests\\add-transaction.test.js","failureMessage":null,"testResults":[{"ancestorTitles":[],"duration":3,"failureMessages":[],"fullName":"testing add-transaction","status":"passed","title":"testing add-transaction"}]},{"numFailingTests":0,"numPassingTests":1,"numPendingTests":0,"numTodoTests":0,"perfStats":{"end":1686066693621,"runtime":133,"slow":false,"start":1686066693488},"testFilePath":"C:\\Users\\gscha\\OneDrive\\Documents\\CSOC\\web3\\week0\\week-0-web3\\tests\\init-blockchain.test.js","failureMessage":null,"testResults":[{"ancestorTitles":[],"duration":2,"failureMessages":[],"fullName":"correctly initialised a blockchain","status":"passed","title":"correctly initialised a blockchain"}]}],"config":{"bail":0,"changedFilesWithAncestor":false,"ci":false,"collectCoverage":false,"collectCoverageFrom":[],"coverageDirectory":"C:\\Users\\gscha\\OneDrive\\Documents\\CSOC\\web3\\week0\\week-0-web3\\coverage","coverageProvider":"babel","coverageReporters":["json","text","lcov","clover"],"detectLeaks":false,"detectOpenHandles":false,"errorOnDeprecated":false,"expand":false,"findRelatedTests":false,"forceExit":false,"json":false,"lastCommit":false,"listTests":false,"logHeapUsage":false,"maxConcurrency":5,"maxWorkers":11,"noStackTrace":false,"nonFlagArgs":[],"notify":false,"notifyMode":"failure-change","onlyChanged":false,"onlyFailures":false,"openHandlesTimeout":1000,"passWithNoTests":false,"projects":[],"reporters":[["default",{}],["C:\\Users\\gscha\\OneDrive\\Documents\\CSOC\\web3\\week0\\week-0-web3\\node_modules\\jest-html-reporters\\index.js",{"filename":"index.html","pageTitle":"Web 3 - Week 0","openReport":true,"hideIcon":true}]],"rootDir":"C:\\Users\\gscha\\OneDrive\\Documents\\CSOC\\web3\\week0\\week-0-web3","runTestsByPath":false,"seed":2013280313,"skipFilter":false,"snapshotFormat":{"escapeString":false,"printBasicPrototype":false},"testFailureExitCode":1,"testPathPattern":"","testSequencer":"C:\\Users\\gscha\\OneDrive\\Documents\\CSOC\\web3\\week0\\week-0-web3\\node_modules\\@jest\\test-sequencer\\build\\index.js","updateSnapshot":"new","useStderr":false,"watch":false,"watchAll":false,"watchman":true,"workerThreads":false},"endTime":1686066693626,"_reporterOptions":{"publicPath":"C:\\Users\\gscha\\OneDrive\\Documents\\CSOC\\web3\\week0\\week-0-web3","filename":"index.html","expand":false,"pageTitle":"Web 3 - Week 0","hideIcon":true,"testCommand":"","openReport":true,"failureMessageOnly":0,"enableMergeData":false,"dataMergeLevel":1,"inlineSource":false,"urlForTestFiles":"","darkTheme":false,"includeConsoleLog":false},"logInfoMapping":{},"attachInfos":{}}) \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 75fc90d..9834fb2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5,6 +5,7 @@ "packages": { "": { "dependencies": { + "crypto-js": "^4.1.1", "fs": "*" }, "devDependencies": { @@ -1457,6 +1458,11 @@ "node": ">= 8" } }, + "node_modules/crypto-js": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/crypto-js/-/crypto-js-4.1.1.tgz", + "integrity": "sha512-o2JlM7ydqd3Qk9CA0L4NL6mTzU2sdx96a+oOfPu8Mkl/PK51vSyoi8/rQ8NknZtk44vq15lmhAj9CIAGwgeWKw==" + }, "node_modules/debug": { "version": "4.3.4", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz", @@ -4676,6 +4682,11 @@ "which": "^2.0.1" } }, + "crypto-js": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/crypto-js/-/crypto-js-4.1.1.tgz", + "integrity": "sha512-o2JlM7ydqd3Qk9CA0L4NL6mTzU2sdx96a+oOfPu8Mkl/PK51vSyoi8/rQ8NknZtk44vq15lmhAj9CIAGwgeWKw==" + }, "debug": { "version": "4.3.4", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz", diff --git a/package.json b/package.json index e0a2f9a..d6e3a7d 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,6 @@ { "dependencies": { + "crypto-js": "^4.1.1", "fs": "*" }, "devDependencies": { diff --git a/src/add-block.js b/src/add-block.js index 2f81b5f..ff0e648 100644 --- a/src/add-block.js +++ b/src/add-block.js @@ -1,3 +1,4 @@ +const { SHA256 } = require("crypto-js"); const { getBlockchain, writeBlockchain, @@ -10,9 +11,20 @@ const addBlock = () => { const previousBlock = blockchain[blockchain.length - 1]; const transactions = getTransactions(); + calculateHash = (block) => { + return SHA256(JSON.stringify(block)); + } + // Create a hash, previousHash and transactions property for newBlock and push that into blockchain + const newBlock = { + index : previousBlock.index + 1, + timestamp : Date.now(), + previousHash : previousBlock.hash, + hash : calculateHash(transactions), + transactions : transactions + } - blockchain.push(); + blockchain.push(newBlock); writeBlockchain(blockchain); writeTransactions([]); }; diff --git a/src/add-transaction.js b/src/add-transaction.js index 85b7549..80d34f8 100644 --- a/src/add-transaction.js +++ b/src/add-transaction.js @@ -9,7 +9,11 @@ const addTransaction = () => { // Refer blockchain-helpers.js for writeTransactions and getTransactions const transactions = getTransactions(); - transactions.push(); + transactions.push({ + fromAddress, + toAddress, + amount + }); writeTransactions(transactions); }; diff --git a/src/init-blockchain.js b/src/init-blockchain.js index 9e6a994..d0fe828 100644 --- a/src/init-blockchain.js +++ b/src/init-blockchain.js @@ -1,11 +1,20 @@ // Start here +const { SHA256 } = require("crypto-js"); const { writeBlockchain } = require("./blockchain-helpers"); const initBlockchain = () => { // Create a genesisBlock and add appropriate hash and previous hash property to genesisBlock - + const genesisBlock = { + index: 0, + timestamp: Date.now(), + hash: "0", + previousHash: null, + transactions: [] + } + // Create a blockchain constant containing genesisBlock + const blockchain = [genesisBlock]; // Refer writeBlockchain function from blockchain-helpers writeBlockchain(blockchain); }; diff --git a/transactions.json b/transactions.json index 0637a08..2416fc0 100644 --- a/transactions.json +++ b/transactions.json @@ -1 +1,7 @@ -[] \ No newline at end of file +[ + { + "fromAddress": "123", + "toAddress": "456", + "amount": 789 + } +] \ No newline at end of file