Skip to content

Commit 72f9728

Browse files
committed
Merge bitcoin/bitcoin#24343: Add descriptor_tests covering tr(), and fix minor bugs
0683f37 Add tr() descriptor unit tests (Pieter Wuille) 4b2e31a Bugfix: make ToPrivateString work with x-only keys (Pieter Wuille) 18ad54c Bugfix: set x-only flag when inferring pk() inside tr() (Pieter Wuille) Pull request description: This fixes two bugs in the current logic for `tr()` descriptors: * ToPrivateString does not always work, because the provided private key may mismatch the parity of the x-only public key. * The descriptors inferred for `pk()` inside `tr()` have the wrong x-only flag, leading to such descriptors generating the wrong scriptPubKey (roundtripping through ToString does fix it however, so this seems unobservable in the current code). These were discovered while adding unit tests to descriptor_tests that cover various aspects of `tr()` descriptors, which are now also added here. ACKs for top commit: achow101: ACK 0683f37 instagibbs: ACK bitcoin/bitcoin@0683f37 jonatack: Code review ACK 0683f37 Tree-SHA512: fc0e11b45da53054a108effff2029d67b64e508b160a6e22e00c98b506c39ec12ccc95afd21ea68a6c691eb62930afc7af18908f2fa3a954d102afdc67bc355a
2 parents cf22191 + 0683f37 commit 72f9728

File tree

2 files changed

+72
-15
lines changed

2 files changed

+72
-15
lines changed

src/script/descriptor.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,15 @@ class ConstPubkeyProvider final : public PubkeyProvider
259259
bool ToPrivateString(const SigningProvider& arg, std::string& ret) const override
260260
{
261261
CKey key;
262-
if (!arg.GetKey(m_pubkey.GetID(), key)) return false;
262+
if (m_xonly) {
263+
for (const auto& keyid : XOnlyPubKey(m_pubkey).GetKeyIDs()) {
264+
arg.GetKey(keyid, key);
265+
if (key.IsValid()) break;
266+
}
267+
} else {
268+
arg.GetKey(m_pubkey.GetID(), key);
269+
}
270+
if (!key.IsValid()) return false;
263271
ret = EncodeSecret(key);
264272
return true;
265273
}
@@ -1253,7 +1261,7 @@ std::unique_ptr<DescriptorImpl> InferScript(const CScript& script, ParseScriptCo
12531261
{
12541262
if (ctx == ParseScriptContext::P2TR && script.size() == 34 && script[0] == 32 && script[33] == OP_CHECKSIG) {
12551263
XOnlyPubKey key{Span{script}.subspan(1, 32)};
1256-
return std::make_unique<PKDescriptor>(InferXOnlyPubkey(key, ctx, provider));
1264+
return std::make_unique<PKDescriptor>(InferXOnlyPubkey(key, ctx, provider), true);
12571265
}
12581266

12591267
std::vector<std::vector<unsigned char>> data;

0 commit comments

Comments
 (0)