From 5fe4c66462e6149c2ed3ce24224a7a7b328a2cfa Mon Sep 17 00:00:00 2001 From: Ava Chow Date: Mon, 29 Jan 2024 17:32:02 -0500 Subject: [PATCH] XOnlyPubKey: Add GetCPubKeys We need to retrieve the even and odd compressed pubkeys for xonly pubkeys, so add a function to do that. Also reuse it in GetKeyIDs. --- src/pubkey.cpp | 20 +++++++++++++------- src/pubkey.h | 4 ++++ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/pubkey.cpp b/src/pubkey.cpp index a4ca9a170a9..6041c89e7f1 100644 --- a/src/pubkey.cpp +++ b/src/pubkey.cpp @@ -197,20 +197,26 @@ constexpr XOnlyPubKey XOnlyPubKey::NUMS_H{ []() consteval { return XOnlyPubKey{"50929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0"_hex_u8}; }(), }; -std::vector XOnlyPubKey::GetKeyIDs() const +std::vector XOnlyPubKey::GetCPubKeys() const { - std::vector out; - // For now, use the old full pubkey-based key derivation logic. As it is indexed by - // Hash160(full pubkey), we need to return both a version prefixed with 0x02, and one - // with 0x03. + std::vector out; unsigned char b[33] = {0x02}; std::copy(m_keydata.begin(), m_keydata.end(), b + 1); CPubKey fullpubkey; fullpubkey.Set(b, b + 33); - out.push_back(fullpubkey.GetID()); + out.push_back(fullpubkey); b[0] = 0x03; fullpubkey.Set(b, b + 33); - out.push_back(fullpubkey.GetID()); + out.push_back(fullpubkey); + return out; +} + +std::vector XOnlyPubKey::GetKeyIDs() const +{ + std::vector out; + for (const CPubKey& pk : GetCPubKeys()) { + out.push_back(pk.GetID()); + } return out; } diff --git a/src/pubkey.h b/src/pubkey.h index cbc827dc606..442dc2d6431 100644 --- a/src/pubkey.h +++ b/src/pubkey.h @@ -283,9 +283,13 @@ public: std::optional> CreateTapTweak(const uint256* merkle_root) const; /** Returns a list of CKeyIDs for the CPubKeys that could have been used to create this XOnlyPubKey. + * As the CKeyID is the Hash160(full pubkey), the produced CKeyIDs are for the versions of this + * XOnlyPubKey with 0x02 and 0x03 prefixes. * This is needed for key lookups since keys are indexed by CKeyID. */ std::vector GetKeyIDs() const; + /** Returns this XOnlyPubKey with 0x02 and 0x03 prefixes */ + std::vector GetCPubKeys() const; CPubKey GetEvenCorrespondingCPubKey() const;