Parse the OP_RETURN push opcode instead of assuming OP_PUSHDATA1
What changed, and why it matters
This commit fixes a bug in SeedSigner, a hardware wallet tool, where the OP_RETURN message shown to users before signing a transaction could be displayed incorrectly. The old code assumed every OP_RETURN payload started with a specific push opcode (OP_PUSHDATA1), so for shorter messages it accidentally cut off the first byte. Because the screen is shown before the user approves the signature, a misleading display could make someone approve a transaction whose embedded message is not what they saw. For non-human-readable payloads shown as hex, the error is invisible. The fix properly reads the push opcode and extracts the correct payload length.
Review and merge the patch, then verify OP_RETURN display for direct pushes, OP_PUSHDATA1/2/4, multiple pushes, truncated scripts, and empty scriptPubKey. Consider adding unit tests for these script patterns in the PSBT parser.
Security signals we found
UI misrepresentation of transaction data before signing
Incorrect fixed-offset parsing of script opcodes
OP_RETURN display bug could mislead user approval
Missing reset of op_return_data across parses
Potential IndexError on empty scriptPubKey
Evidence from the diff
The patch corrects OP_RETURN payload parsing in PSBTOpReturnView via PSBTParser._parse_op_return_payload. Previously, vout[i].script_pubkey.data[3:] was used unconditionally, which only works for OP_PUSHDATA1 (0x4c) scripts. Bitcoin Core minimally encodes pushes of 75 bytes or fewer using direct push opcodes 0x01-0x4b, so those payloads lost their first byte. The new helper parses direct pushes, OP_PUSHDATA1/2/4, concatenates multiple pushes, and appends remaining bytes verbatim on truncation or non-push opcodes. It also resets op_return_data per parse and avoids an IndexError on empty scriptPubKey by comparing script_data[:1] instead of script_data[0].
Changed components
src/seedsigner/models/psbt_parser.pyPSBTOpReturnView pre-signing review screenPSBTParser._parse_outputsOP_RETURN output parsingInspect captured patch +75 / −3
### src/seedsigner/models/psbt_parser.py
@@ -15,7 +15,13 @@
class OPCODES:
OP_RETURN = 106
+
+ # Opcodes 0x01-0x4b are themselves the number of bytes to push, so 75 is the largest
+ # payload a direct push can carry.
+ OP_PUSHDATA_MAX_DIRECT = 75
OP_PUSHDATA1 = 76
+ OP_PUSHDATA2 = 77
+ OP_PUSHDATA4 = 78
@@ -510,6 +516,7 @@ def _parse_outputs(self, child_key_derivation_cache: dict):
self.fee_amount = 0
self.destination_addresses = []
self.destination_amounts = []
+ self.op_return_data = None
# Asking the PSBT for its transaction rebuilds that entire transaction from
# scratch on every single request. The outputs are consulted a dozen times
@@ -719,9 +726,9 @@ def _parse_outputs(self, child_key_derivation_cache: dict):
# an omitted optional field is not a contradiction.
raise PSBTOutputOwnershipContradictionError(f"Output claims this seed at {bip32.path_to_str(verified_derivation_paths[0].derivation)} but its committed script contradicts that")
- if 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 = vout[i].script_pubkey.data[3:]
+ script_data = vout[i].script_pubkey.data
+ if script_data[:1] == bytes([OPCODES.OP_RETURN]):
+ self.op_return_data = PSBTParser._parse_op_return_payload(script_data)
elif is_presumed_change:
# Remember that "change" in this function is ANY output coming back to our
@@ -782,6 +789,71 @@ def sig_count(tx):
return cnt
+ @staticmethod
+ def _parse_op_return_payload(script_data: bytes) -> bytes:
+ """
+ Extracts the data an OP_RETURN output carries, for every way the script can push
+ it.
+
+ Script pushes data with a variable-width opcode: opcodes 0x01 through 0x4b are
+ themselves the byte count, while 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.
+
+ A script may hold more than one push after OP_RETURN. That is unusual but legal,
+ and the pushes are concatenated here rather than returned separately, since where
+ the encoder chose to break the data into pushes carries no consensus meaning.
+
+ Anything that is not a data push, and any push that claims more bytes than the
+ script actually carries, ends the walk and the remaining bytes are appended
+ verbatim, so no committed data is hidden from the review. The caller gets raw
+ bytes and decides how to render them.
+ """
+ pushes = []
+
+ # Start after the OP_RETURN opcode itself
+ i = 1
+ while i < len(script_data):
+ opcode = script_data[i]
+
+ if 1 <= opcode <= OPCODES.OP_PUSHDATA_MAX_DIRECT:
+ # The opcode is itself the length; no separate length field follows
+ length_size = 0
+ elif opcode == OPCODES.OP_PUSHDATA1:
+ length_size = 1
+ elif opcode == OPCODES.OP_PUSHDATA2:
+ length_size = 2
+ elif opcode == OPCODES.OP_PUSHDATA4:
+ length_size = 4
+ else:
+ # Not a data push (OP_0, a numeric opcode, anything else). Surface the
+ # rest as-is rather than silently dropping it.
+ pushes.append(script_data[i:])
+ break
+
+ if length_size == 0:
+ length = opcode
+ else:
+ length_end = i + 1 + length_size
+ if length_end > len(script_data):
+ # The length field itself is truncated
+ pushes.append(script_data[i:])
+ break
+ length = int.from_bytes(script_data[i + 1:length_end], "little")
+
+ data_start = i + 1 + length_size
+ data_end = data_start + length
+ if data_end > len(script_data):
+ # The push claims more data than the script carries
+ pushes.append(script_data[data_start:])
+ break
+
+ pushes.append(script_data[data_start:data_end])
+ i = data_end
+
+ return b"".join(pushes)
+
+
@staticmethod
def _get_script_type(scope, scriptpubkey) -> str:
"""Like script_type(), but tells nested segwit p2sh apart from legacy p2sh"""Why this scored 63/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.