From eeee93bc7498ecaae4dfec0f84f9da5655cdfe3b Mon Sep 17 00:00:00 2001 From: sadiabbasi Date: Tue, 20 Feb 2024 16:52:21 +0300 Subject: [PATCH] added new app admin function --- contracts/tonomy/src/tonomy.cpp | 61 +++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/contracts/tonomy/src/tonomy.cpp b/contracts/tonomy/src/tonomy.cpp index 4263d05..272bb02 100644 --- a/contracts/tonomy/src/tonomy.cpp +++ b/contracts/tonomy/src/tonomy.cpp @@ -212,6 +212,67 @@ namespace tonomysystem row.version = 1; }); } + void newappadmin( + name user_name, + string app_name, + string description, + checksum256 username_hash, + string logo_url, + string origin, + public_key key) { + eosio::require_auth(user_name); + checksum256 description_hash = eosio::sha256(description.c_str(), description.length()); + + // use the password_key public key for the owner authority + authority key_authority = create_authority_with_key(key); + key_authority.accounts.push_back({.permission = create_eosio_code_permission_level(get_self()), .weight = 1}); + + // If the account name exists, this will fail + newaccount_action newaccountaction("eosio"_n, {get_self(), "active"_n}); + newaccountaction.send(get_self(), user_name, key_authority, key_authority); + + // Check the username is not already taken + auto apps_by_username_hash_itr = _apps.get_index<"usernamehash"_n>(); + const auto username_itr = apps_by_username_hash_itr.find(username_hash); + if (username_itr != apps_by_username_hash_itr.end()) + { + throwError("TCON1001", "This app username is already taken"); + } + + // Check the origin is not already taken + auto origin_hash = eosio::sha256(origin.c_str(), std::strlen(origin.c_str())); + auto apps_by_origin_hash_itr = _apps.get_index<"originhash"_n>(); + const auto origin_itr = apps_by_origin_hash_itr.find(origin_hash); + if (origin_itr != apps_by_origin_hash_itr.end()) + { + throwError("TCON1002", "This app origin is already taken"); + } + + tonomy::resource_config_table _resource_config(get_self(), get_self().value); + auto config = _resource_config.get(); + config.total_cpu_weight_allocated = this->initial_cpu_weight_allocation; + config.total_net_weight_allocated = this->initial_net_weight_allocation; + _resource_config.set(config, get_self()); + + // Store the password_salt and hashed username in table + _apps.emplace(get_self(), [&](auto &app_itr) + { + app_itr.account_name = user_name; + app_itr.app_name = app_name; + app_itr.description = description; + app_itr.logo_url = logo_url; + app_itr.origin = origin; + app_itr.username_hash = username_hash; }); + + // Store the account type in the account_type table + account_type_table account_type(get_self(), get_self().value); + account_type.emplace(get_self(), [&](auto &row) + { + row.account_name = user_name; + row.acc_type = enum_account_type::App; + row.version = 1; }); + } + void tonomy::adminsetapp( name account_name, string app_name,