Skip to content

Commit

Permalink
rpc: Add getxpub command
Browse files Browse the repository at this point in the history
  • Loading branch information
achow101 committed Nov 13, 2021
1 parent e1ac81f commit 709a917
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/wallet/keyman.cpp
Expand Up @@ -74,6 +74,14 @@ std::optional<CExtKey> KeyManager::GetActiveHDKey() const
return master_key;
}

std::optional<CExtPubKey> KeyManager::GetActiveHDPubKey() const
{
if (!m_active_xpub.pubkey.IsValid()) {
return std::nullopt;
}
return m_active_xpub;
}

bool KeyManager::AddKeyInner(WalletBatch& batch, const CKey& key, const CPubKey& pubkey)
{
AssertLockHeld(cs_keyman);
Expand Down
1 change: 1 addition & 0 deletions src/wallet/keyman.h
Expand Up @@ -43,6 +43,7 @@ class KeyManager

void GenerateAndSetHDKey();
std::optional<CExtKey> GetActiveHDKey() const EXCLUSIVE_LOCKS_REQUIRED(cs_keyman);
std::optional<CExtPubKey> GetActiveHDPubKey() const EXCLUSIVE_LOCKS_REQUIRED(cs_keyman);
void SetActiveHDKey(const CExtPubKey& extpub);
void LoadActiveHDKey(const CExtPubKey& extpub);

Expand Down
45 changes: 45 additions & 0 deletions src/wallet/rpcwallet.cpp
Expand Up @@ -4839,6 +4839,50 @@ static RPCHelpMan walletdisplayaddress()
}
#endif // ENABLE_EXTERNAL_SIGNER

static RPCHelpMan getxpub()
{
return RPCHelpMan{"getxpub",
"Returns the xpub most recently used to generate descriptors for this descriptor wallet. "
"Not entirely useful right now as it returns the xpub of the root, and there are "
"hardened derivation steps involved in normal key derivation.\n",
{},
RPCResult{
RPCResult::Type::OBJ, "", "",
{
{
{RPCResult::Type::STR, "xpub", "The xpub"},
}},
},
RPCExamples{
HelpExampleCli("getxpub", "")
+ HelpExampleRpc("getxpub", "")
},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
const std::shared_ptr<CWallet> pwallet = GetWalletForJSONRPCRequest(request);
if (!pwallet) return NullUniValue;

if (!pwallet->IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS)) {
throw JSONRPCError(RPC_WALLET_ERROR, "getxpub is not available for non-descriptor wallets");
}

const KeyManager& keyman = pwallet->GetKeyManager();
LOCK2(pwallet->cs_wallet, keyman.cs_keyman);

std::optional<CExtPubKey> extpub = keyman.GetActiveHDPubKey();
if (extpub == std::nullopt) {
throw JSONRPCError(RPC_WALLET_ERROR, "This wallet does not have an active xpub");
}
std::string xpub = EncodeExtPubKey(*extpub);

UniValue obj(UniValue::VOBJ);
obj.pushKV("xpub", xpub);

return obj;
},
};
}

RPCHelpMan abortrescan();
RPCHelpMan dumpprivkey();
RPCHelpMan importprivkey();
Expand Down Expand Up @@ -4880,6 +4924,7 @@ static const CRPCCommand commands[] =
{ "wallet", &gettransaction, },
{ "wallet", &getunconfirmedbalance, },
{ "wallet", &getbalances, },
{ "wallet", &getxpub, },
{ "wallet", &getwalletinfo, },
{ "wallet", &importaddress, },
{ "wallet", &importdescriptors, },
Expand Down

0 comments on commit 709a917

Please sign in to comment.