diff --git a/src/dummywallet.cpp b/src/dummywallet.cpp index b13a115e4..79cb44dd8 100644 --- a/src/dummywallet.cpp +++ b/src/dummywallet.cpp @@ -6,6 +6,12 @@ #include +class CChainParams; +class CWallet; + +namespace interfaces { +class Chain; + class DummyWalletInit : public WalletInitInterface { public: void AddWalletOptions() const override {} @@ -44,6 +50,13 @@ std::vector> GetWallets() { throw std::logic_error("Wallet function called in non-wallet build."); } +std::shared_ptr LoadWallet(const CChainParams &chainParams, + interfaces::Chain &chain, + const std::string &name, std::string &error, + std::string &warning) { + throw std::logic_error("Wallet function called in non-wallet build."); +} + namespace interfaces { class Wallet; diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 4971b6aa8..1d3c41e6e 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -3564,29 +3564,20 @@ static UniValue loadwallet(const Config &config, const CChainParams &chainParams = config.GetChainParams(); - std::string wallet_file = request.params[0].get_str(); - std::string error; - WalletLocation location(wallet_file); - + WalletLocation location(request.params[0].get_str()); + if (!location.Exists()) { throw JSONRPCError(RPC_WALLET_NOT_FOUND, - "Wallet " + wallet_file + " not found."); - } - - std::string warning; - if (!CWallet::Verify(chainParams, *g_rpc_interfaces->chain, location, false, - error, warning)) { - throw JSONRPCError(RPC_WALLET_ERROR, - "Wallet file verification failed: " + error); + "Wallet " + request.params[0].get_str() + " not found."); } - std::shared_ptr const wallet = CWallet::LoadWalletFromFile(chainParams, *g_rpc_interfaces->chain, location); + std::string error, warning; + std::shared_ptr const wallet = LoadWallet( + chainParams, *g_rpc_interfaces->chain, location, error, warning); + if (!wallet) { - throw JSONRPCError(RPC_WALLET_ERROR, "Wallet loading failed."); + throw JSONRPCError(RPC_WALLET_ERROR, error); } - AddWallet(wallet); - - wallet->postInitProcess(); UniValue obj(UniValue::VOBJ); obj.pushKV("name", wallet->GetName()); diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 236db80d2..9677c6f85 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -161,6 +161,33 @@ OutputType g_address_type = OutputType::LEGACY; const char *DEFAULT_WALLET_DAT = "wallet.dat"; +std::shared_ptr LoadWallet(const CChainParams &chainParams, + interfaces::Chain &chain, + const WalletLocation &location, + std::string &error, std::string &warning) { + if (!CWallet::Verify(chainParams, chain, location, false, error, warning)) { + error = "Wallet file verification failed: " + error; + return nullptr; + } + WalletFlag flag; + std::shared_ptr wallet = + CWallet::CreateWalletFromFile(chainParams, chain, location, SecureString(""), std::vector(), flag); + if (!wallet) { + error = "Wallet loading failed."; + return nullptr; + } + AddWallet(wallet); + wallet->postInitProcess(); + return wallet; +} + +std::shared_ptr LoadWallet(const CChainParams &chainParams, + interfaces::Chain &chain, + const std::string &name, std::string &error, + std::string &warning) { + return LoadWallet(chainParams, chain, WalletLocation(name), error, warning); +} + /** * If fee estimation does not have enough data to provide estimates, use this * fee instead. Has no effect if not using fee estimation. diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index e725cfb41..d18643b7a 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -72,6 +72,10 @@ bool RemoveWallet(const std::shared_ptr &wallet); bool HasWallets(); std::vector> GetWallets(); std::shared_ptr GetWallet(const std::string &name); +std::shared_ptr LoadWallet(const CChainParams &chainParams, + interfaces::Chain &chain, + const WalletLocation &location, + std::string &error, std::string &warning); /** * Settings