@@ -786,4 +786,48 @@ RPCHelpMan walletdisplayaddress()
786786 };
787787}
788788#endif // ENABLE_EXTERNAL_SIGNER
789+
790+ RPCHelpMan getxpub ()
791+ {
792+ return RPCHelpMan{" getxpub" ,
793+ " Returns the xpub most recently used to generate descriptors for this descriptor wallet. "
794+ " Not entirely useful right now as it returns the xpub of the root, and there are "
795+ " hardened derivation steps involved in normal key derivation.\n " ,
796+ {},
797+ RPCResult{
798+ RPCResult::Type::OBJ, " " , " " ,
799+ {
800+ {
801+ {RPCResult::Type::STR, " xpub" , " The xpub" },
802+ }},
803+ },
804+ RPCExamples{
805+ HelpExampleCli (" getxpub" , " " )
806+ + HelpExampleRpc (" getxpub" , " " )
807+ },
808+ [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
809+ {
810+ const std::shared_ptr<CWallet> pwallet = GetWalletForJSONRPCRequest (request);
811+ if (!pwallet) return NullUniValue;
812+
813+ if (!pwallet->IsWalletFlagSet (WALLET_FLAG_DESCRIPTORS)) {
814+ throw JSONRPCError (RPC_WALLET_ERROR, " getxpub is not available for non-descriptor wallets" );
815+ }
816+
817+ const KeyManager& keyman = pwallet->GetKeyManager ();
818+ LOCK2 (pwallet->cs_wallet , keyman.cs_keyman );
819+
820+ std::optional<CExtPubKey> extpub = keyman.GetActiveHDPubKey ();
821+ if (extpub == std::nullopt ) {
822+ throw JSONRPCError (RPC_WALLET_ERROR, " This wallet does not have an active xpub" );
823+ }
824+ std::string xpub = EncodeExtPubKey (*extpub);
825+
826+ UniValue obj (UniValue::VOBJ);
827+ obj.pushKV (" xpub" , xpub);
828+
829+ return obj;
830+ },
831+ };
832+ }
789833} // namespace wallet
0 commit comments