Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Add support for account creation with permission level to cleos #6583

Merged
merged 5 commits into from
Jan 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions libraries/chain/include/eosio/chain/authority.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<key_weight> k, vector<permission_level_weight> p = {}, vector<wait_weight> w = {} )
:threshold(t),keys(move(k)),accounts(move(p)),waits(move(w)){}
authority(){}
Expand Down
52 changes: 37 additions & 15 deletions programs/cleos/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ Usage: ./cleos create account [OPTIONS] creator name OwnerKey ActiveKey
#include <fc/io/console.hpp>
#include <fc/exception/exception.hpp>
#include <fc/variant_object.hpp>
#include <fc/static_variant.hpp>

#include <eosio/chain/name.hpp>
#include <eosio/chain/config.hpp>
Expand Down Expand Up @@ -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<public_key_type, permission_level>;

FC_DECLARE_EXCEPTION( explained_exception, 9000000, "explained exception, see error log" );
FC_DECLARE_EXCEPTION( localized_exception, 10000000, "an error occured" );
Expand Down Expand Up @@ -512,14 +514,19 @@ 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 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) {
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.contains<public_key_type>() ? authority(owner.get<public_key_type>()) : authority(owner.get<permission_level>()),
.active = active.contains<public_key_type>() ? authority(active.get<public_key_type>()) : authority(active.get<permission_level>())
}
};
}
Expand Down Expand Up @@ -987,8 +994,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,
Expand All @@ -1008,16 +1015,31 @@ 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;
public_key_type owner_key, active_key;
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));
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);
auth_type owner, active;

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 = public_key_type(owner_key_str);
} EOS_RETHROW_EXCEPTIONS( public_key_type_exception, "Invalid owner public key: ${public_key}", ("public_key", owner_key_str) );
}

if( active_key_str.empty() ) {
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 = 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);
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" );
Expand Down