@@ -2716,16 +2716,8 @@ static RPCHelpMan listwallets()
27162716 };
27172717}
27182718
2719- static std::tuple<std:: shared_ptr<CWallet>, std::vector<bilingual_str>> LoadWalletHelper (WalletContext& context, UniValue load_on_start_param, const std::string wallet_name )
2719+ void HandleWalletError ( const std::shared_ptr<CWallet> wallet, DatabaseStatus& status, bilingual_str& error )
27202720{
2721- DatabaseOptions options;
2722- DatabaseStatus status;
2723- options.require_existing = true ;
2724- bilingual_str error;
2725- std::vector<bilingual_str> warnings;
2726- std::optional<bool > load_on_start = load_on_start_param.isNull () ? std::nullopt : std::optional<bool >(load_on_start_param.get_bool ());
2727- std::shared_ptr<CWallet> const wallet = LoadWallet (*context.chain , *context.m_coinjoin_loader , wallet_name, load_on_start, options, status, error, warnings);
2728-
27292721 if (!wallet) {
27302722 // Map bad format to not found, since bad format is returned when the
27312723 // wallet directory exists, but doesn't contain a data file.
@@ -2738,13 +2730,17 @@ static std::tuple<std::shared_ptr<CWallet>, std::vector<bilingual_str>> LoadWall
27382730 case DatabaseStatus::FAILED_ALREADY_LOADED:
27392731 code = RPC_WALLET_ALREADY_LOADED;
27402732 break ;
2733+ case DatabaseStatus::FAILED_ALREADY_EXISTS:
2734+ code = RPC_WALLET_ALREADY_EXISTS;
2735+ break ;
2736+ case DatabaseStatus::FAILED_INVALID_BACKUP_FILE:
2737+ code = RPC_INVALID_PARAMETER;
2738+ break ;
27412739 default : // RPC_WALLET_ERROR is returned for all other cases.
27422740 break ;
27432741 }
27442742 throw JSONRPCError (code, error.original );
27452743 }
2746-
2747- return { wallet, warnings };
27482744}
27492745
27502746static RPCHelpMan upgradetohd ()
@@ -2872,7 +2868,15 @@ static RPCHelpMan loadwallet()
28722868 WalletContext& context = EnsureWalletContext (request.context );
28732869 const std::string name (request.params [0 ].get_str ());
28742870
2875- auto [wallet, warnings] = LoadWalletHelper (context, request.params [1 ], name);
2871+ DatabaseOptions options;
2872+ DatabaseStatus status;
2873+ options.require_existing = true ;
2874+ bilingual_str error;
2875+ std::vector<bilingual_str> warnings;
2876+ std::optional<bool > load_on_start = request.params [1 ].isNull () ? std::nullopt : std::optional<bool >(request.params [1 ].get_bool ());
2877+ std::shared_ptr<CWallet> const wallet = LoadWallet (*context.chain , *context.m_coinjoin_loader , name, load_on_start, options, status, error, warnings);
2878+
2879+ HandleWalletError (wallet, status, error);
28762880
28772881 UniValue obj (UniValue::VOBJ);
28782882 obj.pushKV (" name" , wallet->GetName ());
@@ -3072,27 +3076,17 @@ static RPCHelpMan restorewallet()
30723076
30733077 std::string backup_file = request.params [1 ].get_str ();
30743078
3075- if (!fs::exists (backup_file)) {
3076- throw JSONRPCError (RPC_INVALID_PARAMETER, " Backup file does not exist" );
3077- }
3078-
30793079 std::string wallet_name = request.params [0 ].get_str ();
30803080
3081- const fs::path wallet_path = fsbridge::AbsPathJoin (GetWalletDir (), wallet_name);
3082-
3083- if (fs::exists (wallet_path)) {
3084- throw JSONRPCError (RPC_INVALID_PARAMETER, " Wallet name already exists." );
3085- }
3086-
3087- if (!TryCreateDirectories (wallet_path)) {
3088- throw JSONRPCError (RPC_WALLET_ERROR, strprintf (" Failed to create database path '%s'. Database already exists." , wallet_path.string ()));
3089- }
3081+ std::optional<bool > load_on_start = request.params [2 ].isNull () ? std::nullopt : std::optional<bool >(request.params [2 ].get_bool ());
30903082
3091- auto wallet_file = wallet_path / " wallet.dat" ;
3083+ DatabaseStatus status;
3084+ bilingual_str error;
3085+ std::vector<bilingual_str> warnings;
30923086
3093- fs::copy_file ( backup_file, wallet_file, fs::copy_option::fail_if_exists );
3087+ const std::shared_ptr<CWallet> wallet = RestoreWallet (*context. chain , *context. m_coinjoin_loader , backup_file, wallet_name, load_on_start, status, error, warnings );
30943088
3095- auto [ wallet, warnings] = LoadWalletHelper (context, request. params [ 2 ], wallet_name );
3089+ HandleWalletError ( wallet, status, error );
30963090
30973091 UniValue obj (UniValue::VOBJ);
30983092 obj.pushKV (" name" , wallet->GetName ());
0 commit comments