From 43dd1a5082a4365cb0f27143cbf4b48eaffc000c Mon Sep 17 00:00:00 2001 From: Dario Date: Tue, 1 Sep 2020 11:06:20 +0200 Subject: [PATCH] update grpc to new version, update some things they don't work for create the first query to hedera --- CMakeLists.txt | 7 ++- skeema/gradido_login/crypto_keys.sql | 2 +- src/cpp/Crypto/KeyPairHedera.cpp | 5 ++ .../HTTPInterface/AdminHederaAccountPage.cpp | 49 ++++++++++--------- src/cpp/controller/CryptoKey.cpp | 4 ++ src/cpp/controller/HederaAccount.cpp | 12 ++++- src/cpp/controller/HederaAccount.h | 2 +- src/cpp/controller/NodeServer.cpp | 15 +++++- src/cpp/controller/NodeServer.h | 6 ++- src/cpp/lib/DataTypeConverter.h | 1 + src/cpp/model/hedera/Query.h | 2 +- 11 files changed, 73 insertions(+), 32 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f246d7e52..6a24af9a3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -98,6 +98,7 @@ set(GRPC_PATH "dependencies/grpc/_build/Debug") set(GRPC_ABSL_PATH "dependencies/grpc/_build/third_party/abseil-cpp/absl/types/Debug") set(GRPC_CARES_PATH "dependencies/grpc/_build/third_party/cares/cares/lib/Debug") set(GRPC_BORING_SSL_PATH "dependencies/grpc/_build/third_party/boringssl-with-bazel/Debug") +set(GRPC_RE2_PATH "dependencies/grpc/_build/third_party/re2/Debug") find_library(GRPC_PLUSPLUS grpc++ PATHS ${GRPC_PATH} REQUIRED) find_library(GRPC_UNSECURE grpc++_unsecure PATHS ${GRPC_PATH} REQUIRED) find_library(GRPC_REFLECTION grpc++_reflection PATHS ${GRPC_PATH} REQUIRED) @@ -110,13 +111,17 @@ find_library(GRPC_ADDRESS_SORTING address_sorting PATHS ${GRPC_PATH} REQUIRED) find_library(GRPC_CARES cares PATHS ${GRPC_CARES_PATH} REQUIRED) find_library(GRPC_BORING_SSL_CRYPTO crypto ${GRPC_BORING_SSL_PATH} REQUIRED) find_library(GRPC_BORING_SSL_SSL ssl ${GRPC_BORING_SSL_PATH} REQUIRED) +find_library(GRPC_RE2 re2 ${GRPC_RE2_PATH} REQUIRED) find_library(GRPC_ABSL_BAD_OPTIONAL_ACCESS absl_bad_optional_access ${GRPC_ABSL_PATH} REQUIRED) set(GRPC_LIBS ${GRPC_PLUSPLUS} ${GRPC_REFLECTION} ${GRPC_UNSECURE} ${GRPC_LIB} ${GRPC_ERROR_DETAILS} ${GRPC_RLIB} ${GRPC_UPB} ${GRPC_ALTS} ${GRPC_ABSL_BAD_OPTIONAL_ACCESS} ${GRPC_ADDRESS_SORTING} - ${GRPC_CARES} ${GRPC_BORING_SSL_CRYPTO} ${GRPC_BORING_SSL_SSL} + ${GRPC_CARES} + ${GRPC_BORING_SSL_CRYPTO} + ${GRPC_BORING_SSL_SSL} + ${GRPC_RE2} ) set(MYSQL_INCLUDE_DIR "dependencies/mariadb-connector-c/include") diff --git a/skeema/gradido_login/crypto_keys.sql b/skeema/gradido_login/crypto_keys.sql index c90750ae3..c79987755 100644 --- a/skeema/gradido_login/crypto_keys.sql +++ b/skeema/gradido_login/crypto_keys.sql @@ -1,6 +1,6 @@ CREATE TABLE `crypto_keys` ( `id` int unsigned NOT NULL AUTO_INCREMENT, - `private_key` varbinary(64) NOT NULL, + `private_key` binary(80) NOT NULL, `public_key` binary(32) NOT NULL, `crypto_key_type_id` int NOT NULL DEFAULT '0', PRIMARY KEY (`id`), diff --git a/src/cpp/Crypto/KeyPairHedera.cpp b/src/cpp/Crypto/KeyPairHedera.cpp index 2846e9c43..27d9731a7 100644 --- a/src/cpp/Crypto/KeyPairHedera.cpp +++ b/src/cpp/Crypto/KeyPairHedera.cpp @@ -162,8 +162,13 @@ MemoryBin* KeyPairHedera::getCryptedPrivKey(const Poco::AutoPtrencrypt(mPrivateKey, &encryptedKey)) { + auto encrypted_private_key_hex_string = DataTypeConverter::binToHex(encryptedKey); + printf("[KeyPairHedera::getCryptedPrivKey] encryptet private key hex: %s\n", encrypted_private_key_hex_string.data()); return encryptedKey; } else { diff --git a/src/cpp/HTTPInterface/AdminHederaAccountPage.cpp b/src/cpp/HTTPInterface/AdminHederaAccountPage.cpp index 0169083ef..a9d4165c3 100644 --- a/src/cpp/HTTPInterface/AdminHederaAccountPage.cpp +++ b/src/cpp/HTTPInterface/AdminHederaAccountPage.cpp @@ -48,16 +48,14 @@ void AdminHederaAccountPage::handleRequest(Poco::Net::HTTPServerRequest& request auto uri_query = uri.getQueryParameters(); std::string action = ""; std::string account_id_from_query; - if(uri_query[0].first == "action") { - action = uri_query[0].second; + if(uri_query.size() >= 2) { + if(uri_query[0].first == "action") { + action = uri_query[0].second; + } + if(uri_query[1].first == "account_id") { + account_id_from_query = uri_query[1].second; + } } - if(uri_query[1].first == "account_id") { - account_id_from_query = uri_query[1].second; - } - for(auto it = uri_query.begin(); it != uri_query.end(); it++) { - printf("first: %s, second: %s\n", it->first.data(), it->second.data()); - } - if(action == "updateBalance") { int account_id = 0; if(DataTypeConverter::strToInt(account_id_from_query, account_id) != DataTypeConverter::NUMBER_PARSE_OKAY) { @@ -67,7 +65,7 @@ void AdminHederaAccountPage::handleRequest(Poco::Net::HTTPServerRequest& request if(!hedera_account.size() || hedera_account[0].isNull()) { addError(new Error("Action Update Balance", "hedera id not found")); } else { - hedera_account[0]->updateBalanceFromHedera(user); + hedera_account[0]->updateBalanceFromHedera(user, this); } } } @@ -227,7 +225,7 @@ void AdminHederaAccountPage::handleRequest(Poco::Net::HTTPServerRequest& request responseStream << "\t\t
"; // end include header_large.cpsp responseStream << "\n"; -#line 152 "F:\\Gradido\\gradido_login_server\\src\\cpsp\\adminHederaAccount.cpsp" +#line 150 "F:\\Gradido\\gradido_login_server\\src\\cpsp\\adminHederaAccount.cpsp" responseStream << ( getErrorsHtml() ); responseStream << "\n"; responseStream << "
\n"; @@ -244,38 +242,38 @@ void AdminHederaAccountPage::handleRequest(Poco::Net::HTTPServerRequest& request responseStream << "\t\t\t\t
Aktionen
\n"; responseStream << "\t\t\t
\n"; responseStream << "\t\t\t"; -#line 166 "F:\\Gradido\\gradido_login_server\\src\\cpsp\\adminHederaAccount.cpsp" +#line 164 "F:\\Gradido\\gradido_login_server\\src\\cpsp\\adminHederaAccount.cpsp" for(auto it = hedera_accounts.begin(); it != hedera_accounts.end(); it++) { auto hedera_account_model = (*it)->getModel(); auto updateUrl = ServerConfig::g_serverPath + "/hedera_account?action=updateBalance&account_id=" + std::to_string(hedera_account_model->getID()); responseStream << "\n"; responseStream << "\t\t\t\t
\n"; responseStream << "\t\t\t\t\t
"; -#line 171 "F:\\Gradido\\gradido_login_server\\src\\cpsp\\adminHederaAccount.cpsp" +#line 169 "F:\\Gradido\\gradido_login_server\\src\\cpsp\\adminHederaAccount.cpsp" responseStream << ( (*it)->getHederaId()->getModel()->toString() ); responseStream << "
\n"; responseStream << "\t\t\t\t\t
"; -#line 172 "F:\\Gradido\\gradido_login_server\\src\\cpsp\\adminHederaAccount.cpsp" +#line 170 "F:\\Gradido\\gradido_login_server\\src\\cpsp\\adminHederaAccount.cpsp" responseStream << ( hedera_account_model->getBalanceDouble() ); responseStream << " hbar
\n"; responseStream << "\t\t\t\t\t
"; -#line 173 "F:\\Gradido\\gradido_login_server\\src\\cpsp\\adminHederaAccount.cpsp" +#line 171 "F:\\Gradido\\gradido_login_server\\src\\cpsp\\adminHederaAccount.cpsp" responseStream << ( model::table::HederaAccount::hederaNetworkTypeToString(hedera_account_model->getNetworkType()) ); responseStream << "
\n"; responseStream << "\t\t\t\t\t
"; -#line 174 "F:\\Gradido\\gradido_login_server\\src\\cpsp\\adminHederaAccount.cpsp" +#line 172 "F:\\Gradido\\gradido_login_server\\src\\cpsp\\adminHederaAccount.cpsp" responseStream << ( hedera_account_model->getUpdatedString() ); responseStream << "
\n"; responseStream << "\t\t\t\t\t
\n"; responseStream << "\t\t\t\t\t\n"; responseStream << "\t\t\t\t
\n"; responseStream << "\t\t\t"; -#line 180 "F:\\Gradido\\gradido_login_server\\src\\cpsp\\adminHederaAccount.cpsp" +#line 178 "F:\\Gradido\\gradido_login_server\\src\\cpsp\\adminHederaAccount.cpsp" } responseStream << "\n"; responseStream << "\t\t
\n"; responseStream << "\t
\n"; @@ -283,7 +281,10 @@ void AdminHederaAccountPage::handleRequest(Poco::Net::HTTPServerRequest& request responseStream << "\t

