@@ -200,30 +200,42 @@ fn read_from_file(
200
200
201
201
if file_format_is_old {
202
202
info ! ( "Your consensus keys are in the old format. Migrating to the new format and saving the old file as {}.old" , path. display( ) ) ;
203
- save_old_file ( & path) ?;
204
- let keys_filename = path
205
- . file_name ( )
206
- . expect ( "keys file should have a name" )
207
- . to_str ( )
208
- . expect ( "keys file should be a valid string" ) ;
209
- let keys_file_dir = path
210
- . parent ( )
211
- . expect ( "keys file should have a parent directory" ) ;
212
- let temp_keys_name = format ! ( "{}_new" , keys_filename) ;
213
- save_consensus_keys ( keys_file_dir, & temp_keys_name, & pk, & sk, pwd) ?;
214
- fs:: rename (
215
- keys_file_dir. join ( & temp_keys_name) . with_extension ( "keys" ) ,
216
- & path,
217
- ) ?;
218
- fs:: remove_file (
219
- keys_file_dir. join ( temp_keys_name) . with_extension ( "cpk" ) ,
220
- )
221
- . expect ( "The new cpk file should be deleted" ) ;
203
+ migrate_file_to_new_format ( & path, & pk, & sk, pwd) . map_err ( |e| {
204
+ anyhow:: anyhow!(
205
+ "failed to migrate consensus keys to the new format: {e}"
206
+ )
207
+ } ) ?;
222
208
}
223
209
224
210
Ok ( ( pk, sk) )
225
211
}
226
212
213
+ fn migrate_file_to_new_format (
214
+ path : & Path ,
215
+ pk : & BlsPublicKey ,
216
+ sk : & BlsSecretKey ,
217
+ pwd : & str ,
218
+ ) -> Result < ( ) , ConsensusKeysError > {
219
+ save_old_file ( path) ?;
220
+ let keys_filename = path
221
+ . file_name ( )
222
+ . expect ( "keys file should have a name" )
223
+ . to_str ( )
224
+ . expect ( "keys file should be a valid string" ) ;
225
+ let keys_file_dir = path
226
+ . parent ( )
227
+ . expect ( "keys file should have a parent directory" ) ;
228
+ let temp_keys_name = format ! ( "{}_new" , keys_filename) ;
229
+ save_consensus_keys ( keys_file_dir, & temp_keys_name, pk, sk, pwd) ?;
230
+ fs:: rename (
231
+ keys_file_dir. join ( & temp_keys_name) . with_extension ( "keys" ) ,
232
+ path,
233
+ ) ?;
234
+ fs:: remove_file ( keys_file_dir. join ( temp_keys_name) . with_extension ( "cpk" ) )
235
+ . expect ( "The new cpk file should be deleted" ) ;
236
+ Ok ( ( ) )
237
+ }
238
+
227
239
fn save_old_file ( path : & Path ) -> Result < ( ) , ConsensusKeysError > {
228
240
let old_path = path. with_extension ( "keys.old" ) ;
229
241
fs:: copy ( path, old_path) ?;
0 commit comments