@@ -317,10 +317,7 @@ where
317317 . map ( |w| w. map_wallet ( RuntimeWallet :: Trezor ) ) ,
318318 } ;
319319
320- if res. is_err ( ) {
321- let _ = fs:: remove_file ( file_path) ;
322- }
323-
320+ Self :: delete_wallet_file_on_wallet_creation_failure ( & res, file_path) ;
324321 res
325322 }
326323
@@ -338,10 +335,10 @@ where
338335 )
339336 ) ;
340337
341- let db = wallet:: wallet:: open_or_create_wallet_file ( file_path)
338+ let db = wallet:: wallet:: open_or_create_wallet_file ( file_path. as_ref ( ) )
342339 . map_err ( ControllerError :: WalletError ) ?;
343340
344- match args {
341+ let res = match args {
345342 WalletTypeArgsComputed :: Software {
346343 mnemonic,
347344 passphrase,
@@ -382,6 +379,31 @@ where
382379 . map_err ( ControllerError :: WalletError ) ?;
383380 Ok ( wallet. map_wallet ( RuntimeWallet :: Trezor ) )
384381 }
382+ } ;
383+
384+ Self :: delete_wallet_file_on_wallet_creation_failure ( & res, file_path) ;
385+ res
386+ }
387+
388+ /// If wallet creation/recovery didn't succeed (e.g. due to a hard error, or because
389+ /// user intervention is required), we must delete the wallet file.
390+ fn delete_wallet_file_on_wallet_creation_failure (
391+ result : & Result < WalletCreation < RuntimeWallet < DefaultBackend > > , ControllerError < N > > ,
392+ file_path : impl AsRef < Path > ,
393+ ) {
394+ let must_remove_wallet_file = match result {
395+ Err ( _) => true ,
396+ Ok ( wallet_creation) => match wallet_creation {
397+ // Wallet was created successfully.
398+ WalletCreation :: Wallet ( _) => false ,
399+ // Wallet was not created successfully. The caller will need to handle this result
400+ // and either fail or try again.
401+ WalletCreation :: MultipleAvailableTrezorDevices ( _) => true ,
402+ } ,
403+ } ;
404+
405+ if must_remove_wallet_file {
406+ let _ = fs:: remove_file ( file_path) ;
385407 }
386408 }
387409
0 commit comments