Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions server/api.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

first it should be new Blockchain(...) and also it should not be initalized to genesis block. It should query the MySQL database and initalize the Blockchain object with the data fetched from the db

bc.addBlock(trans);

res.json({
"status": 200,
});
}
catch(err) {
res.json({
"status": 400,
"error": err.message,
});
}
});
12 changes: 9 additions & 3 deletions server/blockchain.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const Block = require("./block.js");
const mysql = require('mysql');

class Blockchain {
constructor() {
this.chain = [Block.genesis()];
constructor(chain) {
this.chain = chain;
}

addBlock(data) {
Expand All @@ -12,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;
Expand All @@ -22,7 +28,7 @@ class Blockchain {
if (
block.lastHash !== lastBlock.hash ||
block.hash !== Block.blockHash(block)
)
)
return false;
}

Expand Down