Skip to content

Commit 5f6cc8d

Browse files
committed
Add XOnlyPubKey::CreateTapTweak
1 parent 2fbfb1b commit 5f6cc8d

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/pubkey.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,24 @@ bool XOnlyPubKey::CheckTapTweak(const XOnlyPubKey& internal, const uint256& merk
209209
return secp256k1_xonly_pubkey_tweak_add_check(secp256k1_context_verify, m_keydata.begin(), parity, &internal_key, tweak.begin());
210210
}
211211

212+
std::optional<std::pair<XOnlyPubKey, bool>> XOnlyPubKey::CreateTapTweak(const uint256* merkle_root) const
213+
{
214+
secp256k1_xonly_pubkey base_point;
215+
if (!secp256k1_xonly_pubkey_parse(secp256k1_context_verify, &base_point, data())) return std::nullopt;
216+
secp256k1_pubkey out;
217+
uint256 tweak = ComputeTapTweakHash(merkle_root);
218+
if (!secp256k1_xonly_pubkey_tweak_add(secp256k1_context_verify, &out, &base_point, tweak.data())) return std::nullopt;
219+
int parity = -1;
220+
std::pair<XOnlyPubKey, bool> ret;
221+
secp256k1_xonly_pubkey out_xonly;
222+
if (!secp256k1_xonly_pubkey_from_pubkey(secp256k1_context_verify, &out_xonly, &parity, &out)) return std::nullopt;
223+
secp256k1_xonly_pubkey_serialize(secp256k1_context_verify, ret.first.begin(), &out_xonly);
224+
assert(parity == 0 || parity == 1);
225+
ret.second = parity;
226+
return ret;
227+
}
228+
229+
212230
bool CPubKey::Verify(const uint256 &hash, const std::vector<unsigned char>& vchSig) const {
213231
if (!IsValid())
214232
return false;

src/pubkey.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <uint256.h>
1414

1515
#include <cstring>
16+
#include <optional>
1617
#include <vector>
1718

1819
const unsigned int BIP32_EXTKEY_SIZE = 74;
@@ -251,6 +252,9 @@ class XOnlyPubKey
251252
* Merkle root, and parity. */
252253
bool CheckTapTweak(const XOnlyPubKey& internal, const uint256& merkle_root, bool parity) const;
253254

255+
/** Construct a Taproot tweaked output point with this point as internal key. */
256+
std::optional<std::pair<XOnlyPubKey, bool>> CreateTapTweak(const uint256* merkle_root) const;
257+
254258
const unsigned char& operator[](int pos) const { return *(m_keydata.begin() + pos); }
255259
const unsigned char* data() const { return m_keydata.begin(); }
256260
static constexpr size_t size() { return decltype(m_keydata)::size(); }

0 commit comments

Comments
 (0)