Changeset View
Changeset View
Standalone View
Standalone View
src/wallet/rpcwallet.cpp
Show First 20 Lines • Show All 3,122 Lines • ▼ Show 20 Lines | RPCHelpMan{ | ||||
}}, | }}, | ||||
RPCExamples{HelpExampleCli("loadwallet", "\"test.dat\"") + | RPCExamples{HelpExampleCli("loadwallet", "\"test.dat\"") + | ||||
HelpExampleRpc("loadwallet", "\"test.dat\"")}, | HelpExampleRpc("loadwallet", "\"test.dat\"")}, | ||||
} | } | ||||
.Check(request); | .Check(request); | ||||
WalletContext &context = EnsureWalletContext(request.context); | WalletContext &context = EnsureWalletContext(request.context); | ||||
const std::string name(request.params[0].get_str()); | const std::string name(request.params[0].get_str()); | ||||
fs::path path(fs::absolute(name, GetWalletDir())); | |||||
if (fs::symlink_status(path).type() == fs::file_not_found) { | |||||
throw JSONRPCError(RPC_WALLET_NOT_FOUND, | |||||
"Wallet " + name + " not found."); | |||||
} else if (fs::is_directory(path)) { | |||||
// The given filename is a directory. Check that there's a wallet.dat | |||||
// file. | |||||
fs::path wallet_dat_file = path / "wallet.dat"; | |||||
if (fs::symlink_status(wallet_dat_file).type() == fs::file_not_found) { | |||||
throw JSONRPCError(RPC_WALLET_NOT_FOUND, | |||||
"Directory " + name + | |||||
" does not contain a wallet.dat file."); | |||||
} | |||||
} | |||||
DatabaseOptions options; | DatabaseOptions options; | ||||
DatabaseStatus status; | DatabaseStatus status; | ||||
options.require_existing = true; | |||||
bilingual_str error; | bilingual_str error; | ||||
std::vector<bilingual_str> warnings; | std::vector<bilingual_str> warnings; | ||||
std::optional<bool> load_on_start = | std::optional<bool> load_on_start = | ||||
request.params[1].isNull() | request.params[1].isNull() | ||||
? std::nullopt | ? std::nullopt | ||||
: std::make_optional<bool>(request.params[1].get_bool()); | : std::make_optional<bool>(request.params[1].get_bool()); | ||||
std::shared_ptr<CWallet> const wallet = LoadWallet( | std::shared_ptr<CWallet> const wallet = LoadWallet( | ||||
*context.chain, name, load_on_start, options, status, error, warnings); | *context.chain, name, load_on_start, options, status, error, warnings); | ||||
if (!wallet) { | if (!wallet) { | ||||
throw JSONRPCError(RPC_WALLET_ERROR, error.original); | // Map bad format to not found, since bad format is returned when the | ||||
// wallet directory exists, but doesn't contain a data file. | |||||
RPCErrorCode code = status == DatabaseStatus::FAILED_NOT_FOUND || | |||||
status == DatabaseStatus::FAILED_BAD_FORMAT | |||||
? RPC_WALLET_NOT_FOUND | |||||
: RPC_WALLET_ERROR; | |||||
throw JSONRPCError(code, error.original); | |||||
} | } | ||||
UniValue obj(UniValue::VOBJ); | UniValue obj(UniValue::VOBJ); | ||||
obj.pushKV("name", wallet->GetName()); | obj.pushKV("name", wallet->GetName()); | ||||
obj.pushKV("warning", Join(warnings, Untranslated("\n")).original); | obj.pushKV("warning", Join(warnings, Untranslated("\n")).original); | ||||
return obj; | return obj; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 1,854 Lines • Show Last 20 Lines |