Skip to content

Commit c87e0b8

Browse files
committed
wallet, rpc: Add exportwatchonlywallet RPC
1 parent 8eadc7c commit c87e0b8

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
@@ -934,6 +934,46 @@ static RPCHelpMan createwalletdescriptor()
934934
};
935935
}
936936

937+
static RPCHelpMan exportwatchonlywallet()
938+
{
939+
return RPCHelpMan{"exportwatchonlywallet",
940+
"Creates a wallet file at the specified path and name containing a watchonly version "
941+
"of the wallet. This watchonly wallet contains the wallet's public descriptors, "
942+
"its transactions, and address book data. The watchonly wallet can be imported to "
943+
"another node using 'restorewallet'.",
944+
{
945+
{"destination", RPCArg::Type::STR, RPCArg::Optional::NO, "The path to the filename the exported watchonly wallet will be saved to"},
946+
},
947+
RPCResult{
948+
RPCResult::Type::OBJ, "", "",
949+
{
950+
{RPCResult::Type::STR, "exported_file", "The full path that the file has been exported to"},
951+
},
952+
},
953+
RPCExamples{
954+
HelpExampleCli("exportwatchonlywallet", "\"home\\user\\\"")
955+
+ HelpExampleRpc("exportwatchonlywallet", "\"home\\user\\\"")
956+
},
957+
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
958+
{
959+
std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
960+
if (!pwallet) return UniValue::VNULL;
961+
WalletContext& context = EnsureWalletContext(request.context);
962+
963+
std::string dest = request.params[0].get_str();
964+
965+
LOCK(pwallet->cs_wallet);
966+
util::Result<std::string> exported = pwallet->ExportWatchOnlyWallet(fs::PathFromString(dest), context);
967+
if (!exported) {
968+
throw JSONRPCError(RPC_WALLET_ERROR, util::ErrorString(exported).original);
969+
}
970+
UniValue out{UniValue::VOBJ};
971+
out.pushKV("exported_file", *exported);
972+
return out;
973+
}
974+
};
975+
}
976+
937977
// addresses
938978
RPCHelpMan getaddressinfo();
939979
RPCHelpMan getnewaddress();
@@ -1010,6 +1050,7 @@ std::span<const CRPCCommand> GetWalletRPCCommands()
10101050
{"wallet", &createwalletdescriptor},
10111051
{"wallet", &restorewallet},
10121052
{"wallet", &encryptwallet},
1053+
{"wallet", &exportwatchonlywallet},
10131054
{"wallet", &getaddressesbylabel},
10141055
{"wallet", &getaddressinfo},
10151056
{"wallet", &getbalance},

0 commit comments

Comments
 (0)