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.
This commit is contained in:
Ava Chow
2024-01-29 17:32:02 -05:00
parent 87ec923d3a
commit 5fe4c66462
2 changed files with 17 additions and 7 deletions

View File

@@ -197,20 +197,26 @@ constexpr XOnlyPubKey XOnlyPubKey::NUMS_H{
[]() consteval { return XOnlyPubKey{"50929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0"_hex_u8}; }(),
};
std::vector<CKeyID> XOnlyPubKey::GetKeyIDs() const
std::vector<CPubKey> XOnlyPubKey::GetCPubKeys() const
{
std::vector<CKeyID> 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<CPubKey> 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<CKeyID> XOnlyPubKey::GetKeyIDs() const
{
std::vector<CKeyID> out;
for (const CPubKey& pk : GetCPubKeys()) {
out.push_back(pk.GetID());
}
return out;
}

View File

@@ -283,9 +283,13 @@ public:
std::optional<std::pair<XOnlyPubKey, bool>> 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<CKeyID> GetKeyIDs() const;
/** Returns this XOnlyPubKey with 0x02 and 0x03 prefixes */
std::vector<CPubKey> GetCPubKeys() const;
CPubKey GetEvenCorrespondingCPubKey() const;