From 198fe16359926f51edb8a689e5208046404e7e14 Mon Sep 17 00:00:00 2001 From: ravinsp <33562092+ravinsp@users.noreply.github.com> Date: Mon, 11 Nov 2019 15:49:01 +0530 Subject: [PATCH] User message protocol fixes. --- src/conf.cpp | 45 ++++++++-------------- src/jsonschema/usrmsg_helpers.cpp | 63 ++++++++++++++++++++----------- 2 files changed, 57 insertions(+), 51 deletions(-) diff --git a/src/conf.cpp b/src/conf.cpp index 81032058..df6c7e98 100644 --- a/src/conf.cpp +++ b/src/conf.cpp @@ -65,8 +65,7 @@ int rekey() return -1; crypto::generate_signing_keys(cfg.pubkey, cfg.seckey); - if (binpair_to_hex() != 0) - return -1; + binpair_to_hex(); if (save_config() != 0) return -1; @@ -99,8 +98,7 @@ int create_contract() //We populate the in-memory struct with default settings and then save it to the file. crypto::generate_signing_keys(cfg.pubkey, cfg.seckey); - if (binpair_to_hex() != 0) - return -1; + binpair_to_hex(); cfg.mode = OPERATING_MODE::ACTIVE; cfg.listenip = "0.0.0.0"; @@ -317,14 +315,11 @@ int save_config() { rapidjson::Value v; std::string hex_pubkey; - if (util::bin2hex( - hex_pubkey, - reinterpret_cast(nodepk.data()), - nodepk.length()) != 0) - { - std::cerr << "Error encoding npl list.\n"; - return -1; - } + util::bin2hex( + hex_pubkey, + reinterpret_cast(nodepk.data()), + nodepk.length()); + v.SetString(rapidjson::StringRef(hex_pubkey.data()), allocator); unl.PushBack(v, allocator); } @@ -375,27 +370,19 @@ int save_config() /** * Decode current binary keys in 'cfg' and populate the it with hex keys. * - * @return 0 for successful conversion. -1 for failure. + * @return Always returns 0. */ int binpair_to_hex() { - if (util::bin2hex( - cfg.pubkeyhex, - reinterpret_cast(cfg.pubkey.data()), - cfg.pubkey.length()) != 0) - { - std::cout << "Error encoding public key bytes.\n"; - return -1; - } + util::bin2hex( + cfg.pubkeyhex, + reinterpret_cast(cfg.pubkey.data()), + cfg.pubkey.length()); - if (util::bin2hex( - cfg.seckeyhex, - reinterpret_cast(cfg.seckey.data()), - cfg.seckey.length()) != 0) - { - std::cout << "Error encoding secret key bytes.\n"; - return -1; - } + util::bin2hex( + cfg.seckeyhex, + reinterpret_cast(cfg.seckey.data()), + cfg.seckey.length()); return 0; } diff --git a/src/jsonschema/usrmsg_helpers.cpp b/src/jsonschema/usrmsg_helpers.cpp index 172818d1..0296bf92 100644 --- a/src/jsonschema/usrmsg_helpers.cpp +++ b/src/jsonschema/usrmsg_helpers.cpp @@ -9,26 +9,26 @@ namespace jsonschema::usrmsg { // User JSON message schema version -constexpr const char* SCHEMA_VERSION = "0.1"; +constexpr const char *SCHEMA_VERSION = "0.1"; // Separators -constexpr const char* SEP_COMMA = "\",\""; -constexpr const char* SEP_COLON = "\":\""; -constexpr const char* SEP_COMMA_NOQUOTE = ",\""; -constexpr const char* SEP_COLON_NOQUOTE = "\":"; +constexpr const char *SEP_COMMA = "\",\""; +constexpr const char *SEP_COLON = "\":\""; +constexpr const char *SEP_COMMA_NOQUOTE = ",\""; +constexpr const char *SEP_COLON_NOQUOTE = "\":"; // Message field names -const char* const FLD_VERSION = "version"; -constexpr const char* FLD_TYPE = "type"; -constexpr const char* FLD_CHALLENGE = "challenge"; -constexpr const char* FLD_SIG = "sig"; -constexpr const char* FLD_PUBKEY = "pubkey"; -constexpr const char* FLD_INPUT = "input"; -constexpr const char* FLD_MAX_LED_SEQ = "max_ledger_seqno"; -constexpr const char* FLD_CONTENT = "content"; -constexpr const char* FLD_NONCE = "nonce"; -constexpr const char* FLD_LCL = "lcl"; -constexpr const char* FLD_LCL_SEQ = "lcl_seqno"; +const char *const FLD_VERSION = "version"; +constexpr const char *FLD_TYPE = "type"; +constexpr const char *FLD_CHALLENGE = "challenge"; +constexpr const char *FLD_SIG = "sig"; +constexpr const char *FLD_PUBKEY = "pubkey"; +constexpr const char *FLD_INPUT = "input"; +constexpr const char *FLD_MAX_LED_SEQ = "max_ledger_seqno"; +constexpr const char *FLD_CONTENT = "content"; +constexpr const char *FLD_NONCE = "nonce"; +constexpr const char *FLD_LCL = "lcl"; +constexpr const char *FLD_LCL_SEQ = "lcl_seqno"; // Length of user random challenge bytes. const size_t CHALLENGE_LEN = 16; @@ -41,7 +41,7 @@ const size_t CHALLENGE_LEN = 16; * @param msg String reference to copy the generated json message string into. * Message format: * { - * "version": "", + * "version": "", * "type": "public_challenge", * "challenge": "" * } @@ -83,6 +83,12 @@ void create_user_challenge(std::string &msg, std::string &challengehex) /** * Constructs a status response message. * @param msg String reference to copy the generated json message string into. + * Message format: + * { + * "type": "stat_resp", + * "lcl": "" + * "lcl_seqno": + * } */ void create_status_response(std::string &msg) { @@ -105,11 +111,24 @@ void create_status_response(std::string &msg) /** * Constructs a contract output container message. * @param msg String reference to copy the generated json message string into. - * @param content The contract output content to be put in the message. + * Message format: + * { + * "type": "contract_output", + * "lcl": "" + * "lcl_seqno": , + * "content": "" + * } + * @param content The contract binary output content to be put in the message. */ void create_contract_output_container(std::string &msg, std::string_view content) { - msg.reserve(128); + std::string contenthex; + util::bin2hex( + contenthex, + reinterpret_cast(content.data()), + content.length()); + + msg.reserve(256); msg.append("{\"") .append(FLD_TYPE) .append(SEP_COLON) @@ -125,7 +144,7 @@ void create_contract_output_container(std::string &msg, std::string_view content .append(SEP_COMMA_NOQUOTE) .append(FLD_CONTENT) .append(SEP_COLON) - .append(content) + .append(contenthex) .append("\"}"); } @@ -204,7 +223,7 @@ int verify_user_challenge_response(std::string &extracted_pubkeyhex, std::string * Accepted signed input container format: * { * "type": "contract_input", - * "content": "", + * "content": "", * "sig": "" * } * @return 0 on successful extraction. -1 for failure. @@ -247,7 +266,7 @@ int extract_signed_input_container( * { * "nonce": "", * "input": "", - * "max_ledger_seqno": 4562712334 + * "max_ledger_seqno": * } * @return 0 on succesful extraction. -1 on failure. */