Skip to content

Commit add9824

Browse files
remove keydata_singleton
1 parent b8e59bd commit add9824

File tree

5 files changed

+25
-81
lines changed

5 files changed

+25
-81
lines changed

keychain_cmd_app/cmd_parser.cpp

+3-30
Original file line numberDiff line numberDiff line change
@@ -115,39 +115,12 @@ int cmd_parser::run(int argc, const char* const argv[])
115115
sec_mod->connect(keychain_ref);
116116
gui_mod->connect(keychain_ref);
117117

118+
118119
auto& keyfiles = keyfile_singleton::instance();
119120
auto it = keyfiles.begin();
120121
if ( it==keyfiles.end() )
121-
{
122-
auto res = keychain_ref.entropy();
123-
auto& key_data = keydata_singleton::instance();
124-
dev::bytes ue;
125-
126-
std::string pass("blank");
127-
// std::string level("{\"root\": \"m\",\"purpose\": 0, \"coin_type\": 0, \"account\": 0, \"change\": 0, \"address_index\": 0}");
128-
keydata::path_levels_t path;
129-
path.root="m";
130-
path.purpose=0;
131-
path.coin_type=0;
132-
path.account=0;
133-
path.change=0;
134-
path.address_index=0;
135-
136-
keydata::create_t cmd;
137-
cmd.keyname = "superkey";
138-
cmd.description = "";
139-
cmd.encrypted = false;
140-
cmd.cipher = keyfile_format::cipher_etype::aes256;
141-
cmd.curve = keyfile_format::curve_etype::secp256k1;
142-
cmd.password = pass;
143-
cmd.path = fc_light::variant(path);
144-
// auto variant = fc_light::variant(cmd);
145-
auto json = fc_light::json::to_string(fc_light::variant(cmd));
146-
147-
auto mnemonics = std::move(key_data.seed(ue));
148-
key_data.create_masterkey(mnemonics,pass);
149-
key_data.derive_key(pass, json);
150-
}
122+
auto res = keychain_ref.entropy();
123+
151124

152125
keychain_invoke_f f = std::bind(&keychain_base::operator(), &keychain_ref, std::placeholders::_1);
153126
pipeline_parser pipe_line_parser_(std::move(f), fileno(stdin), fileno(stdout));

keychain_lib/include/keychain_lib/keychain_commands.hpp

+2-14
Original file line numberDiff line numberDiff line change
@@ -834,15 +834,7 @@ struct keychain_command<command_te::seed>: keychain_command_base
834834
using params_t = params;
835835
virtual std::string operator()(keychain_base* keychain, const fc_light::variant& params_variant, int id) const override
836836
{
837-
auto param = params_variant.as<params_t>();
838-
std::vector<unsigned char> entropy;
839-
entropy.resize(param.entropy.length());
840-
auto res = from_hex(param.entropy, entropy.data(), entropy.size());
841-
entropy.resize(res);
842-
//TODO: need to impl
843-
std::string seed_phrase = "witch collapse practice feed shame open despair creek road again ice least";
844-
json_response response(seed_phrase, id);
845-
return fc_light::json::to_string(fc_light::variant(response));
837+
FC_LIGHT_THROW_EXCEPTION(fc_light::command_depreciated, "");
846838
}
847839
};
848840

@@ -856,11 +848,7 @@ struct keychain_command<command_te::restore>: keychain_command_base
856848
using params_t = params;
857849
virtual std::string operator()(keychain_base* keychain, const fc_light::variant& params_variant, int id) const override
858850
{
859-
auto param = params_variant.as<params_t>();
860-
//TODO: need to impl
861-
std::string private_key = "5fda7b741910b05738c5e0ca8961cf7a9c2f3afe8dfcae8d57df5f01690f2a02";
862-
json_response response(private_key, id);
863-
return fc_light::json::to_string(fc_light::variant(response));
851+
FC_LIGHT_THROW_EXCEPTION(fc_light::command_depreciated, "");
864852
}
865853
};
866854

keychain_lib/include/keychain_lib/keydata_singleton.hpp

+9-19
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,20 @@
1616
#include <boost/hana/size.hpp>
1717
#include "keyfile_singleton.hpp"
1818

