2022-11-10 01:31:47 +01:00
|
|
|
#include "crow.h"
|
2022-12-11 17:04:21 +01:00
|
|
|
#include "crow/common.h"
|
|
|
|
#include "crow/http_response.h"
|
2022-11-10 01:31:47 +01:00
|
|
|
#include "model.hpp"
|
|
|
|
|
2022-12-11 17:04:21 +01:00
|
|
|
int main() {
|
|
|
|
crow::SimpleApp app;
|
2023-04-20 22:55:17 +02:00
|
|
|
std::shared_ptr<Model> model = Model::get_model("simple");
|
2022-11-10 01:31:47 +01:00
|
|
|
|
2022-12-11 17:04:21 +01:00
|
|
|
CROW_ROUTE(app, "/cddc")
|
2023-04-20 22:55:17 +02:00
|
|
|
.methods(crow::HTTPMethod::POST)
|
|
|
|
([&model](const crow::request &req) {
|
2022-12-11 17:04:21 +01:00
|
|
|
auto req_cddc = RequestCDDC::from_string(req.body);
|
|
|
|
if (!req_cddc) {
|
|
|
|
return crow::response(crow::status::BAD_REQUEST);
|
|
|
|
} else {
|
|
|
|
ResponseCDDC res;
|
|
|
|
res.message_reference = req_cddc->message_reference;
|
2023-04-20 22:55:17 +02:00
|
|
|
auto cddc = model->get_cddc(req_cddc->cdd_serial);
|
2022-12-11 17:04:21 +01:00
|
|
|
if (!cddc) {
|
|
|
|
res.status_code = crow::status::NOT_FOUND;
|
|
|
|
} else {
|
|
|
|
res.cddc = *cddc.value();
|
|
|
|
res.status_code = crow::status::OK;
|
|
|
|
}
|
|
|
|
return crow::response(res.to_json());
|
|
|
|
}
|
2022-11-10 01:31:47 +01:00
|
|
|
});
|
|
|
|
|
2022-12-11 17:04:21 +01:00
|
|
|
CROW_ROUTE(app, "/cddc/serial")
|
|
|
|
.methods(crow::HTTPMethod::POST)([&model](const crow::request &request) {
|
2022-12-31 13:49:58 +01:00
|
|
|
auto req = RequestCDDCSerial::from_string(request.body);
|
2022-12-11 17:04:21 +01:00
|
|
|
if (!req) {
|
|
|
|
return crow::response(crow::status::BAD_REQUEST);
|
|
|
|
} else {
|
2022-12-31 13:49:58 +01:00
|
|
|
ResponseCDDCSerial res;
|
2022-12-11 17:04:21 +01:00
|
|
|
res.message_reference = req->message_reference;
|
2022-11-10 01:31:47 +01:00
|
|
|
|
2023-04-20 22:55:17 +02:00
|
|
|
auto cddc = model->get_current_cddc();
|
2022-12-11 17:04:21 +01:00
|
|
|
if (!cddc) {
|
|
|
|
res.status_code = crow::status::NOT_FOUND;
|
|
|
|
} else {
|
|
|
|
res.cdd_serial = (*cddc)->cdd.cdd_serial;
|
|
|
|
res.status_code = crow::status::OK;
|
|
|
|
}
|
|
|
|
return crow::response(res.to_json());
|
|
|
|
}
|
2022-11-18 15:47:52 +01:00
|
|
|
});
|
2022-11-10 01:31:47 +01:00
|
|
|
|
2023-04-20 22:55:17 +02:00
|
|
|
CROW_ROUTE(app, "/mkcs").methods(crow::HTTPMethod::POST)
|
|
|
|
([&model](const crow::request &request) {
|
2022-12-11 17:04:21 +01:00
|
|
|
auto req = RequestMKCs::from_string(request.body);
|
|
|
|
if (!req) {
|
|
|
|
return crow::response(crow::status::BAD_REQUEST);
|
|
|
|
} else {
|
|
|
|
ResponseMKCs res;
|
|
|
|
res.message_reference = req->message_reference;
|
|
|
|
res.keys = model->getMKCs(req->denominations, req->mint_key_ids);
|
|
|
|
res.status_code = crow::status::OK;
|
|
|
|
return crow::response(res.to_json());
|
|
|
|
}
|
2022-11-18 15:47:52 +01:00
|
|
|
});
|
2022-11-10 01:31:47 +01:00
|
|
|
|
2023-04-20 22:55:17 +02:00
|
|
|
CROW_ROUTE(app, "/mint").methods(crow::HTTPMethod::POST)
|
|
|
|
([&model](const crow::request &request) {
|
2022-12-11 17:04:21 +01:00
|
|
|
auto req = RequestMint::from_string(request.body);
|
|
|
|
if (!req) {
|
|
|
|
return crow::response(crow::status::BAD_REQUEST);
|
|
|
|
} else {
|
|
|
|
ResponseMint res;
|
|
|
|
res.message_reference = req->message_reference;
|
2022-11-10 01:31:47 +01:00
|
|
|
|
2023-04-20 22:55:17 +02:00
|
|
|
/// \todo change argument transaction_reference to bigint
|
|
|
|
auto minted =
|
|
|
|
model->mint(req->transaction_reference.to_string(), req->blinds);
|
2022-12-11 17:04:21 +01:00
|
|
|
|
|
|
|
res.blind_signatures = minted;
|
|
|
|
res.status_code = crow::status::OK;
|
|
|
|
|
|
|
|
return crow::response(res.to_json());
|
|
|
|
}
|
2022-11-18 15:47:52 +01:00
|
|
|
});
|
2022-11-10 01:31:47 +01:00
|
|
|
|
2022-12-11 17:04:21 +01:00
|
|
|
CROW_ROUTE(app, "/renew")
|
|
|
|
.methods(crow::HTTPMethod::POST)([&model](const crow::request &request) {
|
|
|
|
auto req = RequestRenew::from_string(request.body);
|
|
|
|
if (!req) {
|
|
|
|
return crow::response(crow::status::BAD_REQUEST);
|
|
|
|
} else {
|
|
|
|
// \todo implement ResponseDelay
|
|
|
|
ResponseMint res;
|
|
|
|
res.message_reference = req->message_reference;
|
|
|
|
res.status_code = crow::status::OK;
|
|
|
|
res.blind_signatures =
|
|
|
|
model->mint(req->transaction_reference, req->blinds);
|
|
|
|
return crow::response(res.to_json());
|
|
|
|
}
|
2022-11-18 15:47:52 +01:00
|
|
|
});
|
2022-11-10 01:31:47 +01:00
|
|
|
|
2022-12-11 17:04:21 +01:00
|
|
|
CROW_ROUTE(app, "/resume")
|
|
|
|
.methods(crow::HTTPMethod::POST)([](const crow::request &request) {
|
|
|
|
auto req = RequestResume::from_string(request.body);
|
|
|
|
if (!req) {
|
|
|
|
return crow::response(crow::status::BAD_REQUEST);
|
|
|
|
} else {
|
|
|
|
// \todo implement request
|
|
|
|
ResponseMint res;
|
|
|
|
res.message_reference = req->message_reference;
|
|
|
|
res.status_code = crow::status::NOT_IMPLEMENTED; // crow::status::OK;
|
|
|
|
return crow::response(crow::status::NOT_IMPLEMENTED);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
CROW_ROUTE(app, "/redeem")
|
|
|
|
.methods(crow::HTTPMethod::POST)([&model](const crow::request &request) {
|
|
|
|
auto req = RequestRedeem::from_string(request.body);
|
|
|
|
if (!req) {
|
|
|
|
return crow::response(crow::status::BAD_REQUEST);
|
|
|
|
} else {
|
|
|
|
ResponseRedeem res;
|
|
|
|
res.message_reference = req->message_reference;
|
|
|
|
bool success = model->redeem(req->coins);
|
|
|
|
res.status_code =
|
|
|
|
success ? crow::status::OK : crow::status::NOT_FOUND;
|
|
|
|
|
|
|
|
return crow::response(res.to_json());
|
|
|
|
}
|
2022-11-18 15:47:52 +01:00
|
|
|
});
|
2022-11-10 01:31:47 +01:00
|
|
|
|
2022-12-11 17:04:21 +01:00
|
|
|
app.port(18080).run();
|
2022-11-10 01:31:47 +01:00
|
|
|
}
|