fixed http request types

reformatted sources
This commit is contained in:
gulliver 2022-12-11 17:04:21 +01:00
parent ce784c38dd
commit 79094c8a6d
3 changed files with 371 additions and 334 deletions

View File

@ -1,94 +1,127 @@
#include "crow.h" #include "crow.h"
#include "crow/common.h"
#include "crow/http_parser_merged.h"
#include "crow/http_response.h"
#include "model.hpp" #include "model.hpp"
int main() int main() {
{
crow::SimpleApp app; crow::SimpleApp app;
std::shared_ptr<Model> model = Model::getModel("simple"); std::shared_ptr<Model> model = Model::getModel("simple");
CROW_ROUTE(app, "/cddc") CROW_ROUTE(app, "/cddc")
([&model](const crow::request& req){ .methods(crow::HTTPMethod::POST)([&model](const crow::request &req) {
auto req_cddc = RequestCDDC::from_string(req.body); auto req_cddc = RequestCDDC::from_string(req.body);
if (!req_cddc) { if (!req_cddc) {
return crow::response(crow::status::BAD_REQUEST); return crow::response(crow::status::BAD_REQUEST);
} else { } else {
ResponseCDDC res; ResponseCDDC res;
// TBD use serial from req
res.cddc = model->getCDDC();
res.message_reference = req_cddc->message_reference; res.message_reference = req_cddc->message_reference;
auto cddc = model->getCDDC(req_cddc->cdd_serial);
if (!cddc) {
res.status_code = crow::status::NOT_FOUND;
} else {
res.cddc = *cddc.value();
res.status_code = crow::status::OK; res.status_code = crow::status::OK;
}
return crow::response(res.to_json()); return crow::response(res.to_json());
} }
}); });
CROW_ROUTE(app, "/cddc/serial") CROW_ROUTE(app, "/cddc/serial")
([&model](const crow::request& request){ .methods(crow::HTTPMethod::POST)([&model](const crow::request &request) {
auto req = RequestCDDSerial::from_string(request.body); auto req = RequestCDDSerial::from_string(request.body);
if (!req) { if (!req) {
return crow::response(crow::status::BAD_REQUEST); return crow::response(crow::status::BAD_REQUEST);
} else { } else {
// \todo check serial input
ResponseCDDSerial res; ResponseCDDSerial res;
res.cdd_serial = model->getCurrentCDDC().cdd.cdd_serial;
res.message_reference = req->message_reference; res.message_reference = req->message_reference;
auto cddc = model->getCurrentCDDC();
if (!cddc) {
res.status_code = crow::status::NOT_FOUND;
} else {
res.cdd_serial = (*cddc)->cdd.cdd_serial;
res.status_code = crow::status::OK; res.status_code = crow::status::OK;
}
return crow::response(res.to_json()); return crow::response(res.to_json());
} }
}); });
CROW_ROUTE(app, "/mkcs") CROW_ROUTE(app, "/mkcs")
([&model](const crow::request& request){ .methods(crow::HTTPMethod::POST)([&model](const crow::request &request) {
auto req = RequestMKCs::from_string(request.body); auto req = RequestMKCs::from_string(request.body);
if (!req) { if (!req) {
return crow::response(crow::status::BAD_REQUEST); return crow::response(crow::status::BAD_REQUEST);
} else { } else {
// \todo implement request ResponseMKCs res;
return crow::response(crow::status::NOT_IMPLEMENTED); 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());
} }
}); });
CROW_ROUTE(app, "/mint").methods(crow::HTTPMethod::GET) CROW_ROUTE(app, "/mint")
([&model](const crow::request& request){ .methods(crow::HTTPMethod::POST)([&model](const crow::request &request) {
auto req = RequestMint::from_string(request.body); auto req = RequestMint::from_string(request.body);
if (!req) { if (!req) {
return crow::response(crow::status::BAD_REQUEST); return crow::response(crow::status::BAD_REQUEST);
} else { } else {
// \todo implement request ResponseMint res;
return crow::response(crow::status::NOT_IMPLEMENTED); res.message_reference = req->message_reference;
auto minted = model->mint(req->transaction_reference, req->blinds);
res.blind_signatures = minted;
res.status_code = crow::status::OK;
return crow::response(res.to_json());
} }
}); });
CROW_ROUTE(app, "/renew")
CROW_ROUTE(app, "/renew").methods(crow::HTTPMethod::GET) .methods(crow::HTTPMethod::POST)([&model](const crow::request &request) {
([&model](const crow::request& request){ auto req = RequestRenew::from_string(request.body);
auto req = RequestMint::from_string(request.body);
if (!req) { if (!req) {
return crow::response(crow::status::BAD_REQUEST); return crow::response(crow::status::BAD_REQUEST);
} else { } else {
// \todo implement request // \todo implement ResponseDelay
return crow::response(crow::status::NOT_IMPLEMENTED); 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());
} }
}); });
CROW_ROUTE(app, "/resume").methods(crow::HTTPMethod::GET) CROW_ROUTE(app, "/resume")
([&model](const crow::request& request){ .methods(crow::HTTPMethod::POST)([](const crow::request &request) {
auto req = RequestResume::from_string(request.body); auto req = RequestResume::from_string(request.body);
if (!req) { if (!req) {
return crow::response(crow::status::BAD_REQUEST); return crow::response(crow::status::BAD_REQUEST);
} else { } else {
// \todo implement request // \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); return crow::response(crow::status::NOT_IMPLEMENTED);
} }
}); });
CROW_ROUTE(app, "/redeem").methods(crow::HTTPMethod::GET) CROW_ROUTE(app, "/redeem")
([&model](const crow::request& request){ .methods(crow::HTTPMethod::POST)([&model](const crow::request &request) {
auto req = RequestRedeem::from_string(request.body); auto req = RequestRedeem::from_string(request.body);
if (!req) { if (!req) {
return crow::response(crow::status::BAD_REQUEST); return crow::response(crow::status::BAD_REQUEST);
} else { } else {
// \todo implement request ResponseRedeem res;
return crow::response(crow::status::NOT_IMPLEMENTED); 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());
} }
}); });

