Skip to content

Commit

Permalink
Rename cleos command to remove_key. Add password to command
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulCalabrese committed Jun 14, 2018
1 parent 6e9a9d9 commit 88754e9
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 9 deletions.
7 changes: 6 additions & 1 deletion plugins/wallet_api_plugin/wallet_api_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ using namespace eosio;
api_handle.call_name(vs.at(0).as<in_param0>(), vs.at(1).as<in_param1>()); \
eosio::detail::wallet_api_plugin_empty result;

#define INVOKE_V_R_R_R(api_handle, call_name, in_param0, in_param1, in_param2) \
const auto& vs = fc::json::json::from_string(body).as<fc::variants>(); \
api_handle.call_name(vs.at(0).as<in_param0>(), vs.at(1).as<in_param1>(), vs.at(2).as<in_param2>()); \
eosio::detail::wallet_api_plugin_empty result;

#define INVOKE_V_V(api_handle, call_name) \
api_handle.call_name(); \
eosio::detail::wallet_api_plugin_empty result;
Expand Down Expand Up @@ -89,7 +94,7 @@ void wallet_api_plugin::plugin_startup() {
CALL(wallet, wallet_mgr, import_key,
INVOKE_V_R_R(wallet_mgr, import_key, std::string, std::string), 201),
CALL(wallet, wallet_mgr, remove_key,
INVOKE_V_R_R(wallet_mgr, remove_key, std::string, std::string), 201),
INVOKE_V_R_R_R(wallet_mgr, remove_key, std::string, std::string, std::string), 201),
CALL(wallet, wallet_mgr, create_key,
INVOKE_R_R_R(wallet_mgr, create_key, std::string, std::string), 201),
CALL(wallet, wallet_mgr, list_wallets,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,10 @@ class wallet_manager {
/// Removes a key from the specified wallet.
/// Wallet must be opened and unlocked.
/// @param name the name of the wallet to import into.
/// @param password the plaintext password returned from ::create.
/// @param key the Public Key to remove, e.g. EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV
/// @throws fc::exception if wallet not found or locked or key is not imported.
void remove_key(const std::string& name, const std::string& key);
/// @throws fc::exception if wallet not found or locked or key is not removed.
void remove_key(const std::string& name, const std::string& password, const std::string& key);

/// Creates a key within the specified wallet.
/// Wallet must be opened and unlocked
Expand Down
3 changes: 2 additions & 1 deletion plugins/wallet_plugin/wallet_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ void wallet_manager::import_key(const std::string& name, const std::string& wif_
w->import_key(wif_key);
}

void wallet_manager::remove_key(const std::string& name, const std::string& key) {
void wallet_manager::remove_key(const std::string& name, const std::string& password, const std::string& key) {
check_timeout();
if (wallets.count(name) == 0) {
EOS_THROW(chain::wallet_nonexistent_exception, "Wallet not found: ${w}", ("w", name));
Expand All @@ -193,6 +193,7 @@ void wallet_manager::remove_key(const std::string& name, const std::string& key)
if (w->is_locked()) {
EOS_THROW(chain::wallet_locked_exception, "Wallet is locked: ${w}", ("w", name));
}
w->check_password(password); //throws if bad password
w->remove_key(key);
}

Expand Down
13 changes: 10 additions & 3 deletions programs/cleos/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2218,17 +2218,24 @@ int main( int argc, char** argv ) {

// remove keys from wallet
string wallet_rm_key_str;
auto removeKeyWallet = wallet->add_subcommand("remove", localized("Remove key from wallet"), false);
auto removeKeyWallet = wallet->add_subcommand("remove_key", localized("Remove key from wallet"), false);
removeKeyWallet->add_option("-n,--name", wallet_name, localized("The name of the wallet to remove key from"));
removeKeyWallet->add_option("key", wallet_rm_key_str, localized("Public key in WIF format to remove"))->required();
removeKeyWallet->set_callback([&wallet_name, &wallet_rm_key_str] {
removeKeyWallet->add_option("--password", wallet_pw, localized("The password returned by wallet create"));
removeKeyWallet->set_callback([&wallet_name, &wallet_pw, &wallet_rm_key_str] {
if( wallet_pw.size() == 0 ) {
std::cout << localized("password: ");
fc::set_console_echo(false);
std::getline( std::cin, wallet_pw, '\n' );
fc::set_console_echo(true);
}
public_key_type pubkey;
try {
pubkey = public_key_type( wallet_rm_key_str );
} catch (...) {
EOS_THROW(public_key_type_exception, "Invalid public key: ${public_key}", ("public_key", wallet_rm_key_str))
}
fc::variants vs = {fc::variant(wallet_name), fc::variant(wallet_rm_key_str)};
fc::variants vs = {fc::variant(wallet_name), fc::variant(wallet_pw), fc::variant(wallet_rm_key_str)};
call(wallet_url, wallet_remove_key, vs);
std::cout << localized("removed private key for: ${pubkey}", ("pubkey", wallet_rm_key_str)) << std::endl;
});
Expand Down
5 changes: 3 additions & 2 deletions tests/wallet_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,17 @@ BOOST_AUTO_TEST_CASE(wallet_manager_test)
// key3 was not automatically imported
BOOST_CHECK(std::find(keys.cbegin(), keys.cend(), pub_pri_pair(key3)) == keys.cend());

wm.remove_key("test", key2);
wm.remove_key("test", pw, key2);
BOOST_CHECK_EQUAL(1, wm.get_public_keys().size());
keys = wm.list_keys("test", pw);
BOOST_CHECK(std::find(keys.cbegin(), keys.cend(), pub_pri_pair(key2)) == keys.cend());
wm.import_key("test", key2);
BOOST_CHECK_EQUAL(2, wm.get_public_keys().size());
keys = wm.list_keys("test", pw);
BOOST_CHECK(std::find(keys.cbegin(), keys.cend(), pub_pri_pair(key2)) != keys.cend());
wm.remove_key("test", key3);
wm.remove_key("test", pw, key3);
BOOST_CHECK_EQUAL(2, wm.get_public_keys().size());
BOOST_CHECK_THROW(wm.remove_key("test2", "PWnogood", key2), wallet_invalid_password_exception);

wm.lock("test");
BOOST_CHECK_THROW(wm.list_keys("test", pw), wallet_locked_exception);
Expand Down

0 comments on commit 88754e9

Please sign in to comment.