From b9237fc13da71ab0ddc181e11d71887d88c57316 Mon Sep 17 00:00:00 2001 From: Jeeyong Um Date: Fri, 11 Jan 2019 13:24:18 +0900 Subject: [PATCH 1/5] Add chain::authority constructor with permission_level --- libraries/chain/include/eosio/chain/authority.hpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/libraries/chain/include/eosio/chain/authority.hpp b/libraries/chain/include/eosio/chain/authority.hpp index 373deb56120..e7a7307bb56 100644 --- a/libraries/chain/include/eosio/chain/authority.hpp +++ b/libraries/chain/include/eosio/chain/authority.hpp @@ -66,6 +66,15 @@ struct authority { } } + authority( permission_level p, uint32_t delay_sec = 0 ) + :threshold(1),accounts({{p,1}}) + { + if( delay_sec > 0 ) { + threshold = 2; + waits.push_back(wait_weight{delay_sec, 1}); + } + } + authority( uint32_t t, vector k, vector p = {}, vector w = {} ) :threshold(t),keys(move(k)),accounts(move(p)),waits(move(w)){} authority(){} From b1d7f3b4f23975590fb22646e4ee3daeb2fcc0f4 Mon Sep 17 00:00:00 2001 From: Jeeyong Um Date: Fri, 11 Jan 2019 13:25:49 +0900 Subject: [PATCH 2/5] Enable to create account with permission level --- programs/cleos/main.cpp | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/programs/cleos/main.cpp b/programs/cleos/main.cpp index 7abce5f79da..7057975039f 100644 --- a/programs/cleos/main.cpp +++ b/programs/cleos/main.cpp @@ -84,6 +84,7 @@ Usage: ./cleos create account [OPTIONS] creator name OwnerKey ActiveKey #include #include #include +#include #include #include @@ -133,6 +134,7 @@ using namespace eosio::client::http; using namespace eosio::client::localize; using namespace eosio::client::config; using namespace boost::filesystem; +using auth_type = fc::static_variant; FC_DECLARE_EXCEPTION( explained_exception, 9000000, "explained exception, see error log" ); FC_DECLARE_EXCEPTION( localized_exception, 10000000, "an error occured" ); @@ -512,14 +514,20 @@ void send_transaction( signed_transaction& trx, int32_t extra_kcpu, packed_trans } } -chain::action create_newaccount(const name& creator, const name& newaccount, public_key_type owner, public_key_type active) { +chain::permission_level get_permission_level(const std::string& str) { + auto at_pos = str.find('@'); + EOSC_ASSERT(at_pos != std::string::npos, "invalid permission level"); + return permission_level { str.substr(0, at_pos), str.substr(at_pos + 1) }; +} + +chain::action create_newaccount(const name& creator, const name& newaccount, auth_type owner, auth_type active) { return action { get_account_permissions(tx_permission, {creator,config::active_name}), eosio::chain::newaccount{ .creator = creator, .name = newaccount, - .owner = eosio::chain::authority{1, {{owner, 1}}, {}}, - .active = eosio::chain::authority{1, {{active, 1}}, {}} + .owner = !owner.which() ? authority(owner.get()) : authority(owner.get()), + .active = !active.which() ? authority(active.get()) : authority(active.get()) } }; } @@ -987,8 +995,8 @@ struct create_account_subcommand { ); createAccount->add_option("creator", creator, localized("The name of the account creating the new account"))->required(); createAccount->add_option("name", account_name, localized("The name of the new account"))->required(); - createAccount->add_option("OwnerKey", owner_key_str, localized("The owner public key for the new account"))->required(); - createAccount->add_option("ActiveKey", active_key_str, localized("The active public key for the new account")); + createAccount->add_option("OwnerKey", owner_key_str, localized("The owner public key or permission level for the new account"))->required(); + createAccount->add_option("ActiveKey", active_key_str, localized("The active public key or permission level for the new account")); if (!simple) { createAccount->add_option("--stake-net", stake_net, @@ -1010,14 +1018,23 @@ struct create_account_subcommand { createAccount->set_callback([this] { if( !active_key_str.size() ) active_key_str = owner_key_str; - public_key_type owner_key, active_key; + auth_type owner, active; try { - owner_key = public_key_type(owner_key_str); - } EOS_RETHROW_EXCEPTIONS(public_key_type_exception, "Invalid owner public key: ${public_key}", ("public_key", owner_key_str)); + owner = public_key_type(owner_key_str); + } catch (...) { + try { + owner = get_permission_level(owner_key_str); + } EOS_RETHROW_EXCEPTIONS(public_key_type_exception, "Invalid owner public key: ${public_key}", ("public_key", owner_key_str)); + } try { - active_key = public_key_type(active_key_str); - } EOS_RETHROW_EXCEPTIONS(public_key_type_exception, "Invalid active public key: ${public_key}", ("public_key", active_key_str)); - auto create = create_newaccount(creator, account_name, owner_key, active_key); + active = public_key_type(active_key_str); + } catch (...) { + try { + active = get_permission_level(active_key_str); + } EOS_RETHROW_EXCEPTIONS(public_key_type_exception, "Invalid active public key: ${public_key}", ("public_key", active_key_str)); + } + + auto create = create_newaccount(creator, account_name, owner, active); if (!simple) { EOSC_ASSERT( buy_ram_eos.size() || buy_ram_bytes_in_kbytes || buy_ram_bytes, "ERROR: One of --buy-ram, --buy-ram-kbytes or --buy-ram-bytes should have non-zero value" ); EOSC_ASSERT( !buy_ram_bytes_in_kbytes || !buy_ram_bytes, "ERROR: --buy-ram-kbytes and --buy-ram-bytes cannot be set at the same time" ); From 5abc5fd3e68a612b05a7e7de77f5eef8dd9b1842 Mon Sep 17 00:00:00 2001 From: Jeeyong Um Date: Sat, 12 Jan 2019 15:25:52 +0900 Subject: [PATCH 3/5] Check the type of given authority explicitly during account creation --- programs/cleos/main.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/programs/cleos/main.cpp b/programs/cleos/main.cpp index 7057975039f..4ea3baab6fb 100644 --- a/programs/cleos/main.cpp +++ b/programs/cleos/main.cpp @@ -526,8 +526,8 @@ chain::action create_newaccount(const name& creator, const name& newaccount, aut eosio::chain::newaccount{ .creator = creator, .name = newaccount, - .owner = !owner.which() ? authority(owner.get()) : authority(owner.get()), - .active = !active.which() ? authority(active.get()) : authority(active.get()) + .owner = owner.contains() ? authority(owner.get()) : authority(owner.get()), + .active = active.contains() ? authority(active.get()) : authority(active.get()) } }; } From fb22ee56dfc155d4e10f399524cb8835bae2b1a7 Mon Sep 17 00:00:00 2001 From: Jeeyong Um Date: Sat, 12 Jan 2019 20:53:42 +0900 Subject: [PATCH 4/5] Fix capturing exceptions to distinguish cases properly --- programs/cleos/main.cpp | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/programs/cleos/main.cpp b/programs/cleos/main.cpp index 4ea3baab6fb..04f12cafe22 100644 --- a/programs/cleos/main.cpp +++ b/programs/cleos/main.cpp @@ -514,10 +514,9 @@ void send_transaction( signed_transaction& trx, int32_t extra_kcpu, packed_trans } } -chain::permission_level get_permission_level(const std::string& str) { - auto at_pos = str.find('@'); - EOSC_ASSERT(at_pos != std::string::npos, "invalid permission level"); - return permission_level { str.substr(0, at_pos), str.substr(at_pos + 1) }; +chain::permission_level to_permission_level(const std::string& s) { + auto at_pos = s.find('@'); + return permission_level { s.substr(0, at_pos), s.substr(at_pos + 1) }; } chain::action create_newaccount(const name& creator, const name& newaccount, auth_type owner, auth_type active) { @@ -1016,22 +1015,28 @@ struct create_account_subcommand { add_standard_transaction_options(createAccount, "creator@active"); createAccount->set_callback([this] { - if( !active_key_str.size() ) - active_key_str = owner_key_str; auth_type owner, active; - try { - owner = public_key_type(owner_key_str); - } catch (...) { + + if( owner_key_str.find('@') != string::npos ) { + try { + owner = to_permission_level(owner_key_str); + } EOS_RETHROW_EXCEPTIONS( explained_exception, "Invalid owner permission level: ${permission}", ("permission", owner_key_str) ) + } else { try { - owner = get_permission_level(owner_key_str); - } EOS_RETHROW_EXCEPTIONS(public_key_type_exception, "Invalid owner public key: ${public_key}", ("public_key", owner_key_str)); + owner = public_key_type(owner_key_str); + } EOS_RETHROW_EXCEPTIONS( public_key_type_exception, "Invalid owner public key: ${public_key}", ("public_key", owner_key_str) ); } - try { - active = public_key_type(active_key_str); - } catch (...) { + + if( !active_key_str.size() ) { + active = owner; + } else if( active_key_str.find('@') != string::npos ) { + try { + active = to_permission_level(active_key_str); + } EOS_RETHROW_EXCEPTIONS( explained_exception, "Invalid active permission level: ${permission}", ("permission", active_key_str) ) + } else { try { - active = get_permission_level(active_key_str); - } EOS_RETHROW_EXCEPTIONS(public_key_type_exception, "Invalid active public key: ${public_key}", ("public_key", active_key_str)); + active = public_key_type(active_key_str); + } EOS_RETHROW_EXCEPTIONS( public_key_type_exception, "Invalid active public key: ${public_key}", ("public_key", active_key_str) ); } auto create = create_newaccount(creator, account_name, owner, active); From f7d964472b9928d182380a938a41f853a6226816 Mon Sep 17 00:00:00 2001 From: Jeeyong Um Date: Sun, 13 Jan 2019 13:44:34 +0900 Subject: [PATCH 5/5] Use empty instead of negation of size when check active_key_str --- programs/cleos/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/programs/cleos/main.cpp b/programs/cleos/main.cpp index 04f12cafe22..9150a7fcafb 100644 --- a/programs/cleos/main.cpp +++ b/programs/cleos/main.cpp @@ -1027,7 +1027,7 @@ struct create_account_subcommand { } EOS_RETHROW_EXCEPTIONS( public_key_type_exception, "Invalid owner public key: ${public_key}", ("public_key", owner_key_str) ); } - if( !active_key_str.size() ) { + if( active_key_str.empty() ) { active = owner; } else if( active_key_str.find('@') != string::npos ) { try {