19-
2019
namespace keychain_app {
2120

22-
struct keydata_singleton
23-
{
24-
static keydata_singleton& instance();
25-
std::string seed(dev::bytes& );
26-
void create_masterkey(std::string&, std::string&);
27-
void derive_key(std::string&, std::string& );
28-
void restore(const char *, std::string&, std::string& );
29-
void backup(const char * );
30-
31-
private:
32-
std::pair<dev::Secret, dev::bytes> get_master_key(get_password_create_f&& );
33-
keydata_singleton(){}
34-
~keydata_singleton(){}
35-
std::vector<char> pbkdf2(std::string const& _pass);
36-
};
37-
3821
namespace keydata
3922
{
4023

24+
std::string seed(dev::bytes& );
25+
void derive_masterkey(std::string&, std::string&);
26+
void derive_key(std::string&, std::string& );
27+
void restore(const char *, std::string&, std::string& );
28+
void backup(const char * );
29+
std::pair<dev::Secret, dev::bytes> get_master_key(get_password_create_f&& );
30+
std::vector<char> pbkdf2(std::string const& _pass);
31+
32+
4133
struct path_levels_t
4234
{
4335
path_levels_t(){}
@@ -50,7 +42,6 @@ struct path_levels_t
5042
int address_index;
5143
};
5244

53-
5445
struct backup_t
5546
{
5647
backup_t(){}
@@ -108,5 +99,4 @@ FC_LIGHT_REFLECT(keychain_app::keydata::path_levels_t, (root)(purpose)(coin_type
10899
FC_LIGHT_REFLECT(keychain_app::keydata::create_t, (keyname)(description)(encrypted)(cipher)(curve)(password)(path))
109100
FC_LIGHT_REFLECT(keychain_app::keydata::backup_t, (keyname)(path))
110101

111-
112102
#endif //KEYCHAINAPP_KEYDATA_SINGLETON_HPP

keychain_lib/src/keydata_singleton.cpp

+10-17
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,7 @@
1616

1717
using namespace keychain_app;
1818

19-
keydata_singleton& keydata_singleton::instance()
20-
{
21-
static keydata_singleton instance;
22-
return instance;
23-
}
24-
25-
std::string keydata_singleton::seed(dev::bytes& user_entropy)
19+
std::string keydata::seed(dev::bytes& user_entropy)
2620
{
2721
CryptoPP::SecByteBlock ent(16);
2822
CryptoPP::OS_GenerateRandomBlock(false, ent, ent.size());
@@ -68,7 +62,7 @@ std::string keydata_singleton::seed(dev::bytes& user_entropy)
6862
}
6963

7064

71-
std::vector<char> keydata_singleton::pbkdf2(std::string const& _pass)
65+
std::vector<char> keydata::pbkdf2(std::string const& _pass)
7266
{
7367
unsigned _iterations = 2048;
7468
unsigned _dkLen = 64;
@@ -90,7 +84,7 @@ std::vector<char> keydata_singleton::pbkdf2(std::string const& _pass)
9084
}
9185

9286

93-
void keydata_singleton::create_masterkey(std::string& mnemonics, std::string& pass)
87+
void keydata::derive_masterkey(std::string& mnemonics, std::string& pass)
9488
{
9589
std::regex re(" +");
9690
std::string mnemonics_ = std::regex_replace(mnemonics, re, "");
@@ -115,7 +109,7 @@ void keydata_singleton::create_masterkey(std::string& mnemonics, std::string& pa
115109
BOOST_LOG_SEV(log.lg, info) << "create master key";
116110
}
117111

118-
void keydata_singleton::derive_key(std::string& masterkey_pass, std::string& json)
112+
void keydata::derive_key(std::string& masterkey_pass, std::string& json)
119113
{
120114
using namespace keydata;
121115
auto& log = logger_singleton::instance();
@@ -176,7 +170,7 @@ void keydata_singleton::derive_key(std::string& masterkey_pass, std::string& jso
176170
}
177171

178172

179-
std::pair<dev::Secret, dev::bytes> keydata_singleton::get_master_key( get_password_create_f&& get_passwd)
173+
std::pair<dev::Secret, dev::bytes> keydata::get_master_key( get_password_create_f&& get_passwd)
180174
{
181175
dev::Secret priv_key;
182176
dev::bytes chain_code;
@@ -214,7 +208,8 @@ std::pair<dev::Secret, dev::bytes> keydata_singleton::get_master_key( get_passwo
214208
return std::make_pair(priv_key, chain_code);
215209
}
216210

217-
void keydata_singleton::restore(const char * filename, std::string& mnemonics, std::string& masterkey_pass)
211+
212+
void keydata::restore(const char * filename, std::string& mnemonics, std::string& masterkey_pass)
218213
{
219214
using namespace keydata;
220215

@@ -237,7 +232,7 @@ void keydata_singleton::restore(const char * filename, std::string& mnemonics, s
237232
BOOST_LOG_SEV(log.lg, info) << "restore path: " << json.back();
238233
}
239234

240-
create_masterkey(mnemonics, masterkey_pass);
235+
derive_masterkey(mnemonics, masterkey_pass);
241236

242237
for (auto &a: json)
243238
{
@@ -263,7 +258,7 @@ void keydata_singleton::restore(const char * filename, std::string& mnemonics, s
263258
}
264259

265260

266-
void keydata_singleton::backup(const char * filename)
261+
void keydata::backup(const char * filename)
267262
{
268263
auto file = std::ofstream(filename);
269264
if (!file.is_open())
@@ -275,7 +270,5 @@ void keydata_singleton::backup(const char * filename)
275270
auto& sql = sql_singleton::instance();
276271
auto backup_list = std::move(sql.select_path());
277272
for (auto& a : backup_list)
278-
file << fc_light::json::to_pretty_string(a) << std::endl;
279-
280-
273+
file << fc_light::json::to_string(a) << std::endl;
281274
}

keychain_lib/src/sql_singleton.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ const std::vector<keydata::backup_t> sql_singleton::select_path()
115115
{
116116
sqlite3_stmt * stmt;
117117
std::vector<keydata::backup_t> records;
118-
const char * statement = "select keyname, root, purpose, coin_type, account, change, address_index";
118+
const char * statement = "select keyname, root, purpose, coin_type, account, change, address_index from keypath";
119119

120120
if ( sqlite3_prepare_v2(db, statement, -1, &stmt, NULL) != SQLITE_OK )
121121
FC_LIGHT_THROW_EXCEPTION(fc_light::internal_error_exception, "sqlite3_prepare_v2");

0 commit comments

Comments
 (0)