From 5fb51db498e65808037b03b5c519bd77433dd9b1 Mon Sep 17 00:00:00 2001 From: Wendy Huang <65386301+WendyH1108@users.noreply.github.com> Date: Thu, 15 Oct 2020 14:43:24 +0800 Subject: [PATCH 1/7] Add files via upload --- server/blockchain.js | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/server/blockchain.js b/server/blockchain.js index 3d4c7bb..6a74a24 100644 --- a/server/blockchain.js +++ b/server/blockchain.js @@ -1,9 +1,30 @@ const Block = require("./block.js"); +const con = mysql.createConnection({ + host: 'cbdc.cjymkpun4qnd.us-east-1.rds.amazonaws.com', + user: 'admin', + port: 3306, + password: 'cornellblockchain', + database: 'blockchain' +}); class Blockchain { constructor() { - this.chain = [Block.genesis()]; - } + con.connect((err) => { + if(err){ console.log('Cannot connect to database');} + }); + con.query('SELECT * FROM chain', (err,rows) => { + if(err) throw err; + rows.forEach( (row) => { + if(row){ + let block= new Block(row.timestamp, row.lastHash, row.data, row.hash); + this.chain.push(block); + } + else{this.chain = [Block.genesis()];} + + }); + }); + } + addBlock(data) { const block = Block.mineBlock(this.chain[this.chain.length - 1], data); From da83806ca497274cb3e0f88fb6491e6f6cf64d9f Mon Sep 17 00:00:00 2001 From: Wendy Huang <65386301+WendyH1108@users.noreply.github.com> Date: Thu, 15 Oct 2020 15:04:14 +0800 Subject: [PATCH 2/7] Add files via upload --- server/blockchain.js | 1 + 1 file changed, 1 insertion(+) diff --git a/server/blockchain.js b/server/blockchain.js index 6a74a24..801ac80 100644 --- a/server/blockchain.js +++ b/server/blockchain.js @@ -9,6 +9,7 @@ const con = mysql.createConnection({ class Blockchain { constructor() { + this.chain=[]; con.connect((err) => { if(err){ console.log('Cannot connect to database');} }); From 067eeee2ddd248db2ecd00f8dd8139e6fb1bb08b Mon Sep 17 00:00:00 2001 From: Wendy Huang <65386301+WendyH1108@users.noreply.github.com> Date: Wed, 28 Oct 2020 16:19:25 +0800 Subject: [PATCH 3/7] Add files via upload --- api.js | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 api.js diff --git a/api.js b/api.js new file mode 100644 index 0000000..e4752e0 --- /dev/null +++ b/api.js @@ -0,0 +1,54 @@ +const express = require("express"); +const bodyParser = require("body-parser"); +const Wallet = require("./wallet"); +const Transaction = require("./transaction") +const Blockchain = require("./blockchain"); +const Block = require("./block"); + +//get the port from the user or set the default port +const HTTP_PORT = process.env.HTTP_PORT || 3001; + +//create a new app +const app = express(); + +//using the blody parser middleware +app.use(bodyParser.json()); + +//EXPOSED APIs + +app.get("/getNewWallet", (req, res) => { + const w = new Wallet(); + const w2 = new Wallet( + "6156fecec41ea1efe37c6fdef285dd9208f864c64453235ee234a1502743cdbf" + ); + res.json({ one: w.getKeyPairJSON(), two: w2.getKeyPairJSON() }); +}); + +app.post("/mine", (req, res) => { + res.json("sdfsdf"); +}); + +// app server configurations +app.listen(HTTP_PORT, () => { + console.log(`listening on port ${HTTP_PORT}`); +}); + +app.post("/transact", (req, res) =>{ + try{ + const wallet=new Wallet(req.body.senderPrivateKey); + const trans=new Transaction(); + trans.newTransaction(wallet, req.body.recipientPublicKey, req.body.amount); + const bc=Blockchain(Block.genesis()); + bc.addBlock(trans); + + res.json({ + "status": 200, + }); + } + catch(err) { + res.json({ + "status": 400, + "error": err.message, + }); + } +}); \ No newline at end of file From 078b706923bc84beefcd67d3b2e5981614607c19 Mon Sep 17 00:00:00 2001 From: Wendy Huang <65386301+WendyH1108@users.noreply.github.com> Date: Wed, 28 Oct 2020 16:20:23 +0800 Subject: [PATCH 4/7] Delete api.js --- api.js | 54 ------------------------------------------------------ 1 file changed, 54 deletions(-) delete mode 100644 api.js diff --git a/api.js b/api.js deleted file mode 100644 index e4752e0..0000000 --- a/api.js +++ /dev/null @@ -1,54 +0,0 @@ -const express = require("express"); -const bodyParser = require("body-parser"); -const Wallet = require("./wallet"); -const Transaction = require("./transaction") -const Blockchain = require("./blockchain"); -const Block = require("./block"); - -//get the port from the user or set the default port -const HTTP_PORT = process.env.HTTP_PORT || 3001; - -//create a new app -const app = express(); - -//using the blody parser middleware -app.use(bodyParser.json()); - -//EXPOSED APIs - -app.get("/getNewWallet", (req, res) => { - const w = new Wallet(); - const w2 = new Wallet( - "6156fecec41ea1efe37c6fdef285dd9208f864c64453235ee234a1502743cdbf" - ); - res.json({ one: w.getKeyPairJSON(), two: w2.getKeyPairJSON() }); -}); - -app.post("/mine", (req, res) => { - res.json("sdfsdf"); -}); - -// app server configurations -app.listen(HTTP_PORT, () => { - console.log(`listening on port ${HTTP_PORT}`); -}); - -app.post("/transact", (req, res) =>{ - try{ - const wallet=new Wallet(req.body.senderPrivateKey); - const trans=new Transaction(); - trans.newTransaction(wallet, req.body.recipientPublicKey, req.body.amount); - const bc=Blockchain(Block.genesis()); - bc.addBlock(trans); - - res.json({ - "status": 200, - }); - } - catch(err) { - res.json({ - "status": 400, - "error": err.message, - }); - } -}); \ No newline at end of file From f3321a1baeb0dd8d0f5f491a7b71dcda37357ca5 Mon Sep 17 00:00:00 2001 From: Wendy Huang <65386301+WendyH1108@users.noreply.github.com> Date: Wed, 28 Oct 2020 16:20:54 +0800 Subject: [PATCH 5/7] Add files via upload --- server/api.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/server/api.js b/server/api.js index 6f90ff8..e4752e0 100644 --- a/server/api.js +++ b/server/api.js @@ -1,6 +1,9 @@ const express = require("express"); const bodyParser = require("body-parser"); const Wallet = require("./wallet"); +const Transaction = require("./transaction") +const Blockchain = require("./blockchain"); +const Block = require("./block"); //get the port from the user or set the default port const HTTP_PORT = process.env.HTTP_PORT || 3001; @@ -29,3 +32,23 @@ app.post("/mine", (req, res) => { app.listen(HTTP_PORT, () => { console.log(`listening on port ${HTTP_PORT}`); }); + +app.post("/transact", (req, res) =>{ + try{ + const wallet=new Wallet(req.body.senderPrivateKey); + const trans=new Transaction(); + trans.newTransaction(wallet, req.body.recipientPublicKey, req.body.amount); + const bc=Blockchain(Block.genesis()); + bc.addBlock(trans); + + res.json({ + "status": 200, + }); + } + catch(err) { + res.json({ + "status": 400, + "error": err.message, + }); + } +}); \ No newline at end of file From 11e4a63228fcec076e481ab082387f89e05ce174 Mon Sep 17 00:00:00 2001 From: Wendy Huang <65386301+WendyH1108@users.noreply.github.com> Date: Wed, 28 Oct 2020 16:26:19 +0800 Subject: [PATCH 6/7] Update blockchain.js --- server/blockchain.js | 36 ++++++++++-------------------------- 1 file changed, 10 insertions(+), 26 deletions(-) diff --git a/server/blockchain.js b/server/blockchain.js index 801ac80..f724982 100644 --- a/server/blockchain.js +++ b/server/blockchain.js @@ -1,31 +1,10 @@ const Block = require("./block.js"); -const con = mysql.createConnection({ - host: 'cbdc.cjymkpun4qnd.us-east-1.rds.amazonaws.com', - user: 'admin', - port: 3306, - password: 'cornellblockchain', - database: 'blockchain' -}); +const mysql = require('mysql'); class Blockchain { - constructor() { - this.chain=[]; - con.connect((err) => { - if(err){ console.log('Cannot connect to database');} - }); - con.query('SELECT * FROM chain', (err,rows) => { - if(err) throw err; - rows.forEach( (row) => { - if(row){ - let block= new Block(row.timestamp, row.lastHash, row.data, row.hash); - this.chain.push(block); - } - else{this.chain = [Block.genesis()];} - - }); - }); - } - + constructor(chain) { + this.chain = chain; + } addBlock(data) { const block = Block.mineBlock(this.chain[this.chain.length - 1], data); @@ -34,6 +13,11 @@ class Blockchain { return block; } + /** + * Given a chain, return true if it is valid + * @param {array} chain + * @returns {boolean} + */ isValidChain(chain) { if (JSON.stringify(chain[0]) !== JSON.stringify(Block.genesis())) return false; @@ -44,7 +28,7 @@ class Blockchain { if ( block.lastHash !== lastBlock.hash || block.hash !== Block.blockHash(block) - ) + ) return false; } From 9794eb7f910d2e561cf852b304c7459fff97aa61 Mon Sep 17 00:00:00 2001 From: Wendy Huang <65386301+WendyH1108@users.noreply.github.com> Date: Wed, 28 Oct 2020 16:28:22 +0800 Subject: [PATCH 7/7] Update api.js --- server/api.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/api.js b/server/api.js index e4752e0..4e1af42 100644 --- a/server/api.js +++ b/server/api.js @@ -51,4 +51,4 @@ app.post("/transact", (req, res) =>{ "error": err.message, }); } -}); \ No newline at end of file +});