View File

@ -1,36 +1,34 @@
#include "model.hpp" #include "model.hpp"
#include "crow/json.h" #include "crow/json.h"
#define TO_JSON(name) r[#name]=name #define TO_JSON(name) r[#name] = name
#define TO_JSON_JSON(name) r[#name]=name.to_json() #define TO_JSON_JSON(name) r[#name] = name.to_json()
#define TO_JSON_ARRAY(name) r[#name]=list_to_json(name) #define TO_JSON_ARRAY(name) r[#name] = list_to_json(name)
template <class T> template <class T>
crow::json::wvalue list_to_json(const std::vector<T>& array) { crow::json::wvalue list_to_json(const std::vector<T> &array) {
crow::json::wvalue::list l; crow::json::wvalue::list l;
for(auto item:array) for (auto item : array)
l.push_back(item.to_json()); l.push_back(item.to_json());
return crow::json::wvalue(l); return crow::json::wvalue(l);
} }
crow::json::wvalue list_to_json(const std::vector<unsigned int>& array) { crow::json::wvalue list_to_json(const std::vector<unsigned int> &array) {
crow::json::wvalue::list l; crow::json::wvalue::list l;
for(auto item:array) for (auto item : array)
l.push_back(item); l.push_back(item);
return crow::json::wvalue(l); return crow::json::wvalue(l);
} }
crow::json::wvalue PublicKey::to_json() const { crow::json::wvalue PublicKey::to_json() const {
crow::json::wvalue r; crow::json::wvalue r;
TO_JSON(modulus); TO_JSON(modulus);
TO_JSON(public_exponent); TO_JSON(public_exponent);
r["type"]="rsa public key"; r["type"] = "rsa public key";
return r; return r;
} }
crow::json::wvalue WeightedUrl::to_json() const crow::json::wvalue WeightedUrl::to_json() const {
{
crow::json::wvalue::list l; crow::json::wvalue::list l;
crow::json::wvalue w(weight); crow::json::wvalue w(weight);
@ -48,7 +46,7 @@ crow::json::wvalue CDD::to_json() const {
TO_JSON(cdd_serial); TO_JSON(cdd_serial);
TO_JSON(cdd_signing_date); TO_JSON(cdd_signing_date);
TO_JSON(currency_divisor); TO_JSON(currency_divisor);
TO_JSON( currency_name); TO_JSON(currency_name);
TO_JSON_ARRAY(denominations); TO_JSON_ARRAY(denominations);
TO_JSON(id); TO_JSON(id);
TO_JSON_ARRAY(info_service); TO_JSON_ARRAY(info_service);
@ -59,15 +57,15 @@ crow::json::wvalue CDD::to_json() const {
TO_JSON_ARRAY(redeem_service); TO_JSON_ARRAY(redeem_service);
TO_JSON_ARRAY(renew_service); TO_JSON_ARRAY(renew_service);
r["type"]= "cdd"; r["type"] = "cdd";
return r; return r;
} }
crow::json::wvalue CDDC::to_json() const{ crow::json::wvalue CDDC::to_json() const {
crow::json::wvalue r; crow::json::wvalue r;
TO_JSON_JSON(cdd); TO_JSON_JSON(cdd);
TO_JSON(signature); TO_JSON(signature);
r["type"]= "cdd certificate"; r["type"] = "cdd certificate";
return r; return r;
} }
@ -84,21 +82,19 @@ crow::json::wvalue MintKey::to_json() const {
TO_JSON(sign_coins_not_after); TO_JSON(sign_coins_not_after);
TO_JSON(sign_coins_not_before); TO_JSON(sign_coins_not_before);
r["type"]= "mint key"; r["type"] = "mint key";
return r; return r;
} }
crow::json::wvalue MintKeyCert::to_json() const { crow::json::wvalue MintKeyCert::to_json() const {
crow::json::wvalue r; crow::json::wvalue r;
TO_JSON_JSON(mint_key); TO_JSON_JSON(mint_key);
TO_JSON(signature); TO_JSON(signature);
r["type"]= "mint key certificate"; r["type"] = "mint key certificate";
return r; return r;
} }
crow::json::wvalue Response::to_json() const crow::json::wvalue Response::to_json() const {
{
crow::json::wvalue r; crow::json::wvalue r;
TO_JSON(message_reference); TO_JSON(message_reference);
TO_JSON(status_code); TO_JSON(status_code);
@ -106,92 +102,82 @@ crow::json::wvalue Response::to_json() const
return r; return r;
} }
crow::json::wvalue ResponseCDDSerial::to_json() const {
crow::json::wvalue ResponseCDDSerial::to_json() const
{
crow::json::wvalue r = Response::to_json(); crow::json::wvalue r = Response::to_json();
TO_JSON(cdd_serial); TO_JSON(cdd_serial);
r["type"]= "response cdd serial"; r["type"] = "response cdd serial";
return r; return r;
} }
tl::expected<RequestCDDSerial,eError> tl::expected<RequestCDDSerial, eError>
RequestCDDSerial::from_string(const std::string& str) RequestCDDSerial::from_string(const std::string &str) {
{
auto json = crow::json::load(str); auto json = crow::json::load(str);
if (!json) { if (!json) {
return tl::make_unexpected(eError::JSON_PARSE_ERROR); return tl::make_unexpected(eError::JSON_PARSE_ERROR);
} else if ( !json.has("type") || !json.has("message_reference")) { } else if (!json.has("type") || !json.has("message_reference")) {
return tl::make_unexpected(eError::JSON_ERROR); return tl::make_unexpected(eError::JSON_ERROR);
} else if ( json["type"]!="request cdd serial") { } else if (json["type"] != "request cdd serial") {
return tl::make_unexpected(eError::JSON_ERROR); return tl::make_unexpected(eError::JSON_ERROR);
} else { } else {
RequestCDDSerial r; RequestCDDSerial r;
r.message_reference= json["message_reference"].u(); r.message_reference = json["message_reference"].u();
return r; return r;
} }
} }
crow::json::wvalue ResponseCDDC::to_json() const crow::json::wvalue ResponseCDDC::to_json() const {
{
crow::json::wvalue r = Response::to_json(); crow::json::wvalue r = Response::to_json();
TO_JSON_JSON(cddc); TO_JSON_JSON(cddc);
r["type"]= "response cdd serial"; r["type"] = "response cdd serial";
return r; return r;
} }
tl::expected<RequestCDDC,eError> tl::expected<RequestCDDC, eError>
RequestCDDC::from_string(const std::string& str) RequestCDDC::from_string(const std::string &str) {
{
auto json = crow::json::load(str); auto json = crow::json::load(str);
if (!json) { if (!json) {
return tl::make_unexpected(eError::JSON_PARSE_ERROR); return tl::make_unexpected(eError::JSON_PARSE_ERROR);
} else if ( !( json.has("type") } else if (!(json.has("type") && json.has("message_reference") &&
&& json.has("message_reference") json.has("cdd_serial"))) {
&& json.has("cdd_serial")
) ) {
return tl::make_unexpected(eError::JSON_MISSING_KEY); return tl::make_unexpected(eError::JSON_MISSING_KEY);
} else if ( json["type"]!="request cddc" ) { } else if (json["type"] != "request cddc") {
return tl::make_unexpected(eError::JSON_WRONG_REQUEST_TYPE); return tl::make_unexpected(eError::JSON_WRONG_REQUEST_TYPE);
} else { } else {
RequestCDDC r; RequestCDDC r;
r.cdd_serial=json["cdd_serial"].u(); r.cdd_serial = json["cdd_serial"].u();
r.message_reference= json["message_reference"].u(); r.message_reference = json["message_reference"].u();
return r; return r;
} }
} }
tl::expected<RequestMKCs,eError> tl::expected<RequestMKCs, eError>
RequestMKCs::from_string(const std::string& str) { RequestMKCs::from_string(const std::string &str) {
auto json = crow::json::load(str); auto json = crow::json::load(str);
if (!json) { if (!json) {
return tl::make_unexpected(eError::JSON_PARSE_ERROR); return tl::make_unexpected(eError::JSON_PARSE_ERROR);
} else if ( !(json.has("denominations") } else if (!(json.has("denominations") && json.has("message_reference") &&
&& json.has("message_reference") json.has("mint_key_ids") && json.has("type"))) {
&& json.has("mint_key_ids")
&& json.has("type")
) ) {
return tl::make_unexpected(eError::JSON_MISSING_KEY); return tl::make_unexpected(eError::JSON_MISSING_KEY);
} else if ( json["type"]!="request mint key certificates" ) { } else if (json["type"] != "request mint key certificates") {
return tl::make_unexpected(eError::JSON_WRONG_REQUEST_TYPE); return tl::make_unexpected(eError::JSON_WRONG_REQUEST_TYPE);
} else { } else {
RequestMKCs r; RequestMKCs r;
r.message_reference= json["message_reference"].u(); r.message_reference = json["message_reference"].u();
auto denominations = json["denominations"]; auto denominations = json["denominations"];
if ( denominations.t()!=crow::json::type::List) { if (denominations.t() != crow::json::type::List) {
return tl::make_unexpected(eError::JSON_WRONG_REQUEST_TYPE); return tl::make_unexpected(eError::JSON_WRONG_REQUEST_TYPE);
} else { } else {
for (auto d: denominations.lo()) { for (auto d : denominations.lo()) {
r.denominations.push_back(d.u()); r.denominations.push_back(d.u());
} }
} }
auto mint_key_ids = json["mint_key_ids"]; auto mint_key_ids = json["mint_key_ids"];
if ( mint_key_ids.t()!=crow::json::type::List) { if (mint_key_ids.t() != crow::json::type::List) {
return tl::make_unexpected(eError::JSON_WRONG_REQUEST_TYPE); return tl::make_unexpected(eError::JSON_WRONG_REQUEST_TYPE);
} else { } else {
for (auto k: mint_key_ids.lo()) { for (auto k : mint_key_ids.lo()) {
r.mint_key_ids.push_back(k.u()); r.mint_key_ids.push_back(k.u());
} }
} }
@ -199,11 +185,10 @@ RequestMKCs::from_string(const std::string& str) {
} }
} }
crow::json::wvalue ResponseMKCs::to_json() const {
crow::json::wvalue ResponseMKCs::to_json() const{
crow::json::wvalue r = Response::to_json(); crow::json::wvalue r = Response::to_json();
TO_JSON_ARRAY(keys); TO_JSON_ARRAY(keys);
r["type"]= "response mint key certificates"; r["type"] = "response mint key certificates";
return r; return r;
} }
@ -212,19 +197,15 @@ crow::json::wvalue Blind::to_json() const {
TO_JSON(blinded_payload_hash); TO_JSON(blinded_payload_hash);
TO_JSON(mint_key_id); TO_JSON(mint_key_id);
TO_JSON(reference); TO_JSON(reference);
r["type"]= "blinded payload hash"; r["type"] = "blinded payload hash";
return r; return r;
} }
tl::expected<Blind,eError> Blind::from_json(const crow::json::rvalue& json) tl::expected<Blind, eError> Blind::from_json(const crow::json::rvalue &json) {
{ if (!(json.has("type") && json.has("blinded_payload_hash") &&
if ( !( json.has("type") json.has("mint_key_id") && json.has("reference"))) {
&& json.has("blinded_payload_hash")
&& json.has("mint_key_id")
&& json.has("reference")
) ) {
return tl::make_unexpected(eError::JSON_ERROR); return tl::make_unexpected(eError::JSON_ERROR);
} else if ( json["type"]!="blinded payload hash" ) { } else if (json["type"] != "blinded payload hash") {
return tl::make_unexpected(eError::JSON_ERROR); return tl::make_unexpected(eError::JSON_ERROR);
} else { } else {
Blind r; Blind r;
@ -239,35 +220,31 @@ crow::json::wvalue BlindSignature::to_json() const {
crow::json::wvalue r; crow::json::wvalue r;
TO_JSON(blind_signature); TO_JSON(blind_signature);
TO_JSON(reference); TO_JSON(reference);
r["type"]= "blind signature"; r["type"] = "blind signature";
return r; return r;
} }
tl::expected<RequestMint,eError> tl::expected<RequestMint, eError>
RequestMint::from_string(const std::string& str){ RequestMint::from_string(const std::string &str) {
std::vector<Blind> blinds; std::vector<Blind> blinds;
// "type": "request mint" // "type": "request mint"
auto json = crow::json::load(str); auto json = crow::json::load(str);
if (!json) { if (!json) {
return tl::make_unexpected(eError::JSON_PARSE_ERROR); return tl::make_unexpected(eError::JSON_PARSE_ERROR);
} else if ( !( json.has("type") } else if (!(json.has("type") && json.has("message_reference") &&
&& json.has("message_reference") json.has("transaction_reference") && json.has("blinds"))) {
&& json.has("transaction_reference")
&& json.has("blinds")
) ) {
return tl::make_unexpected(eError::JSON_ERROR); return tl::make_unexpected(eError::JSON_ERROR);
} else if ( json["type"]!="request mint" ) { } else if (json["type"] != "request mint") {
return tl::make_unexpected(eError::JSON_ERROR); return tl::make_unexpected(eError::JSON_ERROR);
} else { } else {
RequestMint r; RequestMint r;
r.message_reference= json["message_reference"].u(); r.message_reference = json["message_reference"].u();
r.transaction_reference= json["transaction_reference"].s(); r.transaction_reference = json["transaction_reference"].s();
if (json["blinds"].t()!=crow::json::type::List) { if (json["blinds"].t() != crow::json::type::List) {
return tl::make_unexpected(eError::JSON_WRONG_VALUE_TYPE); return tl::make_unexpected(eError::JSON_WRONG_VALUE_TYPE);
} }
for (auto item: json["blinds"]) { for (auto item : json["blinds"]) {
auto b = Blind::from_json(item); auto b = Blind::from_json(item);
if (!b.has_value()) { if (!b.has_value()) {
return tl::make_unexpected(b.error()); return tl::make_unexpected(b.error());
@ -282,7 +259,7 @@ RequestMint::from_string(const std::string& str){
crow::json::wvalue ResponseMint::to_json() const { crow::json::wvalue ResponseMint::to_json() const {
crow::json::wvalue r = Response::to_json(); crow::json::wvalue r = Response::to_json();
TO_JSON_ARRAY(blind_signatures); TO_JSON_ARRAY(blind_signatures);
r["type"]= "response mint"; r["type"] = "response mint";
return r; return r;
} }
@ -295,29 +272,26 @@ crow::json::wvalue Coin::Payload::to_json() const {
TO_JSON(protocol_version); TO_JSON(protocol_version);
TO_JSON(serial); TO_JSON(serial);
r["type"]= "payload"; r["type"] = "payload";
return r; return r;
} }
crow::json::wvalue Coin::to_json() const crow::json::wvalue Coin::to_json() const {
{
crow::json::wvalue r; crow::json::wvalue r;
TO_JSON_JSON(payload); TO_JSON_JSON(payload);
TO_JSON(signature); TO_JSON(signature);
r["type"]= "coin"; r["type"] = "coin";
return r; return r;
} }
tl::expected<Coin::Payload,eError> Coin::Payload::from_json(const crow::json::rvalue& json) { tl::expected<Coin::Payload, eError>
if ( !( json.has("cdd_location") Coin::Payload::from_json(const crow::json::rvalue &json) {
&& json.has("denomination") if (!(json.has("cdd_location") && json.has("denomination") &&
&& json.has("issuer_id") json.has("issuer_id") && json.has("mint_key_id") &&
&& json.has("mint_key_id") json.has("protocol_version") && json.has("serial") &&
&& json.has("protocol_version") json.has("type"))) {
&& json.has("serial")
&& json.has("type"))) {
return tl::make_unexpected(eError::JSON_ERROR); return tl::make_unexpected(eError::JSON_ERROR);
} else if ( json["type"]!="payload" ) { } else if (json["type"] != "payload") {
return tl::make_unexpected(eError::JSON_ERROR); return tl::make_unexpected(eError::JSON_ERROR);
} else { } else {
Coin::Payload payload; Coin::Payload payload;
@ -331,13 +305,10 @@ tl::expected<Coin::Payload,eError> Coin::Payload::from_json(const crow::json::rv
} }
} }
tl::expected<Coin,eError> Coin::from_json(const crow::json::rvalue& json) { tl::expected<Coin, eError> Coin::from_json(const crow::json::rvalue &json) {
if ( !( json.has("type") if (!(json.has("type") && json.has("payload") && json.has("signature"))) {
&& json.has("payload")
&& json.has("signature")
) ) {
return tl::make_unexpected(eError::JSON_ERROR); return tl::make_unexpected(eError::JSON_ERROR);
} else if ( json["type"]!="coin" ) { } else if (json["type"] != "coin") {
return tl::make_unexpected(eError::JSON_ERROR); return tl::make_unexpected(eError::JSON_ERROR);
} else { } else {
auto pl = Payload::from_json(json["payload"]); auto pl = Payload::from_json(json["payload"]);
@ -356,33 +327,29 @@ crow::json::wvalue CoinStack::to_json() const {
crow::json::wvalue r; crow::json::wvalue r;
TO_JSON_ARRAY(coins); TO_JSON_ARRAY(coins);
TO_JSON(subject); TO_JSON(subject);
r["type"]= "coinstack"; r["type"] = "coinstack";
return r; return r;
} }
tl::expected<RequestRenew, eError>
tl::expected<RequestRenew,eError> RequestRenew::from_string(const std::string &str) {
RequestRenew::from_string(const std::string& str) {
auto json = crow::json::load(str); auto json = crow::json::load(str);
if (!json) { if (!json) {
return tl::make_unexpected(eError::JSON_PARSE_ERROR); return tl::make_unexpected(eError::JSON_PARSE_ERROR);
} else if ( !( json.has("blinds") } else if (!(json.has("blinds") && json.has("coins") &&
&& json.has("coins") json.has("transaction_reference") &&
&& json.has("transaction_reference") json.has("message_reference") && json.has("type"))) {
&& json.has("message_reference")
&& json.has("type")
) ) {
return tl::make_unexpected(eError::JSON_MISSING_KEY); return tl::make_unexpected(eError::JSON_MISSING_KEY);
} else if ( json["type"]!="request renew" ) { } else if (json["type"] != "request renew") {
return tl::make_unexpected(eError::JSON_WRONG_REQUEST_TYPE); return tl::make_unexpected(eError::JSON_WRONG_REQUEST_TYPE);
} else if ( (json["coins"].t()!=crow::json::type::List) } else if ((json["coins"].t() != crow::json::type::List) ||
|| (json["blinds"].t()!=crow::json::type::List) ) { (json["blinds"].t() != crow::json::type::List)) {
return tl::make_unexpected(eError::JSON_WRONG_VALUE_TYPE); return tl::make_unexpected(eError::JSON_WRONG_VALUE_TYPE);
} else { } else {
RequestRenew r; RequestRenew r;
for (auto item: json["coins"]) { for (auto item : json["coins"]) {
auto coin = Coin::from_json(item); auto coin = Coin::from_json(item);
if (!coin.has_value()) { if (!coin.has_value()) {
return tl::make_unexpected(coin.error()); return tl::make_unexpected(coin.error());
@ -391,7 +358,7 @@ RequestRenew::from_string(const std::string& str) {
} }
} }
for (auto item: json["blinds"]) { for (auto item : json["blinds"]) {
auto blind = Blind::from_json(item); auto blind = Blind::from_json(item);
if (!blind.has_value()) { if (!blind.has_value()) {
return tl::make_unexpected(blind.error()); return tl::make_unexpected(blind.error());
@ -408,21 +375,19 @@ RequestRenew::from_string(const std::string& str) {
crow::json::wvalue ResponseDelay::to_json() const { crow::json::wvalue ResponseDelay::to_json() const {
crow::json::wvalue r = Response::to_json(); crow::json::wvalue r = Response::to_json();
r["type"]= "response delay"; r["type"] = "response delay";
return r; return r;
} }
tl::expected<RequestResume,eError> tl::expected<RequestResume, eError>
RequestResume::from_string(const std::string& str) { RequestResume::from_string(const std::string &str) {
auto json = crow::json::load(str); auto json = crow::json::load(str);
if (!json) { if (!json) {
return tl::make_unexpected(eError::JSON_PARSE_ERROR); return tl::make_unexpected(eError::JSON_PARSE_ERROR);
} else if ( !( json.has("transaction_reference") } else if (!(json.has("transaction_reference") &&
&& json.has("message_reference") json.has("message_reference") && json.has("type"))) {
&& json.has("type")
) ) {
return tl::make_unexpected(eError::JSON_MISSING_KEY); return tl::make_unexpected(eError::JSON_MISSING_KEY);
} else if ( json["type"]!="request resume" ) { } else if (json["type"] != "request resume") {
return tl::make_unexpected(eError::JSON_WRONG_REQUEST_TYPE); return tl::make_unexpected(eError::JSON_WRONG_REQUEST_TYPE);
} else { } else {
RequestResume r; RequestResume r;
@ -432,27 +397,25 @@ RequestResume::from_string(const std::string& str) {
} }
} }
tl::expected<RequestRedeem,eError> tl::expected<RequestRedeem, eError>
RequestRedeem::from_string(const std::string& str) { RequestRedeem::from_string(const std::string &str) {
// "type": // "type":
auto json = crow::json::load(str); auto json = crow::json::load(str);
if (!json) { if (!json) {
return tl::make_unexpected(eError::JSON_PARSE_ERROR); return tl::make_unexpected(eError::JSON_PARSE_ERROR);
} else if ( !( json.has("coins") } else if (!(json.has("coins") && json.has("message_reference") &&
&& json.has("message_reference") json.has("type"))) {
&& json.has("type")
) ) {
return tl::make_unexpected(eError::JSON_MISSING_KEY); return tl::make_unexpected(eError::JSON_MISSING_KEY);
} else if ( json["type"]!="request redeem" ) { } else if (json["type"] != "request redeem") {
return tl::make_unexpected(eError::JSON_WRONG_REQUEST_TYPE); return tl::make_unexpected(eError::JSON_WRONG_REQUEST_TYPE);
} else { } else {
RequestRedeem r; RequestRedeem r;
r.message_reference = json["message_reference"].u(); r.message_reference = json["message_reference"].u();
if (json["coins"].t()!=crow::json::type::List) { if (json["coins"].t() != crow::json::type::List) {
return tl::make_unexpected(eError::JSON_WRONG_VALUE_TYPE); return tl::make_unexpected(eError::JSON_WRONG_VALUE_TYPE);
} }
for (auto item: json["coins"]) { for (auto item : json["coins"]) {
auto coin = Coin::from_json(item); auto coin = Coin::from_json(item);
if (!coin.has_value()) { if (!coin.has_value()) {
return tl::make_unexpected(coin.error()); return tl::make_unexpected(coin.error());
@ -464,29 +427,58 @@ auto json = crow::json::load(str);
} }
} }
crow::json::wvalue ResponseReedem::to_json() const { crow::json::wvalue ResponseRedeem::to_json() const {
crow::json::wvalue r = Response::to_json(); crow::json::wvalue r = Response::to_json();
r["type"]= "response redeem"; r["type"] = "response redeem";
return r; return r;
} }
using std::cout;
using std::endl;
/** this is for now our sample model */ /** this is for now our sample model */
class DefaultModel : public Model { class DefaultModel : public Model {
public: public:
DefaultModel() {} DefaultModel() {}
const CDDC& getCDDC() override {return m_cddc; }; tl::expected<CDDC *, bool> getCDDC(unsigned int cdd_serial) override {
const CDDC& getCurrentCDDC() override {return m_cddc; }; cout << __FUNCTION__ << "(" << cdd_serial << ")" << endl;
void mint() override {}; return &m_cddc;
};
tl::expected<CDDC *, bool> getCurrentCDDC() override {
cout << __FUNCTION__ << "()" << endl;
return &m_cddc;
}
std::vector<BlindSignature> mint(const std::string &transaction_reference,
const std::vector<Blind> &blinds) override {
std::vector<BlindSignature> res;
cout << __FUNCTION__ << "("
<< ")" << endl;
return res;
}
const std::vector<MintKeyCert>
getMKCs(const std::vector<unsigned int> &denominations,
const std::vector<unsigned int> &mint_key_ids) override {
std::vector<MintKeyCert> res;
cout << __FUNCTION__ << endl;
return res;
}
bool redeem(const std::vector<Coin> &coins) override {
cout << __FUNCTION__ << endl;
return false;
}
private: private:
CDDC m_cddc; CDDC m_cddc;
}; };
std::unique_ptr<Model> Model::getModel(const std::string& /*backend_name*/) std::unique_ptr<Model> Model::getModel(const std::string & /*backend_name*/) {
{ cout << __FUNCTION__ << endl;
//:wq //:wq
//if (backend_name=="default") // if (backend_name=="default")
return std::make_unique<DefaultModel>(); return std::make_unique<DefaultModel>();
} }

View File

@ -1,19 +1,18 @@
#ifndef MODEL_HPP #ifndef MODEL_HPP
#define MODEL_HPP #define MODEL_HPP
#include <memory>
#include <string>
#include <chrono> #include <chrono>
#include <memory>
#include <optional> #include <optional>
#include <string>
#include "crow/json.h" #include "crow/json.h"
#include "tl/expected.hpp" #include "tl/expected.hpp"
struct PublicKey { struct PublicKey {
std::string modulus; //: "daaa63ddda38c189b8c49020c8276adbe0a695685a...", std::string modulus; //: "daaa63ddda38c189b8c49020c8276adbe0a695685a...",
std::string public_exponent;//: 65537, std::string public_exponent; //: 65537,
crow::json::wvalue to_json() const; crow::json::wvalue to_json() const;
}; };
@ -29,18 +28,20 @@ struct WeightedUrl {
struct CDD { struct CDD {
std::string additional_info; std::string additional_info;
time_t cdd_expiry_date;//: 2023-07-22T15:45:53.164685 time_t cdd_expiry_date; //: 2023-07-22T15:45:53.164685
std::string cdd_location;//: https://opencent.org, std::string cdd_location; //: https://opencent.org,
size_t cdd_serial;//: 1, size_t cdd_serial; //: 1,
time_t cdd_signing_date;//: 2022-07-22T15:45:53.164685, time_t cdd_signing_date; //: 2022-07-22T15:45:53.164685,
size_t currency_divisor;//: 100, size_t currency_divisor; //: 100,
std::string currency_name;//: OpenCent, std::string currency_name; //: OpenCent,
std::vector<unsigned> denominations;//: [1, 2, 5], std::vector<unsigned> denominations; //: [1, 2, 5],
std::string id;//: 23ed956e629ba35f0002eaf833ea436aea7db5c2, std::string id; //: 23ed956e629ba35f0002eaf833ea436aea7db5c2,
std::vector<WeightedUrl> info_service; std::vector<WeightedUrl> info_service;
/* eCipherSuite*/ std::string issuer_cipher_suite;//: RSA-SHA256-PSS-CHAUM82, /* eCipherSuite*/ std::string issuer_cipher_suite; //: RSA-SHA256-PSS-CHAUM82,
PublicKey issuer_public_master_key;//: { PublicKey
// modulus: daaa63ddda38c189b8c49020c8276adbe0a695685a..., issuer_public_master_key; //: {
// modulus:
// daaa63ddda38c189b8c49020c8276adbe0a695685a...,
// public_exponent: 65537, // public_exponent: 65537,
// type: rsa public key // type: rsa public key
//}, //},
@ -100,20 +101,21 @@ struct Response {
struct RequestCDDSerial { struct RequestCDDSerial {
unsigned int message_reference; /// Client internal message reference. unsigned int message_reference; /// Client internal message reference.
/// (Integer) /// (Integer)
static tl::expected<RequestCDDSerial,eError> from_string(const std::string& str); static tl::expected<RequestCDDSerial, eError>
from_string(const std::string &str);
}; };
struct ResponseCDDSerial: Response { struct ResponseCDDSerial : Response {
unsigned int cdd_serial; unsigned int cdd_serial;
crow::json::wvalue to_json() const override; crow::json::wvalue to_json() const override;
}; };
struct RequestCDDC { struct RequestCDDC {
unsigned int cdd_serial;/// The version of the CDD. (Int) unsigned int cdd_serial; /// The version of the CDD. (Int)
unsigned int message_reference; /// Client internal message reference. unsigned int message_reference; /// Client internal message reference.
/// (Integer) /// (Integer)
static tl::expected<RequestCDDC,eError> from_string(const std::string& str); static tl::expected<RequestCDDC, eError> from_string(const std::string &str);
}; };
struct ResponseCDDC : Response { struct ResponseCDDC : Response {
@ -128,21 +130,21 @@ struct RequestMKCs {
/// (Integer) /// (Integer)
std::vector<unsigned int> mint_key_ids; std::vector<unsigned int> mint_key_ids;
// "type": "request mint key certificates" // "type": "request mint key certificates"
static tl::expected<RequestMKCs,eError> from_string(const std::string& str); static tl::expected<RequestMKCs, eError> from_string(const std::string &str);
}; };
struct ResponseMKCs: Response { struct ResponseMKCs : Response {
std::vector<MintKeyCert> keys; std::vector<MintKeyCert> keys;
crow::json::wvalue to_json() const override; crow::json::wvalue to_json() const override;
}; };
struct Blind { struct Blind {
std::string blinded_payload_hash; //bigint std::string blinded_payload_hash; // bigint
std::string mint_key_id; //bigint std::string mint_key_id; // bigint
std::string reference; std::string reference;
crow::json::wvalue to_json() const; crow::json::wvalue to_json() const;
static tl::expected<Blind,eError> from_json(const crow::json::rvalue& json); static tl::expected<Blind, eError> from_json(const crow::json::rvalue &json);
}; };
struct BlindSignature { struct BlindSignature {
@ -157,7 +159,7 @@ struct RequestMint {
std::string transaction_reference; std::string transaction_reference;
std::vector<Blind> blinds; std::vector<Blind> blinds;
// "type": "request mint" // "type": "request mint"
static tl::expected<RequestMint,eError> from_string(const std::string& str); static tl::expected<RequestMint, eError> from_string(const std::string &str);
}; };
struct ResponseMint : Response { struct ResponseMint : Response {
@ -166,7 +168,6 @@ struct ResponseMint : Response {
crow::json::wvalue to_json() const override; crow::json::wvalue to_json() const override;
}; };
struct Coin { struct Coin {
struct Payload { struct Payload {
std::string cdd_location; std::string cdd_location;
@ -177,14 +178,15 @@ struct Coin {
std::string serial; std::string serial;
crow::json::wvalue to_json() const; crow::json::wvalue to_json() const;
static tl::expected<Payload,eError> from_json(const crow::json::rvalue& json); static tl::expected<Payload, eError>
from_json(const crow::json::rvalue &json);
}; };
Payload payload; Payload payload;
std::string signature; std::string signature;
crow::json::wvalue to_json() const; crow::json::wvalue to_json() const;
static tl::expected<Coin,eError> from_json(const crow::json::rvalue& json); static tl::expected<Coin, eError> from_json(const crow::json::rvalue &json);
}; };
struct CoinStack { struct CoinStack {
@ -201,7 +203,7 @@ struct RequestRenew {
/// (Integer) /// (Integer)
std::string transaction_reference; std::string transaction_reference;
// "type": "request renew" // "type": "request renew"
static tl::expected<RequestRenew,eError> from_string(const std::string& str); static tl::expected<RequestRenew, eError> from_string(const std::string &str);
}; };
struct ResponseDelay : Response { struct ResponseDelay : Response {
@ -213,7 +215,8 @@ struct RequestResume {
/// (Integer) /// (Integer)
std::string transaction_reference; std::string transaction_reference;
// "type": "request resume" // "type": "request resume"
static tl::expected<RequestResume,eError> from_string(const std::string& str); static tl::expected<RequestResume, eError>
from_string(const std::string &str);
}; };
struct RequestRedeem { struct RequestRedeem {
@ -221,10 +224,11 @@ struct RequestRedeem {
unsigned int message_reference; /// Client internal message reference. unsigned int message_reference; /// Client internal message reference.
/// (Integer) /// (Integer)
// "type": "request redeem" // "type": "request redeem"
static tl::expected<RequestRedeem,eError> from_string(const std::string& str); static tl::expected<RequestRedeem, eError>
from_string(const std::string &str);
}; };
struct ResponseReedem : Response { struct ResponseRedeem : Response {
crow::json::wvalue to_json() const override; crow::json::wvalue to_json() const override;
}; };
@ -232,13 +236,21 @@ class Model {
public: public:
virtual ~Model(){}; virtual ~Model(){};
virtual const CDDC& getCDDC() = 0; virtual tl::expected<CDDC *, bool> getCDDC(unsigned int cdd_serial) = 0;
virtual const CDDC& getCurrentCDDC() = 0; virtual tl::expected<CDDC *, bool> getCurrentCDDC() = 0;
virtual void mint() = 0;
virtual const std::vector<MintKeyCert>
getMKCs(const std::vector<unsigned int> &denominations,
const std::vector<unsigned int> &mint_key_ids) = 0;
virtual std::vector<BlindSignature>
mint(const std::string &transaction_reference,
const std::vector<Blind> &blinds) = 0;
virtual bool redeem(const std::vector<Coin> &coins) = 0;
static std::unique_ptr<Model> getModel(const std::string &backend_name);
static std::unique_ptr<Model> getModel(const std::string& backend_name);
private: private:
}; };
#endif // #ifndef MODEL_HPP #endif // #ifndef MODEL_HPP