Repository navigation
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. Code Coverage & BenchmarksFor details see: https://corecheck.dev/bitcoin/bitcoin/pulls/36133. ReviewsSee the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste ConflictsReviewers, this pull request conflicts with the following ones:
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. |
There was a problem hiding this comment.
Concept ACK
The matching approaches in #36075 work off descriptors a wallet already has, and storing a record instead means wallets created before this get none on their own.
Those get no record, and createwalletdescriptor on a pair that exists just answers Descriptor already exists. You can still rebuild one by hand, dumping the private descriptor, turning /0/* into /<0;1>/* and re-importing, which stores exactly what the new code would have. That needs the xprv though, since the public form is rejected on a wallet with private keys. Is that the intended path for existing wallets?
| return DBErrors::CORRUPT; | ||
| } | ||
| } | ||
| pwallet->LoadMultipathDescriptor(std::move(multipath_desc)); |
There was a problem hiding this comment.
The two checks above reject a record whose id doesn't match its own string, and one that names a descriptor the wallet doesn't have. The string itself is never checked against those descriptors, so a record saying wpkh(record/<0;1>/*) next to an unrelated descriptor loads fine and both RPCs serve it. Is this deliberate?
There was a problem hiding this comment.
IIUC you're asking if we should Parse() the multipath string at load time and verify that it expands as expected. That's more heavy-lifting than we typically do at wallet load.
It could make sense to introduce a bitcoin-wallet command to perform a more thorough integrity check..
| AssertLockHeld(cs_wallet); | ||
| for (const auto& [id, multipath_desc] : m_multipath_descriptors) { | ||
| if (std::find(multipath_desc.desc_ids.begin(), multipath_desc.desc_ids.end(), desc_id) != multipath_desc.desc_ids.end()) { | ||
| return multipath_desc.descriptor; |
There was a problem hiding this comment.
<0;1> and <0;2> are different policies that share their /0/* expansion, so one descriptor can sit in two records and this hands back whichever the map reaches first. Would it be worth returning all of them, or refusing the ambiguity?
There was a problem hiding this comment.
It doesn't seem realistic for someone to import a <0;1> and <0;2> descriptor, so I'm not too worried about supporting that. Is there a more realistic scenario?
There was a problem hiding this comment.
Fair enough, I don't have a more realistic one. The idea was to avoid an arbitrary pick, but I take the point that it's over-protective for a case nobody hits.
There was a problem hiding this comment.
This is now prevented. But what you can do is import a multipath descriptor into a pre-existing wallet that lacked it; this then adds the record. Not a documented feature, but it's tested.
| // exists and if this call created at least one of its members | ||
| const bool pair_complete{GetScriptPubKeyMan(receive_id) && GetScriptPubKeyMan(change_id)}; | ||
| if (!new_descs.empty() && pair_complete) { | ||
| AddMultipathDescriptor(batch, MultipathDescriptorRecord(std::move(*multipath_normalized), {receive_id, change_id})); |
There was a problem hiding this comment.
The other two callers check this return, one warns and the other errors out. Dropped here, a failed write leaves the wallet created and reporting success with no record, and the transaction wrapping wallet setup never learns of it. Is this intended?
There was a problem hiding this comment.
Added the missing handling and a [[nodiscard]].
| multipath_str.replace(pos, kr.text.size(), kr.replacement); | ||
| next_pos = pos; | ||
| } | ||
| *multipath = AddChecksum(multipath_str); |
There was a problem hiding this comment.
The origin comes through as typed here when the key is an xpub, and normalized when it's the xprv, so desc and multipath can end up disagreeing on 84h versus 84' for the same key. Is this intended?
There was a problem hiding this comment.
Good catch, fixed multipath normalization to always uses h.
Additionally, 620bd8f now uses h when expanding multipath, so the expanded descriptors are consistent with the parent.
Correct, I'll clarify in the description that this doesn't "upgrade" existing wallets. I think that's fine, because any multisig wallet using BIP388 registration is going to be new - since we don't support it yet.
See #35377. |
0948b92 to
912bf55
Compare
|
🚧 At least one of the CI tasks failed. HintsTry to run the tests locally, according to the documentation. However, a CI failure may still
Leave a comment here, if you need help tracking down a confusing failure. |
|
Concept NACKish My suggestion was not to store an extra record, but rather an extra field in Additionally, multipath descriptors do not inherently have a concept of receive and change descriptors. They are not limited to 2 descriptors once expanded. There should be no mention of "receive" or "change" when discussing multipath descriptors in the code. |
912bf55 to
2bd57be
Compare
|
IIUC you're objecting to two separate things:
Given that we know the actual intended multipath descriptor at import (and wallet creation) time, it seems better to store it then. But doing so is indeed more involved, and storing the multipath becomes pointless if we do (1).
The meat of the The easiest implementation is just a regex. But having a mix of parser strategies in the already hard to understand Descriptor class, does not sound appealing. My approach tries to stick to the existing recursive parser design.
So we give a multipath descriptor to Parse, it gets split apart and then we reconstruct it? It feels wrong to throw away information this way, and essentially treat I tried the approach in 2026/09/multipath-reconstruct, commit
Indeed, the Descriptor class should be agnostic, so I adjusted Also fixed the CI fuzzer failure. |
2bd57be to
1cdbfec
Compare
|
🚧 At least one of the CI tasks failed. HintsTry to run the tests locally, according to the documentation. However, a CI failure may still
Leave a comment here, if you need help tracking down a confusing failure. |
Even if parse could produce multipath descriptor strings, that doesn't help. We need to go from individual separated descriptor objects (not strings) to a multipath string. The wallet must not use multipath strings internally, it must not make any assumptions about the purposes of derivation paths.
What I have in mind is that given multiple descriptor objects (not strings), you can walk them in simultaneously (a la |
This lets the next commit record key positions in the descriptor string while its miniscript expression is parsed in place.
Use it to check whether parsing expands a descriptor into multiple descriptor.
The descriptors that a multipath descriptor expands to are new, derived descriptors rather than user input, so give them canonical form. When a multipath descriptor contains an apostrophe, replace them all with 'h' and parse again.
Use util::Expected for ParsePubkeyInner, ParsePubkey, and ParseScript, so errors are returned directly instead of passed through an output argument. The only behavior change is that a preexisting error string is cleared when nested descriptor parsing fails without producing an error message. Previously, the stale error was left intact.
Group the key expression index and signing provider shared by ParsePubkeyInner, ParsePubkey, and ParseScript in ParseState. A later commit extends this state with key replacement collection.
Add an optional Parse() output for the unexpanded multipath descriptor. This is always a public descriptor, so ParseState optionally collects KeyReplacement entries tracking key locations in the input for private-to-public conversion. The replacement sink is null unless the caller requests this output.
Split finding the last hardened derivation step out of BIP32PubkeyProvider::ToNormalizedString. No behavior change. A later commit reuses it when normalizing the keys of a multipath descriptor string.
Split formatting the normalized public form of a derived key, i.e. its origin followed by the extended public key at the last hardened step, out of BIP32PubkeyProvider::ToNormalizedString. No behavior change. A later commit reuses it when normalizing the keys of a multipath descriptor string.
Split merging an outer key origin into an already normalized key expression out of OriginPubkeyProvider::ToNormalizedString. No behavior change. A later commit reuses it when normalizing the keys of a multipath descriptor string.
Private extended keys followed by a fixed hardened prefix are now replaced by their key origin and the extended public key at the last hardened step, merging any explicit origin. This matches Descriptor::ToNormalizedString and produces BIP 388 wallet policy key expressions suitable for registering the multipath descriptor on a signing device. A hardened multipath element or wildcard cannot be represented by a single publicly derivable multipath descriptor. Leave the Parse multipath output unset in those cases. ParsePubkeyInner now returns an optional replacement alongside parsed providers so ParsePubkey can merge explicit key origin information before collecting it.
When importdescriptors expands a BIP 389 multipath descriptor into separate receive and change descriptors, additionally store a record containing the multipath descriptor string, as reported by the parser in public form, along with the IDs of the wallet descriptors it expanded to. Reject overlap with a different multipath record before importing any of the expanded descriptors. Older wallet software ignores the new record.
Add an optional multipath field to getaddressinfo, containing the multipath descriptor that this address' parent descriptor was expanded from, in public form.
Add an optional multipath field to each listdescriptors entry that was expanded from a multipath descriptor, containing the stored multipath descriptor record. Every descriptor from the same multipath descriptor returns the same value, always in public form.
The descriptor IDs in the record are translated to those of the watchonly wallet.
Add SetupDescriptorScriptPubKeyManPair, used by default wallet creation and createwalletdescriptor, which derives the receive and change descriptors of an output type from a single multipath descriptor string, built by a new GenerateMultipathDescriptorString. This commit does not change behavior.
When SetupDescriptorScriptPubKeyManPair creates a descriptor pair, store a record with the normalized multipath descriptor they derive from. When createwalletdescriptor is used with the internal option, the record is stored by whichever call completes the pair.
f26ecc2 to
9323653
Compare
|
Final rebase (after #36230 and #34861) so I can keep using it in Sjors#91 until there's an alternative. (I'll close after CI is green)
I'm going to leave that for someone else to implement.
That's a reasonable constraint on its own, but it leads to the (imo problematic) situation where you need to reconstruct something you already had. Unless we implement a native multipath descriptor object, which IIUC you also don't want - and is probably even more complicated than what I did here. |
# Conflicts: # test/functional/wallet_createwalletdescriptor.py
Status: this PR needs further conceptual review / discussion, before going too deep into code review.
HWI recently added a
registerdescriptorcommand (bitcoin-core/HWI#842) which some hardware wallets require in order to (more safely) support multisig. This registration can then be used withdisplayaddressandsigntx(using the--registrationargument, see bitcoin-core/HWI#841 and bitcoin-core/HWI#841). Internally HWI converts the descriptor to a BIP388 policy. The only catch is that we need to provide a multipath descriptor, rather than separate receive and change descriptors. This PR makes that possible.Our wallet already supported importing multipath descriptors, but upon import they are irreversibly turned into change and receive descriptors. We don't overhaul this fundamental descriptor wallet design. Rather (as per approach 5 from #36075) we store an additional record to hold on to the multipath descriptor strings and point to its matching receive and change descriptor.
Existing wallets are not impacted (and not "upgraded").
The main commits are:
These commits make wallet handling consistent and expose the record through RPC:
createwalletdescriptor.The remaining commits provide parser plumbing, tests, and refactors that make the main changes easier to follow and keep the descriptor code maintainable:
&errorand introducesParsePubkeyResultand other aliases that are expanded later (with less churn).internal)Based on:
HasWalletDescriptorperformance and other canonical descriptor string followups #36230