diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -258,10 +258,6 @@ "Error: This wallet has no available keys"); } - if (!pwallet->IsLocked()) { - pwallet->TopUpKeyPool(); - } - OutputType output_type = pwallet->m_default_change_type != OutputType::CHANGE_AUTO ? pwallet->m_default_change_type @@ -274,15 +270,11 @@ } } - ReserveDestination reservedest(pwallet); CTxDestination dest; - if (!reservedest.GetReservedDestination(output_type, dest, true)) { - throw JSONRPCError( - RPC_WALLET_KEYPOOL_RAN_OUT, - "Error: Keypool ran out, please call keypoolrefill first"); + std::string error; + if (!pwallet->GetNewChangeDestination(output_type, dest, error)) { + throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, error); } - - reservedest.KeepDestination(); return EncodeDestination(dest, config); } diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -1398,6 +1398,8 @@ bool GetNewDestination(const OutputType type, const std::string label, CTxDestination &dest, std::string &error); + bool GetNewChangeDestination(const OutputType type, CTxDestination &dest, + std::string &error); isminetype IsMine(const CTxIn &txin) const; /** diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -4127,6 +4127,24 @@ return true; } +bool CWallet::GetNewChangeDestination(const OutputType type, + CTxDestination &dest, + std::string &error) { + error.clear(); + if (!IsLocked()) { + TopUpKeyPool(); + } + + ReserveDestination reservedest(this); + if (!reservedest.GetReservedDestination(type, dest, true)) { + error = "Error: Keypool ran out, please call keypoolrefill first"; + return false; + } + + reservedest.KeepDestination(); + return true; +} + static int64_t GetOldestKeyTimeInPool(const std::set &setKeyPool, WalletBatch &batch) { if (setKeyPool.empty()) {