initial checkin

This commit is contained in:
Gulliver
2022-11-10 01:31:47 +01:00
parent cd09d38ac4
commit 1696e6a88d
6 changed files with 511 additions and 52 deletions

72
src/main.cpp Normal file
View File

@ -0,0 +1,72 @@
#include "crow/http_response.h"
#include "crow/json.h"
#include <optional>
#include "crow.h"
#include "model.hpp"
int main()
{
crow::SimpleApp app;
std::shared_ptr<Model> model = Model::getModel("simple");
CROW_ROUTE(app, "/cddc")//.methods(crow::HTTPMethod::GET)
([&model](const crow::request& req){
auto json = crow::json::load(req.body);
if (!json) {
return crow::response(crow::status::BAD_REQUEST,
"json parse error");
} else {
auto req_cddc = RequestCDDC::from_json(json);
if (!req_cddc)
return crow::response(crow::status::BAD_REQUEST,
"json decode error ");
else {
ResponseCDDC res;
// TBD use serial from req
res.cddc = model->getCDDC();
res.message_reference = req_cddc->message_reference;
res.status_code = 200;
return crow::response(res.to_json());
}
}
});
CROW_ROUTE(app, "/cddc/serial").methods(crow::HTTPMethod::GET)
([](){
return crow::response(crow::status::NOT_IMPLEMENTED);
});
CROW_ROUTE(app, "/mkcs").methods(crow::HTTPMethod::GET)
([](){
return crow::response(crow::status::NOT_IMPLEMENTED);
});
CROW_ROUTE(app, "/mint").methods(crow::HTTPMethod::GET)
([](){
return crow::response(crow::status::NOT_IMPLEMENTED);
});
CROW_ROUTE(app, "/renew").methods(crow::HTTPMethod::GET)
([](){
return crow::response(crow::status::NOT_IMPLEMENTED);
});
CROW_ROUTE(app, "/resume").methods(crow::HTTPMethod::GET)
([](){
return crow::response(crow::status::NOT_IMPLEMENTED);
});
CROW_ROUTE(app, "/redeem").methods(crow::HTTPMethod::GET)
([](){
return crow::response(crow::status::NOT_IMPLEMENTED);
});
app.port(18080).run();
}

130
src/model.cpp Normal file
View File

@ -0,0 +1,130 @@
#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 SEQ_TO_JSON(name) r[#name]=list_to_json(name)
template <class T>
crow::json::wvalue list_to_json(const std::vector<T>& array) {
crow::json::wvalue::list l;
for(auto item:array)
l.push_back(item.to_json());
return crow::json::wvalue(l);
}
crow::json::wvalue list_to_json(const std::vector<unsigned int>& array) {
crow::json::wvalue::list l;
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";
return r;
}
crow::json::wvalue WeightedUrl::to_json() const
{
crow::json::wvalue::list l;
crow::json::wvalue w(weight);
l.push_back(w);
l.push_back(url);
return l;
}
crow::json::wvalue CDD::to_json() const {
crow::json::wvalue r;
TO_JSON(additional_info);
TO_JSON(cdd_expiry_date);
TO_JSON(cdd_location);
TO_JSON(cdd_serial);
TO_JSON(cdd_signing_date);
TO_JSON(currency_divisor);
TO_JSON( currency_name);
SEQ_TO_JSON(denominations);
TO_JSON(id);
SEQ_TO_JSON(info_service);
TO_JSON(issuer_cipher_suite);
TO_JSON_JSON(issuer_public_master_key);
SEQ_TO_JSON(mint_service);
TO_JSON(protocol_version);
SEQ_TO_JSON(redeem_service);
SEQ_TO_JSON(renew_service);
r["type"]= "cdd";
return r;
}
crow::json::wvalue CDDC::to_json() const{
crow::json::wvalue r;
TO_JSON_JSON(cdd);
TO_JSON(signature);
r["type"]= "cdd certificate";
return r;
}
crow::json::wvalue MintKey::to_json() const {
crow::json::wvalue r;
TO_JSON(cdd_serial);
TO_JSON(coins_expiry_date);
TO_JSON(denomination);
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";
return r;
}
crow::json::wvalue ResponseCDDC::to_json() const
{
crow::json::wvalue r;
TO_JSON_JSON(cddc);
TO_JSON(message_reference);
TO_JSON(status_code);
TO_JSON(status_description);
r["type"]= "response cddc";
return r;
}
/** blafasel */
class DefaultModel : public Model {
public:
DefaultModel() {}
const CDDC& getCDDC() override {return m_cddc; };
void mint() override {};
private:
CDDC m_cddc;
};
std::unique_ptr<Model> Model::getModel(const std::string& backend_name)
{
//:wq
//if (backend_name=="default")
return std::make_unique<DefaultModel>();
}

111
src/model.hpp Normal file
View File

@ -0,0 +1,111 @@
#ifndef MODEL_HPP
#define MODEL_HPP
#include <memory>
#include <string>
#include <chrono>
#include "crow/json.h"
#include "tl/expected.hpp"
struct PublicKey {
std::string modulus; //: "daaa63ddda38c189b8c49020c8276adbe0a695685a...",
std::string public_exponent;//: 65537,
crow::json::wvalue to_json() const;
};
struct WeightedUrl {
uint32_t weight;
std::string url;
crow::json::wvalue to_json() const;
};
/** currency description document */
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<unsigned> denominations;//: [1, 2, 5],
std::string id;//: 23ed956e629ba35f0002eaf833ea436aea7db5c2,
std::vector<WeightedUrl> 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
//},
std::vector<WeightedUrl> mint_service;
std::string protocol_version; //: https://opencoin.org/1.0,
std::vector<WeightedUrl> redeem_service;
std::vector<WeightedUrl> renew_service;
crow::json::wvalue to_json() const;
};
struct CDDC {
CDD cdd;
std::string signature;
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",
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;
};
struct MintKeyCert {
MintKey mint_key;
std::string signature;
crow::json::wvalue to_json() const;
};
struct RequestCDDC {
unsigned int cdd_serial;/// The version of the CDD. (Int)
unsigned int message_reference; /// Client internal message reference.
/// (Integer)
static std::optional<RequestCDDC> from_json(const crow::json::rvalue & json);
};
struct ResponseCDDC {
CDDC cddc;
unsigned int message_reference;
unsigned int status_code;
std::string status_description;
crow::json::wvalue to_json() const;
};
class Model {
public:
virtual ~Model(){};
virtual const CDDC& getCDDC() = 0;
virtual void mint() = 0;
static std::unique_ptr<Model> getModel(const std::string& backend_name);
private:
};
#endif // #ifndef MODEL_HPP