mirror of
https://github.com/bitcoin/bitcoin
synced 2026-12-04 12:17:38 +00:00
wallet, rpc: Add analyzedescriptor for multisig setups #36885
No reviewers
Labels
No labels
Android
Backport
Block storage
Brainstorming
Bug
Build system
CI failed
Consensus
Data corruption
Descriptors
Docs
DrahtBot Guix build requested
Feature
Fuzzing
GUI
Good First Review
IPC
Insufficient Review
Kernel
Linux/Unix
Mempool
Mining
Needs Backport (31.x)
Needs Backport (32.x)
Needs Benchmark
Needs Conceptual Review
Needs backport (30.x)
Needs rebase
Needs release note
Net
P2P
PSBT
Privacy
Private Broadcast
Questions and Help
RPC/REST/ZMQ
Refactoring
Resource usage
Review club
Scripts and tools
Settings
TX fees and policy
Tests
Tracking Issue
UTXO Db and Indexes
Up for grabs
Upstream
Utils/log/libs
Validation
Waiting for author
Wallet
Windows
good first issue
interfaces
macOS
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
bitcoin/bitcoin!36885
Loading…
Reference in a new issue
No description provided.
Delete branch "analyze-descriptor"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Part of the multisig setup work tracked in #35645. Based on #35918 (the first 3 commits).
Before importing a multisig descriptor received from a coordinator, each participant needs to check that it is the setup they agreed on: a k-of-n where any k cosigners can spend and nothing else can, that their own key is in it, and that they all derived the same first addresses (receive and change). Today nothing in Core reports what a descriptor commits to, so tools had to parse the descriptor string by hand, as the multisig wizard in #36325 (now closed) did.
This PR adds that check in three layers:
GetKeyInfo(),GetKeyInventory()andGetPolicy()describe a parsed descriptor's key expressions, where each key is used, and how they combine.getdescriptorinfogains averboseoption that returns them askeysandpolicy.AnalyzeDescriptor()tells whether a descriptor is a recognized k-of-n setup, and if not, why.getdescriptorinfo verbosereturns it asanalysis, so it works even with-disablewallet.interfaces::Wallet::analyzeDescriptor(for the GUI) and theanalyzedescriptorwallet RPC add which keys the wallet holds, whether it can sign, and the first address of each path (receive and change). They accept any descriptor, single-sig included;analysis.recognizedtells whether it is a recognized k-of-n.Recognized setups are
wsh(multi)/wsh(sortedmulti), andtr(K,multi_a)/tr(K,sortedmulti_a)with a single leaf, whereKis the BIP 341 NUMS point or amusig()of at least k distinct cosigners. Every cosigner must be an extended key other than the NUMS point, with an unhardened ranged derivation that can derive addresses, nomusig()participant may need a hardened derivation step, and no two cosigners or participants may be found to be the same key. Anything else is still analyzed and returned, withrecognized: falseand the list of issues: the analysis warns, it never refuses a descriptor.Example, run by one of the three cosigners:
A locked wallet is not an error: a cosigner key with a hardened origin path can't be checked without the private key, so it is reported as
"unknown", andcan_signis left out unless another key is already known to be the wallet's. This holds even when the wallet also has that key without its private key (e.g. an imported watch-only copy of its own xpub)."unknown"only appears when the wallet is locked, so the caller can always ask the user to unlock and retry. Everything else (analysis,first_addresses) is still returned. Only the candidate private key is decrypted, and none when the key is one of the wallet's HD keys itself.A plain public key, as
getaddressinfoorcreatemultisiguse, is matched the same way through its origin: the one in the descriptor, or the one the wallet recorded when it derived the key (read with the newDescriptorScriptPubKeyMan::GetPubKeyOrigin(), which needs no private key). A plain public key the wallet has but can't sign for (from an imported watch-only descriptor, or another cosigner's key in a multisig it imported) iswatch_only. Keys the wallet holds only asmusig()participants are matched too, althoughgethdkeysdoesn't list them. An origin doesn't have to start at a key the wallet holds: if the wallet imported only an account xprv with its master's origin (e.g.[fp/48h/1h/0h/2h]tprv...), a key whose origin starts with that one is matched by deriving the rest of the path from the account key.Commits
getdescriptorinfo verbosescript: add AnalyzeDescriptor() for multisig setupsrpc: move descriptor key JSON helpers to a shared filerpc: add multisig analysis to getdescriptorinfo verbosewallet: add analyzeDescriptor to the wallet interfacewallet, rpc: add analyzedescriptordoc: add release note for #36885Changes made to the inspection commits (4-7)
The inspection commits come from w0xlt's descriptor inspection branch. These changes were made on top, for w0xlt to review and agree with or not (this section will be removed once agreed):
m_root_extkeyinstead of*GetRootExtPubKey()./0h/*and/1/*h, checking the last-hardened and derived caches.wide += ')'instead of")"in the participant test (clang-tidyperformance-prefer-single-char-overloads).KeyRole()is pure virtual; descriptors without key arguments (addr,raw,sh,wsh) say so explicitly withNONFATAL_UNREACHABLE().frameis invalidated by the followingpush_back.return {}.Assert→CHECK_NONFATAL; explicitstatic_cast<uint32_t>for the threshold;/*value=*/0named on theaddr()/raw()nodes.tr,scriptonly undersh/wsh, the Taproot internal key only ontr, the output key only onrawtr).verboseargument help; thepolicyhelp explains both forms.Open question:
script/descriptor_analysis.hdescribes and gives no verdict, whilescript/descriptor_analyzer.hgives the verdict; the names are easy to mix up. Rename the first toscript/descriptor_inspection.h?Design choices
wsh(sortedmulti(2,A,A,C))is not a 2-of-3, since A alone can spend. Each issue lists the key IDs involved instead.repeated_key(so is the same public key with another chain code, ignoring parity), as is an xpub and its own unhardened child xpub (checked by deriving it, whatever the child's chain code or parity), and so are slots linked through a chain of these. BIP 388 allows the same key with disjoint paths in general policies, but in a k-of-n it lets one holder fill two slots.numsormusig. Any other internal key, or amusig()of fewer than k distinct cosigners or with an outsider, iskey_path_spendable: it lets someone spend without k cosigners. Amusig()participant with a hardened derivation step (the other cosigners couldn't derive the key) is reported ashardened_derivationon that participant, and a repeated participant (same root key, or same derived key) asrepeated_key.musig()participant or Taproot internal key that can't derive addresses at all (e.g. an xpub claiming the maximum BIP32 depth, or amusig()followed by more than 255 derivation steps) isnot_derivable.unspendable_key: no one can sign for it, so a 2-of-2 with it could never be spent.branches_differotherwise), and the issues of every path are returned.keysandpolicydescribe the first path, andfirst_addresseslists index 0 of every path, so participants comparing them also catch a different change path. This is a quick check only: descriptors with the same first addresses and analysis can still differ later (e.g.multi()andsortedmulti()of the same keys), so to confirm a shared setup, participants compare the descriptors themselves. Forcombo(), that is its first address inderiveaddresses(P2PKH).hasprivatekeys(as ingetdescriptorinfo) tells whether the descriptor contains private keys: whoever holds it can sign for those keys, so a descriptor received from a coordinator shouldn't have any. It isn't an issue, since importing one's own xprv this way is normal, sorecognizeddoesn't look at private keys. Those keys never make a keymine.can_signmeans the wallet holds the private key for at least one key of the descriptor, whatever its shape. Whether it is a recognized k-of-n is a separate answer,analysis.recognized.The functional test scenarios of #36325 were adapted here. The analysis differs from that wizard in a few places:
wsh(multi(...))is recognized, withsorted: false: it is a valid k-of-n, and requiringsortedmultiwas the wizard's own policy.musig()key path of at least k distinct cosigners is recognized: those cosigners can already spend through the leaf./0/*) is recognized;multipath_expansionalready shows whether it is multipath.Known limits
repeated_key. Likewise, only the NUMS point itself is anunspendable_key: any other key no one can sign for (e.g. a child of it) looks like a normal key. No check can show that different keys belong to different people: each cosigner should confirm withanalyzedescriptorthat their own key ismine.mineonly if a descriptor holding it also holds its private key, as ingethdkeys. If the wallet holds the private key alone in another descriptor (e.g. imported as a WIF inpk()), the key iswatch_onlyandcan_signis false. That matches what the wallet can do: each descriptor signs with its own keys, and none has both the private key and the chain code.getaddressinfoknows about.walletfield shows it: count the keys markedmine.can_signdoesn't count keys held on an external signer, since the wallet doesn't hold their private keys: a hardware-wallet user's own cosigner key shows aswatch_onlyornot_mine. The wallet can't tell which keys the device holds.keysshows each origin.A release note is included (
doc/release-notes-36885.md).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.
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:
-Wunusedby fanquake)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.
Earlier concerns neither republished nor resolved: 1 suggestion. See the Run archive linked from the Review report for their assessments.
🟠 Minor · Wallet analysis does not flag hardened cosigner keys controlled by one seed
An unlocked wallet can recognize a nominal 2-of-3 containing two hardened account keys from its own seed, with only two
mineentries revealing shared control; consumer reliance on the clean verdict is unconfirmed.Location:
src/wallet/descriptor_analyzer.cpp:207(head).🟠 Minor ·
analyzedescriptorleaves private descriptors in Qt console historyA private descriptor passed to
analyzedescriptorremains unredacted in the Qt console's session scrollback and in-memory history, continuing an existing gap in descriptor RPCs.Location:
src/wallet/rpc/wallet.cpp:1129(head).🔵 Suggestion · Wallet analysis omits public descriptor strings for the recommended comparison
The interface recommends comparing complete descriptors but returns no public serialization, leaving a GUI using it to parse the input again; RPC callers can work around this with
getdescriptorinfo.Location:
src/wallet/rpc/wallet.cpp:1109(head).🔵 Suggestion · HD candidate discovery repeats the wallet-wide traversal
WalletHDKeys()visits the same descriptor managers throughGetHDPubKeys(All)and then their inventories, although those inventories already contain every required BIP32 root.Location:
src/wallet/descriptor_analyzer.cpp:163(head).Review coverage was incomplete.
Reviewed:
be7a2d1cfd97· Review report · Previous reportspermalink
Approach ACK
🚧 At least one of the CI tasks failed.
Task
iwyu: https://github.com/bitcoin/bitcoin/actions/runs/44330374279/job/134892869102LLM reason (✨ experimental): CI failed because the IWYU lint check modified
src/script/descriptor_analyzer.cpp, leaving a diff that causedgit diff --exit-codeto fail.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.
🚧 At least one of the CI tasks failed.
Task
Alpine (musl): https://github.com/bitcoin/bitcoin/actions/runs/45706057177/job/139528199943LLM reason (✨ experimental): CI failed to build
test_bitcoin:-Werror=range-loop-constructrejects bindingconst std::string&to temporary strings indescriptor_analyzer_tests.cpp:329.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.
-Updates:
dd636a5856→be7a2d1cfd, rebased onto master3e4c803da8(it had thesecp256k1CMake 4.5 fix from #36890, which the "Windows native, fuzz" job needed). Commits 1–5 and 7 are unchanged; the commits after 8 were reordered (see below), and a release note was added as commit 13.Analysis (
getdescriptorinfo verboseandanalyzedescriptor)musig()participants: one that needs a hardened derivation step is reported ashardened_derivation, and a repeated one (same root key, or deriving the same key) asrepeated_key, on the participant IDs.repeated_keynow also catches:The help says which related keys can be detected, and that each cosigner should still confirm their own key is
mine. A direct child xpub is found through its parent fingerprint and child number.not_derivable: a cosigner,musig()participant or Taproot internal key that can't derive addresses (e.g. an xpub claiming the maximum BIP32 depth, or amusig()followed by more than 255 derivation steps).unspendable_key: a cosigner that is the NUMS point, as a key or as an xpub (no one can sign for it).musig()can have any number of participants, and a large one took time quadratic in their count (32,000 participants: 0.95s → 0.24s forgetdescriptorinfo verbose; the plain call takes 0.17s). A child xpub is also derived only from the first of the xpubs sharing a root key, since any number of them can share a fingerprint with different chain codes (500 such parents and 499 children: 4.22s → 0.12s). I can add a benchmark if reviewers want one.Wallet (
analyzedescriptor/interfaces::Wallet::analyzeDescriptor)first_addressis nowfirst_addresses: index 0 of every path (receive and change), so a descriptor with a different change path no longer looks identical. It uses private keys given in the descriptor. Forcombo()it is the P2PKH address, the first onederiveaddresseslists (before, it was left out, since the first script is P2PK). The help says they are a quick check only: descriptors with the same first addresses and analysis can still differ later (e.g.multi()andsortedmulti()of the same keys), so to confirm a shared setup, compare the descriptors themselves.hasprivatekeys(as ingetdescriptorinfo): a received descriptor with a cosigner's xprv lets anyone holding it sign for that key. It isn't reported as an issue, since importing one's own xprv this way is normal, and therecognizedhelp says private keys aren't looked at. Those keys never make a keymine(tested).getnewaddress/getaddressinfo) is matched through its origin: either the one in the descriptor or the one the wallet recorded (newDescriptorScriptPubKeyMan::GetPubKeyOrigin(), no private key needed). If the wallet has it without its private key, it iswatch_only.musig()participants are matched.watch_onlyin that case, and xpubs below the account werenot_mine. This was listed as a known limit, which is now removed.02/03/x-only)."unknown"only appears when the wallet is locked, and a locked wallet keeps it even when it also has the key without its private key. An HD key without its private key givesnot_mineandcan_sign: false.can_signhelp andCanSign()doc say "recognized k-of-n" instead of "safe k-of-n". They also say that keys held on an external signer don't count, since the wallet doesn't hold their private keys (added to the known limits).key_path_spendablehelp. Theanalyzedescriptorhelp says, likegetdescriptorinfo, thatkeysdescribes the first path and that the format is experimental.Not changed (added to the known limits)
walletfield shows it. There is also no warning for cosigners sharing an origin fingerprint: the fingerprint is unverified, and sharing one can be intended.pk()) iswatch_onlywithcan_sign: false. This is the same rule ashas_privateingethdkeys, and it is what the wallet can actually do, since each descriptor signs with its own keys. A functional test checks it withwalletprocesspsbt.analyzedescriptorstay in the Qt console history, as they do forgetdescriptorinfoandderiveaddresses, it should go intohistoryFilteras they can contain BIP32 extended private keys (xprv) or WIF private keys. Thinking to add them here in a separate commit.Also
descriptor_parsefuzz targets now run the analysis and check its invariants (including that no issue is reported twice).rpc/descriptor_util.cppfirst (move-only, commit 9). The analysis helpers are then added there directly, instead of being added torpc/output_script.cppand moved later. The final code is the same.encryptwalletneeds-deprecatedrpc, so the locked-wallet test now creates its wallet with a passphrase.assert_not_equal, where the LLM linter suggested them.mineand anunknownkey (can_signtrue), andfirst_addressesleft out when only a later path can't be derived. Other new tests: an xpub with a different depth, parent fingerprint and child number is still the wallet's key, but one with a different chain code isn't; a key repeated only on the change path is found; the unsortedmulti_acase is recognized.