Skip to content

Commit

Permalink
[backport#15153] wallet: Factor out LoadWallet
Browse files Browse the repository at this point in the history
Summary:
bitcoin/bitcoin@17abc0f

---

Depends on D6084

This is a partial backport of Core [[bitcoin/bitcoin#15153 | PR15153]]

Test Plan:
  cmake .. -GNinja [-DENABLE_BITCOIN_WALLET=OFF]
  ninja check-all

Reviewers: #bitcoin_abc, deadalnix

Reviewed By: #bitcoin_abc, deadalnix

Subscribers: deadalnix

Differential Revision: https://reviews.bitcoinabc.org/D6096
  • Loading branch information
promag authored and jonspock committed Sep 23, 2020
1 parent 46f03fc commit 7fe4364
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 17 deletions.
13 changes: 13 additions & 0 deletions src/dummywallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@

#include <walletinitinterface.h>

class CChainParams;
class CWallet;

namespace interfaces {
class Chain;

class DummyWalletInit : public WalletInitInterface {
public:
void AddWalletOptions() const override {}
Expand Down Expand Up @@ -44,6 +50,13 @@ std::vector<std::shared_ptr<CWallet>> GetWallets() {
throw std::logic_error("Wallet function called in non-wallet build.");
}

std::shared_ptr<CWallet> 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;
Expand Down
25 changes: 8 additions & 17 deletions src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<CWallet> const wallet = CWallet::LoadWalletFromFile(chainParams, *g_rpc_interfaces->chain, location);
std::string error, warning;
std::shared_ptr<CWallet> 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());
Expand Down
27 changes: 27 additions & 0 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,33 @@ OutputType g_address_type = OutputType::LEGACY;

const char *DEFAULT_WALLET_DAT = "wallet.dat";

std::shared_ptr<CWallet> 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<CWallet> wallet =
CWallet::CreateWalletFromFile(chainParams, chain, location, SecureString(""), std::vector<std::string>(), flag);
if (!wallet) {
error = "Wallet loading failed.";
return nullptr;
}
AddWallet(wallet);
wallet->postInitProcess();
return wallet;
}

std::shared_ptr<CWallet> 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.
Expand Down
4 changes: 4 additions & 0 deletions src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ bool RemoveWallet(const std::shared_ptr<CWallet> &wallet);
bool HasWallets();
std::vector<std::shared_ptr<CWallet>> GetWallets();
std::shared_ptr<CWallet> GetWallet(const std::string &name);
std::shared_ptr<CWallet> LoadWallet(const CChainParams &chainParams,
interfaces::Chain &chain,
const WalletLocation &location,
std::string &error, std::string &warning);

/**
* Settings
Expand Down

0 comments on commit 7fe4364

Please sign in to comment.