Skip to content

Parse OP_RETURN payloads that use a minimal push opcode - #965

Closed
ruipereira1 wants to merge 1 commit into
SeedSigner:devfrom
ruipereira1:fix/op-return-push-encoding
Closed

ruipereira1 wants to merge 1 commit into
SeedSigner:devfrom
ruipereira1:fix/op-return-push-encoding

Conversation

@ruipereira1

@ruipereira1 ruipereira1 commented Aug 1, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #963

Description

Problem or Issue being addressed

PSBTParser._parse_outputs() extracts the OP_RETURN payload at a fixed offset:

if self.psbt.tx.vout[i].script_pubkey.data[0] == OPCODES.OP_RETURN:
    # The data is written as: OP_RETURN + OP_PUSHDATA1 + len(payload) + payload
    self.op_return_data = self.psbt.tx.vout[i].script_pubkey.data[3:]

That comment only holds for payloads of 76–80 bytes. Bitcoin Core emits the minimal
push encoding, so a payload of 75 bytes or fewer is written as
OP_RETURN + <len> + payload — the prefix is two bytes, not three, and [3:] eats the
payload's first byte.

Reproduction (40-byte payload, so a direct push is used):

scriptPubKey:  6a 28 "Chancellor on the brink of third bailout"
parsed:           b'hancellor on the brink of third bailout'
expected:         b'Chancellor on the brink of third bailout'

PSBTOpReturnView is a pre-signing review screen, so the user approves a transaction
whose OP_RETURN content was displayed incorrectly. When the payload isn't human-readable
it renders as hex, where a one-byte shift isn't noticeable at all.

This wasn't caught because every existing test vector encodes OP_PUSHDATA1 explicitly:
tests/screenshot_generator/generator.py:84-88 builds it by hand, and the regtest PSBT in
test_parse_op_return_content was built the same way (... 2b 6a 4c 28 43 68 61 6e ...).

Solution

