diff --git a/README.md b/README.md index e29c56f..46d1eb0 100644 --- a/README.md +++ b/README.md @@ -18,8 +18,8 @@ The following table gives an overview of the mapping of requests to URLs: | Request | URL | Response | C++ Interface Method | |:------------------|:-------------|:------------------------------|:---------------------------------------------------| -| RequestCDDCSerial | /cddc/serial | ResponseCDDCSerial | cdd.cdd_serial of Model::getCurrentCDDC() | -| RequestCDDC | /cddc | ResponseCDDC | Model::getCurrentCDDC() | +| RequestCDDCSerial | /cddc/serial | ResponseCDDCSerial | cdd.cdd_serial of Model::get_current_cddc() | +| RequestCDDC | /cddc | ResponseCDDC | Model::get_current_cddc() | | RequestMKCs | /mkcs | ResponseMKCs | Model::getMKCs | | RequestMint | /mint | ResponseMint | Model::mint | | RequestRenew | /renew | ResponseMint or ResponseDelay | ? | diff --git a/src/big_int.cpp b/src/big_int.cpp index b43a2f5..b297fc7 100644 --- a/src/big_int.cpp +++ b/src/big_int.cpp @@ -1,82 +1,76 @@ #include "big_int.hpp" #include "tl/expected.hpp" -#include #include -#include +#pragma clang diagnostic push +#pragma ide diagnostic ignored "clion-misra-cpp2008-6-4-5" inline uint8_t hex(char c) { switch(c) { - case '0': return 0; - case '1': return 1; - case '2': return 2; - case '3': return 3; - case '4': return 4; - case '5': return 5; - case '6': return 6; - case '7': return 7; - case '8': return 8; - case '9': return 9; - case 'a': return 10; - case 'b': return 11; - case 'c': return 12; - case 'd': return 13; - case 'e': return 14; - case 'f': return 15; - case 'A': return 10; - case 'B': return 11; - case 'C': return 12; - case 'D': return 13; - case 'E': return 14; - case 'F': return 15; + case '0': return 0U; + case '1': return 1U; + case '2': return 2U; + case '3': return 3U; + case '4': return 4U; + case '5': return 5U; + case '6': return 6U; + case '7': return 7U; + case '8': return 8U; + case '9': return 9U; + case 'a': return 10U; + case 'b': return 11U; + case 'c': return 12U; + case 'd': return 13U; + case 'e': return 14U; + case 'f': return 15U; + case 'A': return 10U; + case 'B': return 11U; + case 'C': return 12U; + case 'D': return 13U; + case 'E': return 14U; + case 'F': return 15U; default: - return 0xff; + return 0xffU; } } +#pragma clang diagnostic pop tl::expected BigInt::from_string(const std::string& str) { BigInt b; - // std::cout << str << std::endl; - uint8_t hval=0; + uint8_t hval=0U; uint8_t nibble; - size_t i = str.size()+1; + size_t i = str.size()+1U; for(auto c : str) { nibble = hex(c); - if (nibble ==0xFF) { + if (nibble ==0xFFU) { return tl::make_unexpected(eError::PARSE_ERROR); } - if (i%2) { - hval = nibble << 4; + if ( i%2U != 0U ) { + hval = nibble << 4U; } else { hval |= nibble; - b.data[256 - (i/2)]= hval; - hval = 0; + b.data[256U - (i/2U)]= hval; + hval = 0U; } i--; } - // std::cout << std::hex; - // for (auto b: b.data) - // { - // std::cout << (int)b; - // } - // std::cout << std::dec << std::endl; return b; } BigInt BigInt::from_int(uint64_t value) { BigInt b; - b.data[248]=(value >> 56 & 0xff); - b.data[249]=(value >> 48 & 0xff); - b.data[250]=(value >> 40 & 0xff); - b.data[251]=(value >> 32 & 0xff); - b.data[252]=(value >> 24 & 0xff); - b.data[253]=(value >> 16 & 0xff); - b.data[254]=(value >> 8 & 0xff); - b.data[255]=(value & 0xff); + b.data[248U]=static_cast(value >> 56 & 0xffU); + b.data[249U]=static_cast(value >> 48 & 0xffU); + b.data[250U]=static_cast(value >> 40U & 0xffU); + b.data[251U]=static_cast(value >> 32U & 0xffU); + b.data[252U]=static_cast(value >> 24U & 0xffU); + b.data[253U]=static_cast(value >> 16U & 0xffU); + b.data[254U]=static_cast(value >> 8U & 0xffU); + b.data[255U]=static_cast(value & 0xffU); return b; } @@ -85,14 +79,16 @@ std::string BigInt::to_string() const { std::string s; uint8_t b; - uint8_t first_digit = 0; - for (size_t i = 0; i<256;i++) { + uint8_t first_digit = 0U; + for (size_t i = 0U; i<256U;i++) { b = data[i]; - if (first_digit==0) { - if (b==0) { - continue; - } else if (b>0xf) { - s.push_back(hex_char[b >> 4]); + if (first_digit==0U) { + if (b==0U) { + continue; + } else if (b>0xfU) { + s.push_back(hex_char[b >> 4]); + } else { + /* nothing to do here */ } s.push_back(hex_char[b & 0xf]); first_digit = b; diff --git a/src/big_int.hpp b/src/big_int.hpp index 93a2a60..0c42edb 100644 --- a/src/big_int.hpp +++ b/src/big_int.hpp @@ -1,5 +1,5 @@ -#ifndef BIG_INT_HPP -#define BIG_INT_HPP +#ifndef OC_ISSUER_BIG_INT_HPP +#define OC_ISSUER_BIG_INT_HPP #include #include @@ -9,19 +9,19 @@ struct BigInt { BigInt() : data() {} - virtual ~BigInt() {} + virtual ~BigInt() = default; enum class eError : uint8_t { PARSE_ERROR }; static tl::expected from_string(const std::string &str); static BigInt from_int(uint64_t value); - std::string to_string() const; + [[nodiscard]] std::string to_string() const; friend bool operator == (const BigInt& rhs, const BigInt& lhs); private: - std::array data; + std::array data; }; bool operator==(const BigInt &rhs, const BigInt &lhs); -#endif // #ifndef #ifndef BIG_INT_HPP +#endif // #ifndef #ifndef OC_ISSUER_BIG_INT_HPP diff --git a/src/json_serialisation.cpp b/src/json_serialisation.cpp index 6030775..2fc5609 100644 --- a/src/json_serialisation.cpp +++ b/src/json_serialisation.cpp @@ -10,16 +10,18 @@ template crow::json::wvalue list_to_json(const std::vector &array) { crow::json::wvalue::list l; - for (auto item : array) + for (const auto& item : array) { l.push_back(item.to_json()); - return crow::json::wvalue(l); + } + return {l}; } crow::json::wvalue list_to_json(const std::vector &array) { crow::json::wvalue::list l; - for (auto item : array) - l.push_back(item); - return crow::json::wvalue(l); + for (auto item : array) { + l.emplace_back(item); + } + return {l}; } crow::json::wvalue PublicKey::to_json() const { @@ -35,7 +37,7 @@ crow::json::wvalue WeightedUrl::to_json() const { crow::json::wvalue w(weight); l.push_back(w); - l.push_back(url); + l.emplace_back(url); return l; } @@ -171,7 +173,7 @@ RequestMKCs::from_string(const std::string &str) { if (denominations.t() != crow::json::type::List) { return tl::make_unexpected(eError::JSON_WRONG_REQUEST_TYPE); } else { - for (auto d : denominations.lo()) { + for (const auto& d : denominations.lo()) { r.denominations.push_back(d.u()); } } @@ -179,7 +181,7 @@ RequestMKCs::from_string(const std::string &str) { 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()) { + for (const auto& k: mint_key_ids.lo()) { auto kv = BigInt::from_string(k.s()); if (!kv.has_value()) { return tl::make_unexpected(eError::JSON_PARSE_ERROR); @@ -269,7 +271,7 @@ RequestMint::from_string(const std::string &str) { return tl::make_unexpected(eError::JSON_WRONG_VALUE_TYPE); } - for (auto item : json["blinds"]) { + for (const auto& item : json["blinds"]) { auto b = Blind::from_json(item); if (!b.has_value()) { return tl::make_unexpected(b.error()); @@ -384,7 +386,7 @@ RequestRenew::from_string(const std::string &str) { } else { RequestRenew r; - for (auto item : json["coins"]) { + for (const auto& item : json["coins"]) { auto coin = Coin::from_json(item); if (!coin.has_value()) { return tl::make_unexpected(coin.error()); @@ -393,7 +395,7 @@ RequestRenew::from_string(const std::string &str) { } } - for (auto item : json["blinds"]) { + for (const auto& item : json["blinds"]) { auto blind = Blind::from_json(item); if (!blind.has_value()) { return tl::make_unexpected(blind.error()); @@ -453,7 +455,7 @@ RequestRedeem::from_string(const std::string &str) { return tl::make_unexpected(eError::JSON_WRONG_VALUE_TYPE); } - for (auto item : json["coins"]) { + for (const auto& item : json["coins"]) { auto coin = Coin::from_json(item); if (!coin.has_value()) { return tl::make_unexpected(coin.error()); diff --git a/src/main.cpp b/src/main.cpp index c76baa7..e8e35c7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,22 +1,22 @@ #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"); + std::shared_ptr model = Model::get_model("simple"); CROW_ROUTE(app, "/cddc") - .methods(crow::HTTPMethod::POST)([&model](const crow::request &req) { + .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); + auto cddc = model->get_cddc(req_cddc->cdd_serial); if (!cddc) { res.status_code = crow::status::NOT_FOUND; } else { @@ -36,7 +36,7 @@ int main() { ResponseCDDCSerial res; res.message_reference = req->message_reference; - auto cddc = model->getCurrentCDDC(); + auto cddc = model->get_current_cddc(); if (!cddc) { res.status_code = crow::status::NOT_FOUND; } else { @@ -47,8 +47,8 @@ int main() { } }); - CROW_ROUTE(app, "/mkcs") - .methods(crow::HTTPMethod::POST)([&model](const crow::request &request) { + 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); @@ -61,8 +61,8 @@ int main() { } }); - CROW_ROUTE(app, "/mint") - .methods(crow::HTTPMethod::POST)([&model](const crow::request &request) { + 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); @@ -70,8 +70,9 @@ int main() { ResponseMint res; res.message_reference = req->message_reference; - /// \todo change argument transaction_reference to bigint - auto minted = model->mint(req->transaction_reference.to_string(), req->blinds); + /// \todo change argument transaction_reference to bigint + auto minted = + model->mint(req->transaction_reference.to_string(), req->blinds); res.blind_signatures = minted; res.status_code = crow::status::OK; diff --git a/src/model.cpp b/src/model.cpp index cd76be3..f2eebda 100644 --- a/src/model.cpp +++ b/src/model.cpp @@ -4,53 +4,53 @@ using std::cout; using std::endl; #define UNUSED(s) /* s */ + /** this is for now our sample model */ class DefaultModel : public Model { public: - DefaultModel() {} - tl::expected getCDDC(unsigned int cdd_serial) override { - cout << __FUNCTION__ << "(" << cdd_serial << ")" << endl; - return &m_cddc; - }; + DefaultModel() = default; - tl::expected getCurrentCDDC() override { - cout << __FUNCTION__ << "()" << endl; + tl::expected get_cddc(unsigned int cdd_serial) override { + cout << __FUNCTION__ << "(" << cdd_serial << ")" << endl; // NOLINT(clion-misra-cpp2008-5-2-12) + return &m_cddc; + }; - return &m_cddc; - } + tl::expected get_current_cddc() override { + cout << __FUNCTION__ << "()" << endl; - std::vector - mint(const std::string& UNUSED(transaction_reference), - const std::vector &UNUSED(blinds) ) override - { - std::vector res; - cout << __FUNCTION__ << "(" - << ")" << endl; + return &m_cddc; + } - return res; - } + std::vector + mint(const std::string & UNUSED(transaction_reference), + const std::vector &UNUSED(blinds)) override { + std::vector res; + cout << __FUNCTION__ << "(" + << ")" << endl; - const std::vector - getMKCs(const std::vector & UNUSED(denominations), - const std::vector & UNUSED(mint_key_ids) ) override - { - std::vector res; - cout << __FUNCTION__ << endl; - return res; - } + return res; + } - bool redeem(const std::vector &UNUSED(coins)) override { - cout << __FUNCTION__ << endl; - return false; - } + std::vector + getMKCs(const std::vector & UNUSED(denominations), + const std::vector & UNUSED(mint_key_ids)) override { + std::vector res; + cout << __FUNCTION__ << endl; + return res; + } + + bool redeem(const std::vector &UNUSED(coins)) override { + cout << __FUNCTION__ << endl; + return false; + } private: - CDDC m_cddc; + CDDC m_cddc; }; -std::unique_ptr Model::getModel(const std::string & /*backend_name*/) { - cout << __FUNCTION__ << endl; - //:wq - // if (backend_name=="default") - return std::make_unique(); +std::unique_ptr Model::get_model(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 6da0b8b..e47e415 100644 --- a/src/model.hpp +++ b/src/model.hpp @@ -1,5 +1,5 @@ -#ifndef MODEL_HPP -#define MODEL_HPP +#ifndef OC_ISSUER_MODEL_HPP +#define OC_ISSUER_MODEL_HPP #include #include @@ -15,14 +15,14 @@ struct PublicKey { BigInt modulus; //: "daaa63ddda38c189b8c49020c8276adbe0a695685a...", BigInt public_exponent;//: 65537, - crow::json::wvalue to_json() const; + [[nodiscard]] crow::json::wvalue to_json() const; }; struct WeightedUrl { uint32_t weight; std::string url; - crow::json::wvalue to_json() const; + [[nodiscard]] crow::json::wvalue to_json() const; }; /** @brief currency description document @@ -58,14 +58,14 @@ struct CDD { std::vector redeem_service; std::vector renew_service; - crow::json::wvalue to_json() const; + [[nodiscard]] crow::json::wvalue to_json() const; }; struct CDDC { CDD cdd; std::string signature; - crow::json::wvalue to_json() const; + [[nodiscard]] crow::json::wvalue to_json() const; }; struct MintKey { @@ -79,14 +79,14 @@ struct MintKey { std::string sign_coins_not_after; std::string sign_coins_not_before; // "type": "mint key" - crow::json::wvalue to_json() const; + [[nodiscard]] crow::json::wvalue to_json() const; }; struct MintKeyCert { MintKey mint_key; std::string signature; - crow::json::wvalue to_json() const; + [[nodiscard]] crow::json::wvalue to_json() const; }; enum class eError { @@ -103,7 +103,7 @@ struct Response { unsigned int status_code; std::string status_description; - virtual crow::json::wvalue to_json() const; + [[nodiscard]] virtual crow::json::wvalue to_json() const=0; }; struct RequestCDDCSerial { @@ -114,9 +114,9 @@ struct RequestCDDCSerial { }; struct ResponseCDDCSerial : Response { - unsigned int cdd_serial; + unsigned int cdd_serial{0U}; - crow::json::wvalue to_json() const override; + [[nodiscard]] crow::json::wvalue to_json() const override; }; struct RequestCDDC { @@ -129,7 +129,7 @@ struct RequestCDDC { struct ResponseCDDC : Response { CDDC cddc; - crow::json::wvalue to_json() const override; + [[nodiscard]] crow::json::wvalue to_json() const override; }; struct RequestMKCs { @@ -144,21 +144,21 @@ struct RequestMKCs { struct ResponseMKCs : Response { std::vector keys; - crow::json::wvalue to_json() const override; + [[nodiscard]] crow::json::wvalue to_json() const override; }; struct Blind { BigInt blinded_payload_hash; //bigint BigInt mint_key_id; //bigint std::string reference; - crow::json::wvalue to_json() const; + [[nodiscard]] crow::json::wvalue to_json() const; static tl::expected from_json(const crow::json::rvalue &json); }; struct BlindSignature { BigInt blind_signature; std::string reference; - crow::json::wvalue to_json() const; + [[nodiscard]] crow::json::wvalue to_json() const; }; struct RequestMint { @@ -173,7 +173,7 @@ struct RequestMint { struct ResponseMint : Response { std::vector blind_signatures; - crow::json::wvalue to_json() const override; + [[nodiscard]] crow::json::wvalue to_json() const override; }; struct Coin { @@ -184,15 +184,15 @@ struct Coin { BigInt mint_key_id; std::string protocol_version; BigInt serial; - - crow::json::wvalue to_json() const; + + [[nodiscard]] 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; + [[nodiscard]] crow::json::wvalue to_json() const; static tl::expected from_json(const crow::json::rvalue &json); }; @@ -200,7 +200,7 @@ struct CoinStack { std::vector coins; std::string subject; // "type": "coinstack" - crow::json::wvalue to_json() const; + [[nodiscard]] crow::json::wvalue to_json() const; }; struct RequestRenew { @@ -214,7 +214,7 @@ struct RequestRenew { }; struct ResponseDelay : Response { - crow::json::wvalue to_json() const override; + [[nodiscard]] crow::json::wvalue to_json() const override; }; struct RequestResume { @@ -236,12 +236,12 @@ struct RequestRedeem { }; struct ResponseRedeem : Response { - crow::json::wvalue to_json() const override; + [[nodiscard]] crow::json::wvalue to_json() const override; }; class Model { public: - virtual ~Model(){}; + virtual ~Model()=default; /** * return the CurrencyDocumentDescription certifikate for a specific @@ -249,13 +249,13 @@ public: * [see spec](https://opencoin.org/0.4/schemata.html#cddc) * @return returns a pointer to the CDDC if successful, false otherwise */ - virtual tl::expected getCDDC(unsigned int cdd_serial) = 0; + virtual tl::expected get_cddc(unsigned int cdd_serial) = 0; /** * return the CurrencyDocumentDescription certifikate * [see spec](https://opencoin.org/0.4/schemata.html#cddc) * @return returns a pointer to the CDDC if successful, false otherwise - */virtual tl::expected getCurrentCDDC() = 0; + */virtual tl::expected get_current_cddc() = 0; /** * return the MintKey certificates for a given list of denominations @@ -266,7 +266,7 @@ public: * * @return mint key certificates for given denominations and mint_key_ids */ - virtual const std::vector + virtual std::vector getMKCs(const std::vector &denominations, const std::vector &mint_key_ids) = 0; @@ -300,9 +300,9 @@ public: * * @return pointer to backend instance or null on invalid backend name */ - static std::unique_ptr getModel(const std::string &backend_name); + static std::unique_ptr get_model(const std::string &backend_name); private: }; -#endif // #ifndef MODEL_HPP +#endif // #ifndef OC_ISSUER_MODEL_HPP diff --git a/test/test_big_int.cpp b/test/test_big_int.cpp index 0a168dd..57667bc 100644 --- a/test/test_big_int.cpp +++ b/test/test_big_int.cpp @@ -2,7 +2,7 @@ #include TEST_CASE("BigInt::from_string", "[big_int]") { - std::string VALID [] = { + const std::string VALID [] = { "1", "12", "123", @@ -17,10 +17,10 @@ TEST_CASE("BigInt::from_string", "[big_int]") { "abcdef1" }; - for (size_t i= 0; ito_string() == VALID[i]); + for (const auto & i : VALID) { + auto b = BigInt::from_string(i); REQUIRE(b.has_value()); + REQUIRE(b->to_string() == i); } auto invalid_hex = "aabbcc..";