Skip to content

Commit a987438

Browse files
committed
wallet: Remove path checking code from loadwallet RPC
This commit does not change behavior except for error messages which now include more complete information.
1 parent 8b5e729 commit a987438

File tree

3 files changed

+13
-15
lines changed

3 files changed

+13
-15
lines changed

src/wallet/rpcwallet.cpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2503,25 +2503,20 @@ static UniValue loadwallet(const JSONRPCRequest& request)
25032503

25042504
WalletContext& context = EnsureWalletContext(request.context);
25052505
const std::string name(request.params[0].get_str());
2506-
fs::path path(fs::absolute(name, GetWalletDir()));
2507-
2508-
if (fs::symlink_status(path).type() == fs::file_not_found) {
2509-
throw JSONRPCError(RPC_WALLET_NOT_FOUND, "Wallet " + name + " not found.");
2510-
} else if (fs::is_directory(path)) {
2511-
// The given filename is a directory. Check that there's a wallet.dat file.
2512-
fs::path wallet_dat_file = path / "wallet.dat";
2513-
if (fs::symlink_status(wallet_dat_file).type() == fs::file_not_found) {
2514-
throw JSONRPCError(RPC_WALLET_NOT_FOUND, "Directory " + name + " does not contain a wallet.dat file.");
2515-
}
2516-
}
25172506

25182507
DatabaseOptions options;
25192508
DatabaseStatus status;
2509+
options.require_existing = true;
25202510
bilingual_str error;
25212511
std::vector<bilingual_str> warnings;
25222512
Optional<bool> load_on_start = request.params[1].isNull() ? nullopt : Optional<bool>(request.params[1].get_bool());
25232513
std::shared_ptr<CWallet> const wallet = LoadWallet(*context.chain, name, load_on_start, options, status, error, warnings);
2524-
if (!wallet) throw JSONRPCError(RPC_WALLET_ERROR, error.original);
2514+
if (!wallet) {
2515+
// Map bad format to not found, since bad format is returned when the
2516+
// wallet directory exists, but doesn't contain a data file.
2517+
RPCErrorCode code = status == DatabaseStatus::FAILED_NOT_FOUND || status == DatabaseStatus::FAILED_BAD_FORMAT ? RPC_WALLET_NOT_FOUND : RPC_WALLET_ERROR;
2518+
throw JSONRPCError(code, error.original);
2519+
}
25252520

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

test/functional/rpc_createmultisig.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ def do_multisig(self):
129129
try:
130130
node1.loadwallet('wmulti')
131131
except JSONRPCException as e:
132-
if e.error['code'] == -18 and 'Wallet wmulti not found' in e.error['message']:
132+
path = os.path.join(self.options.tmpdir, "node1", "regtest", "wallets", "wmulti")
133+
if e.error['code'] == -18 and "Wallet file verification failed. Failed to load database path '{}'. Path does not exist.".format(path) in e.error['message']:
133134
node1.createwallet(wallet_name='wmulti', disable_private_keys=True)
134135
else:
135136
raise

test/functional/wallet_multiwallet.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,8 @@ def wallet_file(name):
244244
assert_equal(set(self.nodes[0].listwallets()), set(wallet_names))
245245

246246
# Fail to load if wallet doesn't exist
247-
assert_raises_rpc_error(-18, 'Wallet wallets not found.', self.nodes[0].loadwallet, 'wallets')
247+
path = os.path.join(self.options.tmpdir, "node0", "regtest", "wallets", "wallets")
248+
assert_raises_rpc_error(-18, "Wallet file verification failed. Failed to load database path '{}'. Path does not exist.".format(path), self.nodes[0].loadwallet, 'wallets')
248249

249250
# Fail to load duplicate wallets
250251
path = os.path.join(self.options.tmpdir, "node0", "regtest", "wallets", "w1", "wallet.dat")
@@ -266,7 +267,8 @@ def wallet_file(name):
266267

267268
# Fail to load if a directory is specified that doesn't contain a wallet
268269
os.mkdir(wallet_dir('empty_wallet_dir'))
269-
assert_raises_rpc_error(-18, "Directory empty_wallet_dir does not contain a wallet.dat file", self.nodes[0].loadwallet, 'empty_wallet_dir')
270+
path = os.path.join(self.options.tmpdir, "node0", "regtest", "wallets", "empty_wallet_dir")
271+
assert_raises_rpc_error(-18, "Wallet file verification failed. Failed to load database path '{}'. Data is not in recognized format.".format(path), self.nodes[0].loadwallet, 'empty_wallet_dir')
270272

271273
self.log.info("Test dynamic wallet creation.")
272274

0 commit comments

Comments
 (0)