oc-mint/src/main.cpp

132 lines
4.4 KiB
C++
Raw Normal View History

2022-11-10 01:31:47 +01:00
#include "crow.h"
#include "crow/common.h"
#include "crow/http_response.h"
2022-11-10 01:31:47 +01:00
#include "model.hpp"
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
CROW_ROUTE(app, "/cddc")
2023-04-20 22:55:17 +02:00
.methods(crow::HTTPMethod::POST)
([&model](const crow::request &req) {
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);
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
});
CROW_ROUTE(app, "/cddc/serial")
.methods(crow::HTTPMethod::POST)([&model](const crow::request &request) {
auto req = RequestCDDCSerial::from_string(request.body);
if (!req) {
return crow::response(crow::status::BAD_REQUEST);
} else {
ResponseCDDCSerial res;
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();
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-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) {
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-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) {
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);
res.blind_signatures = minted;
res.status_code = crow::status::OK;
return crow::response(res.to_json());
}
});
2022-11-10 01:31:47 +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-10 01:31:47 +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-10 01:31:47 +01:00
app.port(18080).run();
2022-11-10 01:31:47 +01:00
}