wallet, rpc: Add analyzedescriptor for multisig setups

bitcoin/bitcoin: PR #36885

5 suggestions

Optional improvements

Link

[suggestion] · A wallet meeting the threshold alone has no ownership summary

Per-key rows already expose both matches, but no summary flags when one wallet holds enough hardened-sibling cosigner keys to meet a recognized setup's threshold.

Location: src/wallet/descriptor_analyzer.cpp:189 (head).

Details and suggested fix

For an unlocked wallet holding the master, account keys at m/48h/1h/0h/2h and m/48h/1h/1h/2h both match as mine. In a 2-of-3 with an outside key, the analysis remains recognized because the hardened siblings have distinct roots and no publicly verifiable direct-child relationship. CanSign() only reports that at least one key matches. Return the IDs in analysis.cosigners whose wallet state is MINE, and add an advisory when their count reaches the threshold. Exclude UNKNOWN and WATCH_ONLY, and preserve the existing meanings of recognized and can_sign.


Link

[suggestion] · Shared origin fingerprints produce no cosigner advisory

Matching origin fingerprints could reveal accidental same-seed cosigners, but the wallet-free analysis offers no advisory; these unauthenticated 32-bit values cannot prove common ownership.

Location: src/script/descriptor_analyzer.cpp:182 (head).

Details and suggested fix

CheckRepeatedKeys considers root keys, first derived keys and verified unhardened direct children, but never reads node.origin. The functional test recognizes a_parent at m/48h/1h/0h alongside its hardened child a at m/48h/1h/0h/2h, although both origins name the same master fingerprint. Add a separate, non-blocking warning listing cosigner IDs that share an origin fingerprint, so other participants can notice this possible setup mistake. Keep recognized unchanged and do not reuse repeated_key or issues, since any issue currently makes the setup unrecognized.


Link

[suggestion] · Wallet tests miss first-address failure on a later multipath branch

The wallet tests do not distinguish all-or-nothing first_addresses from a partial receive-only result when a later multipath branch fails.

Location: src/wallet/descriptor_analyzer.cpp:209 (head).

Details and suggested fix

The implementation correctly clears addresses accumulated from earlier paths when any path cannot derive an address. Existing multipath assertions exercise only successful derivation, while the failure assertions start with an empty list. In the existing unit test, analyze "wpkh(" + EncodeExtPubKey(other.Neuter()) + "/<0;1h>/*)" and assert first_addresses.empty(). Its first branch derives publicly and its second requires an unavailable private key, so the assertion would catch removal of the clearing step.


Link

[suggestion] · Analysis JSON helpers take an avoidable intermediate location

Introducing the shared key helpers first would let the analysis serialization and help land directly in their final file, avoiding a second move-and-extraction review without changing the final tree.

Location: src/rpc/output_script.cpp:365 at 369ec52f6ca1 (commit) (commit).

Details and suggested fix

Commit 369ec52f6ca140783787c4e34af1fe1f1ac8792f adds the analysis serializer and inline help to src/rpc/output_script.cpp. Commit ee75275324678341d3752fba6c1c9e273b97ec7f then moves them to src/rpc/descriptor_util.cpp and extracts HELP_DESCRIPTOR_ANALYSIS and DescriptorAnalysisResultFields(). Move a reduced version of the latter commit before the former, carrying only the key serialization helpers and DescriptorKeyResultFields(), then add the analysis helpers directly in the shared file. Commit a3c4c85b40cab5fa96e2d455d0e14d75856161ad already places the analyzer in bitcoin_common, so the dependencies are available. This is a modest review-flow improvement, not a head defect; it trades keeping the extraction near its wallet RPC consumer, 9821e2d868add12e5870c575190c4f238f33d7ba, for reviewing the analysis output once in its final location.


Link

[suggestion] · WalletHDKeys walks every descriptor manager twice

The inventory loop already sees every BIP32 root collected by GetHDPubKeys(All), so one pass can build the same map with less work under cs_wallet.

Location: src/wallet/descriptor_analyzer.cpp:160 (head).

Details and suggested fix

GetHDPubKeys(All) visits every manager and builds temporary key sets through GetPubKeys; WalletHDKeys then visits every manager again for its inventory and origins. Each extended root collected by the first traversal is also a BIP32Key occurrence in the second. Initialize an empty HD-key map and insert every BIP32Key root with its manager in the existing inventory loop, rather than filtering insertion to MUSIG_PARTICIPANT. This preserves the manager associations and MuSig support, leaves gethdkeys unchanged, and removes the redundant traversal.


Coverage details

Background

Ralph's summary of historical context for the behaviour affected by this change. The historical record reviewed here is incomplete.

Documented decisions

BIP341 deliberately permits either an aggregate-key spending condition or an internal key with unknown discrete logarithm. It suggests an aggregate of all participating keys as an “everyone agrees” branch; otherwise it identifies NUMS point H as a way to disable key-path spending.

