@@ -16,54 +16,42 @@ apps::apps(name receiver, name code, eosio::datastream<const char *> ds)
1616 _smartcontracts(receiver, receiver.value) {}
1717
1818// Admin create app with random account name
19- void apps::admncrtapp (name creator,
19+ void apps::admncrtapp (name account_name,
20+ name creator,
2021 string json_data,
2122 string username,
2223 string origin)
2324{
2425 require_auth (get_self ());
26+ check (is_account (creator), " Creator account does not exist" );
27+ create_app_account (account_name, app_controller_account, creator);
28+ register_app_data (account_name, creator, json_data, username, origin);
29+ }
2530
31+ // Admin register app data for an existing account (without creating account)
32+ void apps::adminregapp (name account_name,
33+ name creator,
34+ string json_data,
35+ string username,
36+ string origin)
37+ {
38+ require_auth (get_self ());
39+ check (is_account (account_name), " Account does not exist" );
2640 check (is_account (creator), " Creator account does not exist" );
41+ register_app_data (account_name, creator, json_data, username, origin);
42+ }
2743
44+ // Private helper: Register app data, account type, and update authority
45+ void apps::register_app_data (name account_name, name creator, const string &json_data, const string &username, const string &origin)
46+ {
2847 check_app_username_chars (username);
29-
30- // Uniqueness checks for username and origin
31- checksum256 username_hash = eosio::sha256 (username.c_str (), std::strlen (username.c_str ()));
32- {
33- auto uidx = _appsv3.get_index <" usernamehash" _n>();
34- check (uidx.find (username_hash) == uidx.end (), " Username already taken" );
35- }
36- checksum256 origin_hash = eosio::sha256 (origin.c_str (), std::strlen (origin.c_str ()));
37- {
38- auto oidx = _appsv3.get_index <" originhash" _n>();
39- check (oidx.find (origin_hash) == oidx.end (), " Origin already taken" );
40- }
41-
42- // Generate random account name from username and json_data hashes
43- auto json_hash = eosio::sha256 (json_data.c_str (), std::strlen (json_data.c_str ()));
44- const eosio::name random_name = random_account_name (username_hash, json_hash, enum_account_type::App);
45-
46- // Create account with owner=gov.tmy, active=creator
47- authority owner_authority = create_authority_with_account (app_controller_account);
48- authority active_authority = create_authority_with_account (creator);
49- active_authority.accounts .push_back ({.permission = create_eosio_code_permission_level (get_self ()), .weight = 1 });
50-
51- newaccount_action newaccountaction (" eosio" _n, {get_self (), " active" _n});
52- newaccountaction.send (get_self (), random_name, owner_authority, active_authority);
53-
54- // Update resource config
55- tonomy::resource_config_table _resource_config (get_self (), get_self ().value );
56- auto config = _resource_config.get ();
57- config.total_cpu_weight_allocated = this ->initial_cpu_weight_allocation ;
58- config.total_net_weight_allocated = this ->initial_net_weight_allocation ;
59- _resource_config.set (config, get_self ());
60-
61- // Set resource limits: ram=0, net=initial, cpu=initial
62- eosio::set_resource_limits (random_name, 0 , this ->initial_net_weight_allocation , this ->initial_cpu_weight_allocation );
48+ check_username_is_unique (username);
49+ check_app_origin_is_unique (origin);
50+ update_resource_config_and_limits (account_name);
6351
6452 // Register in appsv3
6553 _appsv3.emplace (get_self (), [&](auto &row) {
66- row.account_name = random_name ;
54+ row.account_name = account_name ;
6755 row.json_data = json_data;
6856 row.version = 3 ;
6957 row.username = username;
@@ -74,23 +62,23 @@ void apps::admncrtapp(name creator,
7462 // Set account type
7563 tonomy::account_type_table account_type (get_self (), get_self ().value );
7664 account_type.emplace (get_self (), [&](auto &row) {
77- row.account_name = random_name ;
65+ row.account_name = account_name ;
7866 row.acc_type = enum_account_type::App;
7967 row.version = 1 ;
8068 });
8169}
8270
83-
84- void apps::check_app_username (const checksum256 &username_hash)
71+ void apps::check_username_is_unique (const string &username)
8572{
73+ checksum256 username_hash = eosio::sha256 (username.c_str (), std::strlen (username.c_str ()));
8674 auto apps_by_username_hash_itr = _appsv3.get_index <" usernamehash" _n>();
8775 const auto username_itr = apps_by_username_hash_itr.find (username_hash);
8876 if (username_itr != apps_by_username_hash_itr.end ()) {
8977 throwError (" TCON1001" , " This app username is already taken" );
9078 }
9179}
9280
93- void apps::check_app_origin (const string &origin)
81+ void apps::check_app_origin_is_unique (const string &origin)
9482{
9583 auto origin_hash = eosio::sha256 (origin.c_str (), std::strlen (origin.c_str ()));
9684 auto apps_by_origin_hash_itr = _appsv3.get_index <" originhash" _n>();
@@ -113,29 +101,48 @@ void apps::check_app_username_chars(const string &username)
113101 }
114102}
115103
104+ void apps::update_resource_config_and_limits (name account_name)
105+ {
106+ // Update resource config
107+ tonomy::resource_config_table _resource_config (get_self (), get_self ().value );
108+ auto config = _resource_config.get ();
109+ config.total_cpu_weight_allocated += this ->initial_cpu_weight_allocation ;
110+ config.total_net_weight_allocated += this ->initial_net_weight_allocation ;
111+ _resource_config.set (config, get_self ());
112+
113+ // Set resource limits: ram=0, net=initial, cpu=initial
114+ eosio::set_resource_limits (account_name, 0 , this ->initial_net_weight_allocation , this ->initial_cpu_weight_allocation );
115+ }
116+
117+ void apps::create_app_account (name account_name, name owner_account, name active_account)
118+ {
119+ // Create account with specified owner and active authorities
120+ authority owner_authority = create_authority_with_account (owner_account);
121+ authority active_authority = create_authority_with_account (active_account);
122+ active_authority.accounts .push_back ({.permission = create_eosio_code_permission_level (get_self ()), .weight = 1 });
123+
124+ newaccount_action newaccountaction (" eosio" _n, {get_self (), " active" _n});
125+ newaccountaction.send (get_self (), account_name, owner_authority, active_authority);
126+ }
127+
116128void apps::admnupdapp (name account_name,
117129 string json_data,
118130 string username,
119131 string origin,
120132 uint8_t plan)
121133{
122134 require_auth (get_self ());
123- check (is_account (account_name), " Account does not exist" );
124135
125136 auto itr = _appsv3.find (account_name.value );
126137 check (itr != _appsv3.end (), " App does not exist; use admncrtapp to create" );
127138
128139 // validate and uniqueness checks if changed
129140 if (itr->username != username) {
130141 check_app_username_chars (username);
131- auto uidx = _appsv3.get_index <" usernamehash" _n>();
132- checksum256 username_hash = eosio::sha256 (username.c_str (), std::strlen (username.c_str ()));
133- check (uidx.find (username_hash) == uidx.end (), " Username already taken" );
142+ check_username_is_unique (username);
134143 }
135144 if (itr->origin != origin) {
136- auto oidx = _appsv3.get_index <" originhash" _n>();
137- checksum256 origin_hash = eosio::sha256 (origin.c_str (), std::strlen (origin.c_str ()));
138- check (oidx.find (origin_hash) == oidx.end (), " Origin already taken" );
145+ check_app_origin_is_unique (origin);
139146 }
140147 _appsv3.modify (itr, get_self (), [&](auto &row) {
141148 row.json_data = json_data;
@@ -276,41 +283,17 @@ void apps::appcreate(name creator,
276283 string origin)
277284{
278285 require_auth (creator);
279-
280- // Uniqueness checks for username and origin
281286 check_app_username_chars (username);
282- checksum256 username_hash = eosio::sha256 (username.c_str (), std::strlen (username.c_str ()));
283- {
284- auto uidx = _appsv3.get_index <" usernamehash" _n>();
285- check (uidx.find (username_hash) == uidx.end (), " Username already taken" );
286- }
287- checksum256 origin_hash = eosio::sha256 (origin.c_str (), std::strlen (origin.c_str ()));
288- {
289- auto oidx = _appsv3.get_index <" originhash" _n>();
290- check (oidx.find (origin_hash) == oidx.end (), " Origin already taken" );
291- }
287+ check_username_is_unique (username);
288+ check_app_origin_is_unique (origin);
292289
293290 // Generate random account name from username and json_data hashes
291+ checksum256 username_hash = eosio::sha256 (username.c_str (), std::strlen (username.c_str ()));
294292 auto json_hash = eosio::sha256 (json_data.c_str (), std::strlen (json_data.c_str ()));
295293 const eosio::name random_name = random_account_name (username_hash, json_hash, enum_account_type::App);
296294
297- // Create account with owner=gov.tmy, active=creator
298- authority owner_authority = create_authority_with_account (app_controller_account);
299- authority active_authority = create_authority_with_account (creator);
300- active_authority.accounts .push_back ({.permission = create_eosio_code_permission_level (get_self ()), .weight = 1 });
301-
302- newaccount_action newaccountaction (" eosio" _n, {get_self (), " active" _n});
303- newaccountaction.send (get_self (), random_name, owner_authority, active_authority);
304-
305- // Update resource config
306- tonomy::resource_config_table _resource_config (get_self (), get_self ().value );
307- auto config = _resource_config.get ();
308- config.total_cpu_weight_allocated = this ->initial_cpu_weight_allocation ;
309- config.total_net_weight_allocated = this ->initial_net_weight_allocation ;
310- _resource_config.set (config, get_self ());
311-
312- // Set resource limits: ram=0, net=initial, cpu=initial
313- eosio::set_resource_limits (random_name, 0 , this ->initial_net_weight_allocation , this ->initial_cpu_weight_allocation );
295+ create_app_account (random_name, app_controller_account, creator);
296+ update_resource_config_and_limits (random_name);
314297
315298 // Register in appsv3
316299 _appsv3.emplace (get_self (), [&](auto &row) {
@@ -342,9 +325,7 @@ void apps::appupdate(name account_name,
342325 // If username changed, ensure uniqueness
343326 if (itr->username != username) {
344327 check_app_username_chars (username);
345- checksum256 username_hash = eosio::sha256 (username.c_str (), std::strlen (username.c_str ()));
346- auto uidx = _appsv3.get_index <" usernamehash" _n>();
347- check (uidx.find (username_hash) == uidx.end (), " Username already taken" );
328+ check_username_is_unique (username);
348329 }
349330
350331 _appsv3.modify (itr, get_self (), [&](auto &row) {
@@ -359,7 +340,7 @@ void apps::appupdplan(name account_name,
359340 uint8_t plan)
360341{
361342 // Plan updates assumed admin-governed
362- require_auth (get_self () );
343+ require_auth (account_name );
363344 auto itr = _appsv3.find (account_name.value );
364345 check (itr != _appsv3.end (), " App does not exist" );
365346 _appsv3.modify (itr, get_self (), [&](auto &row) {
@@ -374,7 +355,7 @@ void apps::scdeploy(name account_name,
374355 const std::vector<char > &abi,
375356 string source_code_url)
376357{
377- require_auth (get_self () );
358+ require_auth (account_name );
378359 // TODO: Implement smart contract deployment
379360 check (false , " scdeploy not yet implemented" );
380361}
@@ -386,23 +367,23 @@ void apps::scupdate(name account_name,
386367 const std::vector<char > &abi,
387368 string source_code_url)
388369{
389- require_auth (get_self () );
370+ require_auth (account_name );
390371 // TODO: Implement smart contract update
391372 check (false , " scupdate not yet implemented" );
392373}
393374
394375void apps::appaddkey (name account_name,
395376 public_key key)
396377{
397- require_auth (get_self () );
378+ require_auth (account_name );
398379 // TODO: Implement key addition to app account's active permission
399380 check (false , " appaddkey not yet implemented" );
400381}
401382
402383void apps::appremkey (name account_name,
403384 public_key key)
404385{
405- require_auth (get_self () );
386+ require_auth (account_name );
406387 // TODO: Implement key removal from app account's active permission
407388 check (false , " appremkey not yet implemented" );
408389}
@@ -412,7 +393,7 @@ void apps::admnmigapp(name account_name,
412393 uint8_t plan,
413394 public_key key)
414395{
415- require_auth (get_self () );
396+ require_auth (account_name );
416397 // TODO: Implement V2 to V3 app migration
417398 check (false , " admnmigapp not yet implemented" );
418399}
0 commit comments