Skip to content

Commit

Permalink
Merge bitcoin/bitcoin#24343: Add descriptor_tests covering tr(), and …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
fanquake committed Feb 21, 2022
2 parents cf22191 + 0683f37 commit 72f9728
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 15 deletions.
12 changes: 10 additions & 2 deletions src/script/descriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,15 @@ class ConstPubkeyProvider final : public PubkeyProvider
bool ToPrivateString(const SigningProvider& arg, std::string& ret) const override
{
CKey key;
if (!arg.GetKey(m_pubkey.GetID(), key)) return false;
if (m_xonly) {
for (const auto& keyid : XOnlyPubKey(m_pubkey).GetKeyIDs()) {
arg.GetKey(keyid, key);
if (key.IsValid()) break;
}
} else {
arg.GetKey(m_pubkey.GetID(), key);
}
if (!key.IsValid()) return false;
ret = EncodeSecret(key);
return true;
}
Expand Down Expand Up @@ -1253,7 +1261,7 @@ std::unique_ptr<DescriptorImpl> InferScript(const CScript& script, ParseScriptCo
{
if (ctx == ParseScriptContext::P2TR && script.size() == 34 && script[0] == 32 && script[33] == OP_CHECKSIG) {
XOnlyPubKey key{Span{script}.subspan(1, 32)};
return std::make_unique<PKDescriptor>(InferXOnlyPubkey(key, ctx, provider));
return std::make_unique<PKDescriptor>(InferXOnlyPubkey(key, ctx, provider), true);
}

std::vector<std::vector<unsigned char>> data;
Expand Down
Loading

0 comments on commit 72f9728

Please sign in to comment.