Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5022619

Browse files
#93 - add last usage time to key meta data
1 parent 88396df commit 5022619

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

keychain_lib/include/keychain_lib/key_file_parser.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include <fc_light/reflect/reflect.hpp>
99
#include <fc_light/variant.hpp>
1010

11+
#include <fc_light/time.hpp>
12+
1113
namespace keychain_app {
1214

1315
namespace keyfile_format
@@ -52,6 +54,8 @@ struct keyfile_t
5254
std::string keyname;
5355
std::string description;
5456
std::string keychain_version;
57+
fc_light::time_point creation_time;
58+
fc_light::time_point usage_time;
5559
struct keyinfo_t
5660
{
5761
bool encrypted;
@@ -69,7 +73,7 @@ FC_LIGHT_REFLECT_ENUM(keychain_app::keyfile_format::file_type, (TYPE_UNKNOWN)(TY
6973
FC_LIGHT_REFLECT_ENUM(keychain_app::keyfile_format::curve_etype, (unknown)(secp256k1))
7074
FC_LIGHT_REFLECT(keychain_app::keyfile_format::encrypted_data, (cipher_type)(iv)(enc_data))
7175
FC_LIGHT_REFLECT(keychain_app::keyfile_format::keyfile_t::keyinfo_t, (encrypted)(curve_type)(priv_key_data)(public_key))
72-
FC_LIGHT_REFLECT(keychain_app::keyfile_format::keyfile_t, (filetype)(keyname)(description)(keychain_version)(keyinfo))
76+
FC_LIGHT_REFLECT(keychain_app::keyfile_format::keyfile_t, (filetype)(keyname)(description)(keychain_version)(creation_time)(usage_time)(keyinfo))
7377

7478

7579
#endif //KEYCHAINAPP_KEY_FILE_PARSER_HPP

keychain_lib/include/keychain_lib/keychain_commands.hpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,9 @@ struct keychain_command<command_te::sign_hex> : keychain_command_base
469469
"Unknown blockchain_type, blockchain = ${type}", ("type", params.blockchain_type));
470470
}
471471

472+
auto& keyfile = keyfiles[params.keyname];
473+
keyfile.usage_time = fc_light::time_point::now();
474+
keyfiles.update(std::move(keyfile));
472475
json_response response(to_hex(signature.data(), signature.size()).c_str(), id);
473476
fc_light::variant res(response);
474477
return fc_light::json::to_string(res);
@@ -539,7 +542,10 @@ struct keychain_command<command_te::sign_hash> : keychain_command_base
539542
break;
540543
}
541544
}
542-
545+
546+
auto& keyfile = keyfiles[params.keyname];
547+
keyfile.usage_time = fc_light::time_point::now();
548+
keyfiles.update(std::move(keyfile));
543549
json_response response(to_hex(signature.data(), signature.size()).c_str(), id);
544550
fc_light::variant res(response);
545551
return fc_light::json::to_string(res);
@@ -616,6 +622,7 @@ struct keychain_command<command_te::create>: keychain_command_base
616622
keyfile.keyinfo.public_key = pb_hex;
617623
keyfile.keyname = keyname;
618624
keyfile.description = params.description;
625+
keyfile.creation_time = fc_light::time_point::now();
619626
keyfile.keychain_version = version_info::short_version();
620627
keyfile.filetype = keyfile_format::TYPE_KEY;
621628
keyfile.keyinfo.curve_type = params.curve;
@@ -624,7 +631,6 @@ struct keychain_command<command_te::create>: keychain_command_base
624631
FC_LIGHT_THROW_EXCEPTION(fc_light::internal_error_exception, "Keyname (filename) is empty");
625632

626633
keyfiles.insert(std::move(keyfile));
627-
keyfiles.flush_keyfile(params.keyname);
628634
json_response response(keyname, id);
629635
return fc_light::json::to_string(fc_light::variant(response));
630636
}

keychain_lib/include/keychain_lib/keyfile_singleton.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class keyfile_singleton
2424
static keyfile_singleton& instance();
2525
keyfile_format::keyfile_t& operator[](const std::string& key);
2626
void insert(keyfile_format::keyfile_t&& keyfile_data); //NOTE: keyfile_t (fc_light::variant) is not support move semantic
27+
void update(keyfile_format::keyfile_t&& keyfile_data);
2728
bool is_exist(const std::string& key) const;
2829
void flush_keyfile(const std::string& key) const;
2930
void flush_all() const;

keychain_lib/src/keyfile_singleton.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ void keyfile_singleton::insert(keyfile_format::keyfile_t&& keyfile_data)
4545
flush_keyfile(keyfile_data.keyname);
4646
}
4747

48+
void keyfile_singleton::update(keyfile_format::keyfile_t&& keyfile_data)
49+
{
50+
auto it = m_keydata_map.find(keyfile_data.keyname);
51+
if(it == m_keydata_map.end())
52+
FC_LIGHT_THROW_EXCEPTION(fc_light::key_not_found_exception, "Keyfile with name \"${key_}\" not found", ("key_", keyfile_data.keyname));
53+
it->second = keyfile_data;
54+
flush_keyfile(keyfile_data.keyname);
55+
}
56+
4857
bool keyfile_singleton::is_exist(const std::string& key) const
4958
{
5059
auto it = m_keydata_map.find(key);

0 commit comments

Comments
 (0)