Skip to content

Commit fcb6f13

Browse files
committed
pubkey: introduce a GetEvenCorrespondingCPubKey helper
We'll need to get a compressed key out of an x-only one in other places. Avoid duplicating the code.
1 parent ce8845f commit fcb6f13

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

src/pubkey.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,13 @@ std::vector<CKeyID> XOnlyPubKey::GetKeyIDs() const
204204
return out;
205205
}
206206

207+
CPubKey XOnlyPubKey::GetEvenCorrespondingCPubKey() const
208+
{
209+
unsigned char full_key[CPubKey::COMPRESSED_SIZE] = {0x02};
210+
std::copy(begin(), end(), full_key + 1);
211+
return CPubKey{full_key};
212+
}
213+
207214
bool XOnlyPubKey::IsFullyValid() const
208215
{
209216
secp256k1_xonly_pubkey pubkey;

src/pubkey.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,8 @@ class XOnlyPubKey
282282
*/
283283
std::vector<CKeyID> GetKeyIDs() const;
284284

285+
CPubKey GetEvenCorrespondingCPubKey() const;
286+
285287
const unsigned char& operator[](int pos) const { return *(m_keydata.begin() + pos); }
286288
const unsigned char* data() const { return m_keydata.begin(); }
287289
static constexpr size_t size() { return decltype(m_keydata)::size(); }

src/script/descriptor.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,9 +1401,7 @@ std::unique_ptr<PubkeyProvider> InferPubkey(const CPubKey& pubkey, ParseScriptCo
14011401

14021402
std::unique_ptr<PubkeyProvider> InferXOnlyPubkey(const XOnlyPubKey& xkey, ParseScriptContext ctx, const SigningProvider& provider)
14031403
{
1404-
unsigned char full_key[CPubKey::COMPRESSED_SIZE] = {0x02};
1405-
std::copy(xkey.begin(), xkey.end(), full_key + 1);
1406-
CPubKey pubkey(full_key);
1404+
CPubKey pubkey{xkey.GetEvenCorrespondingCPubKey()};
14071405
std::unique_ptr<PubkeyProvider> key_provider = std::make_unique<ConstPubkeyProvider>(0, pubkey, true);
14081406
KeyOriginInfo info;
14091407
if (provider.GetKeyOriginByXOnly(xkey, info)) {

0 commit comments

Comments
 (0)