Skip to content

Commit 5d15260

Browse files
committed
[wallet] [rpc] Add loadwallet RPC
The new `loadwallet` RPC method allows an existing wallet to be loaded dynamically at runtime. `unloadwallet` and `createwallet` are not implemented. Notably, `loadwallet` can only be used to load existing wallets, not to create a new wallet.
1 parent 876eb64 commit 5d15260

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

src/wallet/rpcwallet.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2994,6 +2994,53 @@ static UniValue listwallets(const JSONRPCRequest& request)
29942994
return obj;
29952995
}
29962996

2997+
UniValue loadwallet(const JSONRPCRequest& request)
2998+
{
2999+
if (request.fHelp || request.params.size() != 1)
3000+
throw std::runtime_error(
3001+
"loadwallet \"filename\"\n"
3002+
"\nLoads a wallet from a wallet file or directory."
3003+
"\nNote that all wallet command-line options used when starting bitcoind will be"
3004+
"\napplied to the new wallet (eg -zapwallettxes, upgradewallet, rescan, etc).\n"
3005+
"\nArguments:\n"
3006+
"1. \"filename\" (string, required) The wallet directory or .dat file.\n"
3007+
"\nResult:\n"
3008+
"{\n"
3009+
" \"name\" : <wallet_name>, (string) The wallet name if loaded successfully.\n"
3010+
" \"warning\" : <warning>, (string) Warning message if wallet was not loaded cleanly.\n"
3011+
"}\n"
3012+
"\nExamples:\n"
3013+
+ HelpExampleCli("loadwallet", "\"test.dat\"")
3014+
+ HelpExampleRpc("loadwallet", "\"test.dat\"")
3015+
);
3016+
std::string wallet_file = request.params[0].get_str();
3017+
std::string error;
3018+
3019+
fs::path wallet_path = fs::absolute(wallet_file, GetWalletDir());
3020+
if (fs::symlink_status(wallet_path).type() == fs::file_not_found) {
3021+
throw JSONRPCError(RPC_WALLET_NOT_FOUND, "Wallet " + wallet_file + " not found.");
3022+
}
3023+
3024+
std::string warning;
3025+
if (!CWallet::Verify(wallet_file, false, error, warning)) {
3026+
throw JSONRPCError(RPC_WALLET_ERROR, "Wallet file verification failed: " + error);
3027+
}
3028+
3029+
CWallet * const wallet = CWallet::CreateWalletFromFile(wallet_file, fs::absolute(wallet_file, GetWalletDir()));
3030+
if (!wallet) {
3031+
throw JSONRPCError(RPC_WALLET_ERROR, "Wallet loading failed.");
3032+
}
3033+
AddWallet(wallet);
3034+
3035+
wallet->postInitProcess();
3036+
3037+
UniValue obj(UniValue::VOBJ);
3038+
obj.pushKV("name", wallet->GetName());
3039+
obj.pushKV("warning", warning);
3040+
3041+
return obj;
3042+
}
3043+
29973044
static UniValue resendwallettransactions(const JSONRPCRequest& request)
29983045
{
29993046
CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
@@ -4197,6 +4244,7 @@ static const CRPCCommand commands[] =
41974244
{ "wallet", "listtransactions", &listtransactions, {"account|dummy","count","skip","include_watchonly"} },
41984245
{ "wallet", "listunspent", &listunspent, {"minconf","maxconf","addresses","include_unsafe","query_options"} },
41994246
{ "wallet", "listwallets", &listwallets, {} },
4247+
{ "wallet", "loadwallet", &loadwallet, {"filename"} },
42004248
{ "wallet", "lockunspent", &lockunspent, {"unlock","transactions"} },
42014249
{ "wallet", "sendfrom", &sendfrom, {"fromaccount","toaddress","amount","minconf","comment","comment_to"} },
42024250
{ "wallet", "sendmany", &sendmany, {"fromaccount|dummy","amounts","minconf","comment","subtractfeefrom","replaceable","conf_target","estimate_mode"} },

0 commit comments

Comments
 (0)