Skip to content

Conversation

sipa
Copy link
Member

@sipa sipa commented Feb 14, 2022

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.

@DrahtBot
Copy link
Contributor

DrahtBot commented Feb 15, 2022

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #24361 (Descriptor unit tests and simplifications by sipa)
  • #22838 (descriptors: Be able to specify change and receiving in a single descriptor string by achow101)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@sipa sipa force-pushed the 202202_trunittests branch from 5e093e9 to 9f5c783 Compare February 15, 2022 16:58
@sipa sipa changed the title Add descriptor_tests covering tr(), and fix infer bug Add descriptor_tests covering tr(), and fix minor bugs Feb 15, 2022
@achow101
Copy link
Member

ACK 9f5c783

@sipa sipa force-pushed the 202202_trunittests branch from 9f5c783 to 0683f37 Compare February 15, 2022 23:35
@sipa
Copy link
Member Author

sipa commented Feb 15, 2022

Improved the new tests a bit, adding a case with multiple tree branches, and signing with mixed pub/priv descriptors.

@laanwj
Copy link
Member

laanwj commented Feb 17, 2022

Added to 23.0 milestone because bugfix.

@achow101
Copy link
Member

ACK 0683f37

Copy link
Member

@instagibbs instagibbs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 0683f37

Copy link
Member

@jonatack jonatack left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review ACK 0683f37

A couple of nitty suggestions in the comments, feel free to ignore.

@@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

18ad54c

Suggested change
return std::make_unique<PKDescriptor>(InferXOnlyPubkey(key, ctx, provider), true);
return std::make_unique<PKDescriptor>(InferXOnlyPubkey(key, ctx, provider), /*xonly=*/true);

Verified descriptor_tests fail without this commit:
test/descriptor_tests.cpp(327): error: in "descriptor_tests/descriptor_test": check spks_inferred[0] == spks[n] has failed

std::vector<CTxOut> utxos(1);
PrecomputedTransactionData txdata;
txdata.Init(spend, std::move(utxos), /* force */ true);
MutableTransactionSignatureCreator creator(&spend, 0, CAmount{0}, &txdata, SIGHASH_DEFAULT);
Copy link
Member

@jonatack jonatack Feb 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0683f37 clang-tidy compatible named args

-                    txdata.Init(spend, std::move(utxos), /* force */ true);
-                    MutableTransactionSignatureCreator creator(&spend, 0, CAmount{0}, &txdata, SIGHASH_DEFAULT);
+                    txdata.Init(spend, std::move(utxos), /*force=*/true);
+                    MutableTransactionSignatureCreator creator(&spend, /*input_idx=*/0, CAmount{0}, &txdata, SIGHASH_DEFAULT);

@fanquake fanquake merged commit 72f9728 into bitcoin:master Feb 21, 2022
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Feb 22, 2022
…r 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@0683f37
  jonatack:
    Code review ACK 0683f37

Tree-SHA512: fc0e11b45da53054a108effff2029d67b64e508b160a6e22e00c98b506c39ec12ccc95afd21ea68a6c691eb62930afc7af18908f2fa3a954d102afdc67bc355a
@michaelfolkson
Copy link

Are you going to open a PR with your nits @jonatack? Maybe should be included in 23.0 milestone if you are?

@jonatack
Copy link
Member

@michaelfolkson no, if the author considers it worthwhile they could be done in a follow-up.

@michaelfolkson
Copy link

@jonatack: Ok will wait to see whether @sipa thinks the nits are worth it or not.

This was merged before #24433 but since that was merged the guidance is:

"You are expected to reply to any review comments before your pull request is merged. You may update the code or reject the feedback if you do not agree with it, but you should express so in a reply." :)

@jonatack
Copy link
Member

It's more likely I'd follow up for, say, a bug or missing tests.

@bitcoin bitcoin locked and limited conversation to collaborators Feb 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants