mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
save entry of hedera account in db
This commit is contained in:
parent
e3cea5968b
commit
48b46e6345
@ -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`),
|
||||
|
||||
@ -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<div class=\"content\">";
|
||||
// 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 << "<div class=\"center-form-container\">\n";
|
||||
@ -84,15 +179,31 @@ void AdminHederaAccountPage::handleRequest(Poco::Net::HTTPServerRequest& request
|
||||
responseStream << "\t<div class=\"center-form-form\">\n";
|
||||
responseStream << "\t\t<form method=\"POST\">\n";
|
||||
responseStream << "\t\t\t<label class=\"form-label\">Hedera Account ID</label>\n";
|
||||
responseStream << "\t\t\t<input class=\"form-control\" id=\"account-shard-num\" type=\"number\" name=\"account-shard-num\"/>\n";
|
||||
responseStream << "\t\t\t<input class=\"form-control\" id=\"account-realm-num\" type=\"number\" name=\"account-realm-num\"/>\n";
|
||||
responseStream << "\t\t\t<input class=\"form-control\" id=\"account-num\" type=\"number\" name=\"account-num\"/>\n";
|
||||
responseStream << "\t\t\t<input class=\"form-control\" id=\"account-shard-num\" placeholder=\"shard\" type=\"number\" name=\"account-shard-num\"/>\n";
|
||||
responseStream << "\t\t\t<input class=\"form-control\" id=\"account-realm-num\" placeholder=\"realm\" type=\"number\" name=\"account-realm-num\"/>\n";
|
||||
responseStream << "\t\t\t<input class=\"form-control\" id=\"account-num\" placeholder=\"num\" type=\"number\" name=\"account-num\"/>\n";
|
||||
responseStream << "\t\t\t<label class=\"form-label\" for=\"account-private-key\">Private Key</label>\n";
|
||||
responseStream << "\t\t\t<input class=\"form-control\" id=\"account-private-key\" type=\"text\" name=\"account-private-key\"/>\n";
|
||||
responseStream << "\t\t\t<label class=\"form-label\" for=\"account-public-key\">Public Key</label>\n";
|
||||
responseStream << "\t\t\t<input class=\"form-control\" id=\"account-public-key\" type=\"text\" name=\"account-public-key\"/>\n";
|
||||
responseStream << "\t\t\t<label class=\"form-label\" for=\"account-network-type\">Network Type</label>\n";
|
||||
responseStream << "\t\t\t<select class=\"form-control\" name=\"account-network-type\" id=\"account-network-type\">\n";
|
||||
responseStream << "\t\t\t";
|
||||
#line 133 "F:\\Gradido\\gradido_login_server\\src\\cpsp\\adminHederaAccount.cpsp"
|
||||
for(int i = 0; i < model::table::HEDERA_NET_COUNT; i++) { responseStream << "\n";
|
||||
responseStream << "\t\t\t\t<option value=\"";
|
||||
#line 134 "F:\\Gradido\\gradido_login_server\\src\\cpsp\\adminHederaAccount.cpsp"
|
||||
responseStream << ( i );
|
||||
responseStream << "\">";
|
||||
#line 134 "F:\\Gradido\\gradido_login_server\\src\\cpsp\\adminHederaAccount.cpsp"
|
||||
responseStream << ( model::table::HederaAccount::hederaNetworkTypeToString((model::table::HederaNetworkType)i) );
|
||||
responseStream << "</option>\n";
|
||||
responseStream << "\t\t\t";
|
||||
#line 135 "F:\\Gradido\\gradido_login_server\\src\\cpsp\\adminHederaAccount.cpsp"
|
||||
} responseStream << "\n";
|
||||
responseStream << "\t\t\t</select>\n";
|
||||
responseStream << "\t\t\t<input class=\"center-form-submit form-button\" type=\"submit\" name=\"submit\" value=\"";
|
||||
#line 36 "F:\\Gradido\\gradido_login_server\\src\\cpsp\\adminHederaAccount.cpsp"
|
||||
#line 137 "F:\\Gradido\\gradido_login_server\\src\\cpsp\\adminHederaAccount.cpsp"
|
||||
responseStream << ( gettext("Add Account") );
|
||||
responseStream << "\">\n";
|
||||
responseStream << "\t</form>\n";
|
||||
|
||||
@ -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].*");
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -13,9 +13,9 @@ namespace controller {
|
||||
|
||||
}
|
||||
|
||||
Poco::AutoPtr<HederaAccount> HederaAccount::create(int user_id, int account_hedera_id, int account_key_id, Poco::UInt64 balance/* = 0*/)
|
||||
Poco::AutoPtr<HederaAccount> 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<HederaAccount>(group);
|
||||
}
|
||||
|
||||
@ -14,7 +14,7 @@ namespace controller {
|
||||
|
||||
~HederaAccount();
|
||||
|
||||
static Poco::AutoPtr<HederaAccount> create(int user_id, int account_hedera_id, int account_key_id, Poco::UInt64 balance = 0);
|
||||
static Poco::AutoPtr<HederaAccount> 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<Poco::AutoPtr<HederaAccount>> load(const std::string& alias);
|
||||
static std::vector<Poco::AutoPtr<HederaAccount>> listAll();
|
||||
|
||||
@ -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 "<unknown type>";
|
||||
}
|
||||
|
||||
@ -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 "<unknown>";
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@ -7,13 +7,19 @@
|
||||
namespace model {
|
||||
namespace table {
|
||||
|
||||
typedef Poco::Tuple<int, int, int, int, Poco::UInt64, Poco::DateTime> HederaAccountTuple;
|
||||
typedef Poco::Tuple<int, int, int, int, Poco::UInt64, int, Poco::DateTime> 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;
|
||||
};
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user