Einen neuen Account anlegen

\n"; responseStream << "\t\n"; responseStream << "\t
\n"; - responseStream << "\t\t
\n"; + responseStream << "\t\t\n"; responseStream << "\t\t\t\n"; responseStream << "\t\t\t\n"; responseStream << "\t\t\t\n"; @@ -295,21 +296,21 @@ void AdminHederaAccountPage::handleRequest(Poco::Net::HTTPServerRequest& request responseStream << "\t\t\t\n"; responseStream << "\t\t\t\n"; responseStream << "\t\t\t\n"; responseStream << "\t
\n"; diff --git a/src/cpp/controller/CryptoKey.cpp b/src/cpp/controller/CryptoKey.cpp index 3fabc25c5..79d68a62e 100644 --- a/src/cpp/controller/CryptoKey.cpp +++ b/src/cpp/controller/CryptoKey.cpp @@ -1,6 +1,7 @@ #include "CryptoKey.h" #include "../SingletonManager/ErrorManager.h" +#include "../lib/DataTypeConverter.h" namespace controller { @@ -72,6 +73,9 @@ namespace controller { return nullptr; } MemoryBin* clearPassword = nullptr; + auto encrypted_private_key = model->getPrivateKeyEncrypted(); + auto encrypted_private_key_hex_string = DataTypeConverter::binToHex(encrypted_private_key); + printf("[CryptoKey::getKeyPair] encrypted private key hex: %s\n", encrypted_private_key_hex_string.data()); if (password->decrypt(model->getPrivateKeyEncrypted(), &clearPassword) != SecretKeyCryptography::AUTH_DECRYPT_OK) { printf("[CryptoKey::getKeyPair] return null, error decrypting\n"); return nullptr; diff --git a/src/cpp/controller/HederaAccount.cpp b/src/cpp/controller/HederaAccount.cpp index 564752f72..6172db46c 100644 --- a/src/cpp/controller/HederaAccount.cpp +++ b/src/cpp/controller/HederaAccount.cpp @@ -73,7 +73,7 @@ namespace controller { return resultVector; } - bool HederaAccount::updateBalanceFromHedera(Poco::AutoPtr user) + bool HederaAccount::updateBalanceFromHedera(Poco::AutoPtr user, ErrorList* errorReceiver/* = nullptr*/) { static const char* functionName = "HederaAccount::updateBalanceFromHedera"; @@ -101,7 +101,15 @@ namespace controller { query->sign(std::move(hedera_key_pair)); HederaRequest request; - request.request(query); + try { + request.request(query); + } + catch (Poco::Exception& ex) { + printf("[HederaAccount::updateBalanceFromHedera] exception calling hedera request: %s\n", ex.displayText().data()); + } + if (errorReceiver) { + errorReceiver->getErrors(&request); + } return false; } diff --git a/src/cpp/controller/HederaAccount.h b/src/cpp/controller/HederaAccount.h index 1a3fd9ace..7f08adddc 100644 --- a/src/cpp/controller/HederaAccount.h +++ b/src/cpp/controller/HederaAccount.h @@ -27,7 +27,7 @@ namespace controller { inline void setHederaId(Poco::AutoPtr hederaId) { mHederaID = hederaId; } inline Poco::AutoPtr getHederaId() { return mHederaID; } - bool updateBalanceFromHedera(Poco::AutoPtr user); + bool updateBalanceFromHedera(Poco::AutoPtr user, ErrorList* errorReceiver = nullptr); protected: HederaAccount(model::table::HederaAccount* dbModel); diff --git a/src/cpp/controller/NodeServer.cpp b/src/cpp/controller/NodeServer.cpp index 6096b2839..625e61dad 100644 --- a/src/cpp/controller/NodeServer.cpp +++ b/src/cpp/controller/NodeServer.cpp @@ -1,9 +1,21 @@ #include "NodeServer.h" #include "../SingletonManager/ErrorManager.h" #include "../SingletonManager/ConnectionManager.h" +#include "Poco/RegularExpression.h" namespace controller { + Poco::RegularExpression g_filterHttp("^https?://"); + + std::string NodeServerConnection::getUriWithPort() const + { + std::string protocol; + g_filterHttp.extract(url, protocol); + return url.substr(protocol.size()) + ":" + std::to_string(port); + } + + + NodeServer::NodeServer(model::table::NodeServer* dbModel) { @@ -74,12 +86,13 @@ namespace controller { if (model::table::NodeServerIsHederaNode(type)) { select << ", node_hedera_id"; } - select << " from node_servers ORDER BY RAND() LIMIT 1" + select << " from node_servers where server_type = ? ORDER BY RAND() LIMIT 1" , Poco::Data::Keywords::into(result.url) , Poco::Data::Keywords::into(result.port); if (model::table::NodeServerIsHederaNode(type)) { select, Poco::Data::Keywords::into(hedera_node_id); } + select , Poco::Data::Keywords::bind((int)type); try { if (1 == select.execute()) { if (model::table::NodeServerIsHederaNode(type)) { diff --git a/src/cpp/controller/NodeServer.h b/src/cpp/controller/NodeServer.h index 8cc4b4e23..6945ac949 100644 --- a/src/cpp/controller/NodeServer.h +++ b/src/cpp/controller/NodeServer.h @@ -15,7 +15,11 @@ namespace controller { NodeServerConnection(const std::string& _url, int _port) : url(_url), port(_port) {} NodeServerConnection() :port(0) {}; - std::string getUrlWithPort() const { return url + ":" + std::to_string(port); } + // with http:// or https:// + inline std::string getUrlWithPort() const { return url + ":" + std::to_string(port); } + + // without http:// or https:// + std::string getUriWithPort() const; bool isValid() { return url != "" && port; } std::string url; diff --git a/src/cpp/lib/DataTypeConverter.h b/src/cpp/lib/DataTypeConverter.h index 46a767d97..0dad46954 100644 --- a/src/cpp/lib/DataTypeConverter.h +++ b/src/cpp/lib/DataTypeConverter.h @@ -31,6 +31,7 @@ namespace DataTypeConverter { std::string binToHex(const unsigned char* data, size_t size); std::string binToHex(const Poco::Nullable& nullableBin); inline std::string binToHex(const MemoryBin* data) { return binToHex(data->data(), data->size());} + inline std::string binToHex(const std::vector& data) { return binToHex(data.data(), data.size()); } //! \param pubkey pointer to array with crypto_sign_PUBLICKEYBYTES size std::string pubkeyToHex(const unsigned char* pubkey); diff --git a/src/cpp/model/hedera/Query.h b/src/cpp/model/hedera/Query.h index 8dff35024..ad9af855a 100644 --- a/src/cpp/model/hedera/Query.h +++ b/src/cpp/model/hedera/Query.h @@ -24,7 +24,7 @@ namespace model { bool sign(std::unique_ptr keyPairHedera); inline const proto::Query* getProtoQuery() const { return &mQueryProto; } - inline std::string getConnectionString() const { return mConnection.getUrlWithPort(); } + inline std::string getConnectionString() const { return mConnection.getUriWithPort(); } protected: Query(const controller::NodeServerConnection& connection);