Skip to content

Commit 17abc0f

Browse files
committed
wallet: Factor out LoadWallet
1 parent 64127b3 commit 17abc0f

File tree

4 files changed

+35
-13
lines changed

4 files changed

+35
-13
lines changed

src/dummywallet.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
class CWallet;
1010

11+
namespace interfaces {
12+
class Chain;
13+
}
14+
1115
class DummyWalletInit : public WalletInitInterface {
1216
public:
1317

@@ -43,6 +47,11 @@ std::vector<std::shared_ptr<CWallet>> GetWallets()
4347
throw std::logic_error("Wallet function called in non-wallet build.");
4448
}
4549

50+
std::shared_ptr<CWallet> LoadWallet(interfaces::Chain& chain, const std::string& name, std::string& error, std::string& warning)
51+
{
52+
throw std::logic_error("Wallet function called in non-wallet build.");
53+
}
54+
4655
namespace interfaces {
4756

4857
class Wallet;

src/wallet/rpcwallet.cpp

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2544,7 +2544,6 @@ static UniValue loadwallet(const JSONRPCRequest& request)
25442544
}.ToString());
25452545

25462546
WalletLocation location(request.params[0].get_str());
2547-
std::string error;
25482547

25492548
if (!location.Exists()) {
25502549
throw JSONRPCError(RPC_WALLET_NOT_FOUND, "Wallet " + location.GetName() + " not found.");
@@ -2556,18 +2555,9 @@ static UniValue loadwallet(const JSONRPCRequest& request)
25562555
}
25572556
}
25582557

2559-
std::string warning;
2560-
if (!CWallet::Verify(*g_rpc_interfaces->chain, location, false, error, warning)) {
2561-
throw JSONRPCError(RPC_WALLET_ERROR, "Wallet file verification failed: " + error);
2562-
}
2563-
2564-
std::shared_ptr<CWallet> const wallet = CWallet::CreateWalletFromFile(*g_rpc_interfaces->chain, location);
2565-
if (!wallet) {
2566-
throw JSONRPCError(RPC_WALLET_ERROR, "Wallet loading failed.");
2567-
}
2568-
AddWallet(wallet);
2569-
2570-
wallet->postInitProcess();
2558+
std::string error, warning;
2559+
std::shared_ptr<CWallet> const wallet = LoadWallet(*g_rpc_interfaces->chain, location, error, warning);
2560+
if (!wallet) throw JSONRPCError(RPC_WALLET_ERROR, error);
25712561

25722562
UniValue obj(UniValue::VOBJ);
25732563
obj.pushKV("name", wallet->GetName());

src/wallet/wallet.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,28 @@ void UnloadWallet(std::shared_ptr<CWallet>&& wallet)
130130
}
131131
}
132132

133+
std::shared_ptr<CWallet> LoadWallet(interfaces::Chain& chain, const WalletLocation& location, std::string& error, std::string& warning)
134+
{
135+
if (!CWallet::Verify(chain, location, false, error, warning)) {
136+
error = "Wallet file verification failed: " + error;
137+
return nullptr;
138+
}
139+
140+
std::shared_ptr<CWallet> wallet = CWallet::CreateWalletFromFile(chain, location);
141+
if (!wallet) {
142+
error = "Wallet loading failed.";
143+
return nullptr;
144+
}
145+
AddWallet(wallet);
146+
wallet->postInitProcess();
147+
return wallet;
148+
}
149+
150+
std::shared_ptr<CWallet> LoadWallet(interfaces::Chain& chain, const std::string& name, std::string& error, std::string& warning)
151+
{
152+
return LoadWallet(chain, WalletLocation(name), error, warning);
153+
}
154+
133155
const uint32_t BIP32_HARDENED_KEY_LIMIT = 0x80000000;
134156

135157
const uint256 CMerkleTx::ABANDON_HASH(uint256S("0000000000000000000000000000000000000000000000000000000000000001"));

src/wallet/wallet.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ bool RemoveWallet(const std::shared_ptr<CWallet>& wallet);
6666
bool HasWallets();
6767
std::vector<std::shared_ptr<CWallet>> GetWallets();
6868
std::shared_ptr<CWallet> GetWallet(const std::string& name);
69+
std::shared_ptr<CWallet> LoadWallet(interfaces::Chain& chain, const WalletLocation& location, std::string& error, std::string& warning);
6970

7071
//! Default for -keypool
7172
static const unsigned int DEFAULT_KEYPOOL_SIZE = 1000;

0 commit comments

Comments
 (0)