From fcb6f13f442d6a3f27689a87e3ed2bb9b431a332 Mon Sep 17 00:00:00 2001 From: Antoine Poinsot Date: Tue, 14 Feb 2023 10:31:32 +0100 Subject: [PATCH] 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. --- src/pubkey.cpp | 7 +++++++ src/pubkey.h | 2 ++ src/script/descriptor.cpp | 4 +--- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/pubkey.cpp b/src/pubkey.cpp index 05808e4c227..11e1b4abb51 100644 --- a/src/pubkey.cpp +++ b/src/pubkey.cpp @@ -204,6 +204,13 @@ std::vector XOnlyPubKey::GetKeyIDs() const return out; } +CPubKey XOnlyPubKey::GetEvenCorrespondingCPubKey() const +{ + unsigned char full_key[CPubKey::COMPRESSED_SIZE] = {0x02}; + std::copy(begin(), end(), full_key + 1); + return CPubKey{full_key}; +} + bool XOnlyPubKey::IsFullyValid() const { secp256k1_xonly_pubkey pubkey; diff --git a/src/pubkey.h b/src/pubkey.h index 4b34fd829bc..2b655c3f73b 100644 --- a/src/pubkey.h +++ b/src/pubkey.h @@ -282,6 +282,8 @@ public: */ std::vector GetKeyIDs() const; + CPubKey GetEvenCorrespondingCPubKey() const; + const unsigned char& operator[](int pos) const { return *(m_keydata.begin() + pos); } const unsigned char* data() const { return m_keydata.begin(); } static constexpr size_t size() { return decltype(m_keydata)::size(); } diff --git a/src/script/descriptor.cpp b/src/script/descriptor.cpp index eaef481c51b..22177504fc1 100644 --- a/src/script/descriptor.cpp +++ b/src/script/descriptor.cpp @@ -1401,9 +1401,7 @@ std::unique_ptr InferPubkey(const CPubKey& pubkey, ParseScriptCo std::unique_ptr InferXOnlyPubkey(const XOnlyPubKey& xkey, ParseScriptContext ctx, const SigningProvider& provider) { - unsigned char full_key[CPubKey::COMPRESSED_SIZE] = {0x02}; - std::copy(xkey.begin(), xkey.end(), full_key + 1); - CPubKey pubkey(full_key); + CPubKey pubkey{xkey.GetEvenCorrespondingCPubKey()}; std::unique_ptr key_provider = std::make_unique(0, pubkey, true); KeyOriginInfo info; if (provider.GetKeyOriginByXOnly(xkey, info)) {