This explains why analyzing only the threshold leaf is insufficient: the internal key is another spending route. H preserves script-only authorization under the discrete-log assumption. BIP341 also recommends randomizing H as H+rG for privacy, with r disclosed to a verifier; consequently, recognizing the particular H representation is narrower than recognizing every legitimately unspendable internal key.

Source 1

The earlier multisig setup discussion explicitly required duplicate-key checks on a received descriptor, not merely during construction. An initial request to refuse duplicates was revised to warning before confirmation: intentional duplication can be valid, and distinct public keys cannot establish independent human control.

A coordinator's claimed threshold and inclusion of the participant's own key were considered insufficient verification. This history supports separating successful parsing/signing from recognized quorum structure, and returning visible issues without refusing to analyze or import every unusual policy.

Source 1 · Source 2 · Source 3 · Source 4

BIP388's key restrictions address reviewability, repeated-key security hazards, and privacy—not proof that each key has a different holder. It requires distinct public keys in the key-information vector but permits reuse of a placeholder with disjoint derivation branches. Its rationale specifically notes that mixed derivation lengths can conceal parent/child collisions and that fixed keys reused across outputs lose privacy.

This supplies precedent for restricting derivation patterns and checking related xpubs, while explaining why BIP388 compatibility is not the same question as a holder-level k-of-n quorum. Disjoint children of one xpub can be different script keys yet remain controlled by one holder; the pinned analyzer groups such slots by their common root.

Source 1

The prior derivehdkey work intentionally shares account xpubs below a hardened path, distinct from default single-signature descriptors. Related public-descriptor import work was motivated by avoiding manual exposure of private keys and proposed matching origins by fingerprint followed by actual derivation to the supplied xpub.

Hardened origins are therefore an expected coordinator workflow, not an exceptional malformed input. They explain why ownership verification may require private-key access even though descriptor structure and address generation use public account keys. The import proposal also explicitly distinguished master-origin matching from the additional work needed to match an imported account key through an origin prefix.

Source 1 · Source 2

Earlier gethdkeys work explicitly accommodated multisig descriptors containing only some private keys. Knowing an xpub in a private-key-enabled wallet did not imply that its xprv was available.

This is historical support for separate watch-only and private-key attribution. The pinned base determines has_private using the descriptor managers associated with each xpub rather than the wallet's general private-key-enabled status.

Source 1

Ralph's inferences

Accepting a MuSig aggregate of at least k distinct leaf cosigners follows authorization-set containment: assuming the aggregate requires all its actual participating holders, that coalition already contains enough holders to satisfy the k-of-n leaf. NUMS instead removes the alternate route. These preserve the leaf's authorization boundary, but are not identical spending procedures or availability guarantees.

The pinned classifier implements this reasoning by requiring every aggregate participant's root to occur among the leaf cosigners and counting distinct roots against the threshold. BIP341 documents the all-participants aggregate precedent; extending it to a threshold-sized subset is an inference, not a prior explicit decision found in the inspected record. It remains conditional on the holder relationships and MuSig assumptions.

Source 1

Related-key detection is a bounded public-evidence check, not a proof of independent cosigners. Matching roots and first derived keys detects visible overlap; checking a claimed direct child by derivation avoids trusting fingerprint metadata as proof. Hardened relationships cannot be checked by public derivation, while altered metadata and unprovided intermediate paths can hide other relationships.

The pinned implementation uses fingerprint-indexed candidate parents, verifies the child's public key and chain code, and groups linked slots transitively. It also considers only one candidate per shared root to bound redundant work. These choices explain a tractable detector, but the inspected history does not establish this exact boundary as a protocol requirement. RPC help explicitly identifies grandchildren and metadata-altered children as outside detection and disclaims identifying who holds the keys.

Source 1

Descriptor-local private-key attribution reflects the wallet's operational signing model rather than every derivation mathematically possible from secrets somewhere in the wallet. An xpub is privately attributable only when a descriptor manager associated with that extended key also has the private key.

In the pinned base, signing providers expand private keys from their own descriptor manager, and gethdkeys checks those same managers. The new matcher follows that boundary: an isolated private scalar in another descriptor does not by itself turn a watched xpub into a signing-capable HD key. Origin-prefix matching supports imported account keys without requiring their master to be present. This is an architectural explanation from pinned code, not a separately documented historical policy decision.

UNKNOWN distinguishes inability to verify an ownership claim while locked from either verified ownership or verified absence. A matching origin fingerprint selects a candidate, but hardened derivation still needs its private key before the supplied key can be compared. UNKNOWN outranks a watch-only match because unlocking may establish private ownership.

The pinned matcher can establish exact or unhardened matches while locked, returns UNKNOWN only when an otherwise eligible private candidate needs hardened derivation, and decrypts only that candidate when unlocked. The RPC communicates the distinction through per-key states and omission of can_sign when undecidable. Its help separately defines can_sign as holding at least one private key—not meeting the quorum or being immediately unlocked—and returns first addresses for participant comparison. Together with the earlier requirement to surface warnings before confirmation, these are separate caller-facing checks rather than one unconditional coordinator-trust verdict.

Source 1 · Source 2