implemented jsn encoding for req and responses

This commit is contained in:
2022-11-18 15:47:52 +01:00
parent b044d1883e
commit 4d9f373d62
4 changed files with 419 additions and 73 deletions

View File

@ -3,7 +3,7 @@
#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)
#define TO_JSON_ARRAY(name) r[#name]=list_to_json(name)
template <class T>
crow::json::wvalue list_to_json(const std::vector<T>& array) {
@ -49,15 +49,15 @@ crow::json::wvalue CDD::to_json() const {
TO_JSON(cdd_signing_date);
TO_JSON(currency_divisor);
TO_JSON( currency_name);
SEQ_TO_JSON(denominations);
TO_JSON_ARRAY(denominations);
TO_JSON(id);
SEQ_TO_JSON(info_service);
TO_JSON_ARRAY(info_service);
TO_JSON(issuer_cipher_suite);
TO_JSON_JSON(issuer_public_master_key);
SEQ_TO_JSON(mint_service);
TO_JSON_ARRAY(mint_service);
TO_JSON(protocol_version);
SEQ_TO_JSON(redeem_service);
SEQ_TO_JSON(renew_service);
TO_JSON_ARRAY(redeem_service);
TO_JSON_ARRAY(renew_service);
r["type"]= "cdd";
return r;
@ -97,36 +97,223 @@ crow::json::wvalue MintKeyCert::to_json() const {
return r;
}
crow::json::wvalue ResponseCDDC::to_json() const
crow::json::wvalue Response::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;
}
std::optional<RequestCDDC> RequestCDDC::from_json(const crow::json::rvalue & json) {
if (json.has("cdd_serial")&&json.has("message_reference")) {
auto r = std::make_optional<RequestCDDC>();
r->cdd_serial=json["cdd_serial"].u();
r->message_reference= json["message_reference"].u();
crow::json::wvalue ResponseCDDSerial::to_json() const
{
crow::json::wvalue r = Response::to_json();
TO_JSON(cdd_serial);
r["type"]= "response cdd serial";
return r;
}
tl::expected<RequestCDDSerial,eError>
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("message_reference")) {
return tl::make_unexpected(eError::JSON_ERROR);
} else {
RequestCDDSerial r;
r.message_reference= json["message_reference"].u();
return r;
}
else {
return std::nullopt;
}
crow::json::wvalue ResponseCDDC::to_json() const
{
crow::json::wvalue r = Response::to_json();
TO_JSON_JSON(cddc);
r["type"]= "response cdd serial";
return r;
}
tl::expected<RequestCDDC,eError>
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("cdd_serial")&&json.has("message_reference"))) {
return tl::make_unexpected(eError::JSON_ERROR);
} else {
RequestCDDC r;
r.cdd_serial=json["cdd_serial"].u();
r.message_reference= json["message_reference"].u();
return r;
}
}
tl::expected<RequestMKCs,eError>
RequestMKCs::from_string(const std::string& str) {
std::vector<unsigned int> denominations;
unsigned int message_reference; /// Client internal message reference.
/// (Integer)
std::vector<unsigned int> mint_key_ids;
// "type": "request mint key certificates"
auto json = crow::json::load(str);
if (!json) {
return tl::make_unexpected(eError::JSON_PARSE_ERROR);
} else {
return tl::make_unexpected(eError::NOT_IMPLEMENTED);
}
}
crow::json::wvalue ResponseMKCs::to_json() const{
crow::json::wvalue r = Response::to_json();
TO_JSON_ARRAY(keys);
r["type"]= "response mint key certificates";
return r;
}
crow::json::wvalue Blind::to_json() const {
crow::json::wvalue r;
TO_JSON(blinded_payload_hash);
TO_JSON(mint_key_id);
TO_JSON(reference);
r["type"]= "blinded payload hash";
return r;
}
/** blafasel */
crow::json::wvalue BlindSignature::to_json() const {
crow::json::wvalue r;
TO_JSON(blind_signature);
TO_JSON(reference);
r["type"]= "blind signature";
return r;
}
tl::expected<RequestMint,eError>
RequestMint::from_string(const std::string& str){
std::vector<Blind> blinds;
unsigned int message_reference; /// Client internal message reference.
/// (Integer)
// "type": "request mint"
auto json = crow::json::load(str);
if (!json) {
return tl::make_unexpected(eError::JSON_PARSE_ERROR);
} else {
return tl::make_unexpected(eError::NOT_IMPLEMENTED);
}
}
crow::json::wvalue ResponseMint::to_json() const {
crow::json::wvalue r = Response::to_json();
TO_JSON_ARRAY(blind_signatures);
r["type"]= "response mint";
return r;
}
crow::json::wvalue Coin::Payload::to_json() const {
crow::json::wvalue r;
TO_JSON(cdd_location);
TO_JSON(denomination);
TO_JSON(issuer_id);
TO_JSON(mint_key_id);
TO_JSON(protocol_version);
TO_JSON(serial);
r["type"]= "payload";
return r;
}
crow::json::wvalue Coin::to_json() const
{
crow::json::wvalue r;
TO_JSON_JSON(payload);
TO_JSON(signature);
r["type"]= "coin";
return r;
}
crow::json::wvalue CoinStack::to_json() const {
crow::json::wvalue r;
TO_JSON_ARRAY(coins);
TO_JSON(subject);
r["type"]= "coinstack";
return r;
}
tl::expected<RequestRenew,eError>
RequestRenew::from_string(const std::string& str) {
std::vector<Blind> blinds;
std::vector<Coin> coins;
unsigned int message_reference; /// Client internal message reference.
/// (Integer)
unsigned int transaction_reference;
// "type": "request renew"
auto json = crow::json::load(str);
if (!json) {
return tl::make_unexpected(eError::JSON_PARSE_ERROR);
} else {
return tl::make_unexpected(eError::NOT_IMPLEMENTED);
}
}
crow::json::wvalue ResponseDelay::to_json() const {
crow::json::wvalue r = Response::to_json();
r["type"]= "response delay";
return r;
}
tl::expected<RequestResume,eError>
RequestResume::from_string(const std::string& str) {
unsigned int message_reference; /// Client internal message reference.
/// (Integer)
unsigned int transaction_reference;
// "type": "request resume"
auto json = crow::json::load(str);
if (!json) {
return tl::make_unexpected(eError::JSON_PARSE_ERROR);
} else {
return tl::make_unexpected(eError::NOT_IMPLEMENTED);
}
}
tl::expected<RequestRedeem,eError>
RequestRedeem::from_string(const std::string& str) {
std::vector<Coin> coins;
unsigned int message_reference; /// Client internal message reference.
/// (Integer)
unsigned int transaction_reference;
// "type": "request redeem"
auto json = crow::json::load(str);
if (!json) {
return tl::make_unexpected(eError::JSON_PARSE_ERROR);
} else {
return tl::make_unexpected(eError::NOT_IMPLEMENTED);
}
}
crow::json::wvalue ResponseReedem::to_json() const {
crow::json::wvalue r = Response::to_json();
r["type"]= "response redeem";
return r;
}
/** 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 {};
private: