Review report

core: PR #36697

3 minor findings · 5 suggestions

Findings

Link

[minor] · Direct-address lookup can export an HD master private key

A direct address for an xprv descriptor’s master pubkey returns the master private key; although the same caller can obtain it through other RPCs, the help does not make this export scope clear.

Location: src/wallet/scriptpubkeyman.cpp:1386 (head).

Details and suggested fix

GetExportPubKeys treats each private derivation root as a candidate for P2PKH, P2WPKH, P2SH-P2WPKH, and direct P2TR destinations. For an xprv descriptor, the root is the extended key’s own pubkey. Default wallets build every descriptor from the master xprv, and this root is not in m_map_pubkeys, so ExportKey(const CPubKey&) falls back to HasPrivKey and GetKey and returns the stored master scalar. Direct P2TR lookup reaches the same fallback through xonly.GetCPubKeys(), even without the root scan.

gethdkeys without private returns the master xpub, including its chain code, without unlocking. That is enough to rebuild the master xprv for every descriptor. No descriptor produces these addresses, but the help describes an address known to the selected descriptor wallet and only warns about a non-hardened child combined with an ancestor xpub. The GetPrivateKeyRoots comment says roots are not output candidates, while a unit test asserts root export through PKHash. The release notes do intend export of directly verifiable stored or cached keys, so this is a scope and documentation concern, not a new authorization boundary. Stop treating BIP32 extended-key roots as direct-address candidates, including in the stored-key fallback, unless the key also appears as an expanded concrete key. If root export is intended, state in the help and release notes that HD master keys can be exported and are seed-equivalent when combined with gethdkeys.


Link

[minor] · Reloaded odd-Y Taproot WIF keys are not found for non-Taproot addresses

For a key held only through tr(WIF) or rawtr(WIF), non-Taproot address export works after import but returns NO_KEY after reload; how often this occurs in practice is unconfirmed.

Location: src/wallet/scriptpubkeyman.cpp:1384 (head).

Details and suggested fix

Importing tr(WIF) or rawtr(WIF) stores the secret under its full public key, which may have odd Y. The in-memory ConstPubkeyProvider retains that full key, so GetExportPubKeys matches it and exports its P2PKH, P2WPKH, and P2SH-P2WPKH addresses. WalletDescriptor::Serialize writes ToString(), which emits only the x-only key; reparsing a 32-byte key creates the 0x02 representative. After reload, Load() fills m_map_pubkeys with the even key and GetPrivateKeyRoots returns it too, so the odd key’s address hash matches nothing. InferDescriptor then yields an unsolvable descriptor and the call returns NO_KEY, even though HasPrivKey can still find the secret under the odd key’s ID. Taproot destinations are unaffected because they try both parities, and another descriptor holding the same full key can mask the failure. The established trigger is a request for a non-Taproot address of a key held only through a Taproot WIF descriptor, which the release notes’ direct stored-key export allows; the production prevalence is unconfirmed. If direct-key lookup is narrowed to wallet-known scripts as proposed for the master-key finding, this becomes an intended NO_KEY. Otherwise, resolve both parity representatives of x-only descriptor keys before comparing hashes and add a reload regression test for an odd-Y Taproot WIF, including encrypted storage.


Link

[minor] · An odd-Y HD root used as an x-only key makes export report inconsistent wallet data

A descriptor that holds an xprv and uses its odd-Y root in x-only form makes both export modes fail with RPC_INTERNAL_ERROR; such descriptors appear uncommon, and production impact is unconfirmed.

Location: src/wallet/scriptpubkeyman.cpp:1438 (head).

Details and suggested fix

ExportKey(index, pubkey) loads the descriptor’s held roots, privately expands the index, and requires every derived key ID to appear in the public expansion with an identical full key. An x-only ConstPubkeyProvider expands publicly as 02||X. Its GetPrivKey uses GetKeyByXOnly, which also accepts a key stored under the 03||X ID and emits it under that ID. For tr(<x-only hex of P>,pk(XPRV/0)), where the xprv root pubkey P has odd Y, the BIP32 root puts P’s secret in private_roots, and the x-only expression emits it under ID(P). The check then fails because the public expansion contains only 02||X and the /0 child. ExportKey(const CPubKey&) returns the error before its stored-key fallback, so ExportAddressKeys reports “wallet key/script data is inconsistent.” The same shape can occur after reload for tr(WIF,pk(XPRV/0)) when the WIF is the xprv’s own odd-Y root scalar, because the WIF is persisted in x-only form. No wallet-generated descriptor has this shape, so production impact is unconfirmed. For x-only expressions, compare expanded secrets with public keys by x-only equivalence, retain exact checks for SEC-bound keys and MuSig2 participants, and add a regression test with an odd-Y root.


Optional improvements

Link

[suggestion] · Top-level aliases create a new, duplicated RPC result format

