Skip to content

descriptor: Reconstruct a multipath descriptor from its expansion - #36443

Draft
achow101 wants to merge 12 commits into
bitcoin:masterfrom
achow101:multipath-reconstruction
Draft

achow101 wants to merge 12 commits into
bitcoin:masterfrom
achow101:multipath-reconstruction

Conversation

@achow101

@achow101 achow101 commented Oct 5, 2026 •

Copy link
Copy Markdown
Member

#36442 allows multipath descriptors to be represented in a single Descriptor object. GetMultipathExpansion returns the multiple descriptors represented by a multipath descriptor. This expansion is reversible - given the expansion in the order of the multipath elements, it is possible to determine what the original multipath descriptor was. This PR adds ReconstructMultipath to produce a new Descriptor representing the multipath descriptor that produced the expansion.

Depends on #36442

@DrahtBot

DrahtBot commented Oct 5, 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
Concept ACK w0xlt, rkrux

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)
  • #36820 (refactor: remove more dead code by fanquake)
  • #36648 (descriptor, rpc, wallet: do not echo key material in error messages by fametrano)
  • #36122 (BIP460: CISA for Taproot key path spends by fjahr)
  • #36070 (wallet: Add deriveHDKey interface by PraneethGunas)
  • #36005 (rpc, descriptor: warn when multipath expansion clones single-path key expressions by pythcoiner)
  • #35918 (descriptor: Expose key expression parsing by pseudoramdom)
  • #35377 (wallet: Allow importing of descriptors without private keys when the wallet has the private keys by achow101)
  • #34909 (wallet, refactor: modularise wallet by extracting out legacy wallet migration by rkrux)

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.

LLM Linter (✨ experimental)

Possible typos and grammar issues:

  • src/util/bip32.h: for a give multipath position → for a given multipath position [misspelling]
  • src/test/descriptor_tests.cpp: Failed reconstrcted to private string → Failed to reconstruct private string [misspelling and unclear phrasing]
  • src/test/descriptor_tests.cpp: Failed reconstructed to private string → Failed to reconstruct private string [broken phrasing]

Possible places where named args for integral literals may be used (e.g. func(x, /*named_arg=*/0) in C++, and func(x, named_arg=0) in Python):

  • src/qt/test/wallettests.cpp: WalletDescriptor w_desc(std::move(desc), 0, 0, 1, 1)
  • src/script/descriptor.cpp: std::make_unique<ConstPubkeyProvider>(key_exp_index, pubkey, false)
  • src/script/descriptor.cpp: std::make_unique<ConstPubkeyProvider>(key_exp_index, pubkey, true)
  • src/test/fuzz/scriptpubkeyman.cpp: Parse(desc_str.value(), keys, error, false)
  • src/wallet/scriptpubkeyman.cpp: WalletDescriptor w_desc(std::move(desc), creation_time, 0, 0, 0)
  • src/wallet/scriptpubkeyman.cpp: Parse(desc_str, provider, error, false)
  • src/wallet/scriptpubkeyman.cpp: Parse(desc_str, parsed_keys, parse_error, false)
  • src/wallet/scriptpubkeyman.cpp: WalletDescriptor w_desc(std::move(desc), 0, 0, chain_counter, 0)
  • src/wallet/test/psbt_wallet_tests.cpp: WalletDescriptor w_desc(std::move(desc), 0, 0, 10, 0)
  • src/wallet/test/util.cpp: Parse(desc_str, keys, error, false)
  • src/wallet/test/wallet_tests.cpp: WalletDescriptor w_desc{std::move(desc), 0, 0, 0, 0}
  • src/wallet/wallet.cpp: Parse(desc_str, keys, desc_error, false)
  • src/wallet/walletutil.cpp: Parse(desc_str, keys, error, false)

2026-11-07 03:08:11

@DrahtBot

DrahtBot commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

🚧 At least one of the CI tasks failed.
Task macOS-cross to arm64: https://github.com/bitcoin/bitcoin/actions/runs/37381386585/job/112003972868
LLM reason (✨ experimental): CI failed to compile walletload_tests.cpp: Parse() returns a std::unique_ptr<Descriptor>, but the code assigns it to a vector of those pointers.

Hints

Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:

  • Possibly due to a silent merge conflict (the changes in this pull request being
    incompatible with the current code in the target branch). If so, make sure to rebase on the latest
    commit of the target branch.

  • A sanitizer issue, which can only be found by compiling with the sanitizer and running the
    affected test.

  • An intermittent issue.

Leave a comment here, if you need help tracking down a confusing failure.

@w0xlt

w0xlt commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

Concept ACK

