Skip to content

descriptor: Expose key expression parsing - #35918

Open
pseudoramdom wants to merge 3 commits into
bitcoin:masterfrom
pseudoramdom:parse-key-expression-v2
Open

pseudoramdom wants to merge 3 commits into
bitcoin:masterfrom
pseudoramdom:parse-key-expression-v2

Conversation

@pseudoramdom

Copy link
Copy Markdown
Contributor

This PR exposes a utility function to parse and validate a standalone descriptor key expression.
This is particularly useful in GUI multisig setup flows where the UI accepts a pasted key expression. The GUI can use this parser to validate input, inspect the parsed key, and apply setup-specific policy i.e. reject private keys, uncompressed keys etc. (Multisig Wizard tracking issue: #35645)

PubkeyProvider is the existing internal representation of a parsed key expression. This PR exposes its interface in descriptor.h and adds a public ParsePubkey() overload that returns the providers produced by the existing parser.

Key changes

  • public overload uses P2TR key rules, permitting compressed, x-only, extended, and MuSig key expressions while rejecting uncompressed keys.
  • Add GetOriginInfo() for retrieving explicitly provided key origin information.

Also, while I'm here, I noticed that musig parser accepted trailing characters apart from /.

  • Add a fix & test to reject unexpected trailing characters after a musig() expression

@DrahtBot

DrahtBot commented Aug 6, 2026 •

Copy link
Copy Markdown
Contributor

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

External sites

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
ACK w0xlt, pablomartin4btc, polespinasa
Approach NACK rkrux
Stale ACK murchandamus

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36885 (wallet, rpc: Add analyzedescriptor for multisig setups by pablomartin4btc)
  • #36648 (descriptor, rpc, wallet: do not echo key material in error messages by fametrano)
  • #36154 (wallet: fill PSBT_GLOBAL_XPUB by jeanpablojp)
  • #36143 (descriptor: add CreateMultisigDescriptor() by rxbryan)
  • #36122 (BIP460: CISA for Taproot key path spends by fjahr)

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.

@w0xlt w0xlt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ACK 93600f9

Comment thread src/script/descriptor.cpp Resolved
@sedited

sedited commented Oct 14, 2026

Copy link
Copy Markdown
Contributor

Ping for rebase @pseudoramdom

@pseudoramdom
pseudoramdom force-pushed the parse-key-expression-v2 branch from 93600f9 to 17ddfdc Compare October 14, 2026 07:28
@pseudoramdom

Copy link
Copy Markdown
Contributor Author

Rebased & force-pushed to 17ddfdc. No other changes.

@fametrano

Copy link
Copy Markdown

The first commit, 7835e0e, fixes a bug I also found on v31.1: tr(musig(X)xyz) is accepted and xyz is dropped. The worse case is a typo such as tr(musig(X)0/*), missing the /, which v31.1 turns into a non-ranged tr(musig(X)) without an error. Since it is a parser fix and the rest of the PR adds a new API, would you split it into its own PR? It could then be reviewed and backported on its own.

@pseudoramdom
pseudoramdom requested a review from w0xlt October 20, 2026 17:56

@w0xlt w0xlt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

reACK 17ddfdc

@pablomartin4btc pablomartin4btc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Concept ACK

A couple of nits in commits:

  • In 2nd commit (c17a6ab) message body, it mentions "Mostly a move-only commit. Implementation of operator< is retained in descriptor.cpp" but there's no operator< in descriptor.cpp or descriptor.h. What remains in the .cpp are the implementations of HavePrivateKeys() and DetermineApostropheUse(). Perhaps a leftover of a previous version of this change?

  • In 3rd commit (17ddfdc), tiny nit: the title specifies ParsePubKey but the function name is ParsePubkey.

Please keep in mind that #36442 changes the internal ParsePubkey to return a single provider for multipath expressions, so the new public overload may need to follow, depending on which one gets merged first.

I left another couple of non-blocking nits inline.

Comment thread src/script/descriptor.h Resolved
Comment thread src/script/descriptor.h Resolved
@pseudoramdom

Copy link
Copy Markdown
Contributor Author

Force-pushed to b0f139d to address @pablomartin4btc's feedback. Cleaned up commit messages and the code comments.

@w0xlt w0xlt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

reACK b0f139d

@pablomartin4btc pablomartin4btc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ACK b0f139d

Thanks for addressing the nits.

I added some extra test cases for the new API in pablomartin4btc@ pr35918-tests (on top of b0f139d), feel free to cherry-pick any of them (non-blocking):

  • 9eea6bf parse_pubkey_origin_info: GetOriginInfo() on each multipath expansion, with a fingerprint and no path, on a constant key, and on a musig() whose participant has an origin.
  • 959f099 parse_pubkey_invalid_expressions: input a "paste a key" field could receive (empty, leading whitespace, a whole descriptor, musig(...)0/*) is rejected without adding private keys. It doesn't assert messages that quote the input, so it won't conflict with #36648.
  • 32e2bcf parse_pubkey_hardened_after_xpub: xpub/0h/* parses but CanSelfExpand() is false, so callers validating a cosigner key know what to check.

@murchandamus murchandamus left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Mostly note to self: I got through the first two commits so far.

crACK 086b0cd

Comment thread src/test/descriptor_tests.cpp Resolved

@polespinasa polespinasa left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ACK b0f139d

@rkrux rkrux left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Concept ACK but Approach NACK

The first commit 7a81907 descriptor: Reject trailing text after musig() should be a separate PR. It's a bug fix unrelated to the goal of this PR and can be reviewed, merged, and back-ported separately.

@pseudoramdom

Copy link
Copy Markdown
Contributor Author

The first commit 7a81907 descriptor: Reject trailing text after musig() should be a separate PR. It's a bug fix unrelated to the goal of this PR and can be reviewed, merged, and back-ported separately.

Thanks for the review. While I agree that the musig() fix stands on its own, I kept it here because it's in the same parser this PR exposes through the new ParsePubkey() overload. A key expression pasted into the GUI goes through this path, so musig(A,B)garbage would otherwise be accepted. So the fix is not unrelated.
If it should be backported, I'm happy to split it into its own PR.

Approach NACK

Apart from the first commit, is there any concern with the rest of the approach?

pseudoramdom added 3 commits December 2, 2026 15:07
The musig() parser split the input at the closing parenthesis without
validating the remaining text. This allowed unexpected characters after
the expression to be ignored.
Add a requirement for nonempty char after musig() to begin with '/'.
Expose the abstract interface by moving to descriptor.h to allow other
modules to consume parsed key expressions.

Mostly move-only. `HavePrivateKeys()` and `DetermineApostropheUse()`
now have their impl in descriptor.cpp.
Add an overload for parsing standalone key expressions using P2TR key
rules.
Also, expose explicitly provided origin information through GetOriginInfo(),
allowing callers to inspect the fingerprint and derivation path without
re-parsing the expression.
@pseudoramdom
pseudoramdom force-pushed the parse-key-expression-v2 branch from b0f139d to fdad1e4 Compare December 2, 2026 23:12
@pseudoramdom

Copy link
Copy Markdown
Contributor Author

Force-pushed to fdad1e4
Deduplicated the ParsePubkey() tests to use CheckParsePubkey()/CheckUnparsablePubkey() helpers, following the Check/CheckUnparsable pattern.

@w0xlt w0xlt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

reACK fdad1e4

@pablomartin4btc pablomartin4btc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

re-ACK fdad1e4

Since my last review, @achow101 feedback was addressed.

@polespinasa polespinasa left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

reACK fdad1e4

@rkrux rkrux left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Apart from the first commit, is there any concern with the rest of the approach?

Rest all is pretty straightforward.

CheckUnparsable("tr(musig(xprvA1RpRA33e1JQ7ifknakTFpgNXPmW2YvmhqLQYMmrj4xJXXWYpDPS3xz7iAxn8L39njGVyuoseXzU6rcxFLJ8HFsTjSyQbLYnMpCqE2VbFWc/<0;1>,xpub68NZiKmJWnxxS6aaHmn81bvJeTESw724CRDs6HbuccFQN9Ku14VQrADWgqbhhTHBaohPX4CjNLf9fq9MYo6oDaPPLPxSb7gwQN3ih19Zm4Y/<2;3>)/<3;4>)","tr(musig(xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL/<0;1>,xpub68NZiKmJWnxxS6aaHmn81bvJeTESw724CRDs6HbuccFQN9Ku14VQrADWgqbhhTHBaohPX4CjNLf9fq9MYo6oDaPPLPxSb7gwQN3ih19Zm4Y/<2;3>)/<3;4>)", "tr(): musig(): Cannot have multipath participant keys if musig() is also multipath");
CheckUnparsable("tr(musig()/0)", "tr(musig()/0)", "tr(): musig(): Must contain key expressions");
CheckUnparsable("tr(musig(xprvA1RpRA33e1JQ7ifknakTFpgNXPmW2YvmhqLQYMmrj4xJXXWYpDPS3xz7iAxn8L39njGVyuoseXzU6rcxFLJ8HFsTjSyQbLYnMpCqE2VbFWc/*,xpub68NZiKmJWnxxS6aaHmn81bvJeTESw724CRDs6HbuccFQN9Ku14VQrADWgqbhhTHBaohPX4CjNLf9fq9MYo6oDaPPLPxSb7gwQN3ih19Zm4Y/*)/0)","tr(musig(xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL/*,xpub68NZiKmJWnxxS6aaHmn81bvJeTESw724CRDs6HbuccFQN9Ku14VQrADWgqbhhTHBaohPX4CjNLf9fq9MYo6oDaPPLPxSb7gwQN3ih19Zm4Y/*)/0)", "tr(): musig(): Cannot have ranged participant keys if musig() also has derivation");
CheckUnparsable("","rawtr(musig(02f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9,03dff1d77f2a671c5f36183726db2341be58feae1da2deced843240f7b502ba659)unexpected_chars)","rawtr(): Unexpected characters after musig() expression");

@rkrux rkrux Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In 48ad4df "descriptor: Reject trailing text after musig()"

While this is fine, a more realistic test case would read more appropriately. Like the one mentioned in #35918 (comment).

+ CheckUnparsable("tr(musig(xprvA1RpRA33e1JQ7ifknakTFpgNXPmW2YvmhqLQYMmrj4xJXXWYpDPS3xz7iAxn8L39njGVyuoseXzU6rcxFLJ8HFsTjSyQbLYnMpCqE2VbFWc,xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi)0/*)","tr(musig(xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL,xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8)0/*)","tr(): Unexpected characters after musig() expression");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.