Cover every OP_RETURN push encoding in the parser tests
What changed, and why it matters
This commit adds tests for how SeedSigner reads small text messages embedded in Bitcoin transactions via OP_RETURN outputs. The tests reveal that the existing parser only handled one specific encoding (OP_PUSHDATA1) and would misread or slice incorrectly when Bitcoin Core used the more common direct-push encoding for short messages. The commit does not change the parser itself; it only expands test coverage and adds helper functions to build OP_RETURN outputs safely. Because the parser bug is demonstrated but not fixed here, the change is primarily a test-hardening patch.
Review and patch PSBTParser._parse_op_return_payload to correctly parse all valid Bitcoin push encodings (direct push 0x01-0x4b, OP_PUSHDATA1/2/4), handle multiple pushes, bare OP_RETURN, and malformed/truncated scripts without crashing or mis-slicing. Then run the new tests to confirm the fix and keep the expanded test coverage.
Security signals we found
Parser assumed a single push encoding (OP_PUSHDATA1) while Bitcoin Core emits direct-push for payloads <=75 bytes
New tests demonstrate boundary cases (75/76 bytes), OP_PUSHDATA2/4, non-minimal encodings, multiple pushes, malformed/truncated length fields, and non-data-push leading opcodes
Helper comments warn that assigning through psbt.tx.vout[i] is silently discarded, which previously let tests assert against stale fixtures
No production code change in this commit; the parser bug is exposed but not fixed
OP_RETURN parsing affects what the user sees on screen, not what the transaction signs
Evidence from the diff
The diff is entirely in tests/psbt_testing_util.py and tests/test_psbt_parser.py. It introduces op_return_script(), create_op_return_output(), and create_op_return_psbt() helpers that construct OP_RETURN scriptPubKeys with minimal or forced push encodings. It then adds test_parse_op_return_payload_push_encodings() and three integration tests verifying PSBTParser.op_return_data for direct-push, OP_PUSHDATA2, and bare OP_RETURN cases. The commit message explicitly states that the parser assumed OP_PUSHDATA1 and had a ‘mis-slice’ that never showed up because all prior test vectors used that encoding. The parser source code is not modified in this commit, so the underlying mis-slice remains unpatched.
Changed components
seedsigner.models.psbt_parser.PSBTParser._parse_op_return_payloadseedsigner.models.psbt_parser.PSBTParser.op_return_datatests/psbt_testing_util.pytests/test_psbt_parser.pyInspect captured patch +193 / −2
### tests/psbt_testing_util.py
@@ -6,7 +6,9 @@
from embit.hashes import tagged_hash
from embit.networks import NETWORKS
from embit.psbt import PSBT, DerivationPath, InputScope, OutputScope
+from embit.script import Script
+from seedsigner.models.psbt_parser import OPCODES
from seedsigner.models.seed import Seed
@@ -254,3 +256,70 @@ def claim_seed_owns_key(scope: InputScope | OutputScope, claimed_derivation_path
scope.taproot_bip32_derivations[public_key] = (leaf_hashes or [], derivation_path)
else:
scope.bip32_derivations[public_key] = derivation_path
+
+
+def op_return_script(payload: bytes, push_opcode: int = None) -> Script:
+ """
+ Build an OP_RETURN scriptPubKey carrying `payload`.
+
+ Defaults to the minimal push encoding, which is what Bitcoin Core emits: a direct
+ push for 75 bytes or fewer, OP_PUSHDATA1 up to 255, OP_PUSHDATA2 up to 65535, and
+ OP_PUSHDATA4 above that.
+
+ `push_opcode` forces a specific OP_PUSHDATA* instead, so a test can build the
+ non-minimal encodings that a coordinator is still free to produce.
+ """
+ if push_opcode is None:
+ if len(payload) <= OPCODES.OP_PUSHDATA_MAX_DIRECT:
+ push = bytes([len(payload)])
+ elif len(payload) <= 0xff:
+ push = bytes([OPCODES.OP_PUSHDATA1, len(payload)])
+ elif len(payload) <= 0xffff:
+ push = bytes([OPCODES.OP_PUSHDATA2]) + len(payload).to_bytes(2, "little")
+ else:
+ push = bytes([OPCODES.OP_PUSHDATA4]) + len(payload).to_bytes(4, "little")
+ else:
+ length_size = {OPCODES.OP_PUSHDATA1: 1, OPCODES.OP_PUSHDATA2: 2, OPCODES.OP_PUSHDATA4: 4}[push_opcode]
+ push = bytes([push_opcode]) + len(payload).to_bytes(length_size, "little")
+
+ return Script(bytes([OPCODES.OP_RETURN]) + push + payload)
+
+
+def create_op_return_output(payload: bytes, value: int = 0, script_pubkey: Script = None) -> OutputScope:
+ """
+ Create an OP_RETURN OutputScope that can be appended to a PSBT's `outputs`.
+
+ Pass `script_pubkey` to supply a hand-built script (a bare OP_RETURN, several pushes,
+ a deliberately malformed one); otherwise `payload` is wrapped in a minimal push.
+
+ Note that the scriptPubKey and value have to be set on the OutputScope. `PSBT.tx`
+ rebuilds the whole transaction from the scopes on every access and `OutputScope.vout`
+ rebuilds its TransactionOutput, so assigning through `psbt.tx.vout[i]` is silently
+ discarded and would leave a test asserting against the fixture it meant to replace.
+ """
+ output = OutputScope()
+ output.script_pubkey = script_pubkey if script_pubkey is not None else op_return_script(payload)
+ output.value = value
+ return output
+
+
+def create_op_return_psbt(op_return_outputs: list, change_amount: int = None, fee_amount: int = 5_000) -> PSBT:
+ """
+ A single-sig native segwit psbt with one change output and the given OP_RETURN
+ OutputScopes (see `create_op_return_output`) appended after it, in order.
+
+ `change_amount` defaults to whatever is left of the input after the OP_RETURN values
+ and the fee, so the psbt balances.
+ """
+ psbt = PSBT.parse(a2b_base64(PSBTTestData.SINGLE_SIG_NATIVE_SEGWIT_1_INPUT))
+ input_amount = sum([inp.utxo.value for inp in psbt.inputs])
+ op_return_amount = sum([output.value for output in op_return_outputs])
+
+ if change_amount is None:
+ change_amount = input_amount - op_return_amount - fee_amount
+
+ psbt.outputs.clear()
+ psbt.outputs.append(create_output(PSBTTestData.SINGLE_SIG_NATIVE_SEGWIT_CHANGE, change_amount))
+ for output in op_return_outputs:
+ psbt.outputs.append(output)
+ return psbt
### tests/test_psbt_parser.py
@@ -12,7 +12,7 @@
from embit.descriptor import Descriptor
from embit.script import Script
-from seedsigner.models.psbt_parser import (PSBTInputAmountVerificationError,
+from seedsigner.models.psbt_parser import (OPCODES, PSBTInputAmountVerificationError,
PSBTInputOwnershipClaimError, PSBTMissingInputUtxoError,
PSBTMixedDerivationPathTypesError, PSBTNegativeFeeError, PSBTOutputOwnershipClaimError,
PSBTOutputOwnershipContradictionError, PSBTParser, PSBTSeedCannotSignError,
@@ -21,7 +21,8 @@
from seedsigner.models.settings_definition import SettingsConstants
from psbt_testing_util import (NUMS_INTERNAL_KEY, PSBTTestData, claim_seed_owns_key,
- create_output, foreign_public_key, p2tr_with_script_tree, root_for_seed, tapleaf_hash)
+ create_op_return_output, create_op_return_psbt, create_output, foreign_public_key,
+ op_return_script, p2tr_with_script_tree, root_for_seed, tapleaf_hash)
@@ -2704,3 +2705,124 @@ def test__parse__accepts_a_zero_fee(self):
psbt.outputs[0].value = sum(inp.utxo.value for inp in psbt.inputs)
assert self._parse(psbt).fee_amount == 0
+
+
+
+def test_parse_op_return_payload_push_encodings():
+ """
+ Should extract the payload whichever push opcode carries it.
+
+ Opcodes 0x01-0x4b are themselves the byte count; OP_PUSHDATA1, 2, and 4 are
+ followed by a 1, 2, or 4 byte little-endian length. Bitcoin Core emits the
+ minimal encoding, so a payload of 75 bytes or fewer arrives as a direct push and
+ the push opcode has to be read rather than assumed. 75/76 is the boundary between
+ the two.
+ """
+ # Pin the constants: every assertion below is written in terms of them, so a typo
+ # in one would otherwise make this test agree with the bug it is meant to catch.
+ assert (OPCODES.OP_RETURN, OPCODES.OP_PUSHDATA_MAX_DIRECT) == (0x6a, 0x4b)
+ assert (OPCODES.OP_PUSHDATA1, OPCODES.OP_PUSHDATA2, OPCODES.OP_PUSHDATA4) == (0x4c, 0x4d, 0x4e)
+
+ op_return = bytes([OPCODES.OP_RETURN])
+
+ # Direct push: the encoding Bitcoin Core produces for payloads up to 75 bytes
+ for length in [1, 2, 40, OPCODES.OP_PUSHDATA_MAX_DIRECT]:
+ payload = b"A" * length
+ assert PSBTParser._parse_op_return_payload(op_return + bytes([length]) + payload) == payload
+
+ # OP_PUSHDATA1 carries a 1-byte length; 76 is the first payload that needs it
+ for length in [76, 80, 0xff]:
+ payload = b"B" * length
+ script_data = op_return + bytes([OPCODES.OP_PUSHDATA1, length]) + payload
+ assert PSBTParser._parse_op_return_payload(script_data) == payload
+
+ # OP_PUSHDATA2 carries a 2-byte length. Now that Bitcoin Core v30 defaults
+ # `-datacarriersize` to 100,000 bytes this is an ordinary encoding, not an exotic one.
+ payload = b"C" * 300
+ script_data = op_return + bytes([OPCODES.OP_PUSHDATA2]) + len(payload).to_bytes(2, "little") + payload
+ assert PSBTParser._parse_op_return_payload(script_data) == payload
+
+ # OP_PUSHDATA4 carries a 4-byte length
+ payload = b"D" * 300
+ script_data = op_return + bytes([OPCODES.OP_PUSHDATA4]) + len(payload).to_bytes(4, "little") + payload
+ assert PSBTParser._parse_op_return_payload(script_data) == payload
+
+ # A non-minimal encoding is unusual but a coordinator is free to produce one
+ payload = b"E" * 10
+ script_data = op_return + bytes([OPCODES.OP_PUSHDATA2]) + len(payload).to_bytes(2, "little") + payload
+ assert PSBTParser._parse_op_return_payload(script_data) == payload
+
+ # Several pushes in one script are legal; they are concatenated
+ script_data = op_return + bytes([4]) + b"data" + bytes([4]) + b"more"
+ assert PSBTParser._parse_op_return_payload(script_data) == b"datamore"
+
+ # Bare OP_RETURN, nothing pushed
+ assert PSBTParser._parse_op_return_payload(op_return) == b""
+
+ # An empty scriptPubKey must not index past the end
+ assert PSBTParser._parse_op_return_payload(b"") == b""
+
+ # A push claiming more data than the script carries yields what is actually there,
+ # rather than raising or returning nothing
+ assert PSBTParser._parse_op_return_payload(op_return + bytes([10]) + b"abc") == b"abc"
+
+ # A truncated OP_PUSHDATA2 length field, same rule
+ assert PSBTParser._parse_op_return_payload(op_return + bytes([OPCODES.OP_PUSHDATA2, 0x01])) == bytes([OPCODES.OP_PUSHDATA2, 0x01])
+
+ # A leading opcode that is not a data push. The remaining bytes are still surfaced;
+ # the user has to be able to see everything the transaction commits to.
+ assert PSBTParser._parse_op_return_payload(op_return + bytes([0x00, 0x51])) == bytes([0x00, 0x51])
+
+
+
+def test_parse_op_return_content_direct_push():
+ """
+ Should parse an OP_RETURN whose payload uses a direct push.
+
+ This is the canonical encoding: `bitcoin-tx outdata=<hex>` and any Bitcoin Core
+ wallet produce it for payloads of 75 bytes or fewer.
+ """
+ message = "Chancellor on the brink of third bailout".encode()
+ assert len(message) <= OPCODES.OP_PUSHDATA_MAX_DIRECT
+
+ script_pubkey = op_return_script(message)
+ assert script_pubkey.data[:2] == bytes([OPCODES.OP_RETURN, len(message)])
+
+ psbt = create_op_return_psbt([create_op_return_output(message)])
+ psbt_parser = PSBTParser(p=psbt, seed=PSBTTestData.seed, network=SettingsConstants.REGTEST)
+
+ assert psbt_parser.op_return_data == message
+
+
+
+def test_parse_op_return_content_pushdata2():
+ """
+ Should parse an OP_RETURN payload large enough to require OP_PUSHDATA2.
+
+ Bitcoin Core v30 raised the default `-datacarriersize` to 100,000 bytes, so
+ payloads past the 255 bytes OP_PUSHDATA1 can describe now arrive from honest
+ coordinators. Consensus has never limited OP_RETURN data at all, so no payload
+ size can be assumed either way.
+ """
+ message = b"F" * 300
+
+ script_pubkey = op_return_script(message)
+ assert script_pubkey.data[:2] == bytes([OPCODES.OP_RETURN, OPCODES.OP_PUSHDATA2])
+
+ psbt = create_op_return_psbt([create_op_return_output(message)])
+ psbt_parser = PSBTParser(p=psbt, seed=PSBTTestData.seed, network=SettingsConstants.REGTEST)
+
+ assert psbt_parser.op_return_data == message
+
+
+
+def test_parse_op_return_with_no_payload():
+ """
+ A bare OP_RETURN output carries no data and must parse to empty, not crash.
+ """
+ psbt = create_op_return_psbt([
+ create_op_return_output(b"", script_pubkey=script.Script(bytes([OPCODES.OP_RETURN])))
+ ])
+ psbt_parser = PSBTParser(p=psbt, seed=PSBTTestData.seed, network=SettingsConstants.REGTEST)
+
+ assert psbt_parser.op_return_data == b""Why this scored 41/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.