@@ -84,17 +84,24 @@ std::vector<char> keydata::pbkdf2(std::string const& _pass)
8484}
8585
8686
87- void keydata::derive_masterkey (std::string& mnemonics, std::string& pass)
87+ bool keydata::derive_masterkey (std::string& mnemonics, std::string& pass)
8888{
89+ auto & log = logger_singleton::instance ();
90+ auto & keyfiles = keyfile_singleton::instance ();
91+ if (keyfiles.is_exist (" master_key" ))
92+ {
93+ BOOST_LOG_SEV (log.lg , info) << " error create master_key: master_key already exist" ;
94+ return false ;
95+ }
96+
8997 std::regex re (" +" );
9098 std::string mnemonics_ = std::regex_replace (mnemonics, re, " " );
9199
92100 std::vector<char > key = std::move (pbkdf2 (mnemonics_));
93101 dev::bytes priv_key (key.begin (), key.begin ()+32 );
94102 dev::bytes chain_code (key.begin ()+32 , key.end ());
95103
96- auto & keyfiles = keyfile_singleton::instance ();
97- keyfiles.create (std::bind (create_new_keyfile,
104+ auto res = keyfiles.create (std::bind (create_new_keyfile,
98105 " master_key" , " master_key" , true , keyfile_format::cipher_etype::aes256,
99106 keyfile_format::curve_etype::secp256k1,
100107 [&pass](const std::string& keyname)->byte_seq_t {
@@ -105,14 +112,32 @@ void keydata::derive_masterkey(std::string& mnemonics, std::string& pass)
105112 priv_key,
106113 chain_code
107114 ));
108- auto & log = logger_singleton::instance ();
109- BOOST_LOG_SEV (log.lg , info) << " create master key" ;
115+ if (res)
116+ BOOST_LOG_SEV (log.lg , info) << " create master key" ;
117+ else
118+ BOOST_LOG_SEV (log.lg , info) << " error to create master key" ;
119+
120+ return res;
110121}
111122
112- void keydata::derive_key (std::string& masterkey_pass, std::string& json)
123+ bool keydata::derive_key (std::string& masterkey_pass, std::string& json)
113124{
114125 using namespace keydata ;
115126 auto & log = logger_singleton::instance ();
127+ bool res = false ;
128+
129+ auto & keyfiles = keyfile_singleton::instance ();
130+ auto count = keyfiles.count (" master_key" );
131+ if (count >1 )
132+ {
133+ {BOOST_LOG_SEV (log.lg , info) << " error derive keys: master key is not the only one" ;}
134+ return false ;
135+ }
136+ else if (count <1 )
137+ {
138+ {BOOST_LOG_SEV (log.lg , info) << " error derive keys: master key not found" ;}
139+ return false ;
140+ }
116141
117142 auto password = [&masterkey_pass](const std::string& keyname)->byte_seq_t {
118143 byte_seq_t res;
@@ -130,10 +155,14 @@ void keydata::derive_key(std::string& masterkey_pass, std::string& json)
130155 catch (const std::exception &e) {throw std::runtime_error (e.what ());}
131156 catch (const fc_light::exception &e) {throw std::runtime_error (e.what ());}
132157
158+ if (params_.keyname == " master_key" )
159+ {
160+ BOOST_LOG_SEV (log.lg , info) << " keyname \" master_key\" is not may be used for private key" ;
161+ return false ;
162+ }
133163
134164 FC_LIGHT_ASSERT (path.root == " m" );
135165
136- auto & keyfiles = keyfile_singleton::instance ();
137166 auto secret = get_master_key (password);
138167 dev::bytes priv_key (secret.first .data (), secret.first .data ()+32 );
139168
@@ -153,7 +182,7 @@ void keydata::derive_key(std::string& masterkey_pass, std::string& json)
153182 }
154183 hd = hd.getChild (0x80000000 |value);
155184 if (level == levels_te::address_index) {
156- keyfiles.create (std::bind (create_new_keyfile,
185+ res = keyfiles.create (std::bind (create_new_keyfile,
157186 params_.keyname , params_.description , params_.encrypted ,
158187 params_.cipher , params_.curve ,
159188 password,
@@ -164,9 +193,13 @@ void keydata::derive_key(std::string& masterkey_pass, std::string& json)
164193 backup_t backup (params_.keyname , params_.path );
165194 sql.insert_path (backup);
166195 auto json = fc_light::json::to_string (backup);
167- BOOST_LOG_SEV (log.lg , info) << " derive key: " << json;
196+ if (res)
197+ {BOOST_LOG_SEV (log.lg , info) << " derive key: " << json;}
198+ else
199+ {BOOST_LOG_SEV (log.lg , info) << " error to derive key: " << json;}
168200 }
169201 });
202+ return res;
170203}
171204
172205
@@ -209,16 +242,24 @@ std::pair<dev::Secret, dev::bytes> keydata::get_master_key( get_password_create_
209242}
210243
211244
212- void keydata::restore (const char * filename, std::string& mnemonics, std::string& masterkey_pass)
245+ int keydata::restore (const char * filename, std::string& mnemonics, std::string& masterkey_pass)
213246{
214247 using namespace keydata ;
215248
249+ auto & log = logger_singleton::instance ();
250+ auto & keyfiles = keyfile_singleton::instance ();
251+ if (keyfiles.is_exist (" master_key" ))
252+ {
253+ BOOST_LOG_SEV (log.lg , info) << " error restore: master_key already exist" ;
254+ return false ;
255+ }
256+
257+ int count = 0 ;
216258 auto file = std::ifstream (filename);
217259 if (!file.is_open ())
218260 FC_LIGHT_THROW_EXCEPTION (fc_light::internal_error_exception,
219261 " Cannot open restore file ${filename}" , (" filename" , filename));
220262
221- auto & log = logger_singleton::instance ();
222263 BOOST_LOG_SEV (log.lg , info) << " restore keydata" ;
223264
224265 const int buf_size = 1000 ;
@@ -232,7 +273,12 @@ void keydata::restore(const char * filename, std::string& mnemonics, std::string
232273 BOOST_LOG_SEV (log.lg , info) << " restore path: " << json.back ();
233274 }
234275
235- derive_masterkey (mnemonics, masterkey_pass);
276+ auto res = derive_masterkey (mnemonics, masterkey_pass);
277+ if (!res)
278+ {
279+ BOOST_LOG_SEV (log.lg , info) << " restore error " ;
280+ return count;
281+ }
236282
237283 for (auto &a: json)
238284 {
@@ -253,13 +299,16 @@ void keydata::restore(const char * filename, std::string& mnemonics, std::string
253299 params.path = backup.path ;
254300
255301 auto params_json = fc_light::json::to_string (params);
256- derive_key (masterkey_pass, params_json);
302+ if (derive_key (masterkey_pass, params_json))
303+ count++;
257304 }
305+ return count;
258306}
259307
260308
261- void keydata::backup (const char * filename)
309+ int keydata::backup (const char * filename)
262310{
311+ int count = 0 ;
263312 auto file = std::ofstream (filename);
264313 if (!file.is_open ())
265314 FC_LIGHT_THROW_EXCEPTION (fc_light::internal_error_exception, " Cannot open backup file (${filename})" , (" filename" , filename));
@@ -270,5 +319,13 @@ void keydata::backup(const char * filename)
270319 auto & sql = sql_singleton::instance ();
271320 auto backup_list = std::move (sql.select_path ());
272321 for (auto & a : backup_list)
273- file << fc_light::json::to_string (a) << std::endl;
322+ {
323+ auto json = fc_light::json::to_string (a);
324+ BOOST_LOG_SEV (log.lg , info) << json;
325+ file << json << std::endl;
326+ count++;
327+ }
328+ BOOST_LOG_SEV (log.lg , info) << " backup " +std::to_string (count)+" keys" ;
329+
330+ return count;
274331}
0 commit comments