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,96 +1,129 @@
#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.message_reference = req_cddc->message_reference;
res.cddc = model->getCDDC(); auto cddc = model->getCDDC(req_cddc->cdd_serial);
res.message_reference = req_cddc->message_reference; if (!cddc) {
res.status_code = crow::status::OK; res.status_code = crow::status::NOT_FOUND;
return crow::response(res.to_json()); } else {
} res.cddc = *cddc.value();
res.status_code = crow::status::OK;
}
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.message_reference = req->message_reference;
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, "/mkcs") auto cddc = model->getCurrentCDDC();
([&model](const crow::request& request){ if (!cddc) {
auto req = RequestMKCs::from_string(request.body); res.status_code = crow::status::NOT_FOUND;
if (!req) { } else {
return crow::response(crow::status::BAD_REQUEST); res.cdd_serial = (*cddc)->cdd.cdd_serial;
} else { res.status_code = crow::status::OK;
// \todo implement request }
return crow::response(crow::status::NOT_IMPLEMENTED); return crow::response(res.to_json());
} }
}); });
CROW_ROUTE(app, "/mint").methods(crow::HTTPMethod::GET) CROW_ROUTE(app, "/mkcs")
([&model](const crow::request& request){ .methods(crow::HTTPMethod::POST)([&model](const crow::request &request) {
auto req = RequestMint::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::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) auto minted = model->mint(req->transaction_reference, req->blinds);
([&model](const crow::request& request){
auto req = RequestMint::from_string(request.body); res.blind_signatures = minted;
if (!req) { res.status_code = crow::status::OK;
return crow::response(crow::status::BAD_REQUEST);
} else { return crow::response(res.to_json());
// \todo implement request }
return crow::response(crow::status::NOT_IMPLEMENTED);
}
}); });
CROW_ROUTE(app, "/resume").methods(crow::HTTPMethod::GET) CROW_ROUTE(app, "/renew")
([&model](const crow::request& request){ .methods(crow::HTTPMethod::POST)([&model](const crow::request &request) {
auto req = RequestResume::from_string(request.body); auto req = RequestRenew::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, "/redeem").methods(crow::HTTPMethod::GET) CROW_ROUTE(app, "/resume")
([&model](const crow::request& request){ .methods(crow::HTTPMethod::POST)([](const crow::request &request) {
auto req = RequestRedeem::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
return crow::response(crow::status::NOT_IMPLEMENTED); 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();
} }

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,26 +46,26 @@ 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);
TO_JSON(issuer_cipher_suite); TO_JSON(issuer_cipher_suite);
TO_JSON_JSON(issuer_public_master_key); TO_JSON_JSON(issuer_public_master_key);
TO_JSON_ARRAY(mint_service); TO_JSON_ARRAY(mint_service);
TO_JSON(protocol_version); TO_JSON(protocol_version);
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;
} }
@ -80,25 +78,23 @@ crow::json::wvalue MintKey::to_json() const {
TO_JSON(id); TO_JSON(id);
TO_JSON(issuer_id); TO_JSON(issuer_id);
TO_JSON_JSON(public_mint_key); TO_JSON_JSON(public_mint_key);
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";
return r;
}
r["type"] = "mint key";
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; 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; crow::json::wvalue r;
TO_JSON(message_reference); TO_JSON(message_reference);
TO_JSON(status_code); TO_JSON(status_code);
@ -106,104 +102,93 @@ 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());
} }
} }
return r; return r;
} }
} }
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,21 +197,17 @@ 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;
r.blinded_payload_hash = json["blinded_payload_hash"].s(); r.blinded_payload_hash = json["blinded_payload_hash"].s();
r.mint_key_id = json["mint_key_id"].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; 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());
} else { } else {
r.blinds.push_back(b.value()); r.blinds.push_back(b.value());
} }
} }
return r; return r;
@ -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,57 +272,51 @@ 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;
payload.cdd_location = json["cdd_location"].s(); payload.cdd_location = json["cdd_location"].s();
payload.denomination = json["denomination"].u(); payload.denomination = json["denomination"].u();
payload.issuer_id = json["issuer_id"].s(); payload.issuer_id = json["issuer_id"].s();
payload.mint_key_id = json["mint_key_id"].s(); payload.mint_key_id = json["mint_key_id"].s();
payload.protocol_version = json["protocol_version"].s(); payload.protocol_version = json["protocol_version"].s();
payload.serial = json["serial"].s(); payload.serial = json["serial"].s();
return payload; return payload;
} }
} }
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"]);
if (!pl.has_value()) { if (!pl.has_value()) {
return tl::make_unexpected(pl.error()); return tl::make_unexpected(pl.error());
} else { } else {
Coin c; Coin c;
c.payload = pl.value(); c.payload = pl.value();
c.signature = json["signature"].s(); c.signature = json["signature"].s();
return c; return c;
} }
@ -356,137 +327,158 @@ 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());
} else { } 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); 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());
} else { } else {
r.blinds.push_back(blind.value()); r.blinds.push_back(blind.value());
} }
} }
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();
return r; return r;
} }
} }
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;
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();
return r; return r;
} }
} }
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());
} else { } else {
r.coins.push_back(coin.value()); r.coins.push_back(coin.value());
} }
} }
return r; return r;
} }
} }
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,21 +1,20 @@
#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;
}; };
struct WeightedUrl { struct WeightedUrl {
@ -29,25 +28,27 @@ 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; //: {
// public_exponent: 65537, // modulus:
// type: rsa public key // daaa63ddda38c189b8c49020c8276adbe0a695685a...,
// public_exponent: 65537,
// type: rsa public key
//}, //},
std::vector<WeightedUrl> mint_service; std::vector<WeightedUrl> mint_service;
std::string protocol_version; //: https://opencoin.org/1.0, std::string protocol_version; //: https://opencoin.org/1.0,
std::vector<WeightedUrl> redeem_service; std::vector<WeightedUrl> redeem_service;
std::vector<WeightedUrl> renew_service; std::vector<WeightedUrl> renew_service;
crow::json::wvalue to_json() const; crow::json::wvalue to_json() const;
}; };
@ -56,28 +57,28 @@ struct CDDC {
CDD cdd; CDD cdd;
std::string signature; std::string signature;
crow::json::wvalue to_json() const; crow::json::wvalue to_json() const;
}; };
struct MintKey { struct MintKey {
unsigned int cdd_serial; unsigned int cdd_serial;
std::string coins_expiry_date; //": "2023-10-30T15:45:53.164685", std::string coins_expiry_date; //": "2023-10-30T15:45:53.164685",
unsigned int denomination; //": 1, unsigned int denomination; //": 1,
std::string id; // "1ceb977bb531c65f133ab8b0d60862b17369d96", std::string id; // "1ceb977bb531c65f133ab8b0d60862b17369d96",
std::string issuer_id; //": "23ed956e629ba35f0002eaf833ea436aea7db5c2", std::string issuer_id; //": "23ed956e629ba35f0002eaf833ea436aea7db5c2",
PublicKey public_mint_key; PublicKey public_mint_key;
std::string sign_coins_not_after; std::string sign_coins_not_after;
std::string sign_coins_not_before; std::string sign_coins_not_before;
// "type": "mint key" // "type": "mint key"
crow::json::wvalue to_json() const; crow::json::wvalue to_json() const;
}; };
struct MintKeyCert { struct MintKeyCert {
MintKey mint_key; MintKey mint_key;
std::string signature; std::string signature;
crow::json::wvalue to_json() const; crow::json::wvalue to_json() const;
}; };
enum class eError { enum class eError {
@ -94,32 +95,33 @@ struct Response {
unsigned int status_code; unsigned int status_code;
std::string status_description; std::string status_description;
virtual crow::json::wvalue to_json() const; virtual crow::json::wvalue to_json() const;
}; };
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 {
CDDC cddc; CDDC cddc;
crow::json::wvalue to_json() const override; crow::json::wvalue to_json() const override;
}; };
struct RequestMKCs { struct RequestMKCs {
@ -128,45 +130,44 @@ 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 {
std::string blind_signature; std::string blind_signature;
std::string reference; std::string reference;
crow::json::wvalue to_json() const; crow::json::wvalue to_json() const;
}; };
struct RequestMint { struct RequestMint {
unsigned int message_reference; /// Client internal message reference. unsigned int message_reference; /// Client internal message reference.
/// (Integer) /// (Integer)
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 {
std::vector<BlindSignature> blind_signatures; std::vector<BlindSignature> blind_signatures;
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;
@ -175,23 +176,24 @@ struct Coin {
std::string mint_key_id; std::string mint_key_id;
std::string protocol_version; std::string protocol_version;
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 {
std::vector<Coin> coins; std::vector<Coin> coins;
std::string subject; std::string subject;
// "type": "coinstack" // "type": "coinstack"
crow::json::wvalue to_json() const; crow::json::wvalue to_json() const;
}; };
struct RequestRenew { struct RequestRenew {
@ -199,21 +201,22 @@ struct RequestRenew {
std::vector<Coin> coins; std::vector<Coin> coins;
unsigned int message_reference; /// Client internal message reference. unsigned int message_reference; /// Client internal message reference.
/// (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 {
crow::json::wvalue to_json() const override; crow::json::wvalue to_json() const override;
}; };
struct RequestResume { struct RequestResume {
unsigned int message_reference; /// Client internal message reference. unsigned int message_reference; /// Client internal message reference.
/// (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,24 +224,33 @@ 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;
}; };
class Model { 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