@@ -3076,7 +3076,7 @@ static UniValue listwallets(const JSONRPCRequest& request)
3076
3076
return obj;
3077
3077
}
3078
3078
3079
- UniValue loadwallet (const JSONRPCRequest& request)
3079
+ static UniValue loadwallet (const JSONRPCRequest& request)
3080
3080
{
3081
3081
if (request.fHelp || request.params .size () != 1 )
3082
3082
throw std::runtime_error (
@@ -3123,7 +3123,7 @@ UniValue loadwallet(const JSONRPCRequest& request)
3123
3123
return obj;
3124
3124
}
3125
3125
3126
- UniValue createwallet (const JSONRPCRequest& request)
3126
+ static UniValue createwallet (const JSONRPCRequest& request)
3127
3127
{
3128
3128
if (request.fHelp || request.params .size () != 1 ) {
3129
3129
throw std::runtime_error (
@@ -3170,6 +3170,55 @@ UniValue createwallet(const JSONRPCRequest& request)
3170
3170
return obj;
3171
3171
}
3172
3172
3173
+ static UniValue unloadwallet (const JSONRPCRequest& request)
3174
+ {
3175
+ if (request.fHelp || request.params .size () > 1 ) {
3176
+ throw std::runtime_error (
3177
+ " unloadwallet ( \" wallet_name\" )\n "
3178
+ " Unloads the wallet referenced by the request endpoint otherwise unloads the wallet specified in the argument.\n "
3179
+ " Specifying the wallet name on a wallet endpoint is invalid."
3180
+ " \n Arguments:\n "
3181
+ " 1. \" wallet_name\" (string, optional) The name of the wallet to unload.\n "
3182
+ " \n Examples:\n "
3183
+ + HelpExampleCli (" unloadwallet" , " wallet_name" )
3184
+ + HelpExampleRpc (" unloadwallet" , " wallet_name" )
3185
+ );
3186
+ }
3187
+
3188
+ std::string wallet_name;
3189
+ if (GetWalletNameFromJSONRPCRequest (request, wallet_name)) {
3190
+ if (!request.params [0 ].isNull ()) {
3191
+ throw JSONRPCError (RPC_INVALID_PARAMETER, " Cannot unload the requested wallet" );
3192
+ }
3193
+ } else {
3194
+ wallet_name = request.params [0 ].get_str ();
3195
+ }
3196
+
3197
+ std::shared_ptr<CWallet> wallet = GetWallet (wallet_name);
3198
+ if (!wallet) {
3199
+ throw JSONRPCError (RPC_WALLET_NOT_FOUND, " Requested wallet does not exist or is not loaded" );
3200
+ }
3201
+
3202
+ // Release the "main" shared pointer and prevent further notifications.
3203
+ // Note that any attempt to load the same wallet would fail until the wallet
3204
+ // is destroyed (see CheckUniqueFileid).
3205
+ if (!RemoveWallet (wallet)) {
3206
+ throw JSONRPCError (RPC_MISC_ERROR, " Requested wallet already unloaded" );
3207
+ }
3208
+ UnregisterValidationInterface (wallet.get ());
3209
+
3210
+ // The wallet can be in use so it's not possible to explicitly unload here.
3211
+ // Just notify the unload intent so that all shared pointers are released.
3212
+ // The wallet will be destroyed once the last shared pointer is released.
3213
+ wallet->NotifyUnload ();
3214
+
3215
+ // There's no point in waiting for the wallet to unload.
3216
+ // At this point this method should never fail. The unloading could only
3217
+ // fail due to an unexpected error which would cause a process termination.
3218
+
3219
+ return NullUniValue;
3220
+ }
3221
+
3173
3222
static UniValue resendwallettransactions (const JSONRPCRequest& request)
3174
3223
{
3175
3224
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest (request);
@@ -4405,6 +4454,7 @@ static const CRPCCommand commands[] =
4405
4454
{ " wallet" , " settxfee" , &settxfee, {" amount" } },
4406
4455
{ " wallet" , " signmessage" , &signmessage, {" address" ," message" } },
4407
4456
{ " wallet" , " signrawtransactionwithwallet" , &signrawtransactionwithwallet, {" hexstring" ," prevtxs" ," sighashtype" } },
4457
+ { " wallet" , " unloadwallet" , &unloadwallet, {" wallet_name" } },
4408
4458
{ " wallet" , " walletlock" , &walletlock, {} },
4409
4459
{ " wallet" , " walletpassphrasechange" , &walletpassphrasechange, {" oldpassphrase" ," newpassphrase" } },
4410
4460
{ " wallet" , " walletpassphrase" , &walletpassphrase, {" passphrase" ," timeout" } },
0 commit comments