From 48b46e6345ab7dcb212f494811446cb1dc6bba4a Mon Sep 17 00:00:00 2001 From: Dario Date: Fri, 28 Aug 2020 18:06:29 +0200 Subject: [PATCH] save entry of hedera account in db --- skeema/gradido_login/hedera_accounts.sql | 1 + .../HTTPInterface/AdminHederaAccountPage.cpp | 127 ++++++++++++++++-- src/cpp/SingletonManager/SessionManager.cpp | 2 + src/cpp/SingletonManager/SessionManager.h | 2 + src/cpp/controller/HederaAccount.cpp | 4 +- src/cpp/controller/HederaAccount.h | 2 +- src/cpp/model/table/CryptoKey.cpp | 4 +- src/cpp/model/table/HederaAccount.cpp | 29 ++-- src/cpp/model/table/HederaAccount.h | 12 +- 9 files changed, 159 insertions(+), 24 deletions(-) diff --git a/skeema/gradido_login/hedera_accounts.sql b/skeema/gradido_login/hedera_accounts.sql index 23ffa33d1..c8fcff4eb 100644 --- a/skeema/gradido_login/hedera_accounts.sql +++ b/skeema/gradido_login/hedera_accounts.sql @@ -4,6 +4,7 @@ CREATE TABLE `hedera_accounts` ( `account_hedera_id` int unsigned NOT NULL, `account_key_id` int unsigned NOT NULL, `balance` bigint unsigned NOT NULL DEFAULT '0', + `network_type` int NOT NULL DEFAULT '0', `updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`), UNIQUE KEY `account_hedera_id` (`account_hedera_id`), diff --git a/src/cpp/HTTPInterface/AdminHederaAccountPage.cpp b/src/cpp/HTTPInterface/AdminHederaAccountPage.cpp index 0500a8996..884bc1cdc 100644 --- a/src/cpp/HTTPInterface/AdminHederaAccountPage.cpp +++ b/src/cpp/HTTPInterface/AdminHederaAccountPage.cpp @@ -7,7 +7,13 @@ #line 7 "F:\\Gradido\\gradido_login_server\\src\\cpsp\\adminHederaAccount.cpsp" - #include "../controller/HederaAccount.h" + +#include "../controller/HederaAccount.h" +#include "../controller/HederaId.h" +#include "../controller/CryptoKey.h" +#include "../lib/DataTypeConverter.h" +#include "../SingletonManager/SessionManager.h" + #line 1 "F:\\Gradido\\gradido_login_server\\src\\cpsp\\header_large.cpsp" #include "../ServerConfig.h" @@ -27,15 +33,104 @@ void AdminHederaAccountPage::handleRequest(Poco::Net::HTTPServerRequest& request if (_compressResponse) response.set("Content-Encoding", "gzip"); Poco::Net::HTMLForm form(request, request.stream()); -#line 10 "F:\\Gradido\\gradido_login_server\\src\\cpsp\\adminHederaAccount.cpsp" +#line 16 "F:\\Gradido\\gradido_login_server\\src\\cpsp\\adminHederaAccount.cpsp" const char* pageName = "Hedera Account"; + auto sm = SessionManager::getInstance(); + auto mm = MemoryManager::getInstance(); + auto user = mSession->getNewUser(); // add if(!form.empty()) { + // collect + auto shardNumString = form.get("account-shard-num", "0"); + auto realmNumString = form.get("account-realm-num", "0"); + auto numString = form.get("account-num", "0"); + auto privateKeyString = form.get("account-private-key", ""); + auto publicKeyString = form.get("account-public-key", ""); + auto networkTypeString = form.get("account-network-type", "0"); + + int shardNum = 0; + int realmNum = 0; + int num = 0; + int networkType = 0; + + MemoryBin* privateKey = nullptr; + MemoryBin* publicKey = nullptr; + + // validate + if(!sm->isValid(shardNumString, VALIDATE_ONLY_INTEGER)) { + addError(new Error("Account ID", "shard num not integer")); + } else { + if(DataTypeConverter::strToInt(shardNumString, shardNum) != DataTypeConverter::NUMBER_PARSE_OKAY) { + addError(new Error("Int Convert Error", "Error converting shardNumString to int")); + } + } + if(!sm->isValid(realmNumString, VALIDATE_ONLY_INTEGER)) { + addError(new Error("Account ID", "realm num not integer")); + } else { + if(DataTypeConverter::strToInt(realmNumString, realmNum) != DataTypeConverter::NUMBER_PARSE_OKAY) { + addError(new Error("Int Convert Error", "Error converting realmNumString to int")); + } + } + if(!sm->isValid(numString, VALIDATE_ONLY_INTEGER)) { + addError(new Error("Account ID", "num not integer")); + } else { + if(DataTypeConverter::strToInt(numString, num) != DataTypeConverter::NUMBER_PARSE_OKAY) { + addError(new Error("Int Convert Error", "Error converting num to int")); + } + } + if(!sm->isValid(privateKeyString, VALIDATE_ONLY_HEX)) { + addError(new Error("Account Keys", "private key not hex")); + } + if(!sm->isValid(publicKeyString, VALIDATE_ONLY_HEX)) { + addError(new Error("Account Keys", "public key not hex")); + } + if(!sm->isValid(networkTypeString, VALIDATE_ONLY_INTEGER)) { + addError(new Error("Network Type", "not integer")); + } else { + if(DataTypeConverter::strToInt(networkTypeString, networkType) != DataTypeConverter::NUMBER_PARSE_OKAY) { + addError(new Error("Int Convert Error", "Error converting network type to int")); + } + if(networkType < 0 || networkType >= (int)model::table::HEDERA_NET_COUNT) { + addError(new Error("Network Type", "invalid value")); + } + } + + if(0 == errorCount()) { + + auto hedera_id = controller::HederaId::create(shardNum, realmNum, num); + if(!hedera_id->getModel()->insertIntoDB(true)) { + addError(new Error("DB Error", "Error saving hedera id in DB")); + } + + privateKey = DataTypeConverter::hexToBin(privateKeyString); + publicKey = DataTypeConverter::hexToBin(publicKeyString); + + + KeyPairHedera key_pair(privateKey, publicKey); + mm->releaseMemory(privateKey); + mm->releaseMemory(publicKey); + auto crypto_key = controller::CryptoKey::create(&key_pair, user); + if(!crypto_key->getModel()->insertIntoDB(true)) { + addError(new Error("DB Error", "Error saving crypto key in DB")); + } + + if(0 == errorCount()) { + auto hedera_account = controller::HederaAccount::create( + user->getModel()->getID(), + hedera_id->getModel()->getID(), + crypto_key->getModel()->getID(), + 0, + (model::table::HederaNetworkType)networkType + ); + if(!hedera_account->getModel()->insertIntoDB(false)) { + addError(new Error("DB Error", "Error saving hedera account into DB")); + } + } + } } - #line 3 "F:\\Gradido\\gradido_login_server\\src\\cpsp\\header_large.cpsp" @@ -74,7 +169,7 @@ void AdminHederaAccountPage::handleRequest(Poco::Net::HTTPServerRequest& request responseStream << "\t\t
"; // end include header_large.cpsp responseStream << "\n"; -#line 21 "F:\\Gradido\\gradido_login_server\\src\\cpsp\\adminHederaAccount.cpsp" +#line 116 "F:\\Gradido\\gradido_login_server\\src\\cpsp\\adminHederaAccount.cpsp" responseStream << ( getErrorsHtml() ); responseStream << "\n"; responseStream << "
\n"; @@ -84,15 +179,31 @@ void AdminHederaAccountPage::handleRequest(Poco::Net::HTTPServerRequest& request responseStream << "\t
\n"; responseStream << "\t\t
\n"; responseStream << "\t\t\t\n"; - responseStream << "\t\t\t\n"; - responseStream << "\t\t\t\n"; - responseStream << "\t\t\t\n"; + responseStream << "\t\t\t\n"; + responseStream << "\t\t\t\n"; + responseStream << "\t\t\t\n"; responseStream << "\t\t\t\n"; responseStream << "\t\t\t\n"; responseStream << "\t\t\t\n"; responseStream << "\t\t\t\n"; + responseStream << "\t\t\t\n"; + responseStream << "\t\t\t\n"; responseStream << "\t\t\t\n"; responseStream << "\t
\n"; diff --git a/src/cpp/SingletonManager/SessionManager.cpp b/src/cpp/SingletonManager/SessionManager.cpp index 8de83d6c5..8d0b77928 100644 --- a/src/cpp/SingletonManager/SessionManager.cpp +++ b/src/cpp/SingletonManager/SessionManager.cpp @@ -47,6 +47,8 @@ bool SessionManager::init() case VALIDATE_PASSWORD: mValidations[i] = new Poco::RegularExpression("^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[@$!%*?&+-_])[A-Za-z0-9@$!%*?&+-_]{8,}$"); break; case VALIDATE_PASSPHRASE: mValidations[i] = new Poco::RegularExpression("^(?:[a-z]* ){23}[a-z]*\s*$"); break; case VALIDATE_HAS_NUMBER: mValidations[i] = new Poco::RegularExpression(".*[0-9].*"); break; + case VALIDATE_ONLY_INTEGER: mValidations[i] = new Poco::RegularExpression("^[0-9]*$"); break; + case VALIDATE_ONLY_HEX: mValidations[i] = new Poco::RegularExpression("^(0x)?[a-fA-F0-9]*$"); break; case VALIDATE_HAS_SPECIAL_CHARACTER: mValidations[i] = new Poco::RegularExpression(".*[@$!%*?&+-].*"); break; case VALIDATE_HAS_UPPERCASE_LETTER: mValidations[i] = new Poco::RegularExpression(".*[A-Z].*"); diff --git a/src/cpp/SingletonManager/SessionManager.h b/src/cpp/SingletonManager/SessionManager.h index 208de09be..d57162305 100644 --- a/src/cpp/SingletonManager/SessionManager.h +++ b/src/cpp/SingletonManager/SessionManager.h @@ -28,6 +28,8 @@ enum SessionValidationTypes { VALIDATE_PASSWORD, VALIDATE_PASSPHRASE, VALIDATE_HAS_NUMBER, + VALIDATE_ONLY_INTEGER, + VALIDATE_ONLY_HEX, VALIDATE_HAS_SPECIAL_CHARACTER, VALIDATE_HAS_UPPERCASE_LETTER, VALIDATE_HAS_LOWERCASE_LETTER, diff --git a/src/cpp/controller/HederaAccount.cpp b/src/cpp/controller/HederaAccount.cpp index 9a858bd7c..42a08eb27 100644 --- a/src/cpp/controller/HederaAccount.cpp +++ b/src/cpp/controller/HederaAccount.cpp @@ -13,9 +13,9 @@ namespace controller { } - Poco::AutoPtr HederaAccount::create(int user_id, int account_hedera_id, int account_key_id, Poco::UInt64 balance/* = 0*/) + Poco::AutoPtr HederaAccount::create(int user_id, int account_hedera_id, int account_key_id, Poco::UInt64 balance/* = 0*/, model::table::HederaNetworkType type/* = HEDERA_MAINNET*/) { - auto db = new model::table::HederaAccount(user_id, account_hedera_id, account_key_id, balance); + auto db = new model::table::HederaAccount(user_id, account_hedera_id, account_key_id, balance, type); auto group = new HederaAccount(db); return Poco::AutoPtr(group); } diff --git a/src/cpp/controller/HederaAccount.h b/src/cpp/controller/HederaAccount.h index 9efd89c12..fd2cb4b23 100644 --- a/src/cpp/controller/HederaAccount.h +++ b/src/cpp/controller/HederaAccount.h @@ -14,7 +14,7 @@ namespace controller { ~HederaAccount(); - static Poco::AutoPtr create(int user_id, int account_hedera_id, int account_key_id, Poco::UInt64 balance = 0); + static Poco::AutoPtr create(int user_id, int account_hedera_id, int account_key_id, Poco::UInt64 balance = 0, model::table::HederaNetworkType type = model::table::HEDERA_MAINNET); static std::vector> load(const std::string& alias); static std::vector> listAll(); diff --git a/src/cpp/model/table/CryptoKey.cpp b/src/cpp/model/table/CryptoKey.cpp index d36f44de6..9588f57e7 100644 --- a/src/cpp/model/table/CryptoKey.cpp +++ b/src/cpp/model/table/CryptoKey.cpp @@ -43,8 +43,8 @@ namespace model { const char* CryptoKey::typeToString(KeyType type) { switch (type) { - case KEY_TYPE_ED25519_REF10: return "ed25519 ref10"; - case KEY_TYPE_SODIUM_ED25519: return "sodium ed22519"; + case KEY_TYPE_ED25519_SODIUM: return "ed25519 ref10"; + case KEY_TYPE_ED25519_HEDERA: return "sodium ed22519"; } return ""; } diff --git a/src/cpp/model/table/HederaAccount.cpp b/src/cpp/model/table/HederaAccount.cpp index 63c6068bc..06538fdcb 100644 --- a/src/cpp/model/table/HederaAccount.cpp +++ b/src/cpp/model/table/HederaAccount.cpp @@ -6,20 +6,21 @@ namespace model { namespace table { HederaAccount::HederaAccount() - : mUserId(0), mAccountHederaId(0), mAccountKeyId(0), mBalance(0) + : mUserId(0), mAccountHederaId(0), mAccountKeyId(0), mBalance(0), mType(0) { } - HederaAccount::HederaAccount(int user_id, int account_hedera_id, int account_key_id, Poco::UInt64 balance/* = 0*/) - : mUserId(user_id), mAccountHederaId(account_hedera_id), mAccountKeyId(account_key_id), mBalance(balance) + HederaAccount::HederaAccount(int user_id, int account_hedera_id, int account_key_id, Poco::UInt64 balance/* = 0*/, HederaNetworkType type /*= HEDERA_MAINNET*/) + : mUserId(user_id), mAccountHederaId(account_hedera_id), mAccountKeyId(account_key_id), mBalance(balance), mType(type) { } HederaAccount::HederaAccount(const HederaAccountTuple& tuple) : ModelBase(tuple.get<0>()), - mUserId(tuple.get<1>()), mAccountHederaId(tuple.get<2>()), mAccountKeyId(tuple.get<3>()), mBalance(tuple.get<4>()), mUpdated(tuple.get<5>()) + mUserId(tuple.get<1>()), mAccountHederaId(tuple.get<2>()), mAccountKeyId(tuple.get<3>()), + mBalance(tuple.get<4>()), mType(tuple.get<5>()), mUpdated(tuple.get<6>()) { } @@ -37,18 +38,28 @@ namespace model { ss << "account crypto key id: " << std::to_string(mAccountKeyId) << std::endl; // balance in tinybars, 100,000,000 tinybar = 1 HashBar ss << "account balance: " << std::to_string((double)(mBalance) * 100000000.0) << " HBAR" << std::endl; + ss << "Hedera Net Type: " << hederaNetworkTypeToString((HederaNetworkType)mType) << std::endl; ss << "last update: " << Poco::DateTimeFormatter::format(mUpdated, "%f.%m.%Y %H:%M:%S") << std::endl; return ss.str(); } + const char* HederaAccount::hederaNetworkTypeToString(HederaNetworkType type) + { + switch (type) { + case HEDERA_MAINNET: return "Mainnet"; + case HEDERA_TESTNET: return "Testnet"; + default: return ""; + } + } + Poco::Data::Statement HederaAccount::_loadFromDB(Poco::Data::Session session, const std::string& fieldName) { Poco::Data::Statement select(session); - select << "SELECT id, user_id, account_hedera_id, account_key_id, balance, updated FROM " << getTableName() + select << "SELECT id, user_id, account_hedera_id, account_key_id, balance, network_type, updated FROM " << getTableName() << " where " << fieldName << " = ?" - , into(mID), into(mUserId), into(mAccountHederaId), into(mAccountKeyId), into(mBalance), into(mUpdated); + , into(mID), into(mUserId), into(mAccountHederaId), into(mAccountKeyId), into(mBalance), into(mType), into(mUpdated); return select; @@ -57,7 +68,7 @@ namespace model { Poco::Data::Statement HederaAccount::_loadMultipleFromDB(Poco::Data::Session session, const std::string& fieldName) { Poco::Data::Statement select(session); - select << "SELECT id, user_id, account_hedera_id, account_key_id, balance, updated FROM " << getTableName() + select << "SELECT id, user_id, account_hedera_id, account_key_id, balance, network_type, updated FROM " << getTableName() << " where " << fieldName << " LIKE ?"; return select; @@ -77,8 +88,8 @@ namespace model { Poco::Data::Statement insert(session); lock(); insert << "INSERT INTO " << getTableName() - << " (user_id, account_hedera_id, account_key_id, balance) VALUES(?,?,?,?)" - , use(mUserId), use(mAccountHederaId), use(mAccountKeyId), use(mBalance); + << " (user_id, account_hedera_id, account_key_id, balance, network_type) VALUES(?,?,?,?,?)" + , use(mUserId), use(mAccountHederaId), use(mAccountKeyId), use(mBalance), use(mType); unlock(); return insert; } diff --git a/src/cpp/model/table/HederaAccount.h b/src/cpp/model/table/HederaAccount.h index ca56dfd62..2219fc521 100644 --- a/src/cpp/model/table/HederaAccount.h +++ b/src/cpp/model/table/HederaAccount.h @@ -7,13 +7,19 @@ namespace model { namespace table { - typedef Poco::Tuple HederaAccountTuple; + typedef Poco::Tuple HederaAccountTuple; + + enum HederaNetworkType { + HEDERA_MAINNET, + HEDERA_TESTNET, + HEDERA_NET_COUNT + }; class HederaAccount : public ModelBase { public: HederaAccount(); - HederaAccount(int user_id, int account_hedera_id, int account_key_id, Poco::UInt64 balance = 0); + HederaAccount(int user_id, int account_hedera_id, int account_key_id, Poco::UInt64 balance = 0, HederaNetworkType type = HEDERA_MAINNET); HederaAccount(const HederaAccountTuple& tuple); ~HederaAccount(); @@ -21,6 +27,7 @@ namespace model { const char* getTableName() const { return "hedera_accounts"; } std::string toString(); + static const char* hederaNetworkTypeToString(HederaNetworkType type); protected: Poco::Data::Statement _loadFromDB(Poco::Data::Session session, const std::string& fieldName); @@ -32,6 +39,7 @@ namespace model { int mAccountHederaId; int mAccountKeyId; Poco::UInt64 mBalance; + int mType; Poco::DateTime mUpdated; };