Skip to content

Commit 1a58ebf

Browse files
committed
wallet, rpc: Add exportwatchonlywallet RPC
1 parent c9df8c8 commit 1a58ebf

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

src/wallet/rpc/wallet.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -953,6 +953,46 @@ static RPCHelpMan createwalletdescriptor()
953953
};
954954
}
955955

956+
static RPCHelpMan exportwatchonlywallet()
957+
{
958+
return RPCHelpMan{"exportwatchonlywallet",
959+
"Creates a wallet file at the specified path and name containing a watchonly version "
960+
"of the wallet. This watchonly wallet contains the wallet's public descriptors, "
961+
"its transactions, and address book data. The watchonly wallet can be imported to "
962+
"another node using 'restorewallet'.",
963+
{
964+
{"destination", RPCArg::Type::STR, RPCArg::Optional::NO, "The path to the filename the exported watchonly wallet will be saved to"},
965+
},
966+
RPCResult{
967+
RPCResult::Type::OBJ, "", "",
968+
{
969+
{RPCResult::Type::STR, "exported_file", "The full path that the file has been exported to"},
970+
},
971+
},
972+
RPCExamples{
973+
HelpExampleCli("exportwatchonlywallet", "\"home\\user\\\"")
974+
+ HelpExampleRpc("exportwatchonlywallet", "\"home\\user\\\"")
975+
},
976+
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
977+
{
978+
std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
979+
if (!pwallet) return UniValue::VNULL;
980+
WalletContext& context = EnsureWalletContext(request.context);
981+
982+
std::string dest = request.params[0].get_str();
983+
984+
LOCK(pwallet->cs_wallet);
985+
util::Result<std::string> exported = pwallet->ExportWatchOnlyWallet(fs::PathFromString(dest), context);
986+
if (!exported) {
987+
throw JSONRPCError(RPC_WALLET_ERROR, util::ErrorString(exported).original);
988+
}
989+
UniValue out{UniValue::VOBJ};
990+
out.pushKV("exported_file", *exported);
991+
return out;
992+
}
993+
};
994+
}
995+
956996
// addresses
957997
RPCHelpMan getaddressinfo();
958998
RPCHelpMan getnewaddress();
@@ -1029,6 +1069,7 @@ std::span<const CRPCCommand> GetWalletRPCCommands()
10291069
{"wallet", &createwalletdescriptor},
10301070
{"wallet", &restorewallet},
10311071
{"wallet", &encryptwallet},
1072+
{"wallet", &exportwatchonlywallet},
10321073
{"wallet", &getaddressesbylabel},
10331074
{"wallet", &getaddressinfo},
10341075
{"wallet", &getbalance},

0 commit comments

Comments
 (0)