Skip to content

Commit 9421317

Browse files
committed
[wallet] [rpc] Add createwallet RPC
Add a `createwallet` RPC to allow wallets to be created dynamically at runtime. This functionality is currently only available through RPC and newly created wallets will not be displayed in the GUI.
1 parent 610f4dd commit 9421317

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

src/wallet/rpcwallet.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3114,6 +3114,52 @@ UniValue loadwallet(const JSONRPCRequest& request)
31143114
return obj;
31153115
}
31163116

3117+
UniValue createwallet(const JSONRPCRequest& request)
3118+
{
3119+
if (request.fHelp || request.params.size() != 1)
3120+
throw std::runtime_error(
3121+
"createwallet \"wallet_name\"\n"
3122+
"\nCreates and loads a new wallet.\n"
3123+
"\nArguments:\n"
3124+
"1. \"wallet_name\" (string, required) The name for the new wallet.\n"
3125+
"\nResult:\n"
3126+
"{\n"
3127+
" \"name\" : <wallet_name>, (string) The wallet name if created successfully.\n"
3128+
" \"warning\" : <warning>, (string) Warning message if wallet was not loaded cleanly.\n"
3129+
"}\n"
3130+
"\nExamples:\n"
3131+
+ HelpExampleCli("createwallet", "\"test.dat\"")
3132+
+ HelpExampleRpc("createwallet", "\"test.dat\"")
3133+
);
3134+
std::string wallet_name = request.params[0].get_str();
3135+
std::string error;
3136+
std::string warning;
3137+
3138+
fs::path wallet_path = fs::absolute(wallet_name, GetWalletDir());
3139+
if (fs::symlink_status(wallet_path).type() != fs::file_not_found) {
3140+
throw JSONRPCError(RPC_WALLET_ERROR, "Wallet " + wallet_name + " already exists.");
3141+
}
3142+
3143+
// Wallet::Verify will check if we're trying to create a wallet with a duplication name.
3144+
if (!CWallet::Verify(wallet_name, false, error, warning)) {
3145+
throw JSONRPCError(RPC_WALLET_ERROR, "Wallet file verification failed: " + error);
3146+
}
3147+
3148+
std::shared_ptr<CWallet> const wallet = CWallet::CreateWalletFromFile(wallet_name, fs::absolute(wallet_name, GetWalletDir()));
3149+
if (!wallet) {
3150+
throw JSONRPCError(RPC_WALLET_ERROR, "Wallet creation failed.");
3151+
}
3152+
AddWallet(wallet);
3153+
3154+
wallet->postInitProcess();
3155+
3156+
UniValue obj(UniValue::VOBJ);
3157+
obj.pushKV("name", wallet->GetName());
3158+
obj.pushKV("warning", warning);
3159+
3160+
return obj;
3161+
}
3162+
31173163
static UniValue resendwallettransactions(const JSONRPCRequest& request)
31183164
{
31193165
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
@@ -4315,6 +4361,7 @@ static const CRPCCommand commands[] =
43154361
{ "hidden", "addwitnessaddress", &addwitnessaddress, {"address","p2sh"} },
43164362
{ "wallet", "backupwallet", &backupwallet, {"destination"} },
43174363
{ "wallet", "bumpfee", &bumpfee, {"txid", "options"} },
4364+
{ "wallet", "createwallet", &createwallet, {"filename"} },
43184365
{ "wallet", "dumpprivkey", &dumpprivkey, {"address"} },
43194366
{ "wallet", "dumpwallet", &dumpwallet, {"filename"} },
43204367
{ "wallet", "encryptwallet", &encryptwallet, {"passphrase"} },

0 commit comments

Comments
 (0)