Repository navigation
wallet, rpc: Add analyzedescriptor for multisig setups - #36885
pablomartin4btc wants to merge 13 commits into
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. External sites
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. |
|
Approach ACK |
dd636a5 to
4275cc2
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. |
4275cc2 to
2155dfd
Compare
2155dfd to
c59b0a3
Compare
0997b01 to
3859d34
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. |
3859d34 to
cc57e7a
Compare
cc57e7a to
c2d5322
Compare
Expose an owned policy tree with typed descriptor and Miniscript operators, local values and data, explicit child roles, and references to the public key occurrence inventory. Preserve Taproot leaf order and depths, and keep raw scripts and addresses explicitly opaque. Build key occurrences and policy nodes in one iterative traversal without serializing subtrees, so the result is linear in the descriptor and each key argument's occurrence ID is known when its policy node is created. The key inventory becomes the policy's key list. Map Miniscript operands through provider-local indices so inferred and cloned descriptors retain their correct key references. Cover structure, thresholds, hashes, opaque forms, reference integrity, and exact node counts. Check linear retained structure for 3,200 nested timelocks and 1,024 Taproot leaves, and exercise parsed and inferred policies, including MuSig participant links, in the descriptor fuzz targets.
With verbose=true, getdescriptorinfo also returns the descriptor's key occurrences and policy structure, built from Descriptor::GetPolicy(). Both are flat arrays whose indices are the IDs used within the result, so deep policies do not become deeply nested JSON. Keys report their role, public key material, derivation, and origin. Private keys in the input are reported by their public form. Policy nodes name their descriptor function or Miniscript fragment, and each child edge states its role in the parent, or its depth for a Taproot leaf. Mark the new fields as experimental, since their names form a new interface. Test the output for wsh, Taproot with MuSig, each child role, hashes, sorted multisig, opaque scripts, private keys, and multipath descriptors.
c2d5322 to
8721e96
Compare
8721e96 to
386c55a
Compare
386c55a to
029bc7f
Compare
029bc7f to
dd81cc5
Compare
dd81cc5 to
cbd3eb2
Compare
Tell whether a parsed descriptor is a recognized k-of-n multisig setup, using only its structure (no wallet). This is the check a participant needs before importing a descriptor received from a coordinator. Recognized shapes are wsh((sorted)multi) and tr(K,(sorted)multi_a) with a single leaf, where K is the BIP 341 NUMS point or a musig() 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, every musig() participant must be derivable without private keys, and the internal key must derive within the maximum BIP32 depth. Anything else is reported as unrecognized with the reasons, each listing the keys involved, and no setup is claimed for it: no threshold, cosigners, sorting or key path. Two cosigner slots are the same holder if they share a root key, derive the same key at the first address, or one has the key of the other's unhardened child xpub (checked by deriving it). Keys are compared without their chain codes or parity: whoever holds a private key holds the key of either parity, with any chain code. Slots linked through a chain of these are one group. Core accepts such descriptors, and fewer signers than the threshold can spend from them. A hardened child can't be derived from the xpub, so it is not linked to it. Likewise, musig() participants that share a root key or derive the same key are one participant. The groups are found with lookups rather than by comparing every pair, as a musig() can have any number of participants, and a child is derived only from the first of the xpubs sharing a root key, as any number of them can share a fingerprint. For multipath descriptors every branch is analyzed and must be the same setup, and the issues of every branch are returned, each once. The descriptor_parse fuzz targets check the analysis invariants. Co-authored-by: w0xlt <[email protected]>
Move the code that turns a descriptor's key occurrences into JSON, and the help for those fields, from rpc/output_script.cpp (node library) to rpc/descriptor_util.cpp (common library), so a wallet RPC can return the same fields. The multisig analysis helpers are added there next. Move-only, apart from exposing the functions and the help fields. Review with --color-moved=dimmed-zebra --color-moved-ws=allow-indentation-change.
Return the result of AnalyzeDescriptor() as an "analysis" object next to "keys" and "policy", referring to the same key IDs. This gives users and tools such as a multisig setup wizard the setup check without parsing the descriptor string or loading a wallet. For a multipath descriptor, "keys" and "policy" still describe the first path, while the analysis checks every path. An unrecognized descriptor returns only its issues. The JSON helpers and help for the analysis go in rpc/descriptor_util.cpp, next to the key ones, so the wallet RPC can return the same fields.
Combine AnalyzeDescriptor() with what only the wallet knows: which of the descriptor's keys it holds, and so whether it can sign. Also return the first address of each path (e.g. receive and change), which participants can compare as a quick check that they built the same wallet; only comparing the descriptors confirms it. For a path with several scripts, like combo(), that is the first script with an address, as deriveaddresses lists. Any descriptor is accepted; the multisig analysis tells whether it is a recognized setup. Whether the descriptor contains private keys is returned too: whoever holds it can sign for those keys, so a received setup shouldn't have any. An extended key is the wallet's if it is one of the wallet's HD keys, or if deriving the rest of its origin path from one of them gives the same key: from the HD key with the origin fingerprint, or after the origin the wallet's descriptors give that HD key, e.g. an account xprv imported with its master's origin. It is mine only if a descriptor holding it also holds its private key, as in gethdkeys: the wallet signs with each descriptor's own keys. A cosigner key usually has a hardened origin path, so checking it needs the private key. A locked wallet is not an error: such keys are reported as unknown, and the caller can ask the user to unlock and retry, even when the wallet also holds the key without its private key. Only the one candidate key is decrypted, and none when the key is one of the wallet's HD keys itself, which is checked first. A constant key is the wallet's if the wallet stores its private key, or if it was derived from one of the wallet's HD keys, checked the same way from its origin: the one in the descriptor, or the one the wallet recorded when it derived the key, as for a public key from getaddressinfo. The new DescriptorScriptPubKeyMan::GetPubKeyOrigin() returns that recorded origin without any private key. A key the wallet has derived is at least watch-only. Keys the wallet holds only as musig() participants are matched too, even though GetHDPubKeys() (and so gethdkeys) leaves them out. Co-authored-by: w0xlt <[email protected]>
Expose the wallet's descriptor analysis as an RPC: the same keys and multisig analysis as getdescriptorinfo verbose, plus which keys this wallet holds, whether it can sign, the first address of each path that participants compare, and hasprivatekeys, as in getdescriptorinfo. Any descriptor is accepted. It follows the usual wallet RPC rules (-rpcwallet when several wallets are loaded, unavailable with -disablewallet). A locked wallet is not an error: keys that need a private key to check are reported as "unknown" and can_sign is left out. Without a wallet, getdescriptorinfo verbose gives the rest. Co-authored-by: w0xlt <[email protected]>
cbd3eb2 to
be7a2d1
Compare
Reconciliation re-checked every finding ever reported on a PR on each push, including ones an earlier round had source-verified as fixed or invalid. On long-lived PRs most of its Opus input went to these: 41 of 47 findings on bitcoin/bitcoin#36885 job 3234 were already fixed, and PR 1142 re-checked the same 27 fixed findings in consecutive runs at 6-7M input each. Closed findings now stay in review memory unchanged, without a new history entry, and are left out of the reconciliation input. Their per-review candidate IDs are cleared so they cannot link to unrelated current findings. Reconciliation is skipped when no open findings remain. A regression that returns after a fix is raised again by discovery as a new finding. Duplicates are still reverified, since their status follows their canonical record.
|
-Updates:
|

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).