From 79094c8a6d9350e9e81b319b1b9c47beaa044dbd Mon Sep 17 00:00:00 2001 From: gulliver Date: Sun, 11 Dec 2022 17:04:21 +0100 Subject: [PATCH] fixed http request types reformatted sources --- src/main.cpp | 187 ++++++++++++++----------- src/model.cpp | 376 ++++++++++++++++++++++++-------------------------- src/model.hpp | 142 ++++++++++--------- 3 files changed, 371 insertions(+), 334 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index c6ac98c..8d4c217 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,96 +1,129 @@ #include "crow.h" +#include "crow/common.h" +#include "crow/http_parser_merged.h" +#include "crow/http_response.h" #include "model.hpp" -int main() -{ - crow::SimpleApp app; - std::shared_ptr model = Model::getModel("simple"); +int main() { + crow::SimpleApp app; + std::shared_ptr model = Model::getModel("simple"); - CROW_ROUTE(app, "/cddc") - ([&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; - // TBD use serial from req - res.cddc = model->getCDDC(); - res.message_reference = req_cddc->message_reference; - res.status_code = crow::status::OK; - return crow::response(res.to_json()); - } + CROW_ROUTE(app, "/cddc") + .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; + 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; + } + return crow::response(res.to_json()); + } }); - CROW_ROUTE(app, "/cddc/serial") - ([&model](const crow::request& request){ - auto req = RequestCDDSerial::from_string(request.body); - if (!req) { - return crow::response(crow::status::BAD_REQUEST); - } else { - // \todo check serial input - ResponseCDDSerial res; - res.cdd_serial = model->getCurrentCDDC().cdd.cdd_serial; - res.message_reference = req->message_reference; - res.status_code = crow::status::OK; - return crow::response(res.to_json()); - } - }); + CROW_ROUTE(app, "/cddc/serial") + .methods(crow::HTTPMethod::POST)([&model](const crow::request &request) { + auto req = RequestCDDSerial::from_string(request.body); + if (!req) { + return crow::response(crow::status::BAD_REQUEST); + } else { + ResponseCDDSerial res; + res.message_reference = req->message_reference; - CROW_ROUTE(app, "/mkcs") - ([&model](const crow::request& request){ - auto req = RequestMKCs::from_string(request.body); - if (!req) { - return crow::response(crow::status::BAD_REQUEST); - } else { - // \todo implement request - return crow::response(crow::status::NOT_IMPLEMENTED); - } + 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; + } + return crow::response(res.to_json()); + } }); - CROW_ROUTE(app, "/mint").methods(crow::HTTPMethod::GET) - ([&model](const crow::request& request){ - auto req = RequestMint::from_string(request.body); - if (!req) { - return crow::response(crow::status::BAD_REQUEST); - } else { - // \todo implement request - return crow::response(crow::status::NOT_IMPLEMENTED); - } + 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()); + } }); + 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; - CROW_ROUTE(app, "/renew").methods(crow::HTTPMethod::GET) - ([&model](const crow::request& request){ - auto req = RequestMint::from_string(request.body); - if (!req) { - return crow::response(crow::status::BAD_REQUEST); - } else { - // \todo implement request - return crow::response(crow::status::NOT_IMPLEMENTED); - } + 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, "/resume").methods(crow::HTTPMethod::GET) - ([&model](const crow::request& request){ - auto req = RequestResume::from_string(request.body); - if (!req) { - return crow::response(crow::status::BAD_REQUEST); - } else { - // \todo implement request - return crow::response(crow::status::NOT_IMPLEMENTED); - } + 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()); + } }); - CROW_ROUTE(app, "/redeem").methods(crow::HTTPMethod::GET) - ([&model](const crow::request& request){ - auto req = RequestRedeem::from_string(request.body); - if (!req) { - return crow::response(crow::status::BAD_REQUEST); - } else { - // \todo implement request - return crow::response(crow::status::NOT_IMPLEMENTED); - } + 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); + } }); - app.port(18080).run(); + 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()); + } + }); + + app.port(18080).run(); } diff --git a/src/model.cpp b/src/model.cpp index f80f6e1..a66de4d 100644 --- a/src/model.cpp +++ b/src/model.cpp @@ -1,36 +1,34 @@ #include "model.hpp" #include "crow/json.h" -#define TO_JSON(name) r[#name]=name -#define TO_JSON_JSON(name) r[#name]=name.to_json() -#define TO_JSON_ARRAY(name) r[#name]=list_to_json(name) +#define TO_JSON(name) r[#name] = name +#define TO_JSON_JSON(name) r[#name] = name.to_json() +#define TO_JSON_ARRAY(name) r[#name] = list_to_json(name) template -crow::json::wvalue list_to_json(const std::vector& array) { +crow::json::wvalue list_to_json(const std::vector &array) { crow::json::wvalue::list l; - for(auto item:array) + for (auto item : array) l.push_back(item.to_json()); return crow::json::wvalue(l); } -crow::json::wvalue list_to_json(const std::vector& array) { +crow::json::wvalue list_to_json(const std::vector &array) { crow::json::wvalue::list l; - for(auto item:array) + for (auto item : array) l.push_back(item); return crow::json::wvalue(l); } - crow::json::wvalue PublicKey::to_json() const { crow::json::wvalue r; TO_JSON(modulus); TO_JSON(public_exponent); - r["type"]="rsa public key"; + r["type"] = "rsa public key"; return r; } -crow::json::wvalue WeightedUrl::to_json() const -{ +crow::json::wvalue WeightedUrl::to_json() const { crow::json::wvalue::list l; crow::json::wvalue w(weight); @@ -48,26 +46,26 @@ crow::json::wvalue CDD::to_json() const { TO_JSON(cdd_serial); TO_JSON(cdd_signing_date); TO_JSON(currency_divisor); - TO_JSON( currency_name); + TO_JSON(currency_name); TO_JSON_ARRAY(denominations); TO_JSON(id); TO_JSON_ARRAY(info_service); TO_JSON(issuer_cipher_suite); TO_JSON_JSON(issuer_public_master_key); TO_JSON_ARRAY(mint_service); - TO_JSON(protocol_version); + TO_JSON(protocol_version); TO_JSON_ARRAY(redeem_service); - TO_JSON_ARRAY(renew_service); + TO_JSON_ARRAY(renew_service); - r["type"]= "cdd"; + r["type"] = "cdd"; return r; } -crow::json::wvalue CDDC::to_json() const{ +crow::json::wvalue CDDC::to_json() const { crow::json::wvalue r; TO_JSON_JSON(cdd); TO_JSON(signature); - r["type"]= "cdd certificate"; + r["type"] = "cdd certificate"; return r; } @@ -80,25 +78,23 @@ crow::json::wvalue MintKey::to_json() const { TO_JSON(id); TO_JSON(issuer_id); TO_JSON_JSON(public_mint_key); - + TO_JSON(sign_coins_not_after); TO_JSON(sign_coins_not_before); - - r["type"]= "mint key"; - return r; -} - -crow::json::wvalue MintKeyCert::to_json() const { - crow::json::wvalue r; - TO_JSON_JSON(mint_key); - TO_JSON(signature); - r["type"]= "mint key certificate"; + r["type"] = "mint key"; return r; } -crow::json::wvalue Response::to_json() const -{ +crow::json::wvalue MintKeyCert::to_json() const { + crow::json::wvalue r; + TO_JSON_JSON(mint_key); + TO_JSON(signature); + r["type"] = "mint key certificate"; + return r; +} + +crow::json::wvalue Response::to_json() const { crow::json::wvalue r; TO_JSON(message_reference); TO_JSON(status_code); @@ -106,104 +102,93 @@ crow::json::wvalue Response::to_json() const return r; } - -crow::json::wvalue ResponseCDDSerial::to_json() const -{ +crow::json::wvalue ResponseCDDSerial::to_json() const { crow::json::wvalue r = Response::to_json(); TO_JSON(cdd_serial); - - r["type"]= "response cdd serial"; + + r["type"] = "response cdd serial"; return r; } -tl::expected -RequestCDDSerial::from_string(const std::string& str) -{ +tl::expected +RequestCDDSerial::from_string(const std::string &str) { auto json = crow::json::load(str); if (!json) { 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); - } else if ( json["type"]!="request cdd serial") { + } else if (json["type"] != "request cdd serial") { return tl::make_unexpected(eError::JSON_ERROR); - } else { + } else { RequestCDDSerial r; - r.message_reference= json["message_reference"].u(); + r.message_reference = json["message_reference"].u(); return r; } } -crow::json::wvalue ResponseCDDC::to_json() const -{ +crow::json::wvalue ResponseCDDC::to_json() const { crow::json::wvalue r = Response::to_json(); TO_JSON_JSON(cddc); - r["type"]= "response cdd serial"; + r["type"] = "response cdd serial"; return r; } -tl::expected -RequestCDDC::from_string(const std::string& str) -{ +tl::expected +RequestCDDC::from_string(const std::string &str) { auto json = crow::json::load(str); if (!json) { return tl::make_unexpected(eError::JSON_PARSE_ERROR); - } else if ( !( json.has("type") - && json.has("message_reference") - && json.has("cdd_serial") - ) ) { + } else if (!(json.has("type") && json.has("message_reference") && + json.has("cdd_serial"))) { 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); - } else { + } else { RequestCDDC r; - r.cdd_serial=json["cdd_serial"].u(); - r.message_reference= json["message_reference"].u(); + r.cdd_serial = json["cdd_serial"].u(); + r.message_reference = json["message_reference"].u(); return r; } } -tl::expected -RequestMKCs::from_string(const std::string& str) { +tl::expected +RequestMKCs::from_string(const std::string &str) { auto json = crow::json::load(str); if (!json) { return tl::make_unexpected(eError::JSON_PARSE_ERROR); - } else if ( !(json.has("denominations") - && json.has("message_reference") - && json.has("mint_key_ids") - && json.has("type") - ) ) { + } else if (!(json.has("denominations") && json.has("message_reference") && + json.has("mint_key_ids") && json.has("type"))) { 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); - } else { + } else { RequestMKCs r; - r.message_reference= json["message_reference"].u(); + r.message_reference = json["message_reference"].u(); 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); } else { - for (auto d: denominations.lo()) { - r.denominations.push_back(d.u()); + for (auto d : denominations.lo()) { + r.denominations.push_back(d.u()); } } 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); } else { - for (auto k: mint_key_ids.lo()) { - r.mint_key_ids.push_back(k.u()); + for (auto k : mint_key_ids.lo()) { + r.mint_key_ids.push_back(k.u()); } } return r; } } - -crow::json::wvalue ResponseMKCs::to_json() const{ +crow::json::wvalue ResponseMKCs::to_json() const { crow::json::wvalue r = Response::to_json(); TO_JSON_ARRAY(keys); - r["type"]= "response mint key certificates"; + r["type"] = "response mint key certificates"; return r; } @@ -212,21 +197,17 @@ crow::json::wvalue Blind::to_json() const { TO_JSON(blinded_payload_hash); TO_JSON(mint_key_id); TO_JSON(reference); - r["type"]= "blinded payload hash"; + r["type"] = "blinded payload hash"; return r; -} +} -tl::expected Blind::from_json(const crow::json::rvalue& json) -{ - if ( !( json.has("type") - && json.has("blinded_payload_hash") - && json.has("mint_key_id") - && json.has("reference") - ) ) { +tl::expected Blind::from_json(const crow::json::rvalue &json) { + if (!(json.has("type") && json.has("blinded_payload_hash") && + json.has("mint_key_id") && json.has("reference"))) { 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); - } else { + } else { Blind r; r.blinded_payload_hash = json["blinded_payload_hash"].s(); r.mint_key_id = json["mint_key_id"].s(); @@ -239,40 +220,36 @@ crow::json::wvalue BlindSignature::to_json() const { crow::json::wvalue r; TO_JSON(blind_signature); TO_JSON(reference); - r["type"]= "blind signature"; + r["type"] = "blind signature"; return r; - } -tl::expected -RequestMint::from_string(const std::string& str){ +tl::expected +RequestMint::from_string(const std::string &str) { std::vector blinds; // "type": "request mint" auto json = crow::json::load(str); if (!json) { return tl::make_unexpected(eError::JSON_PARSE_ERROR); - } else if ( !( json.has("type") - && json.has("message_reference") - && json.has("transaction_reference") - && json.has("blinds") - ) ) { + } else if (!(json.has("type") && json.has("message_reference") && + json.has("transaction_reference") && json.has("blinds"))) { 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); - } else { + } else { RequestMint r; - r.message_reference= json["message_reference"].u(); - r.transaction_reference= json["transaction_reference"].s(); - if (json["blinds"].t()!=crow::json::type::List) { + r.message_reference = json["message_reference"].u(); + r.transaction_reference = json["transaction_reference"].s(); + if (json["blinds"].t() != crow::json::type::List) { 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); if (!b.has_value()) { - return tl::make_unexpected(b.error()); + return tl::make_unexpected(b.error()); } else { - r.blinds.push_back(b.value()); + r.blinds.push_back(b.value()); } } return r; @@ -282,7 +259,7 @@ RequestMint::from_string(const std::string& str){ crow::json::wvalue ResponseMint::to_json() const { crow::json::wvalue r = Response::to_json(); TO_JSON_ARRAY(blind_signatures); - r["type"]= "response mint"; + r["type"] = "response mint"; return r; } @@ -295,57 +272,51 @@ crow::json::wvalue Coin::Payload::to_json() const { TO_JSON(protocol_version); TO_JSON(serial); - r["type"]= "payload"; + r["type"] = "payload"; return r; } -crow::json::wvalue Coin::to_json() const -{ +crow::json::wvalue Coin::to_json() const { crow::json::wvalue r; TO_JSON_JSON(payload); TO_JSON(signature); - r["type"]= "coin"; + r["type"] = "coin"; return r; } -tl::expected Coin::Payload::from_json(const crow::json::rvalue& json) { - if ( !( json.has("cdd_location") - && json.has("denomination") - && json.has("issuer_id") - && json.has("mint_key_id") - && json.has("protocol_version") - && json.has("serial") - && json.has("type"))) { +tl::expected +Coin::Payload::from_json(const crow::json::rvalue &json) { + if (!(json.has("cdd_location") && json.has("denomination") && + json.has("issuer_id") && json.has("mint_key_id") && + json.has("protocol_version") && json.has("serial") && + json.has("type"))) { return tl::make_unexpected(eError::JSON_ERROR); - } else if ( json["type"]!="payload" ) { + } else if (json["type"] != "payload") { return tl::make_unexpected(eError::JSON_ERROR); - } else { + } else { Coin::Payload payload; payload.cdd_location = json["cdd_location"].s(); payload.denomination = json["denomination"].u(); - payload.issuer_id = json["issuer_id"].s(); - payload.mint_key_id = json["mint_key_id"].s(); + payload.issuer_id = json["issuer_id"].s(); + payload.mint_key_id = json["mint_key_id"].s(); payload.protocol_version = json["protocol_version"].s(); - payload.serial = json["serial"].s(); + payload.serial = json["serial"].s(); return payload; } } -tl::expected Coin::from_json(const crow::json::rvalue& json) { - if ( !( json.has("type") - && json.has("payload") - && json.has("signature") - ) ) { +tl::expected Coin::from_json(const crow::json::rvalue &json) { + if (!(json.has("type") && json.has("payload") && json.has("signature"))) { return tl::make_unexpected(eError::JSON_ERROR); - } else if ( json["type"]!="coin" ) { + } else if (json["type"] != "coin") { return tl::make_unexpected(eError::JSON_ERROR); - } else { + } else { auto pl = Payload::from_json(json["payload"]); if (!pl.has_value()) { return tl::make_unexpected(pl.error()); - } else { + } else { Coin c; - c.payload = pl.value(); + c.payload = pl.value(); c.signature = json["signature"].s(); return c; } @@ -356,137 +327,158 @@ crow::json::wvalue CoinStack::to_json() const { crow::json::wvalue r; TO_JSON_ARRAY(coins); TO_JSON(subject); - r["type"]= "coinstack"; + r["type"] = "coinstack"; return r; -} +} - -tl::expected -RequestRenew::from_string(const std::string& str) { +tl::expected +RequestRenew::from_string(const std::string &str) { auto json = crow::json::load(str); if (!json) { return tl::make_unexpected(eError::JSON_PARSE_ERROR); - } else if ( !( json.has("blinds") - && json.has("coins") - && json.has("transaction_reference") - && json.has("message_reference") - && json.has("type") - ) ) { + } else if (!(json.has("blinds") && json.has("coins") && + json.has("transaction_reference") && + json.has("message_reference") && json.has("type"))) { 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); - } else if ( (json["coins"].t()!=crow::json::type::List) - || (json["blinds"].t()!=crow::json::type::List) ) { - return tl::make_unexpected(eError::JSON_WRONG_VALUE_TYPE); - } else { + } else if ((json["coins"].t() != crow::json::type::List) || + (json["blinds"].t() != crow::json::type::List)) { + return tl::make_unexpected(eError::JSON_WRONG_VALUE_TYPE); + } else { RequestRenew r; - for (auto item: json["coins"]) { + for (auto item : json["coins"]) { auto coin = Coin::from_json(item); if (!coin.has_value()) { - return tl::make_unexpected(coin.error()); + return tl::make_unexpected(coin.error()); } else { - r.coins.push_back(coin.value()); + r.coins.push_back(coin.value()); } } - - for (auto item: json["blinds"]) { + + for (auto item : json["blinds"]) { auto blind = Blind::from_json(item); if (!blind.has_value()) { - return tl::make_unexpected(blind.error()); + return tl::make_unexpected(blind.error()); } else { - r.blinds.push_back(blind.value()); + r.blinds.push_back(blind.value()); } } - + r.message_reference = json["message_reference"].u(); - r.transaction_reference = json["transaction_reference"].s(); + r.transaction_reference = json["transaction_reference"].s(); return r; } } -crow::json::wvalue ResponseDelay::to_json() const { +crow::json::wvalue ResponseDelay::to_json() const { crow::json::wvalue r = Response::to_json(); - r["type"]= "response delay"; + r["type"] = "response delay"; return r; } -tl::expected -RequestResume::from_string(const std::string& str) { +tl::expected +RequestResume::from_string(const std::string &str) { auto json = crow::json::load(str); if (!json) { return tl::make_unexpected(eError::JSON_PARSE_ERROR); - } else if ( !( json.has("transaction_reference") - && json.has("message_reference") - && json.has("type") - ) ) { + } else if (!(json.has("transaction_reference") && + json.has("message_reference") && json.has("type"))) { 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); - } else { + } else { RequestResume r; r.message_reference = json["message_reference"].u(); - r.transaction_reference = json["transaction_reference"].s(); + r.transaction_reference = json["transaction_reference"].s(); return r; } } -tl::expected -RequestRedeem::from_string(const std::string& str) { - // "type": -auto json = crow::json::load(str); +tl::expected +RequestRedeem::from_string(const std::string &str) { + // "type": + auto json = crow::json::load(str); if (!json) { return tl::make_unexpected(eError::JSON_PARSE_ERROR); - } else if ( !( json.has("coins") - && json.has("message_reference") - && json.has("type") - ) ) { + } else if (!(json.has("coins") && json.has("message_reference") && + json.has("type"))) { 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); - } else { + } else { RequestRedeem r; 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); } - for (auto item: json["coins"]) { + for (auto item : json["coins"]) { auto coin = Coin::from_json(item); if (!coin.has_value()) { - return tl::make_unexpected(coin.error()); + return tl::make_unexpected(coin.error()); } else { - r.coins.push_back(coin.value()); + r.coins.push_back(coin.value()); } } return r; } } -crow::json::wvalue ResponseReedem::to_json() const { +crow::json::wvalue ResponseRedeem::to_json() const { crow::json::wvalue r = Response::to_json(); - r["type"]= "response redeem"; + r["type"] = "response redeem"; return r; -} +} +using std::cout; +using std::endl; /** this is for now our sample model */ class DefaultModel : public Model { public: - DefaultModel() {} - const CDDC& getCDDC() override {return m_cddc; }; - const CDDC& getCurrentCDDC() override {return m_cddc; }; - void mint() override {}; + tl::expected getCDDC(unsigned int cdd_serial) override { + cout << __FUNCTION__ << "(" << cdd_serial << ")" << endl; + return &m_cddc; + }; + + tl::expected getCurrentCDDC() override { + cout << __FUNCTION__ << "()" << endl; + + return &m_cddc; + } + + std::vector mint(const std::string &transaction_reference, + const std::vector &blinds) override { + std::vector res; + cout << __FUNCTION__ << "(" + << ")" << endl; + + return res; + } + + const std::vector + getMKCs(const std::vector &denominations, + const std::vector &mint_key_ids) override { + std::vector res; + cout << __FUNCTION__ << endl; + return res; + } + bool redeem(const std::vector &coins) override { + cout << __FUNCTION__ << endl; + return false; + } private: CDDC m_cddc; }; -std::unique_ptr Model::getModel(const std::string& /*backend_name*/) -{ -//:wq -//if (backend_name=="default") - return std::make_unique(); +std::unique_ptr Model::getModel(const std::string & /*backend_name*/) { + cout << __FUNCTION__ << endl; + //:wq + // if (backend_name=="default") + return std::make_unique(); } diff --git a/src/model.hpp b/src/model.hpp index fceea2d..e4bb52c 100644 --- a/src/model.hpp +++ b/src/model.hpp @@ -1,21 +1,20 @@ #ifndef MODEL_HPP #define MODEL_HPP -#include -#include #include +#include #include +#include #include "crow/json.h" #include "tl/expected.hpp" - struct PublicKey { 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; }; struct WeightedUrl { @@ -29,25 +28,27 @@ struct WeightedUrl { struct CDD { std::string additional_info; - time_t cdd_expiry_date;//: 2023-07-22T15:45:53.164685 - std::string cdd_location;//: https://opencent.org, - size_t cdd_serial;//: 1, - time_t cdd_signing_date;//: 2022-07-22T15:45:53.164685, - size_t currency_divisor;//: 100, - std::string currency_name;//: OpenCent, - std::vector denominations;//: [1, 2, 5], - std::string id;//: 23ed956e629ba35f0002eaf833ea436aea7db5c2, + time_t cdd_expiry_date; //: 2023-07-22T15:45:53.164685 + std::string cdd_location; //: https://opencent.org, + size_t cdd_serial; //: 1, + time_t cdd_signing_date; //: 2022-07-22T15:45:53.164685, + size_t currency_divisor; //: 100, + std::string currency_name; //: OpenCent, + std::vector denominations; //: [1, 2, 5], + std::string id; //: 23ed956e629ba35f0002eaf833ea436aea7db5c2, std::vector info_service; - /* eCipherSuite*/ std::string issuer_cipher_suite;//: RSA-SHA256-PSS-CHAUM82, - PublicKey issuer_public_master_key;//: { - // modulus: daaa63ddda38c189b8c49020c8276adbe0a695685a..., - // public_exponent: 65537, - // type: rsa public key + /* eCipherSuite*/ std::string issuer_cipher_suite; //: RSA-SHA256-PSS-CHAUM82, + PublicKey + issuer_public_master_key; //: { + // modulus: + // daaa63ddda38c189b8c49020c8276adbe0a695685a..., + // public_exponent: 65537, + // type: rsa public key //}, std::vector mint_service; - std::string protocol_version; //: https://opencoin.org/1.0, + std::string protocol_version; //: https://opencoin.org/1.0, std::vector redeem_service; - std::vector renew_service; + std::vector renew_service; crow::json::wvalue to_json() const; }; @@ -56,28 +57,28 @@ struct CDDC { CDD cdd; std::string signature; - crow::json::wvalue to_json() const; + crow::json::wvalue to_json() const; }; struct MintKey { unsigned int cdd_serial; std::string coins_expiry_date; //": "2023-10-30T15:45:53.164685", - unsigned int denomination; //": 1, - std::string id; // "1ceb977bb531c65f133ab8b0d60862b17369d96", + unsigned int denomination; //": 1, + std::string id; // "1ceb977bb531c65f133ab8b0d60862b17369d96", std::string issuer_id; //": "23ed956e629ba35f0002eaf833ea436aea7db5c2", PublicKey public_mint_key; - + std::string sign_coins_not_after; std::string sign_coins_not_before; // "type": "mint key" - crow::json::wvalue to_json() const; + crow::json::wvalue to_json() const; }; - + struct MintKeyCert { MintKey mint_key; std::string signature; - crow::json::wvalue to_json() const; + crow::json::wvalue to_json() const; }; enum class eError { @@ -94,32 +95,33 @@ struct Response { unsigned int status_code; std::string status_description; - virtual crow::json::wvalue to_json() const; + virtual crow::json::wvalue to_json() const; }; - + struct RequestCDDSerial { unsigned int message_reference; /// Client internal message reference. /// (Integer) - static tl::expected from_string(const std::string& str); + static tl::expected + from_string(const std::string &str); }; -struct ResponseCDDSerial: Response { +struct ResponseCDDSerial : Response { unsigned int cdd_serial; - crow::json::wvalue to_json() const override; + crow::json::wvalue to_json() const override; }; 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. /// (Integer) - static tl::expected from_string(const std::string& str); + static tl::expected from_string(const std::string &str); }; struct ResponseCDDC : Response { CDDC cddc; - crow::json::wvalue to_json() const override; + crow::json::wvalue to_json() const override; }; struct RequestMKCs { @@ -128,45 +130,44 @@ struct RequestMKCs { /// (Integer) std::vector mint_key_ids; // "type": "request mint key certificates" - static tl::expected from_string(const std::string& str); + static tl::expected from_string(const std::string &str); }; -struct ResponseMKCs: Response { +struct ResponseMKCs : Response { std::vector keys; - crow::json::wvalue to_json() const override; + crow::json::wvalue to_json() const override; }; struct Blind { - std::string blinded_payload_hash; //bigint - std::string mint_key_id; //bigint + std::string blinded_payload_hash; // bigint + std::string mint_key_id; // bigint std::string reference; crow::json::wvalue to_json() const; - static tl::expected from_json(const crow::json::rvalue& json); + static tl::expected from_json(const crow::json::rvalue &json); }; struct BlindSignature { std::string blind_signature; std::string reference; - crow::json::wvalue to_json() const; + crow::json::wvalue to_json() const; }; struct RequestMint { unsigned int message_reference; /// Client internal message reference. /// (Integer) - std::string transaction_reference; + std::string transaction_reference; std::vector blinds; // "type": "request mint" - static tl::expected from_string(const std::string& str); + static tl::expected from_string(const std::string &str); }; struct ResponseMint : Response { std::vector blind_signatures; - crow::json::wvalue to_json() const override; + crow::json::wvalue to_json() const override; }; - struct Coin { struct Payload { std::string cdd_location; @@ -175,23 +176,24 @@ struct Coin { std::string mint_key_id; std::string protocol_version; std::string serial; - - crow::json::wvalue to_json() const; - static tl::expected from_json(const crow::json::rvalue& json); + + crow::json::wvalue to_json() const; + static tl::expected + from_json(const crow::json::rvalue &json); }; Payload payload; std::string signature; - crow::json::wvalue to_json() const; - static tl::expected from_json(const crow::json::rvalue& json); + crow::json::wvalue to_json() const; + static tl::expected from_json(const crow::json::rvalue &json); }; struct CoinStack { std::vector coins; std::string subject; // "type": "coinstack" - crow::json::wvalue to_json() const; + crow::json::wvalue to_json() const; }; struct RequestRenew { @@ -199,21 +201,22 @@ struct RequestRenew { std::vector coins; unsigned int message_reference; /// Client internal message reference. /// (Integer) - std::string transaction_reference; + std::string transaction_reference; // "type": "request renew" - static tl::expected from_string(const std::string& str); + static tl::expected from_string(const std::string &str); }; struct ResponseDelay : Response { - crow::json::wvalue to_json() const override; + crow::json::wvalue to_json() const override; }; struct RequestResume { unsigned int message_reference; /// Client internal message reference. /// (Integer) - std::string transaction_reference; + std::string transaction_reference; // "type": "request resume" - static tl::expected from_string(const std::string& str); + static tl::expected + from_string(const std::string &str); }; struct RequestRedeem { @@ -221,24 +224,33 @@ struct RequestRedeem { unsigned int message_reference; /// Client internal message reference. /// (Integer) // "type": "request redeem" - static tl::expected from_string(const std::string& str); + static tl::expected + from_string(const std::string &str); }; -struct ResponseReedem : Response { - crow::json::wvalue to_json() const override; +struct ResponseRedeem : Response { + crow::json::wvalue to_json() const override; }; class Model { public: virtual ~Model(){}; - virtual const CDDC& getCDDC() = 0; - virtual const CDDC& getCurrentCDDC() = 0; - virtual void mint() = 0; + virtual tl::expected getCDDC(unsigned int cdd_serial) = 0; + virtual tl::expected getCurrentCDDC() = 0; + + virtual const std::vector + getMKCs(const std::vector &denominations, + const std::vector &mint_key_ids) = 0; + + virtual std::vector + mint(const std::string &transaction_reference, + const std::vector &blinds) = 0; + virtual bool redeem(const std::vector &coins) = 0; + + static std::unique_ptr getModel(const std::string &backend_name); - static std::unique_ptr getModel(const std::string& backend_name); private: }; #endif // #ifndef MODEL_HPP -