Skip to content

rpc: add key-origin modes to PSBT processing RPCs - #35370

Open
junbyjun1238 wants to merge 2 commits into
bitcoin:masterfrom
junbyjun1238:rpc-strip-psbt-bip32-derivs
Open

junbyjun1238 wants to merge 2 commits into
bitcoin:masterfrom
junbyjun1238:rpc-strip-psbt-bip32-derivs

Conversation

@junbyjun1238

@junbyjun1238 junbyjun1238 commented May 25, 2026 •

Copy link
Copy Markdown
Contributor

This PR extends the existing bip32derivs argument of the
walletprocesspsbt and descriptorprocesspsbt RPCs with explicit key-origin
modes.

Motivation

Setting bip32derivs=false prevents new key-origin fields from being added, but
does not explicitly remove fields already present in an input PSBT. Some
collaborative PSBT workflows need to remove that metadata before sharing the
PSBT.

Behavior

  • true or "add" allows known key-origin fields to be added.
  • false or "preserve" does not add or explicitly remove key-origin fields.
  • "strip" does not add key-origin fields and removes surviving standard
    key-origin fields after normal processing.

The default remains true. The existing boolean values remain accepted for
compatibility.

Stripping removes global xpubs and legacy and Taproot BIP32 derivation fields
from inputs and outputs. Unknown and proprietary PSBT fields are left
unchanged. Finalization may independently remove input key-origin fields.

Closes #30294.

@DrahtBot

DrahtBot commented May 25, 2026 •

Copy link
Copy Markdown
Contributor

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.

Type Reviewers
Concept ACK rkrux
Approach ACK willcl-ark

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:

  • #36744 (refactor: fix mismatched and undefined declarations by fanquake)
  • #36743 ([RFC] build: build with -Wmissing-declarations/prototypes by fanquake)
  • #36384 (rpc: Use self.Arg<_>(name) helper over manual and duplicate parsing by maflcko)
  • #36167 ([RFC] Enable -Wunused by fanquake)
  • #36154 (wallet: fill PSBT_GLOBAL_XPUB by jeanpablojp)
  • #36122 (BIP460: CISA for Taproot key path spends by fjahr)

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.

@willcl-ark willcl-ark left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approach ACK.