Added PSBTParser._parse_op_return_payload(), which reads the push opcode instead of
assuming its width, and slices to the declared length so trailing script bytes aren't
swept into the payload. Handles:

  • direct push (0x01–0x4b) — the opcode is the length
  • OP_PUSHDATA1 + 1-byte length
  • bare OP_RETURN with no payload → b""
  • anything else → everything after OP_RETURN, so the user still sees raw bytes rather
    than nothing (OP_PUSHDATA2/4 can't fit in a standard 80-byte OP_RETURN)

Additional Information

Two tests added to tests/test_psbt_parser.py:

  • test_parse_op_return_payload_push_encodings — both encodings plus the 75/80-byte
    boundaries, empty payload, and over-long script data.
  • test_parse_op_return_content_minimal_push — the existing regtest PSBT with its
    OP_RETURN output re-encoded minimally. On dev this fails with b'hancellor...'.

Note for anyone writing a similar test: embit's PSBT.tx is a derived property, so
mutating psbt.tx.vout[i] is silently discarded — the scriptPubKey has to be replaced on
psbt.outputs[i]. My first attempt at this test passed against both patched and unpatched
code for exactly that reason.

Screenshots

No new or modified screens — PSBTOpReturnScreen is unchanged; it now receives the
correct bytes.


This pull request is categorized as a:

  • New feature
  • Bug fix
  • Code refactor
  • Documentation
  • Other

Checklist

I ran pytest locally

  • All tests passed before submitting the PR
  • I couldn't run the tests
  • N/A

186 passed. 4 tests in test_seedqr.py that decode QR images could not run in my local environment because libzbar does not load on Windows; they fail identically on an unmodified dev checkout and are unrelated to this change. They pass in CI, which installs libzbar0.


I included screenshots of any new or modified screens

Should be part of the PR description above.

  • Yes
  • No
  • N/A

I added or updated tests

  • Yes
  • No, I'm a fool
  • N/A

I tested this PR hands-on on the following platform(s):

  • Raspberry Pi OS Manual Build
  • SeedSigner OS on a Pi0/Pi0W board
  • Emulator

Not tested on hardware or the emulator — I don't have a device. The change is confined to byte-slicing in the parser and is covered by unit tests. Happy for a maintainer to verify on-device, or I can follow up once I have a board.


I have reviewed these notes:

  • I understand

The parser assumed the payload is always preceded by OP_PUSHDATA1 and sliced
the scriptPubKey at a fixed offset of 3. That encoding is only required for
payloads of 76-80 bytes; anything up to 75 bytes uses a single-byte direct
push, which is what Bitcoin Core emits. For those the fixed offset drops the
payload's first byte, so the OP_RETURN review screen shows the user data that
differs from what they are about to sign.

Read the push opcode instead of assuming its width, and slice to the declared
length so trailing bytes are not included.

The existing test vectors all encode OP_PUSHDATA1 explicitly, which is why
this was not caught.
@ruipereira1 ruipereira1 mentioned this pull request Aug 1, 2026
5 of 18 tasks
3rdIteration added a commit to 3rdIteration/seedsigner that referenced this pull request Sep 18, 2026
…ia descriptor

From a review of open upstream SeedSigner PRs and their discussions:

- Previous-tx proof (upstream SeedSigner#995/SeedSigner#977): a non_witness_utxo must hash to the
  txid its input spends (UTXO_MISMATCH), and a legacy p2sh input may not rely
  on a witness_utxo alone (INVALID_WITNESS_UTXO). Legacy signatures commit to
  no amount, so either lie hides fee in a single signing.
- Multisig change without global xpubs (review thread on upstream SeedSigner#1032): a
  change-shaped m-of-n containing our key is not provably this wallet's. It is
  now a payment unless a loaded descriptor identifies it; the new
  PSBTIdentifyChangeView offers "Load descriptor to identify change", and
  returning with a descriptor re-reads the psbt. verify_multisig_output's
  per-entry check is shared as _descriptor_owns_output.
- OP_RETURN payload read from its push opcode (SeedSigner#965/SeedSigner#1003): direct pushes lost
  their first byte on screen.
- INCONSISTENT_FINGERPRINTS (SeedSigner#1040): a global xpub whose fingerprint disagrees
  with the keys it derives.
- Untrusted-QR crashes (SeedSigner#967/SeedSigner#968/SeedSigner#970/SeedSigner#971): SeedQR checksum and whole-payload
  detection, signmessage bounds, parse_derivation_path on short/48h paths, and
  a UR2 seq_len bound.

New psbt_faker vectors cover each (PREV_TX_TXID_MISMATCH, LEGACY_P2SH_WITNESS_ONLY,
OP_RETURN_DIRECT_PUSH, TX-24.xpub_fingerprint_mismatch, TX-21.ms_foreign_quorum*,
TX-21.ms_honest_change_no_xpubs). TX-20 and XTRAS.MIXED_INPUT_TYPES are
re-vendored after generator fixes; TX-20 is no longer vacuous.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
3rdIteration added a commit to 3rdIteration/seedsigner that referenced this pull request Sep 18, 2026
* test+fix: resync psbt_faker corpus and close the gaps it exposes

Re-vendor the regenerated psbt_faker corpus (97 vectors, fixed BIP-371
taproot encoding) and cover every vector in psbt_suite_util.

Parser fixes for vectors that previously parsed:
- FORGED_OUTPUT_OWNERSHIP also when a genuine key of ours annotates an
  output whose script does not pay it (TX-21.claims_us_pays_other,
  TX-21.ms_repointed_*, TX-22.ms_decoy_*).
- MISLABELED_OUTPUT_OWNERSHIP: an output paying our key but attributed to
  a foreign fingerprint (TX-21.pays_us_claims_other, taproot_claims_other,
  ms_fp_relabeled).
- SURPLUS_DERIVATIONS / MIXED_DERIVATION_MAPS for contradictory claims
  (TX-22.surplus_paths, taproot_surplus_internal_key, mixed_path_maps*).
- SCRIPT_TIMELOCK warning for CLTV/CSV in an output's committed script
  (TX-20.cltv_time).
- verify_multisig_output checks every derivation entry instead of embit's
  first-match Descriptor.owns (decoy-last used to verify as ours).

TX-20.cltv_height/csv_relative are vacuous upstream (scriptPubKey is still
p2wpkh) and marked so.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* test: stop flow tests hanging when collected after the refusal-screen tests

tests/test_psbt_refusal_screens.py imported the GUI before tests/base.py
installed its renderer and hardware-button doubles. Any flow test collected
after it then ran against the real, never-configured Renderer (every View and
the error View raise, so the Controller loops forever) or against real buttons
(real-screen flows wait for a key press forever). The full suite only passed
because alphabetical order puts the flow tests first.

The refusal-screen test now imports base first, and base.py also rebinds
seedsigner.gui.Renderer when the package was already imported by some other
module collected earlier.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* fix: port upstream PSBT/QR hardening; identify multisig change only via descriptor

From a review of open upstream SeedSigner PRs and their discussions:

- Previous-tx proof (upstream SeedSigner#995/SeedSigner#977): a non_witness_utxo must hash to the
  txid its input spends (UTXO_MISMATCH), and a legacy p2sh input may not rely
  on a witness_utxo alone (INVALID_WITNESS_UTXO). Legacy signatures commit to
  no amount, so either lie hides fee in a single signing.
- Multisig change without global xpubs (review thread on upstream SeedSigner#1032): a
  change-shaped m-of-n containing our key is not provably this wallet's. It is
  now a payment unless a loaded descriptor identifies it; the new
  PSBTIdentifyChangeView offers "Load descriptor to identify change", and
  returning with a descriptor re-reads the psbt. verify_multisig_output's
  per-entry check is shared as _descriptor_owns_output.
- OP_RETURN payload read from its push opcode (SeedSigner#965/SeedSigner#1003): direct pushes lost
  their first byte on screen.
- INCONSISTENT_FINGERPRINTS (SeedSigner#1040): a global xpub whose fingerprint disagrees
  with the keys it derives.
- Untrusted-QR crashes (SeedSigner#967/SeedSigner#968/SeedSigner#970/SeedSigner#971): SeedQR checksum and whole-payload
  detection, signmessage bounds, parse_derivation_path on short/48h paths, and
  a UR2 seq_len bound.

New psbt_faker vectors cover each (PREV_TX_TXID_MISMATCH, LEGACY_P2SH_WITNESS_ONLY,
OP_RETURN_DIRECT_PUSH, TX-24.xpub_fingerprint_mismatch, TX-21.ms_foreign_quorum*,
TX-21.ms_honest_change_no_xpubs). TX-20 and XTRAS.MIXED_INPUT_TYPES are
re-vendored after generator fixes; TX-20 is no longer vacuous.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* test(screenshots): give the multisig PSBT global xpubs; add PSBTIdentifyChangeView

The screenshot generator's multisig PSBT carries no PSBT_GLOBAL_XPUB, so since
multisig change is only identified through global xpubs or a loaded descriptor,
it had no change output and the PSBTChangeDetailsView_multisig_* screenshots
crashed (get_change_data returned None), failing CI's "Generate screenshots".

- The multisig screenshots now use a copy of that PSBT with global xpubs taken
  from its own wallet descriptor, like a coordinator that includes them.
- The parser is built with the loaded descriptor, and the descriptor is loaded
  before the PSBT, so the "verified" screenshot parses the way the device would.
- New screenshots for PSBTIdentifyChangeView (no descriptor / descriptor loaded),
  from the original xpub-less PSBT.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
@Chaitanya-Keyal

Copy link
Copy Markdown
Contributor

This can be closed now that #1042 has merged, which includes this fix along with the other push encodings. Thanks @ruipereira1!

@newtonick

Copy link
Copy Markdown
Collaborator

Closing since it was replaced by #1042

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OP_RETURN payload is mis-parsed when the minimal push opcode is used

4 participants