diff --git a/src/dummywallet.cpp b/src/dummywallet.cpp --- a/src/dummywallet.cpp +++ b/src/dummywallet.cpp @@ -6,8 +6,13 @@ #include #include +class CChainParams; class CWallet; +namespace interfaces { +class Chain; +} + class DummyWalletInit : public WalletInitInterface { public: bool HasWalletSupport() const override { return false; } @@ -44,6 +49,13 @@ 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 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -3116,7 +3116,6 @@ const CChainParams &chainParams = config.GetChainParams(); WalletLocation location(request.params[0].get_str()); - std::string error; if (!location.Exists()) { throw JSONRPCError(RPC_WALLET_NOT_FOUND, @@ -3132,21 +3131,12 @@ } } - 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); - } - - std::shared_ptr const wallet = CWallet::CreateWalletFromFile( - 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.h b/src/wallet/wallet.h --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -49,6 +49,10 @@ 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); //! Default for -keypool static const unsigned int DEFAULT_KEYPOOL_SIZE = 1000; diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -142,6 +142,33 @@ static const size_t OUTPUT_GROUP_MAX_ENTRIES = 10; +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; + } + + std::shared_ptr wallet = + CWallet::CreateWalletFromFile(chainParams, chain, location); + 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); +} + const uint32_t BIP32_HARDENED_KEY_LIMIT = 0x80000000; const BlockHash CMerkleTx::ABANDON_HASH(uint256S(