Skip to content

Commit 5b2167f

Browse files
committed
MOVEONLY: Move LoadWalletHelper to wallet/rpc/util
1 parent 8b73640 commit 5b2167f

File tree

5 files changed

+36
-40
lines changed

5 files changed

+36
-40
lines changed

src/wallet/rpc/backup.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include <util/system.h>
1818
#include <util/time.h>
1919
#include <util/translation.h>
20-
#include <wallet/rpcwallet.h>
2120
#include <wallet/rpc/util.h>
2221
#include <wallet/wallet.h>
2322

src/wallet/rpc/util.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <wallet/rpc/util.h>
66

77
#include <rpc/util.h>
8+
#include <util/translation.h>
89
#include <util/url.h>
910
#include <wallet/context.h>
1011
#include <wallet/wallet.h>
@@ -120,3 +121,34 @@ std::string LabelFromValue(const UniValue& value)
120121
throw JSONRPCError(RPC_WALLET_INVALID_LABEL_NAME, "Invalid label name");
121122
return label;
122123
}
124+
125+
std::tuple<std::shared_ptr<CWallet>, std::vector<bilingual_str>> LoadWalletHelper(WalletContext& context, UniValue load_on_start_param, const std::string wallet_name)
126+
{
127+
DatabaseOptions options;
128+
DatabaseStatus status;
129+
options.require_existing = true;
130+
bilingual_str error;
131+
std::vector<bilingual_str> warnings;
132+
std::optional<bool> load_on_start = load_on_start_param.isNull() ? std::nullopt : std::optional<bool>(load_on_start_param.get_bool());
133+
std::shared_ptr<CWallet> const wallet = LoadWallet(context, wallet_name, load_on_start, options, status, error, warnings);
134+
135+
if (!wallet) {
136+
// Map bad format to not found, since bad format is returned when the
137+
// wallet directory exists, but doesn't contain a data file.
138+
RPCErrorCode code = RPC_WALLET_ERROR;
139+
switch (status) {
140+
case DatabaseStatus::FAILED_NOT_FOUND:
141+
case DatabaseStatus::FAILED_BAD_FORMAT:
142+
code = RPC_WALLET_NOT_FOUND;
143+
break;
144+
case DatabaseStatus::FAILED_ALREADY_LOADED:
145+
code = RPC_WALLET_ALREADY_LOADED;
146+
break;
147+
default: // RPC_WALLET_ERROR is returned for all other cases.
148+
break;
149+
}
150+
throw JSONRPCError(code, error.original);
151+
}
152+
153+
return { wallet, warnings };
154+
}

src/wallet/rpc/util.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
#include <any>
99
#include <memory>
1010
#include <string>
11+
#include <vector>
1112

13+
struct bilingual_str;
1214
class CWallet;
1315
class JSONRPCRequest;
1416
class LegacyScriptPubKeyMan;
@@ -35,4 +37,6 @@ bool GetAvoidReuseFlag(const CWallet& wallet, const UniValue& param);
3537
bool ParseIncludeWatchonly(const UniValue& include_watchonly, const CWallet& wallet);
3638
std::string LabelFromValue(const UniValue& value);
3739

40+
std::tuple<std::shared_ptr<CWallet>, std::vector<bilingual_str>> LoadWalletHelper(WalletContext& context, UniValue load_on_start_param, const std::string wallet_name);
41+
3842
#endif // BITCOIN_WALLET_RPC_UTIL_H

src/wallet/rpcwallet.cpp

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2191,37 +2191,6 @@ static RPCHelpMan listwallets()
21912191
};
21922192
}
21932193

2194-
std::tuple<std::shared_ptr<CWallet>, std::vector<bilingual_str>> LoadWalletHelper(WalletContext& context, UniValue load_on_start_param, const std::string wallet_name)
2195-
{
2196-
DatabaseOptions options;
2197-
DatabaseStatus status;
2198-
options.require_existing = true;
2199-
bilingual_str error;
2200-
std::vector<bilingual_str> warnings;
2201-
std::optional<bool> load_on_start = load_on_start_param.isNull() ? std::nullopt : std::optional<bool>(load_on_start_param.get_bool());
2202-
std::shared_ptr<CWallet> const wallet = LoadWallet(context, wallet_name, load_on_start, options, status, error, warnings);
2203-
2204-
if (!wallet) {
2205-
// Map bad format to not found, since bad format is returned when the
2206-
// wallet directory exists, but doesn't contain a data file.
2207-
RPCErrorCode code = RPC_WALLET_ERROR;
2208-
switch (status) {
2209-
case DatabaseStatus::FAILED_NOT_FOUND:
2210-
case DatabaseStatus::FAILED_BAD_FORMAT:
2211-
code = RPC_WALLET_NOT_FOUND;
2212-
break;
2213-
case DatabaseStatus::FAILED_ALREADY_LOADED:
2214-
code = RPC_WALLET_ALREADY_LOADED;
2215-
break;
2216-
default: // RPC_WALLET_ERROR is returned for all other cases.
2217-
break;
2218-
}
2219-
throw JSONRPCError(code, error.original);
2220-
}
2221-
2222-
return { wallet, warnings };
2223-
}
2224-
22252194
static RPCHelpMan loadwallet()
22262195
{
22272196
return RPCHelpMan{"loadwallet",

src/wallet/rpcwallet.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,10 @@
77

88
#include <span.h>
99

10-
#include <memory>
11-
#include <string>
12-
#include <vector>
13-
1410
class CRPCCommand;
15-
class CWallet;
16-
struct WalletContext;
1711

1812
Span<const CRPCCommand> GetWalletRPCCommands();
1913

20-
std::tuple<std::shared_ptr<CWallet>, std::vector<bilingual_str>> LoadWalletHelper(WalletContext& context, UniValue load_on_start_param, const std::string wallet_name);
21-
2214
RPCHelpMan getaddressinfo();
2315
RPCHelpMan signrawtransactionwithwallet();
2416
#endif // BITCOIN_WALLET_RPCWALLET_H

0 commit comments

Comments
 (0)