This looks like the correct approach (in contrast to #30341), putting strip functionality in walletprocesspsbt (and also descriptorprocesspsbt).

@achow101

Copy link
Copy Markdown
Member

The option does more than stripping bip32 derivation paths and it's name should reflect that.

I'm not sure about stacking yet another bool argument. Maybe it would be better to convert the existing bip32derivs into a enum/choices argument.

@rkrux

rkrux commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

Concept ACK 2ec9650 for stripping away the bip32 derivations data if the user intends so.

I'm not sure about stacking yet another bool argument. Maybe it would be better to convert the existing bip32derivs into a enum/choices argument.

I'm inclining to agree with this. I think it'd be good to avoid having a new similar looking argument that sort of overlaps with the existing one (based on what's described in the "Behaviour" section). I also believe that updating the existing bip32derivs to accept an enum would be worth the breaking change. Fortunately, the name of the bip32derivs argument already is generic enough that it's functionality can be extended.

@junbyjun1238 junbyjun1238 changed the title rpc: add stripbip32derivs to PSBT processing RPCs rpc: add key-origin modes to PSBT processing RPCs Jul 10, 2026
@junbyjun1238

junbyjun1238 commented Jul 10, 2026 •

Copy link
Copy Markdown
Contributor Author

Reworked in e5adbdfbe9. The separate stripbip32derivs boolean has been removed, and the existing bip32derivs argument now accepts "add", "preserve", and "strip" for both processing RPCs. Existing true and false values remain aliases for "add" and "preserve" so current callers keep their behavior.

The helper and RPC documentation now describe the full key-origin scope: global xpubs and legacy and Taproot BIP32 derivation fields. Normal processing and finalization behavior remain unchanged.

@DrahtBot

Copy link
Copy Markdown
Contributor

🚧 At least one of the CI tasks failed.
Task tidy: https://github.com/bitcoin/bitcoin/actions/runs/29073641590/job/86300384551
LLM reason (✨ experimental): CI failed because clang-tidy reported a performance-unnecessary-copy-initialization warning in src/rpc/rawtransaction_util.cpp (warnings-as-errors), causing the clang-tidy step to fail.

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.

@maflcko maflcko left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread src/rpc/rawtransaction_util.h Outdated
Comment on lines +24 to +26
ADD,
PRESERVE,
STRIP,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: would be good to avoid ALL_CAPS going forward for new code. The dev notes aren't updated yet (c.f. #35588 (comment)), but lately there was a scripted-diff roughly every two weeks to rename an ALL_CAPS to PascalCase or snake_case, due to a third-party macro clash.

Comment thread test/functional/rpc_psbt.py Outdated
finalize=False,
)

cli_stripped = self.nodes[1].cli.walletprocesspsbt(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test supports --usecli, so why is this duplicate?

@junbyjun1238

Copy link
Copy Markdown
Contributor Author

I authored the original PR and the initial revisions myself. For these additional changes, I used an LLM as a tool. Unfortunately, I failed to properly catch the issues before pushing. I have now personally reviewed both the original and current changes, and I fully understand the content. I have also reviewed the AI policy, and going forward, I will take personal responsibility for reviewing changes and responding to feedback.

@maflcko

maflcko commented Jul 10, 2026

Copy link
Copy Markdown
Member

Tests

* `cmake --build build --target bitcoind bitcoin-cli --parallel 2`

* `python3 test/functional/rpc_psbt.py --configfile=build/test/config.ini`

* `python3 test/functional/rpc_help.py --configfile=build/test/config.ini`

* `python test/lint/lint-includes.py`

* `python test/lint/lint-files.py`

* `python test/lint/lint-tests.py`

You can remove this whole LLM generated redundant section. A human can see that you modified test/functional/rpc_psbt.py and can see that this is a test meant for the change.

Remove global xpubs and input and output BIP32 derivation fields
while leaving other PSBT data unchanged.
Allow bip32derivs to select add, preserve, or strip behavior in
walletprocesspsbt and descriptorprocesspsbt. Keep true and false as
aliases for add and preserve.

@fametrano fametrano left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With "strip", a tr(musig(A,B)/<0;1>/*) wallet makes no MuSig2 nonce and no partial signature on a PSBT that has no key origins yet. The signer needs the aggregate key's derivation path to find the tweak, and "strip" runs it with key origins hidden, as false already does on master.

I tested af75215 on top of master 7988380 with a 2-of-2 MuSig2 wallet pair. With "add" both rounds work and the transaction is accepted. With "strip", walletprocesspsbt returns no musig2_pubnonces. Legacy, p2sh-segwit, wpkh, taproot key and script path, and wsh multisig all sign fine with "strip".

Processing with origins and stripping at the end fixes the signing, as in the two inline suggestions. With that change I get nonces and partial signatures, the outputs still carry no derivations, and rpc_psbt.py passes. finalizepsbt still cannot finalize the stripped PSBT, because the aggregate's derivation is gone. I have not tried finalizing inside the walletprocesspsbt call.

Comment thread src/wallet/rpc/spend.cpp
bool sign = request.params[1].isNull() ? true : request.params[1].get_bool();
bool bip32derivs = request.params[3].isNull() ? true : request.params[3].get_bool();
const PSBTKeyOriginMode key_origin_mode{ParseBip32DerivsMode(self.Arg<UniValue>("bip32derivs"))};
const bool bip32derivs{key_origin_mode == PSBTKeyOriginMode::Add};

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fill with key origins and strip them afterwards, so "strip" can still sign MuSig2 inputs:

Suggested change
const bool bip32derivs{key_origin_mode == PSBTKeyOriginMode::Add};
const bool bip32derivs{key_origin_mode != PSBTKeyOriginMode::Preserve};

std::optional<int> sighash_type = ParseSighashString(request.params[2]);
bool bip32derivs = request.params[3].isNull() ? true : request.params[3].get_bool();
const PSBTKeyOriginMode key_origin_mode{ParseBip32DerivsMode(self.Arg<UniValue>("bip32derivs"))};
const bool bip32derivs{key_origin_mode == PSBTKeyOriginMode::Add};

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same change as in walletprocesspsbt:

Suggested change
const bool bip32derivs{key_origin_mode == PSBTKeyOriginMode::Add};
const bool bip32derivs{key_origin_mode != PSBTKeyOriginMode::Preserve};

@fametrano fametrano left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bip32derivs=false is only tested on a PSBT without key origins, where "preserve" and "strip" give the same result. So mapping false to Strip in ParseBip32DerivsMode still passes rpc_psbt.py. One check after the "preserve" call on psbt_with_derivs catches it, as in the inline suggestion. It passes on af75215 and fails with that change.

decoded = self.nodes[0].decodepsbt(preserved)
assert "bip32_derivs" in decoded["inputs"][0]
assert "bip32_derivs" in decoded["outputs"][0]
assert_equal(len(decoded["global_xpubs"]), 1)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert_equal(len(decoded["global_xpubs"]), 1)
assert_equal(len(decoded["global_xpubs"]), 1)
assert_equal(self.nodes[1].walletprocesspsbt(psbt=psbt_with_derivs, sign=False, sighashtype="ALL", bip32derivs=False, finalize=False)["psbt"], preserved)

@fametrano

Copy link
Copy Markdown

With "strip", the change output still carries PSBT_OUT_MUSIG2_PARTICIPANT_PUBKEYS. For a tr(musig(A,B)/<0;1>/*) wallet these are the aggregate and participant keys, the same for every address, so they link the wallet's PSBTs as a fingerprint would. Should "strip" remove them from outputs too, or should the help say they stay?

@DrahtBot

DrahtBot commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

🐙 This pull request conflicts with the target branch and needs rebase.

removes surviving global xpubs and legacy and Taproot BIP32 derivation
fields after normal processing. The default remains `true`, equivalent to
`"add"`; `false` remains accepted as an alias for `"preserve"`.
Finalization may remove input key-origin fields in any mode.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Finalization may remove input key-origin fields in any mode.
Finalization may remove input key-origin fields in any mode. (#35370)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Setting bip32derivs to false with walletprocesspsbt includes bip32_derivs for outputs.

8 participants