Review report

core: PR #36697

3 minor findings · 3 suggestions

Findings

Link

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

A default descriptor wallet can return its master scalar for a matching P2PKH, P2WPKH, or P2TR address; combined with the xpub from gethdkeys, this reconstructs the master xprv, although the same caller can already obtain it through other RPCs.

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

Details and suggested fix

GetExportPubKeys treats each private derivation root as a direct candidate for P2PKH, P2WPKH, P2SH-P2WPKH, and direct P2TR destinations. For an xprv descriptor, the root is the extended key's own pubkey, and default wallets build every descriptor from the master xprv. Because the root is not in m_map_pubkeys, ExportKey(const CPubKey&) falls back to HasPrivKey and GetKey, returning 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. Combined with the exported scalar, that chain code reconstructs the master xprv, covering every descriptor and hardened account. These addresses are not produced by the descriptor, but the help describes an address known to the selected descriptor wallet and warns only about a non-hardened child and an ancestor xpub. The GetPrivateKeyRoots comment says roots are not concrete output candidates, while the unit test asserts root export through PKHash and the functional sentinel test forbids root_secret.

The same caller can already obtain the xprv with listdescriptors true, so this is a scope and documentation concern, not a new authorization boundary. Stop treating BIP32 extended-key roots (GetRootExtPubKey) as direct-address candidates in both the root scan and stored-key fallback, unless the key also appears as an expanded concrete key. If root export is intended, update the help and release notes to state that HD master keys can be exported and are seed-equivalent when combined with gethdkeys.


Link

[minor] · The BIP341 vector path fails for non-symlink functional tests

On Windows, the test resolves the vector file to a path that does not exist in the build tree, causing wallet_exportaddresskey.py to fail in Windows functional CI.

Location: test/functional/wallet_exportaddresskey.py:699 (head).

Details and suggested fix

check_taproot_output_keys builds the vector path from os.path.realpath(__file__) and ../../src/test/data. This reaches the source tree only when the build-tree test file is a symlink. On Windows, test/CMakeLists.txt creates hard links or copies instead, so realpath stays under <build>/test/functional. The resulting path is <build>/src/test/data/bip341_wallet_vectors.json, but only siphash.json is copied there. Opening the vector file raises FileNotFoundError. The test is added to BASE_SCRIPTS, and Windows native CI runs build/test/functional/test_runner.py. Copy the vector file into the build tree alongside siphash.json and read it from there, or resolve it using self.config["environment"]["SRCDIR"].


Link

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

For a key held only through tr(WIF) or rawtr(WIF), non-Taproot address export succeeds after import but returns NO_KEY after reload; how often users request this combination 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 finds it and exports of its P2PKH, P2WPKH, and P2SH-P2WPKH addresses succeed immediately after import.

WalletDescriptor::Serialize writes ToString(), which emits only the x-only key. Reparsing a 32-byte key creates the even-Y representative, so Load() fills m_map_pubkeys with the even key and GetPrivateKeyRoots returns it as well. After reload, the odd key's address hash finds no candidate. InferDescriptor then returns an unsolvable addr() descriptor, and the RPC returns NO_KEY, even though HasPrivKey would still find the secret under the odd key's ID. Taproot destinations are unaffected because they try both parities. Another descriptor holding the same full key also avoids the failure.

The behavior is established for a request for a non-Taproot address of a key held only through a Taproot WIF descriptor; whether this occurs in production is unconfirmed. Resolve both parity representatives of x-only descriptor keys before the hash comparison, then let the existing stored-key check reject an unheld representative. Add a reload regression test for an odd-Y Taproot WIF, including encrypted storage.


Optional improvements

Link

[suggestion] · Taproot discovery repeats key scans and private descriptor expansion under cs_wallet

Work grows roughly with candidate keys times cached wallet pubkeys, plus repeated full private expansions, while the RPC holds cs_wallet and blocks other wallet operations.

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

Details and suggested fix

For each distinct Taproot candidate x-only key, the second discovery loop calls GetExportPubKeys on every manager. Candidates include the output key, internal key, and each recognized leaf key. Each call scans the manager's full m_map_pubkeys and roots. For every key it computes P2PKH, P2WPKH, and P2SH-P2WPKH hashes and runs IsFullyValid, although a Taproot lookup needs only the x-only comparison.

The records loop then calls ExportKey(pubkey) for each candidate on every manager. Each hit repeats ExpandFromCache, root GetKey (which decrypts encrypted wallets), and ExpandPrivateChecked for the full descriptor. With N distinct 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 private expansions. These counts are estimates, not measurements. All this work runs while the RPC holds cs_wallet.

Collect the x-only candidate set first and scan each manager once against it, or build a request-local x-only-to-SEC index. Compute only the hash form needed for the destination and memoize private expansion per (manager, index) within ExportAddressKeys. Keep the commitment checks and exact held-key verification unchanged.


Link

[suggestion] · ExportKey(script, key_id) is unused by production code and tests a different path

The public secret-returning overload has no production caller, and several unit tests exercise its binding semantics instead of the RPC 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 calls 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 exercise script-index binding on the unused overload. In the RPC path, that binding comes from candidate selection instead. This leaves an extra public API that returns secrets under different semantics and makes coverage of the real path look stronger than it is. Remove the overload and route these tests through ExportAddressKeys or ExportKey(const CPubKey&).


Link

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

The second example uses a real BIP341 test-vector address despite the developer notes' instruction to use 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, and the first example already uses EXAMPLE_ADDRESS[0]. No wallet is likely to hold the vector's key, so this is a consistency issue. Replace the address with a deliberately invalid bech32m placeholder and keep the taproot_output argument unchanged.


Coverage details