Skip to content

Commit

Permalink
#121 Merge branch 'create_file' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
roman-modelist-dev committed Feb 13, 2019
2 parents 605abe8 + e5cf582 commit a76cfbe
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
18 changes: 18 additions & 0 deletions keychain_lib/include/keychain_lib/keyfile_singleton.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ class keyfile_singleton
const keyfile_format::keyfile_t& operator[](size_t index); //NOTE: random_access_index for key table
void insert(keyfile_format::keyfile_t&& keyfile_data); //NOTE: keyfile_t (fc_light::variant) is not support move semantic

using create_f = std::function<keyfile_format::keyfile_t()>;
bool create(create_f&& creator);

using unlock_f = std::function<bool(const keyfile_format::keyfile_t&)>;
bool remove(const dev::Public& pkey, unlock_f&& unlocker);

template <typename modifier_f>
void update(const prim_key_type& key, modifier_f&& f)
{
Expand Down Expand Up @@ -181,4 +187,16 @@ class keyfile_singleton
void flush_all() const;
};

using get_password_f = std::function<byte_seq_t(const std::string&)>; //NOTE: may incapsulate call to sec module or just return password string

keyfile_format::keyfile_t create_new_keyfile(
const std::string& keyname,
const std::string& description,
bool encrypted,
keyfile_format::cipher_etype cipher,
keyfile_format::curve_etype curve,
get_password_f&& get_passwd);



}
98 changes: 98 additions & 0 deletions keychain_lib/src/keyfile_singleton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,47 @@ keyfile_singleton::~keyfile_singleton()
flush_all();
}

bool keyfile_singleton::create(create_f&& creator)
{
auto& log = logger_singleton::instance();
try {
insert(std::move(creator()));
return true;
}
catch (fc_light::exception& exc)
{
BOOST_LOG_SEV(log.lg, error) << "keyfile_singleton::create(), error = " << exc.to_detail_string();
}
catch (std::exception& exc)
{
BOOST_LOG_SEV(log.lg, error) << "keyfile_singleton::create(), error = " << exc.what();
}
return false;
}

bool keyfile_singleton::remove(const dev::Public& pkey, unlock_f&& unlock)
{
auto& log = logger_singleton::instance();
try {
auto it = m_keydata_map.find(pkey);
if (it == m_keydata_map.end())
return false;
if(!unlock(*it))
return false;
m_keydata_map.erase(it);
return true;
}
catch (fc_light::exception& exc)
{
BOOST_LOG_SEV(log.lg, error) << "keyfile_singleton::create(), error = " << exc.to_detail_string();
}
catch (std::exception& exc)
{
BOOST_LOG_SEV(log.lg, error) << "keyfile_singleton::create(), error = " << exc.what();
}
return false;
}

const keyfile_format::keyfile_t& keyfile_singleton::operator[](size_t index)
{
bool stop = false;
Expand Down Expand Up @@ -296,4 +337,61 @@ void keyfile_singleton::add_log_record(const dev::Public& pkey, const keyfile_fo
}
it->second.insert(record);
flush_logrecords(pkey);
}

keyfile_format::keyfile_t keychain_app::create_new_keyfile(
const std::string& keyname,
const std::string& description,
bool encrypted,
keyfile_format::cipher_etype cipher,
keyfile_format::curve_etype curve,
get_password_f&& get_passwd)
{
keyfile_format::keyfile_t keyfile;
dev::Secret priv_key;
dev::Public pb_hex;
dev::h256 hash;
std::string filename;
switch (curve)
{
case keyfile_format::curve_etype::secp256k1:
{
auto keys = dev::KeyPair::create();
pb_hex = keys.pub();
hash = dev::ethash::sha3_ethash(keys.pub());
priv_key = keys.secret();
filename = hash.hex().substr(0,16);
filename += ".json";
}
break;
default:
{
FC_LIGHT_THROW_EXCEPTION(fc_light::invalid_arg_exception,
"Unsupported curve format, curve = ${type}", ("type", curve));
}
}

if (encrypted)
{
auto passwd = get_passwd(keyname);//operation canceled exception need to be thrown into get_password functor

if (passwd.empty())
FC_LIGHT_THROW_EXCEPTION(fc_light::password_input_exception, "");
auto& encryptor = encryptor_singleton::instance();
auto enc_data = encryptor.encrypt_private_key(cipher, passwd, priv_key);
keyfile.keyinfo.priv_key_data = fc_light::variant(enc_data);
keyfile.keyinfo.encrypted = true;
}
else{
keyfile.keyinfo.priv_key_data = fc_light::variant(priv_key);
keyfile.keyinfo.encrypted = false;
}

keyfile.keyinfo.public_key = pb_hex;
keyfile.keyname = keyname;
keyfile.description = description;
keyfile.creation_time = fc_light::time_point::now();
keyfile.keychain_version = version_info::short_version();
keyfile.filetype = keyfile_format::TYPE_KEY;
keyfile.keyinfo.curve_type = curve;
}

0 comments on commit a76cfbe

Please sign in to comment.