Sjors added a commit to Sjors/bitcoin that referenced this pull request Oct 20, 2026
Apply the four reconstruction commits on top of the latest bitcoin#36442, keeping its newer predecessor implementation and the private-key substitution integration.
Sjors added a commit to Sjors/bitcoin that referenced this pull request Oct 20, 2026
Apply the wallet relationship commits onto the latest bitcoin#36442 and bitcoin#36443 implementations. Integrate known-key substitution and repair extracted functional-test setup.
@Sjors Sjors mentioned this pull request Oct 20, 2026
7 tasks
Sjors added a commit to Sjors/bitcoin that referenced this pull request Oct 20, 2026
Apply the wallet relationship commits onto the latest bitcoin#36442 and bitcoin#36443 implementations. Integrate known-key substitution and repair extracted functional-test setup.

Integration fixes carried by this merge:
- Fix Clang -Wthread-safety errors by locking each descriptor manager when
  reading its multipath descriptor or updating its relationships.
- Snapshot local keys, cache and relative IDs before acquiring relatives'
  locks, and retain shared descriptor ownership during reconstruction.
- Reconstruct exported multipath descriptors before taking the caller's
  descriptor lock, avoiding receive/change lock-order inversions.
Sjors added a commit to Sjors/bitcoin that referenced this pull request Oct 20, 2026
Apply the four reconstruction commits on top of the latest bitcoin#36442, keeping its newer predecessor implementation and the private-key substitution integration.

Include <variant> for the comparison operators used by multipath
reconstruction, satisfying IWYU 23 without relying on transitive includes.
Sjors added a commit to Sjors/bitcoin that referenced this pull request Oct 20, 2026
Apply the wallet relationship commits onto the latest bitcoin#36442 and bitcoin#36443 implementations. Integrate known-key substitution and repair extracted functional-test setup.

Integration fixes carried by this merge:
- Fix Clang -Wthread-safety errors by locking each descriptor manager when
  reading its multipath descriptor or updating its relationships.
- Snapshot local keys, cache and relative IDs before acquiring relatives'
  locks, and retain shared descriptor ownership during reconstruction.
- Reconstruct exported multipath descriptors before taking the caller's
  descriptor lock, avoiding receive/change lock-order inversions.
Sjors added a commit to Sjors/bitcoin that referenced this pull request Oct 21, 2026
Apply the four reconstruction commits on top of the latest bitcoin#36442, keeping its newer predecessor implementation and the private-key substitution integration.

Include <variant> for the comparison operators used by multipath
reconstruction, satisfying IWYU 23 without relying on transitive includes.
Sjors added a commit to Sjors/bitcoin that referenced this pull request Oct 21, 2026
Apply the wallet relationship commits onto the latest bitcoin#36442 and bitcoin#36443 implementations. Integrate known-key substitution and repair extracted functional-test setup.

Integration fixes carried by this merge:
- Fix Clang -Wthread-safety errors by locking each descriptor manager when
  reading its multipath descriptor or updating its relationships.
- Snapshot local keys, cache and relative IDs before acquiring relatives'
  locks, and retain shared descriptor ownership during reconstruction.
- Reconstruct exported multipath descriptors before taking the caller's
  descriptor lock, avoiding receive/change lock-order inversions.

@fametrano fametrano left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ReconstructMultipath accepts relatives that repeat a path value, and then returns a descriptor that is not their multipath form.

I tested it with a small harness against btclib-wallet, a BIP389 implementation I maintain. Its expansions of 24 multipath descriptors each came back as the original. Thirteen mismatched sets (different keys, function, threshold, timelock, key order, tree shape, origin, wildcard or range, two changed steps in one key, musig steps at both levels) return null, as they should.

