mirror of
https://github.com/bitcoin/bitcoin
synced 2026-12-04 10:44:38 +00:00
wallet: fill PSBT_GLOBAL_XPUB #36154
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!36154
Loading…
Reference in a new issue
No description provided.
Delete branch "psbt-global-xpubs"
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?
Closes #27583.
PSBT_GLOBAL_XPUBalready serializes, deserializes and merges when PSBTs are combined. Nothing writes it, so a PSBT produced by the wallet never carries it, and a signer that only has the file cannot rebuild the multisig from it. Today Specter fills the field itself after getting the PSBT from Core.Before and after with the BitBox02 and Trezor simulators
A 2-of-3
wsh(sortedmulti(...))on regtest with the simulator as one co-signer, signed through HWI 3.2.0. The "before" file is the same PSBT with itsPSBT_GLOBAL_XPUBrecords removed, so nothing else differs.This BitBox02 is not one of the cosignersWhat
parse_multisighands the Trezor for the change output:A new
Descriptoraccessor returns the extended keys a descriptor is built from, andDescriptorScriptPubKeyMan::FillPSBTwrites those of every descriptor with an input or an output in the PSBT. That coverswalletcreatefundedpsbt,walletprocesspsbt,send,sendall,psbtbumpfeeand the GUI send dialog, which all go throughCWallet::FillPSBT.Only the keys BIP 174 allows are added, the ones that derive, through unhardened steps alone, a key their descriptor produced, listed in the PSBT at the origin the descriptor gave it. So a key expression ending in
/*hadds nothing.Whoever holds a PSBT from the wallet can now derive the addresses of the descriptors it uses. Setting
bip32derivsto false inwalletcreatefundedpsbtorwalletprocesspsbtstops the wallet from adding the field, along with the derivation paths, and does not remove what a PSBT already carries.descriptorprocesspsbtis left out, since it signs from the descriptors it is given without going through the wallet, and I intend to cover it as a follow-up.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.
🚧 At least one of the CI tasks failed.
Task
NetBSD Cross: https://github.com/bitcoin/bitcoin/actions/runs/33692512279/job/100454171219LLM reason (✨ experimental): CI failed because the C++ build was stopped by a Clang
-Wthread-safety-analysiserror treated as-Werrorinsrc/wallet/scriptpubkeyman.cpp(callingAddGlobalXpubsviolates capability!cs_desc_man).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.
@jeanpablojp in the RP description, can you point to the source code in Specter where it adds the global xpub? This might be useful for those who want to verify that it's still needed. Similarly, it's useful to provide a before and after example flow with HWI (and e.g. a Trezor / simulator).
(It's also better to not to tag people in the PR description, because IIUC that text ends up in the merge commit, which then keeps triggering notifications as other projects backport it.)
Both are in the description now.
The Specter link points at
Wallet.fill_psbt, pinned to a commit so the line numbers don't drift.On the flow: I ran the Trezor T simulator first, and it signs the same PSBT with or without the field, same signature, same prompts, same screens. What differs is only the data HWI hands it, since without the field
parse_multisigfills the co-signer nodes with zeroed chain codes and no derivation paths. The device that does show a difference is the BitBox02: it refuses withThis BitBox02 is not one of the cosignersand signs once the field is there. Both runs are in the collapsed block.I had written that the Trezor needed the field. That was wrong, and the description is corrected.
Also dropped the tag, thanks for the note about the merge commit.
Thanks. It's useful to know that Trezor doesn't seem to need this, but BitBox02 and perhaps other devices do.
I suspect that once we support the new
registerdescriptorcommand in HWI, and pass that registration back to HWI when signing, none of the devices will need the global xpub. That's because HWI can construct everything it needs from the descriptor, which itself contains the xpubs.But providing global xpubs may still be useful for flows without HWI.
Please shorten your description. We don't need to know the exact details about every single device that needs to know about the global xpub.
Why is this restricted to only multisigs? Just because that is the described use case in the original issue does not mean that it is the only use case. PSBTs and Descriptors are generic objects, we should avoid doing things that are specific to one type of descriptor or psbt.
In
92bf732c4e"wallet: fill PSBT_GLOBAL_XPUB for descriptors with more than one key"This should return the map rather than taking it as an output parameter.
Do not provide
SigningProvider, either the last hardened key is already cached, or it does not exist. There is no need to take aSigningProviderhere, otherwise the wallet wouldn't function.In
92bf732c4e"wallet: fill PSBT_GLOBAL_XPUB for descriptors with more than one key"We should not make assumptions about what the caller is going to use the xpubs for. This should return the root and it's fingerprint for any key where we cannot do the normalized key thing.
In
92bf732c4e"wallet: fill PSBT_GLOBAL_XPUB for descriptors with more than one key"It is not necessary to determine what the last hardened index is, just ask the cache for it, and if the cache doesn't have that key, return the root.
In
92bf732c4e"wallet: fill PSBT_GLOBAL_XPUB for descriptors with more than one key"This is incorrect. It should traverse the participants and get their xpubs.
🐙 This pull request conflicts with the target branch and needs rebase.
@jeanpablojp, in case it saves you time: https://github.com/fametrano/bitcoin/tree/psbt-global-xpub-achow has achow101's points and the NetBSD thread-safety fix, rebased on master.
Changes: the multisig-only restriction is gone.
Descriptor::GetExtPubKeysWithOriginsreturns the map, takes noSigningProvider, and falls back to the root key when the cache lacks the last hardened one.musig()gives the participants' keys.There are two commits. The wallet one is yours, with me as co-author. Take any of it or none. To use it as it is:
git fetch https://github.com/fametrano/bitcoin psbt-global-xpub-achow && git reset --hard FETCH_HEAD, then force-push. A shorter description you could reword is below.Written with LLM, a computer and the Internet; the mistakes, as usual, are all mine.
Suggested description
Closes #27583.
BIP 174 lets a PSBT carry, in
PSBT_GLOBAL_XPUB, the extended public keys its inputs and outputs derive from. The wallet never writes the field. Some signers need them to rebuild the scripts.FillPSBTnow adds the extended keys of every descriptor that an input or output uses, unlessbip32derivsis false. For each key expression this is the key at its last hardened step when the descriptor cache holds it, and the root key otherwise. Formusig()these are the participants' keys.descriptorprocesspsbtdoes not go through the wallet and is left for a follow-up.So, by default, a PSBT from the wallet carries the account xpubs of the descriptors it uses, and whoever gets it can derive the wallet's addresses. With hardened ranged derivation (
/*h) the cache holds no such key, and the root key is published if its origin is known. Keys whose origin is unknown are left out.Earlier concerns remain unresolved; see the Run archive linked from the Review report.
🔴 Major · Single-key PSBTs disclose the whole account by default
Default
bip32derivs=truenow exposes standard single-key receive and change chains to PSBT holders, although the established interoperability need is multisig and no single-key signer requiring the field was identified.Location:
src/wallet/scriptpubkeyman.cpp:1502(head).🟠 Minor ·
send,sendall, fee-bump and GUI PSBT exports lack an xpub opt-outUnlike
walletcreatefundedpsbtandwalletprocesspsbt, these export paths provide no control to suppress eligible account xpubs, extending their pre-existing mandatory derivation metadata to account-wide address disclosure.Location:
doc/release-notes-36154.md:9(head).🔵 Suggestion · External-signer
walletprocesspsbtskips the new xpub populationBecause the pre-existing external-signer dispatch skips metadata filling, default
walletprocesspsbtdoes not add the new xpubs before signing; callers must first process withsign=false.Location:
src/wallet/scriptpubkeyman.cpp:1503(head).🔵 Suggestion · Unhardened master imports expose sibling branches
An imported descriptor using an unhardened master publishes the master xpub, exposing branches outside that descriptor; wallet-generated descriptors avoid this form, and no current caller producing it was identified.
Location:
src/script/descriptor.cpp:689(head).🔵 Suggestion · The
bip32derivstests do not protect the xpub-publication gateBoth new opt-out assertions would still pass without the explicit publication gate because neither attempts to add missing xpubs to a PSBT that already contains eligible derivation paths.
Location:
test/functional/wallet_multisig_descriptor_psbt.py:335(head).🔵 Suggestion ·
sendand fee-bump PSBT outputs lack xpub assertionsThe new tests check
walletcreatefundedpsbtandwalletprocesspsbt, but do not verify global xpubs on the separately populatedsend,sendallorpsbtbumpfeeresults.Location:
test/functional/wallet_multisig_descriptor_psbt.py:331(head).Review coverage was incomplete.
Reviewed:
8ae5b94cadcc· Review report · Previous reportspermalink
Done, it returns the map and takes only the cache.
Done, it now returns the root and its fingerprint,
/*hincluded.Done, it asks the cache and falls back to the root.
Right, done. It now merges whatever its participants return, and
wallet_musig.pychecks that every participant's xpub ends up inglobal_xpubswith the origin that leads to it.@achow101 thanks, shortened the description and dropped the multisig restriction.
@fametrano thanks for the branch. The single-key test over the four address types follows yours. Unlike your branch, keys that derive none of the PSBT's keys stay out, like the root of a
/*hexpression, which in a migrated legacy wallet is the master key.Concept ACK
One issue should be fixed before merge: a watch-only wallet holding one xpub under two origins now gets a PSBT that
decodepsbtrefuses. Withwpkh([A/84h/1h/0h]xpub/0/*)andwpkh([00000000/84h/1h/0h]xpub/1/*),walletcreatefundedpsbtandsend, which has nobip32derivsto turn it off, both report success and return it. No funds are at risk, but the error only shows when the next tool reads the file. This is the first of Ralph's two Minors; both reproduce at437936e5without an edited file.The suggestion in
AddGlobalXpubskeeps the origin already present, asMergeGlobalXPubsdoes since #35665. With all the suggestions, both reproductions exit 0, and the affected unit tests and eight functional tests pass; each regression test fails without its fix. Scripts and output: https://gist.github.com/fametrano/1cb22c31d89e06cd722dadaec3dc7230/b7f27effa3b47cd36468d9ae24407e71cf9970eeNot blocking: the root exclusion falls to two RPCs. A sender calls
descriptorprocesspsbtwithwpkh([root origin]key)on a stranger's key, then a/*hwallet that the PSBT pays adds its root inwalletprocesspsbt. That root derives no key of the wallet's descriptor, so no address is exposed, but the description says/*hadds nothing. The other two suggestions derive the key instead of comparing its path, and test it.walletcreatefundedpsbtandsendboth returned a PSBTdecodepsbtrefuses, in a wallet holding one xpub under two origins.MergeGlobalXPubsalready keeps the first origin it sees (#35665), so this uses it. With this they decode, with one record, and the PR's tests still pass. In that wallet the origin that stays follows the pointer order ofGetAllScriptPubKeyMans, so it can change across a restart.Not blocking: a path in the PSBT is only a claim, so this derives the key and compares it. It keeps the hardened check, since
CPubKey::Deriveasserts on a hardened step, and the regression test is the suggestion in the functional test. Only entries at this origin cost a derivation, and a PSBT's real key stops the search; a hostile 2 MB file of 510,000 steps took 6 to 7 s inwalletprocesspsbthere, against 0.06 s on the head.Not blocking: no edited bytes are needed, since two RPCs give the stranger's key the root's origin. This fails on the head because the root gets in, and passes with the derivation suggestion.
This fails on the head with
Duplicate Keyand passes with the suggestion inAddGlobalXpubs.Taken, thanks.
MergeGlobalXPubsnow takes the map, so the wallet calls it without the throwaway PSBT.Added, with two more cases, the same xpub twice in one descriptor and a PSBT whose sender already lists the wallet's xpub under another origin. The second also made
walletprocesspsbtreturn a PSBT that does not decode.Taken, thanks, with non-taproot entries compared as full keys rather than x-only.
Added, with a second forged output through
tr(), since the taproot comparison had no test otherwise, and a check that both claims reach the PSBT.tACK
1cc67cf916The four suggestions are in, and the new tests pin them. With each fix reverted on this head,
wallet_multisig_descriptor_psbt.pyfails: intest_one_record_per_xpubwhen the xpubs bypassMergeGlobalXPubs, and at the forged claims whenDerivesKeyInPSBTstops comparing the derived key, even in the taproot map alone. Unmodified, the descriptor, PSBT, wallet and key_io unit suites and eight wallet and PSBT functional tests pass.Not blocking: Ralph's first Minor here, the cost of deriving along unrelated paths, comes from my derivation suggestion. Beyond the 2 MB file I timed then, a 17 MB PSBT whose output lists 16,000 stranger keys at the root's origin, each 255 unhardened steps deep, kept
walletprocesspsbtbusy 73 to 96 s on a loaded 10-core machine. Ralph's fix would bound it, and I would take it here.Commands and logs: https://gist.github.com/fametrano/1cb22c31d89e06cd722dadaec3dc7230/29b33df1cda87b91430efbb654e68b901f5f73ef
🐙 This pull request conflicts with the target branch and needs rebase.
Concept ACK, global xpubs are optional, so including them is harmless, and wallets or signers that require them can use them
Good call 👍
Weak nit :-) the naming sounds like an accessor for a member variable, the
Getpart i.eWeak nit:
Read-only operation on named variables warrants explicit
const auto&as can be seen throughout the codebaseWeak nit:
with the explicit
const auto&comesthe only place I have seen .merge used in the codebase is
src/script/miniscript.h:1544: key_set.merge(*sub);where the sets are mutated. The sets used here are not large enough to warrant optimization, and .insert() has no side effects unlike .merge()Same here,
explicit const on auto& and .insert()vsmutable auto& and .merge()Same
Same
Global xpubs populated successfully after this change
master branch
psbt-global-xpubs branch
I'd keep the name, since it follows
GetPubKeyandDescriptor::GetPubKeys, which also build their result rather than read a member.I'd leave the four loops as they are. The sets come from a temporary map, so
mergemoves their nodes instead of copying them, asFlatSigningProvider::Mergedoes.Rebased, and took Ralph's two minor findings on
1cc67cf916.Also fixed the release note.