Skip to content

Commit

Permalink
#117 Add parameter to secmod protocol that allow to show confirm dial…
Browse files Browse the repository at this point in the history
…og even if key is not encrypted
  • Loading branch information
roman-modelist-dev committed Feb 6, 2019
1 parent c42ec40 commit 122b401
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 39 deletions.
20 changes: 10 additions & 10 deletions keychain_lib/include/keychain_lib/keychain_commands.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ enum struct sign_te {
RSV_noncanonical
};

fc_light::variant create_secmod_signhex_cmd(const std::vector<unsigned char>& raw, blockchain_te blockchain, std::string from, int unlock_time, const std::string& keyname);
fc_light::variant create_secmod_signhash_cmd(const std::string& raw, std::string from, const std::string& keyname);
fc_light::variant create_secmod_unlock_cmd(const std::string& keyname, int unlock_time);
fc_light::variant create_secmod_signhex_cmd(const std::vector<unsigned char>& raw, blockchain_te blockchain, std::string from, int unlock_time, const std::string& keyname, bool no_password);
fc_light::variant create_secmod_signhash_cmd(const std::string& raw, std::string from, const std::string& keyname, bool no_password);
fc_light::variant create_secmod_unlock_cmd(const std::string& keyname, int unlock_time, bool no_password);

class streambuf_derived : public std::basic_streambuf<char>
{
Expand Down Expand Up @@ -161,7 +161,7 @@ class keychain_base
{
public:
using string_list = std::list<std::wstring>;
using create_secmod_cmd_f = std::function<std::string(const std::string& keyname)>;
using create_secmod_cmd_f = std::function<std::string(const std::string& keyname, bool no_password)>;
virtual std::string operator()(const fc_light::variant& command) = 0;
boost::signals2::signal<std::string(const std::string&)> run_secmod_cmd;
boost::signals2::signal<dev::Public(void)> select_key;
Expand Down Expand Up @@ -404,10 +404,10 @@ struct keychain_command<command_te::sign_hex> : keychain_command_base
}
};

private_key = keychain->get_private_key(params.public_key, params.unlock_time, [&evaluate_from, &raw, &params](const std::string& keyname)
private_key = keychain->get_private_key(params.public_key, params.unlock_time, [&evaluate_from, &raw, &params](const std::string& keyname, bool no_password)
{
return fc_light::json::to_string(
create_secmod_signhex_cmd(raw, params.blockchain_type, evaluate_from(), params.unlock_time, keyname));
create_secmod_signhex_cmd(raw, params.blockchain_type, evaluate_from(), params.unlock_time, keyname, no_password));
});

switch (params.blockchain_type)
Expand Down Expand Up @@ -506,10 +506,10 @@ struct keychain_command<command_te::sign_hash> : keychain_command_base
};

//TODO: it is more preferable to use move semantic instead copy for json argument
auto private_key = keychain->get_private_key(params.public_key, 0, [&evaluate_from, &params](const std::string& keyname)
auto private_key = keychain->get_private_key(params.public_key, 0, [&evaluate_from, &params](const std::string& keyname, bool no_password)
{
return fc_light::json::to_string(
create_secmod_signhash_cmd(params.hash, evaluate_from(), keyname));
create_secmod_signhash_cmd(params.hash, evaluate_from(), keyname, no_password));
});

//NOTE: using vector instead array because move semantic is implemented in the vector
Expand Down Expand Up @@ -702,10 +702,10 @@ struct keychain_command<command_te::unlock>: keychain_command_base
if (!params.public_key)
FC_LIGHT_THROW_EXCEPTION(fc_light::invalid_arg_exception, "public_key is not specified");

auto private_key = keychain->get_private_key(params.public_key, params.unlock_time, [&params](const std::string& keyname)
auto private_key = keychain->get_private_key(params.public_key, params.unlock_time, [&params](const std::string& keyname, bool no_password)
{
return fc_light::json::to_string(
create_secmod_unlock_cmd(keyname, params.unlock_time));
create_secmod_unlock_cmd(keyname, params.unlock_time, no_password));
});