The top-level private_key_hex and compressed fields have no prior Bitcoin Core contract, but would become API surface that can only be removed through a deprecation cycle.

Location: src/wallet/export.cpp:273 (head).

Details and suggested fix

For P2PKH and P2WPKH wallet-mode results, legacy_aliases makes AddressKeyToJSON add top-level private_key_hex and compressed fields that duplicate keys[0]. The comment calls this the original RPC contract, and the help calls them compatibility aliases, but exportaddresskey does not exist at the comparison base; the only earlier contract is the Bitcoin Roots fork’s. The result shape therefore varies by address type, duplicates the secret in some responses, and requires the RPCResult schema and tests to keep both forms in sync. After release, removing the aliases would require a -deprecatedrpc cycle. Return only keys before the RPC first ships; clients that need the old shape can map keys[0] themselves.


Link

[suggestion] · Taproot discovery rescans key maps and repeats private expansion under cs_wallet

Work grows roughly with candidate keys times cached wallet pubkeys, plus a full private expansion per candidate hit, while other wallet operations are blocked.

Location: src/wallet/export.cpp:198 (head).

Details and suggested fix

For each distinct Taproot candidate x-only key, including the output, internal, and recognized leaf keys, the discovery loop calls GetExportPubKeys on every manager. Each call scans the manager’s entire m_map_pubkeys and roots. For each key it computes PKHash, WitnessV0KeyHash, and P2SH-P2WPKH hashes and runs IsFullyValid, although Taproot lookup needs only an x-only comparison. The records loop then calls ExportKey(pubkey) for each candidate on each manager. Every hit repeats ExpandFromCache, the root GetKey call, which decrypts on encrypted wallets, and ExpandPrivateChecked for the whole descriptor. With N leaf keys, up to 999 in multi_a, and P cached pubkeys across managers, the loop structure implies about (N+2)·P key matches and about 2N full expansions. These counts are inferred from the loops, not measured. All this work occurs under cs_wallet. Collect the x-only set first and scan each manager once, compute only the hash form needed by the destination, and memoize private expansion per (manager, index), while retaining the commitment and exact-key checks.


Link

[suggestion] · Hex-only exported keys require conversion for Core’s WIF-only interfaces

Core interfaces accept private keys as WIF, so using the exported scalars with Core requires external conversion.

Location: src/wallet/rpc/backup.cpp:127 (head).

Details and suggested fix

Each record returns private_key_hex, a bare 32-byte scalar. Core’s descriptor parser, signrawtransactionwithkey, and signmessagewithprivkey accept private keys through DecodeSecret, which takes WIF. The legacy dumpprivkey that motivated this RPC also returned WIF. The exported keys therefore cannot be re-imported or used in Core without conversion; the functional test itself converts with bytes_to_wif. There is also a secondary risk: in tr() and rawtr(), the parser treats any valid 64-character hex string as an x-only public key. Pasting a scalar there can silently produce an address whose key nobody holds, though that requires user misuse. Add a WIF field alongside the hex, since WIF already encodes the compression flag, or document why WIF is unsuitable.


Link

[suggestion] · The secret-returning ExportKey(script, key_id) overload is unused by production and tests another path

A new public overload that returns secrets has no production caller, while several unit tests exercise its index binding instead of the RPC’s actual path.

Location: src/wallet/scriptpubkeyman.h:405 (head).

Details and suggested fix

DescriptorScriptPubKeyMan::ExportKey(const CScript&, const CKeyID&) is new and has no production caller. ExportAddressKeys uses only ExportKey(const CPubKey&) at export.cpp:232, which resolves through the first index recorded in m_map_pubkeys and then falls back to stored keys. Tests such as export_key_script_index_binding and export_key_inconsistent_public_expansion check script-index binding through the unused overload, while candidate selection provides that binding in the RPC path. This leaves an extra public API that returns secrets with different semantics and makes coverage of the actual path look stronger than it is. Remove the overload or move it into ExportTestScriptPubKeyMan, and route those tests through ExportAddressKeys or ExportKey(const CPubKey&).


Link

[suggestion] · The RPC help example uses a valid mainnet Taproot address

The second example is a valid BIP341 test-vector address, contrary to the developer note asking for invalid bech32 addresses in RPCExamples.

Location: src/wallet/rpc/backup.cpp:138 (head).

Details and suggested fix

The second RPCExamples entry hard-codes bc1p2wsldez5mud2yam29q22wgfh9439spgduvct83k3pm50fcxa5dps59h4z5, a valid mainnet address from src/test/data/bip341_wallet_vectors.json. doc/developer-notes.md asks for invalid bech32 addresses in RPCExamples to reduce the chance that users send funds to them, and the first example here already uses EXAMPLE_ADDRESS[0]. Replace the second example with a deliberately invalid bech32m placeholder while retaining the taproot_output argument.


Coverage details