psbt: check sig sighash only if a type is given #36577

pull fametrano wants to merge 2 commits into bitcoin:master from fametrano:psbt-taproot-sighash-absent changing 2 files +105 −21
  1. fametrano commented at 8:49 PM on October 19, 2026: contributor

    SignPSBTInput() checked existing signatures against an assumed sighash type when neither PSBT_IN_SIGHASH_TYPE nor the sighashtype parameter was given: SIGHASH_DEFAULT for taproot, SIGHASH_ALL otherwise. A PSBT holding a valid 65-byte SIGHASH_ALL taproot signature could then be neither finalized nor signed further.

    BIP 174 lets a signer use any sighash type when the field is absent, so the check now runs only when the field or the parameter is given, for every input type. Without the field, RemoveUnnecessaryTransactions() now also looks at the taproot signatures for SIGHASH_ANYONECANPAY, so that the non_witness_utxo is kept. A functional test in rpc_psbt.py covers a taproot input signed with ALL, NONE and ALL|ANYONECANPAY without the field. A second commit also keeps the non_witness_utxo of a finalized taproot input, whose signatures are left only in the final witness, and tests it on a key path and a script path.

    Fixes #36507.

  2. DrahtBot added the label PSBT on Oct 19, 2026
  3. DrahtBot commented at 8:49 PM on October 19, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    <!--006a51241073e994b41acfe9ec718e94-->

    External sites

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline and AI policy for information on the review process.

    Type Reviewers
    ACK Yudis-bit
    Approach ACK Husteem

    If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore.

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #36710 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36710.svg"></sub> (test: Replace hardcoded extended keys with ExtendedKey helpers by nebula-21)
    • #36387 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36387.svg"></sub> (wallet: rpc: Deprecate encryptwallet RPC for encrypting existing wallets. by davidgumberg)
    • #36122 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36122.svg"></sub> (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.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  4. Yudis-bit commented at 8:38 AM on October 20, 2026: none

    Tested ACK 1291d0e389 on Windows.

  5. Husteem commented at 4:57 PM on October 22, 2026: none

    Approach ACK / tACK 1291d0e

    Good fix for the referenced issue. When sighash_type is unset, accepting 65-byte Taproot signatures ending in SIGHASH_ALL is safe because it commits to the identical BIP-341 digest as SIGHASH_DEFAULT. Preserving rejection for other variants keeps Core's safety checks intact.

    Tested on Ubuntu 24.04:

    • psbt_tests passed (including both new absent-field tests).
    • rpc_psbt.py passed cleanly too.
  6. in src/psbt.cpp:711 in 1291d0e389
     703 | @@ -703,11 +704,16 @@ util::Expected<void, PSBTError> SignPSBTInput(const SigningProvider& provider, P
     704 |  
     705 |      // Check all existing signatures use the sighash type
     706 |      if (sighash == SIGHASH_DEFAULT) {
     707 | -        if (!input.m_tap_key_sig.empty() && input.m_tap_key_sig.size() != 64) {
     708 | +        // If neither the field nor the parameter is provided, DEFAULT was only assumed,
     709 | +        // so a taproot signature with an explicit SIGHASH_ALL is also accepted.
     710 | +        const auto sig_matches = [&](const std::vector<unsigned char>& sig) {
     711 | +            return sig.size() == 64 || (!sighash_specified && sig.size() == 65 && sig.back() == SIGHASH_ALL);
     712 | +        };
    


    achow101 commented at 10:26 PM on November 4, 2026:

    In 1291d0e3895791bc700d4901c66d77dcbb42ed69 "psbt: accept taproot ALL sigs with sighash unset"

    I think it would be better to rewrite these checks to be dependent on the presence of the field and/or parameter, rather than assuming something about the sighash value. Allowing SIGHASH_ALL for a SIGHASH_DEFAULT seems like there is a possibility for another bug given that the sighash type is encoded into the signature message itself.


    fametrano commented at 11:20 PM on November 5, 2026:

    Done: existing signatures' sighash types are now checked only when PSBT_IN_SIGHASH_TYPE or the sighashtype parameter is given. Without either, any type is accepted, on non-taproot inputs too, as BIP 174 allows. As you suggest, this replaces the rule I argued for on #36558, which accepted only ALL besides DEFAULT. RemoveUnnecessaryTransactions also finds ANYONECANPAY in taproot signatures. The unit tests became a functional test.

  7. achow101 commented at 10:26 PM on November 4, 2026: member

    Please replace your unit tests with functional tests. We prefer functional tests for PSBTs.

  8. psbt: check sig sighash only if a type is given
    SignPSBTInput() checks that existing signatures use the input's sighash
    type. When neither PSBT_IN_SIGHASH_TYPE nor the sighash_type parameter
    was given, it checked them against an assumed default: SIGHASH_DEFAULT
    for taproot, SIGHASH_ALL otherwise. A PSBT holding a valid 65-byte
    SIGHASH_ALL taproot signature could then be neither finalized nor
    signed further.
    
    BIP 174 lets a signer use any sighash type when the field is absent,
    and asks a finalizer to fail on a mismatch only when it is present. So
    run the check only when the field or the parameter is given. This also
    lets non-taproot partial signatures use any sighash type in that case.
    
    RemoveUnnecessaryTransactions() looked only at the field to find
    SIGHASH_ANYONECANPAY. Without the field, also look at the taproot
    signatures, so that the non_witness_utxo is kept when one of them uses
    it.
    
    Fixes #36507.
    227884d5d9
  9. fametrano force-pushed on Nov 5, 2026
  10. fametrano renamed this:
    psbt: accept taproot ALL sigs with sighash unset
    psbt: check sig sighash only if a type is given
    on Nov 5, 2026
  11. psbt: look at final witness signatures for ANYONECANPAY
    A finalized input is serialized without its sighash type field and its
    taproot signatures, so RemoveUnnecessaryTransactions dropped the
    non_witness_utxos once a PSBT with a finalized ANYONECANPAY taproot
    input was processed again, for example by utxoupdatepsbt or
    walletprocesspsbt.
    
    Also check the signatures in the final witness, which are among the
    items left once the annex and, for a script path, the script and the
    control block are removed. A control block can be 65 bytes long and end
    with a byte that has 0x80 set, so it is not read as a signature.
    f169a27f3a
  12. fametrano commented at 11:57 PM on November 5, 2026: contributor

    Once a taproot input is finalized, its sighash type field and its taproot signatures are no longer serialized, so a later utxoupdatepsbt or walletprocesspsbt drops its non_witness_utxo even when it was signed with ANYONECANPAY. The second commit also reads the signatures in the final witness, through the same check as the first commit.

  13. DrahtBot added the label CI failed on Nov 5, 2026
  14. DrahtBot removed the label CI failed on Nov 6, 2026
  15. Yudis-bit commented at 2:20 PM on November 7, 2026: none

    re-ACK f169a27f3

    Verified range-diff against 1291d0e. The extra commit for finalized ANYONECANPAY witness signatures looks good, tests pass.


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-11-20 15:51 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me