json_response response(true, id);
Expand Down
19 changes: 11 additions & 8 deletions keychain_lib/include/keychain_lib/secmod_protocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,11 @@ template<>
struct secmod_event<events_te::sign_hex>
{
struct params {
params() : is_parsed(false), unlock_time(0) {}

std::string keyname;
params() : is_parsed(false), no_password(false), unlock_time(0) {}

bool is_parsed;
bool no_password;
std::string keyname;
blockchain_secmod_te blockchain;
int unlock_time;
fc_light::variant trx_view;
Expand All @@ -153,6 +154,7 @@ template<>
struct secmod_event<events_te::sign_hash>
{
struct params {
bool no_password = false;
std::string keyname;
std::string from;
std::string hash;
Expand All @@ -165,7 +167,8 @@ struct secmod_event<events_te::unlock>
{
struct params
{
params(): unlock_time(0){}
params(): no_password(false), unlock_time(0){}
bool no_password;
std::string keyname;
int unlock_time;
};
Expand Down Expand Up @@ -260,13 +263,13 @@ struct secmod_resonse_common
FC_LIGHT_REFLECT_ENUM(keychain_app::secmod_commands::blockchain_secmod_te, (unknown)(ethereum)(bitcoin)(ethereum_swap))
FC_LIGHT_REFLECT_ENUM(keychain_app::secmod_commands::events_te,
(unknown)(create_key)(sign_hex)(sign_hash)(unlock)(edit_key)(remove_key)(export_keys)(import_keys)(print_mnemonic))
FC_LIGHT_REFLECT_ENUM(keychain_app::secmod_commands::response_te, (null)(password)(boolean))
FC_LIGHT_REFLECT_ENUM(keychain_app::secmod_commands::response_te, (null)(password)(boolean)(canceled))


FC_LIGHT_REFLECT(keychain_app::secmod_commands::secmod_event<keychain_app::secmod_commands::events_te::create_key>::params_t, (keyname))
FC_LIGHT_REFLECT(keychain_app::secmod_commands::secmod_event<keychain_app::secmod_commands::events_te::sign_hex>::params_t, (keyname)(is_parsed)(blockchain)(unlock_time)(trx_view))
FC_LIGHT_REFLECT(keychain_app::secmod_commands::secmod_event<keychain_app::secmod_commands::events_te::sign_hash>::params_t, (keyname)(from)(hash))
FC_LIGHT_REFLECT(keychain_app::secmod_commands::secmod_event<keychain_app::secmod_commands::events_te::unlock>::params_t, (keyname)(unlock_time))
FC_LIGHT_REFLECT(keychain_app::secmod_commands::secmod_event<keychain_app::secmod_commands::events_te::sign_hex>::params_t, (is_parsed)(no_password)(keyname)(blockchain)(unlock_time)(trx_view))
FC_LIGHT_REFLECT(keychain_app::secmod_commands::secmod_event<keychain_app::secmod_commands::events_te::sign_hash>::params_t, (no_password)(keyname)(from)(hash))
FC_LIGHT_REFLECT(keychain_app::secmod_commands::secmod_event<keychain_app::secmod_commands::events_te::unlock>::params_t, (no_password)(keyname)(unlock_time))
FC_LIGHT_REFLECT(keychain_app::secmod_commands::secmod_event<keychain_app::secmod_commands::events_te::edit_key>::params_t, (keyname)(unlock_time))
FC_LIGHT_REFLECT(keychain_app::secmod_commands::secmod_event<keychain_app::secmod_commands::events_te::remove_key>::params_t, (keyname))

Expand Down
54 changes: 33 additions & 21 deletions keychain_lib/src/keychain_commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ bool swap_action(std::string data, swap_trx_t::swap_t &swap_info) {
return true;
}

fc_light::variant create_secmod_signhex_cmd(const std::vector<unsigned char>& raw, blockchain_te blockchain, std::string from, int unlock_time, const std::string& keyname)
fc_light::variant create_secmod_signhex_cmd(const std::vector<unsigned char>& raw, blockchain_te blockchain, std::string from, int unlock_time, const std::string& keyname, bool no_password)
{
std::string json;
auto& log = logger_singleton::instance();
Expand All @@ -62,6 +62,7 @@ fc_light::variant create_secmod_signhex_cmd(const std::vector<unsigned char>& ra
params_t params;
params.is_parsed = true;
params.keyname = keyname;
params.no_password = no_password;

switch (blockchain)
{
Expand Down Expand Up @@ -142,14 +143,15 @@ fc_light::variant create_secmod_signhex_cmd(const std::vector<unsigned char>& ra
return fc_light::variant(cmd);
}

fc_light::variant create_secmod_signhash_cmd(const std::string& raw, std::string from, const std::string& keyname)
fc_light::variant create_secmod_signhash_cmd(const std::string& raw, std::string from, const std::string& keyname, bool no_password)
{
secmod_commands::secmod_command cmd;
using params_t = secmod_commands::secmod_event<secmod_commands::events_te::sign_hash>::params_t;
params_t params;
params.hash = raw;
params.from = std::move(from);
params.keyname = std::move(keyname);
params.no_password = no_password;

cmd.etype = secmod_commands::events_te::sign_hash;
cmd.params = params;
Expand All @@ -158,13 +160,14 @@ fc_light::variant create_secmod_signhash_cmd(const std::string& raw, std::string
}


fc_light::variant create_secmod_unlock_cmd(const std::string& keyname, int unlock_time)
fc_light::variant create_secmod_unlock_cmd(const std::string& keyname, int unlock_time, bool no_password)
{
secmod_commands::secmod_command cmd;
using params_t = secmod_commands::secmod_event<secmod_commands::events_te::unlock>::params_t;
params_t params;
params.keyname = keyname;
params.unlock_time = unlock_time;
params.no_password = no_password;
cmd.etype = secmod_commands::events_te::unlock;
cmd.params = params;

Expand Down Expand Up @@ -199,32 +202,41 @@ dev::Secret keychain_base::get_private_key(const dev::Public& public_key, int un

auto& keyfiles = keyfile_singleton::instance();
auto& keyfile = keyfiles[public_key];
if(keyfile.keyinfo.encrypted)

auto result = std::move(*(run_secmod_cmd(create_cmd_func(keyfile.keyname, keyfile.keyinfo.encrypted))));
secmod_commands::secmod_result_parser_f parser;
byte_seq_t password;
switch (parser(result))
{
auto result = std::move(*(run_secmod_cmd(create_cmd_func(keyfile.keyname))));
secmod_commands::secmod_result_parser_f parser;
byte_seq_t password;
switch (parser(result))
{
case secmod_commands::response_te::password:
{
password = std::move(parser.params<secmod_commands::response_te::password>());
if (password.empty())
FC_LIGHT_THROW_EXCEPTION(fc_light::password_input_exception, "");
auto encrypted_data = keyfile.keyinfo.priv_key_data.as<keyfile_format::encrypted_data>();
auto& encryptor = encryptor_singleton::instance();
result_secret = encryptor.decrypt_private_key(password, encrypted_data);
if(unlock_time > 0)
key_map.insert(private_key_item(result_secret, unlock_time));

}
break;
case secmod_commands::response_te::boolean:
{
auto confirm = std::move(parser.params<secmod_commands::response_te::boolean>());
if (confirm)
{
result_secret = keyfile.keyinfo.priv_key_data.as<dev::Secret>();
if(unlock_time > 0)
key_map.insert(private_key_item(result_secret, unlock_time));
}
FC_LIGHT_THROW_EXCEPTION(fc_light::operation_canceled, "");
}
break;
case secmod_commands::response_te::canceled:
FC_LIGHT_THROW_EXCEPTION(fc_light::operation_canceled, "");
default:
break;
}
if (password.empty())
FC_LIGHT_THROW_EXCEPTION(fc_light::password_input_exception, "");
auto encrypted_data = keyfile.keyinfo.priv_key_data.as<keyfile_format::encrypted_data>();
auto& encryptor = encryptor_singleton::instance();
result_secret = encryptor.decrypt_private_key(password, encrypted_data);
if(unlock_time > 0)
key_map.insert(private_key_item(result_secret, unlock_time));
} else {
result_secret = keyfile.keyinfo.priv_key_data.as<dev::Secret>();
if(unlock_time > 0)
key_map.insert(private_key_item(result_secret, unlock_time));
}
return result_secret;
}
Expand Down

0 comments on commit 122b401

Please sign in to comment.