Repeated values are not caught. wpkh(X/0/*), wpkh(X/1/*), wpkh(X/0/*) gives wpkh(X/<0;1;0>/*), which Parse refuses as a duplicate. wpkh(X/0/*) twice and then wpkh(X/1/*) gives wpkh(X/<0;1>/*): two elements for three inputs. Two identical inputs give the single-path descriptor back. In BIP32PubkeyProvider::ReconstructMultipath, each relative is compared with p after AddMultipathIndex has already extended it. So a repeat is skipped while p still holds one index, and appended once it holds more. Nothing checks the indexes already in p, or that the step ends with one index per input. The inline suggestions add both checks, with a test for the repeated cases, musig included. I can share the harness and its outputs.

Comment thread src/script/descriptor.cpp
return false;
}
multipath_pos = i;
p.AddMultipathIndex(rp.Index(0));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

A relative whose index is already in the step is a repeat, so reject it.

Suggested change
p.AddMultipathIndex(rp.Index(0));
if (std::ranges::find(p.Indexes(), rp.Index(0)) != p.Indexes().end()) return false;
p.AddMultipathIndex(rp.Index(0));

Comment thread src/script/descriptor.cpp
return false;
}
multipath_pos = i;
p.AddMultipathIndex(rp.Index(0));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Same check for the musig path.

Suggested change
p.AddMultipathIndex(rp.Index(0));
if (std::ranges::find(p.Indexes(), rp.Index(0)) != p.Indexes().end()) return false;
p.AddMultipathIndex(rp.Index(0));

Comment thread src/script/descriptor.cpp
}
}

return out;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Fail unless the step ends with one index per input. This also rejects identical inputs.

Suggested change
return out;
// Each relative must add one multipath index
if (!relatives.empty() && multipath_len != relatives.size() + 1) return nullptr;
return out;

CheckFailedMultipathReconstruction({
"tr(musig(xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL/0/*,xpub68NZiKmJWnxxS6aaHmn81bvJeTESw724CRDs6HbuccFQN9Ku14VQrADWgqbhhTHBaohPX4CjNLf9fq9MYo6oDaPPLPxSb7gwQN3ih19Zm4Y/1))",
"tr(musig(xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL/9/*h,xpub68NZiKmJWnxxS6aaHmn81bvJeTESw724CRDs6HbuccFQN9Ku14VQrADWgqbhhTHBaohPX4CjNLf9fq9MYo6oDaPPLPxSb7gwQN3ih19Zm4Y/1))",
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Add the repeated-value cases, with and without musig.

Suggested change
});
});
CheckFailedMultipathReconstruction({
"wpkh(xpub68NZiKmJWnxxS6aaHmn81bvJeTESw724CRDs6HbuccFQN9Ku14VQrADWgqbhhTHBaohPX4CjNLf9fq9MYo6oDaPPLPxSb7gwQN3ih19Zm4Y/0)",
"wpkh(xpub68NZiKmJWnxxS6aaHmn81bvJeTESw724CRDs6HbuccFQN9Ku14VQrADWgqbhhTHBaohPX4CjNLf9fq9MYo6oDaPPLPxSb7gwQN3ih19Zm4Y/1)",
"wpkh(xpub68NZiKmJWnxxS6aaHmn81bvJeTESw724CRDs6HbuccFQN9Ku14VQrADWgqbhhTHBaohPX4CjNLf9fq9MYo6oDaPPLPxSb7gwQN3ih19Zm4Y/0)",
});
CheckFailedMultipathReconstruction({
"wpkh(xpub68NZiKmJWnxxS6aaHmn81bvJeTESw724CRDs6HbuccFQN9Ku14VQrADWgqbhhTHBaohPX4CjNLf9fq9MYo6oDaPPLPxSb7gwQN3ih19Zm4Y/0)",
"wpkh(xpub68NZiKmJWnxxS6aaHmn81bvJeTESw724CRDs6HbuccFQN9Ku14VQrADWgqbhhTHBaohPX4CjNLf9fq9MYo6oDaPPLPxSb7gwQN3ih19Zm4Y/0)",
"wpkh(xpub68NZiKmJWnxxS6aaHmn81bvJeTESw724CRDs6HbuccFQN9Ku14VQrADWgqbhhTHBaohPX4CjNLf9fq9MYo6oDaPPLPxSb7gwQN3ih19Zm4Y/1)",
});
CheckFailedMultipathReconstruction({
"wpkh(xpub68NZiKmJWnxxS6aaHmn81bvJeTESw724CRDs6HbuccFQN9Ku14VQrADWgqbhhTHBaohPX4CjNLf9fq9MYo6oDaPPLPxSb7gwQN3ih19Zm4Y/0)",
"wpkh(xpub68NZiKmJWnxxS6aaHmn81bvJeTESw724CRDs6HbuccFQN9Ku14VQrADWgqbhhTHBaohPX4CjNLf9fq9MYo6oDaPPLPxSb7gwQN3ih19Zm4Y/0)",
});
CheckFailedMultipathReconstruction({
"tr(musig(xpub6ERApfZwUNrhLC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aHmn81bvJeTESw724CRDs6HbuccFQN9Ku14VQrADWgqbhhTHBaohPX4CjNLf9fq9MYo6oDaPPLPxSb7gwQN3ih19Zm4Y)/1)",
"tr(musig(xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL/0,xpub68NZiKmJWnxxS6aaHmn81bvJeTESw724CRDs6HbuccFQN9Ku14VQrADWgqbhhTHBaohPX4CjNLf9fq9MYo6oDaPPLPxSb7gwQN3ih19Zm4Y)/2)",
});

We will want to have member functions on KeyPath later, so make it a
class now.
Moves all of the multipath parsing into ParseHDKeypath, and let
KeyPathElement be a multipath element.
Internally, descriptor parsing will produce multipath descriptor objects
rather than std::vector<DescriptorImpl>. This single multipath
descriptor becomes std::vector<Descriptor> within Parse. Future commits
will return the single multipath descriptor object for all descriptor parsing.
Instead of a std::vector<std::unique_ptr<Descriptor>>, Parse can return
a single std::unique_ptr<Descriptor> that contains the multipath
information if the descriptor was multipath. Callers that
require the multipath expansion use GetMultipathExpansion() to get the
expanded descriptors.
@DrahtBot

Copy link
Copy Markdown
Contributor

🐙 This pull request conflicts with the target branch and needs rebase.

@rkrux

rkrux commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Concept ACK, seems like a natural follow-up of the precursor #36442.

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.

5 participants