From 31ac982b756c4fc4f9b3200a70d5bb21e06aa075 Mon Sep 17 00:00:00 2001 From: notTanveer Date: Mon, 6 Jul 2026 20:37:35 +0530 Subject: [PATCH 01/44] feat(BIP-370): PSBTv2 support Co-authored-by: odudex <5049478+odudex@users.noreply.github.com> --- src/embit/liquid/pset.py | 28 +- src/embit/psbt.py | 719 +++++++++++++++++++++++++++++++------ src/embit/psbtview.py | 260 +++++++++++--- src/embit/transaction.py | 29 +- tests/tests/test_psbtV2.py | 656 +++++++++++++++++++++++++++++++++ 5 files changed, 1527 insertions(+), 165 deletions(-) create mode 100644 tests/tests/test_psbtV2.py diff --git a/src/embit/liquid/pset.py b/src/embit/liquid/pset.py index ab46d014..6ce50587 100644 --- a/src/embit/liquid/pset.py +++ b/src/embit/liquid/pset.py @@ -5,10 +5,8 @@ else: from ..util import secp256k1 -from .. import compact, hashes +from .. import hashes from ..psbt import * -from collections import OrderedDict -from io import BytesIO from .transaction import ( LTransaction, LTransactionOutput, @@ -140,10 +138,10 @@ def blinded_vin(self): witness=TxInWitness(self.issue_rangeproof, self.token_rangeproof), ) - def read_value(self, stream, k): + def read_value(self, stream, k, version=None): # standard bitcoin stuff if (b"\xfc\x08elements" not in k) and (b"\xfc\x04pset" not in k): - super().read_value(stream, k) + super().read_value(stream, k, version=version) elif k == b"\xfc\x04pset\x0e": # range proof is very large, # so we don't load it if compress flag is set. @@ -191,8 +189,8 @@ def read_value(self, stream, k): else: self.unknown[k] = v - def write_to(self, stream, skip_separator=False, **kwargs) -> int: - r = super().write_to(stream, skip_separator=True, **kwargs) + def write_to(self, stream, skip_separator=False, version=None, **kwargs) -> int: + r = super().write_to(stream, skip_separator=True, version=version, **kwargs) # liquid-specific keys if self.value is not None: r += ser_string(stream, b"\xfc\x08elements\x00") @@ -386,9 +384,9 @@ def reblind(self, nonce, blinding_pubkey=None, extra_message=b""): secp256k1.generator_parse(self.asset_commitment), ) - def read_value(self, stream, k): + def read_value(self, stream, k, version=None): if (b"\xfc\x08elements" not in k) and (b"\xfc\x04pset" not in k): - super().read_value(stream, k) + super().read_value(stream, k, version=version) # range proof and surjection proof are very large, # so we don't load them if compress flag is set. elif k in [b"\xfc\x08elements\x04", b"\xfc\x04pset\x04"]: @@ -503,6 +501,18 @@ class PSET(PSBT): PSBTOUT_CLS = LOutputScope TX_CLS = LTransaction + @classmethod + def _validate_v2_output(cls, out, i): + """PSET allows value_commitment as an alternative to value (blinded outputs).""" + if out.value is None and not getattr(out, "value_commitment", None): + raise PSBTError( + "PSBTv2 output %d missing required PSBT_OUT_AMOUNT (0x03)" % i + ) + if out.script_pubkey is None: + raise PSBTError( + "PSBTv2 output %d missing required PSBT_OUT_SCRIPT (0x04)" % i + ) + def unblind(self, blinding_key): for inp in self.inputs: inp.unblind(blinding_key) diff --git a/src/embit/psbt.py b/src/embit/psbt.py index 0f374f15..835b3c8a 100644 --- a/src/embit/psbt.py +++ b/src/embit/psbt.py @@ -1,5 +1,12 @@ from collections import OrderedDict -from .transaction import Transaction, TransactionOutput, TransactionInput, SIGHASH +from .transaction import ( + Transaction, + TransactionOutput, + TransactionInput, + SIGHASH, + _signed_from_bytes, + _signed_to_bytes, +) from . import compact from . import bip32 from . import ec @@ -11,6 +18,8 @@ from binascii import b2a_base64, a2b_base64, hexlify, unhexlify from io import BytesIO +LOCKTIME_THRESHOLD = 500000000 + class PSBTError(EmbitError): pass @@ -103,12 +112,48 @@ def skip_string(stream) -> int: return len(compact.to_bytes(l)) + l +def resolve_signing_root(root): + """Resolve the signing fingerprint for ``root``, honoring descriptor-key + wrapping. + + Returns ``(fingerprint, can_sign, root)``. ``fingerprint`` is None for + raw/WIF keys (matched by key material instead). ``can_sign`` is False + when ``root`` is a public-only descriptor key that cannot sign. ``root`` + is unwrapped to its underlying WIF key (``root.key``) when it was a + non-extended descriptor key; otherwise it is returned unchanged. + """ + fingerprint = None + if hasattr(root, "origin"): + if not root.is_private: # pubkey can't sign + return None, False, root + if root.is_extended: # use fingerprint only for HDKey + fingerprint = root.fingerprint + else: + root = root.key # WIF key + if not fingerprint and hasattr(root, "my_fingerprint"): + fingerprint = root.my_fingerprint + return fingerprint, True, root + + +def derive_hdkey(root, derivation): + """Derive the HDKey for ``derivation``, honoring a descriptor key's origin prefix.""" + der = derivation.derivation + if hasattr(root, "origin"): + if root.origin: + prefix = root.origin.derivation + if der[: len(prefix)] != prefix: + return None + der = der[len(prefix) :] + return root.key.derive(der) + return root.derive(der) + + class DerivationPath(EmbitBase): def __init__(self, fingerprint: bytes, derivation: list): self.fingerprint = fingerprint self.derivation = derivation - def write_to(self, stream) -> int: + def write_to(self, stream, **kwargs) -> int: r = stream.write(self.fingerprint) for idx in self.derivation: r += stream.write(idx.to_bytes(4, "little")) @@ -158,7 +203,7 @@ def _validate_key(self, key): if len(key) != 1 and key[:1] in self.V2_FIELDS: raise PSBTError("Invalid PSBTv2 field key") - def read_value(self, stream, key, *args, **kwargs): + def read_value(self, stream, key, version=None): # separator if len(key) == 0: return @@ -222,6 +267,8 @@ def __init__(self, unknown: dict = {}, vin=None, compress=CompressMode.KEEP_ALL) self.final_scriptsig = None self.final_scriptwitness = None + self.required_time_locktime = None + self.required_height_locktime = None self.parse_unknowns() def clear_metadata(self, compress=CompressMode.CLEAR_ALL): @@ -260,18 +307,32 @@ def update(self, other): self.witness_script = other.witness_script or self.witness_script self.bip32_derivations.update(other.bip32_derivations) self.taproot_bip32_derivations.update(other.taproot_bip32_derivations) - self.taproot_internal_key = other.taproot_internal_key + self.taproot_internal_key = ( + other.taproot_internal_key or self.taproot_internal_key + ) self.taproot_merkle_root = other.taproot_merkle_root or self.taproot_merkle_root self.taproot_key_sig = other.taproot_key_sig or self.taproot_key_sig self.taproot_sigs.update(other.taproot_sigs) self.taproot_scripts.update(other.taproot_scripts) self.final_scriptsig = other.final_scriptsig or self.final_scriptsig self.final_scriptwitness = other.final_scriptwitness or self.final_scriptwitness + self.required_time_locktime = ( + other.required_time_locktime + if other.required_time_locktime is not None + else self.required_time_locktime + ) + self.required_height_locktime = ( + other.required_height_locktime + if other.required_height_locktime is not None + else self.required_height_locktime + ) @property def vin(self): return TransactionInput( - self.txid, self.vout, sequence=(self.sequence or 0xFFFFFFFF) + self.txid, + self.vout, + sequence=(self.sequence if self.sequence is not None else 0xFFFFFFFF), ) @property @@ -319,7 +380,7 @@ def verify(self, ignore_missing=False): raise PSBTError("Missing non_witness_utxo") return False - def read_value(self, stream, k): + def read_value(self, stream, k, version=None): # separator if len(k) == 0: return @@ -333,9 +394,17 @@ def read_value(self, stream, k): else: length = compact.read_from(stream) value_stream = _BoundedReader(stream, length) + # For PSBTv2, PSBT_IN_OUTPUT_INDEX may follow PSBT_IN_NON_WITNESS_UTXO; + # use the pre-scanned vout (set by read_from) when the field hasn't + # been parsed yet so the OOM protection is key-order independent. + effective_vout = ( + self.vout + if self.vout is not None + else getattr(self, "_prescan_vout", None) + ) # we verified and saved utxo - if self.compress and self.txid and self.vout is not None: - txout, txhash = self.TX_CLS.read_vout(value_stream, self.vout) + if self.compress and effective_vout is not None: + txout, txhash = self.TX_CLS.read_vout(value_stream, effective_vout) value_stream.finish() self._txhash = txhash self._utxo = txout @@ -423,13 +492,64 @@ def read_value(self, stream, k): else: raise PSBTError("Duplicated final scriptwitness") + # PSBTv2 fields elif k == b"\x0e": + if version != 2: + raise PSBTError("PSBT_IN_PREVIOUS_TXID not allowed in PSBTv0") + if len(v) != 32: + raise PSBTError("PSBT_IN_PREVIOUS_TXID must be 32 bytes") + if self.txid is not None: + raise PSBTError("Duplicated PSBT_IN_PREVIOUS_TXID") self.txid = bytes(reversed(v)) elif k == b"\x0f": + if version != 2: + raise PSBTError("PSBT_IN_OUTPUT_INDEX not allowed in PSBTv0") + if len(v) != 4: + raise PSBTError("PSBT_IN_OUTPUT_INDEX must be 4 bytes") + if self.vout is not None: + raise PSBTError("Duplicated PSBT_IN_OUTPUT_INDEX") self.vout = int.from_bytes(v, "little") elif k == b"\x10": + if version != 2: + raise PSBTError("PSBT_IN_SEQUENCE not allowed in PSBTv0") + if len(v) != 4: + raise PSBTError("PSBT_IN_SEQUENCE must be 4 bytes") + if self.sequence is not None: + raise PSBTError("Duplicated PSBT_IN_SEQUENCE") self.sequence = int.from_bytes(v, "little") + # PSBT_IN_REQUIRED_TIME_LOCKTIME + elif k == b"\x11": + if version != 2: + raise PSBTError("PSBT_IN_REQUIRED_TIME_LOCKTIME not allowed in PSBTv0") + if self.required_time_locktime is not None: + raise PSBTError("Duplicated required time locktime") + if len(v) != 4: + raise PSBTError("PSBT_IN_REQUIRED_TIME_LOCKTIME must be 4 bytes") + locktime = int.from_bytes(v, "little") + if locktime < LOCKTIME_THRESHOLD: + raise PSBTError( + "Time-based locktime must be >= %d" % LOCKTIME_THRESHOLD + ) + self.required_time_locktime = locktime + + # PSBT_IN_REQUIRED_HEIGHT_LOCKTIME + elif k == b"\x12": + if version != 2: + raise PSBTError( + "PSBT_IN_REQUIRED_HEIGHT_LOCKTIME not allowed in PSBTv0" + ) + if self.required_height_locktime is not None: + raise PSBTError("Duplicated required height locktime") + if len(v) != 4: + raise PSBTError("PSBT_IN_REQUIRED_HEIGHT_LOCKTIME must be 4 bytes") + locktime = int.from_bytes(v, "little") + if locktime >= LOCKTIME_THRESHOLD or locktime == 0: + raise PSBTError( + "Height-based locktime must be > 0 and < %d" % LOCKTIME_THRESHOLD + ) + self.required_height_locktime = locktime + # PSBT_IN_TAP_KEY_SIG elif k[0] == 0x13: # read the taproot key sig @@ -522,6 +642,20 @@ def write_to(self, stream, skip_separator=False, version=None, **kwargs) -> int: r += ser_string(stream, b"\x10") r += ser_string(stream, self.sequence.to_bytes(4, "little")) + # Add required time locktime if present + if self.required_time_locktime is not None: + r += ser_string(stream, b"\x11") + r += ser_string( + stream, self.required_time_locktime.to_bytes(4, "little") + ) + + # Add required height locktime if present + if self.required_height_locktime is not None: + r += ser_string(stream, b"\x12") + r += ser_string( + stream, self.required_height_locktime.to_bytes(4, "little") + ) + # PSBT_IN_TAP_KEY_SIG if self.taproot_key_sig is not None: r += ser_string(stream, b"\x13") @@ -567,6 +701,41 @@ def write_to(self, stream, skip_separator=False, version=None, **kwargs) -> int: r += stream.write(b"\x00") return r + @classmethod + def read_from(cls, stream, compress=CompressMode.KEEP_ALL, vin=None, version=None): + res = cls({}, vin=vin, compress=compress) + # PSBT maps are unordered, so PSBTv2 input maps may provide the output + # index after the non-witness UTXO. Peek only at compact-sized keys and + # skip values to keep compressed parsing independent of key order. + res._prescan_vout = None + if version == 2 and compress and res.vout is None: + try: + start_pos = stream.seek(0, 1) + while True: + k = read_string(stream) + if len(k) == 0: + break + if k == b"\x0f": + v = read_string(stream) + if len(v) == 4: + res._prescan_vout = int.from_bytes(v, "little") + else: + # Let the real parse raise the precise validation error. + pass + break + skip_string(stream) + stream.seek(start_pos) + except (AttributeError, OSError): + pass # stream not seekable; OOM protection may be unavailable + while True: + key = read_string(stream) + # separator + if len(key) == 0: + break + res.read_value(stream, key, version=version) + del res._prescan_vout + return res + class OutputScope(PSBTScope): V2_FIELDS = (b"\x03", b"\x04") @@ -605,13 +774,15 @@ def update(self, other): self.witness_script = other.witness_script or self.witness_script self.bip32_derivations.update(other.bip32_derivations) self.taproot_bip32_derivations.update(other.taproot_bip32_derivations) - self.taproot_internal_key = other.taproot_internal_key + self.taproot_internal_key = ( + other.taproot_internal_key or self.taproot_internal_key + ) @property def vout(self): return TransactionOutput(self.value, self.script_pubkey) - def read_value(self, stream, k): + def read_value(self, stream, k, version=None): # separator if len(k) == 0: return @@ -643,9 +814,22 @@ def read_value(self, stream, k): else: self.bip32_derivations[pub] = DerivationPath.parse(v) + # PSBTv2 fields elif k == b"\x03": - self.value = int.from_bytes(v, "little") + if version != 2: + raise PSBTError("PSBT_OUT_AMOUNT not allowed in PSBTv0") + if len(v) != 8: + raise PSBTError("PSBT_OUT_AMOUNT must be 8 bytes") + if self.value is not None: + raise PSBTError("Duplicated PSBT_OUT_AMOUNT") + self.value = _signed_from_bytes(v) + if self.value < 0: + raise PSBTError("PSBT_OUT_AMOUNT must be non-negative") elif k == b"\x04": + if version != 2: + raise PSBTError("PSBT_OUT_SCRIPT not allowed in PSBTv0") + if self.script_pubkey is not None: + raise PSBTError("Duplicated PSBT_OUT_SCRIPT") self.script_pubkey = Script(v) # PSBT_OUT_TAP_INTERNAL_KEY @@ -684,7 +868,8 @@ def write_to(self, stream, skip_separator=False, version=None, **kwargs) -> int: if version == 2: if self.value is not None: r += ser_string(stream, b"\x03") - r += ser_string(stream, self.value.to_bytes(8, "little")) + # BIP370: PSBT_OUT_AMOUNT is a signed 64-bit integer + r += ser_string(stream, _signed_to_bytes(self.value, 8)) if self.script_pubkey is not None: r += ser_string(stream, b"\x04") r += self.script_pubkey.write_to(stream) @@ -714,6 +899,23 @@ def write_to(self, stream, skip_separator=False, version=None, **kwargs) -> int: r += stream.write(b"\x00") return r + @classmethod + def read_from(cls, stream, compress=CompressMode.KEEP_ALL, vout=None, version=None): + res = cls({}, vout=vout, compress=compress) + while True: + key = read_string(stream) + # separator + if len(key) == 0: + break + res.read_value(stream, key, version=version) + return res + + +class TxModifiable: + INPUTS = 0b00000001 + OUTPUTS = 0b00000010 + SIGHASH_SINGLE = 0b00000100 + class PSBT(EmbitBase): MAGIC = b"psbt\xff" @@ -722,17 +924,46 @@ class PSBT(EmbitBase): PSBTOUT_CLS = OutputScope TX_CLS = Transaction - def __init__(self, tx=None, unknown={}, version=None): - self.version = version # None for v0 + @classmethod + def _validate_v2_input(cls, inp, i): + """Check that a PSBTv2 input has all required fields. + Subclasses may override this to add extra validation.""" + if inp.txid is None: + raise PSBTError( + "PSBTv2 input %d missing required PSBT_IN_PREVIOUS_TXID (0x0e)" % i + ) + if inp.vout is None: + raise PSBTError( + "PSBTv2 input %d missing required PSBT_IN_OUTPUT_INDEX (0x0f)" % i + ) + + @classmethod + def _validate_v2_output(cls, out, i): + """Check that a PSBTv2 output has all required fields. + Subclasses (e.g. PSET) may override this to accept alternative fields.""" + if out.value is None: + raise PSBTError( + "PSBTv2 output %d missing required PSBT_OUT_AMOUNT (0x03)" % i + ) + if out.script_pubkey is None: + raise PSBTError( + "PSBTv2 output %d missing required PSBT_OUT_SCRIPT (0x04)" % i + ) + + def __init__(self, tx=None, unknown=None, version=None): + self.version = version # None for v0, 2 for v2 self.inputs = [] self.outputs = [] self.tx_version = None self.locktime = None + self.tx_modifiable_flags = None + self._raw_input_count_from_global = None + self._raw_output_count_from_global = None if tx is not None: self.parse_tx(tx) - self.unknown = unknown + self.unknown = {} if unknown is None else unknown self.xpubs = OrderedDict() self.parse_unknowns() @@ -742,11 +973,104 @@ def parse_tx(self, tx): self.inputs = [self.PSBTIN_CLS(vin=vin) for vin in tx.vin] self.outputs = [self.PSBTOUT_CLS(vout=vout) for vout in tx.vout] + def __eq__(self, other): + if type(self) is not type(other): + return False + return ( + self.version == other.version + and self.tx_version == other.tx_version + and self.locktime == other.locktime + and self.tx_modifiable_flags == other.tx_modifiable_flags + and self.inputs == other.inputs + and self.outputs == other.outputs + and self.xpubs == other.xpubs + and self.unknown == other.unknown + ) + + def __hash__(self): + # defining __eq__ sets __hash__ to None, so restore EmbitBase behaviour + return hash(self.serialize()) + + @staticmethod + def _classify_locktimes(inputs): + """Classify inputs by locktime requirement type. + + Returns a tuple of (height_locktimes, time_locktimes, inputs_with_requirements) + where the lists contain the respective locktime values and the count is the + number of inputs that express at least one locktime requirement. + """ + height_locktimes = [] + time_locktimes = [] + inputs_with_requirements = 0 + for inp in inputs: + has_requirement = False + if inp.required_height_locktime is not None: + height_locktimes.append(inp.required_height_locktime) + has_requirement = True + if inp.required_time_locktime is not None: + time_locktimes.append(inp.required_time_locktime) + has_requirement = True + if has_requirement: + inputs_with_requirements += 1 + return height_locktimes, time_locktimes, inputs_with_requirements + + def determine_locktime(self): + """ + Determines the appropriate locktime according to PSBTv2 rules. + Returns the locktime that should be used for the transaction. + """ + if self.version != 2: + return self.locktime or 0 + + height_locktimes, time_locktimes, inputs_with_locktime_requirements = ( + self._classify_locktimes(self.inputs) + ) + + # If no inputs have locktime requirements, use fallback + if inputs_with_locktime_requirements == 0: + return self.locktime or 0 + + # height preferred, if ALL inputs with requirements support it + if len(height_locktimes) == inputs_with_locktime_requirements: + return max(height_locktimes) + + # fall back to time if ALL inputs with requirements support it + if len(time_locktimes) == inputs_with_locktime_requirements: + return max(time_locktimes) + + raise PSBTError( + "Cannot determine locktime: inputs have conflicting height and time locktime requirements" + ) + + def _validate_locktime_compatibility(self, inputs): + """Verify that the given input list has mutually compatible locktime requirements.""" + height_locktimes, time_locktimes, inputs_with_requirements = ( + self._classify_locktimes(inputs) + ) + if inputs_with_requirements == 0: + return + if ( + len(height_locktimes) == inputs_with_requirements + or len(time_locktimes) == inputs_with_requirements + ): + return + raise PSBTError( + "Input locktime requirements are incompatible with existing inputs" + ) + @property def tx(self): + if self.version == 2: + locktime = self.determine_locktime() + else: + locktime = self.locktime or 0 + + if self.version == 2 and self.tx_version is None: + raise PSBTError("PSBTv2 is missing required PSBT_GLOBAL_TX_VERSION") + tx_version = self.tx_version if self.tx_version is not None else 2 return self.TX_CLS( - version=2 if self.tx_version is None else self.tx_version, - locktime=self.locktime or 0, + version=tx_version, + locktime=locktime, vin=[inp.vin for inp in self.inputs], vout=[out.vout for out in self.outputs], ) @@ -784,6 +1108,9 @@ def fee(self): fee -= sum([out.value for out in self.tx.vout]) return fee + def _write_extra_globals(self, stream) -> int: + return 0 + def write_to(self, stream) -> int: # magic bytes r = stream.write(self.MAGIC) @@ -799,9 +1126,10 @@ def write_to(self, stream) -> int: r += ser_string(stream, self.xpubs[xpub].serialize()) if self.version == 2: - tx_version = 2 if self.tx_version is None else self.tx_version + tx_version = self.tx_version if self.tx_version is not None else 2 r += ser_string(stream, b"\x02") - r += ser_string(stream, tx_version.to_bytes(4, "little")) + # BIP370: PSBT_GLOBAL_TX_VERSION is a signed int32 + r += ser_string(stream, _signed_to_bytes(tx_version, 4)) if self.locktime is not None: r += ser_string(stream, b"\x03") r += ser_string(stream, self.locktime.to_bytes(4, "little")) @@ -809,8 +1137,13 @@ def write_to(self, stream) -> int: r += ser_string(stream, compact.to_bytes(len(self.inputs))) r += ser_string(stream, b"\x05") r += ser_string(stream, compact.to_bytes(len(self.outputs))) + if self.tx_modifiable_flags is not None: + r += ser_string(stream, b"\x06") + r += ser_string(stream, bytes([self.tx_modifiable_flags])) r += ser_string(stream, b"\xfb") r += ser_string(stream, self.version.to_bytes(4, "little")) + + r += self._write_extra_globals(stream) # unknown for key in self.unknown: r += ser_string(stream, key) @@ -853,84 +1186,188 @@ def read_from(cls, stream, compress=CompressMode.KEEP_ALL): without storing them in memory and save the utxo internally for signing. This helps against out-of-memory errors. """ - tx = None - unknown = {} - version = None - # check magic if stream.read(len(cls.MAGIC)) != cls.MAGIC: raise PSBTError("Invalid PSBT magic") + + global_kvs = OrderedDict() while True: key = read_string(stream) - # separator - if len(key) == 0: + if len(key) == 0: # Separator break _validate_global_key(key) value = read_string(stream) - # tx - if key == b"\x00": - if tx is None: - tx = cls.TX_CLS.parse(value) - # BIP-174: the global transaction must be unsigned - for inp in tx.vin: - if len(inp.script_sig.data) > 0: - raise PSBTError( - "Global transaction input has a non-empty scriptSig" - ) - else: + if key in global_kvs: + raise PSBTError("Duplicated global key: %s" % hexlify(key).decode()) + global_kvs[key] = value + + # Determine PSBT version from PSBT_GLOBAL_VERSION (0xfb) + version = None + if b"\xfb" in global_kvs: + if len(global_kvs[b"\xfb"]) != 4: + raise PSBTError("PSBT_GLOBAL_VERSION must be 4 bytes") + parsed_version = int.from_bytes(global_kvs[b"\xfb"], "little") + if parsed_version == 2: + version = 2 + elif parsed_version == 0: + version = ( + None # explicit PSBT_GLOBAL_VERSION=0 is valid for v0 per BIP174 + ) + else: + raise PSBTError( + "Unsupported PSBT_GLOBAL_VERSION value: %d" % parsed_version + ) + + if version == 2: # PSBTv2 + if b"\x00" in global_kvs: # PSBT_GLOBAL_UNSIGNED_TX + raise PSBTError("PSBT_GLOBAL_UNSIGNED_TX is not allowed in PSBTv2") + # Pass all global KVs to unknown; __init__ calls parse_unknowns. + psbt = cls(tx=None, unknown=global_kvs, version=version) + + # Validate that input/output counts were processed by parse_unknowns + if psbt._raw_input_count_from_global is None: + raise PSBTError( + "PSBTv2 missing or invalid PSBT_GLOBAL_INPUT_COUNT (0x04)" + ) + if psbt._raw_output_count_from_global is None: + raise PSBTError( + "PSBTv2 missing or invalid PSBT_GLOBAL_OUTPUT_COUNT (0x05)" + ) + if psbt.tx_version is None: + raise PSBTError("PSBTv2 missing required PSBT_GLOBAL_TX_VERSION (0x02)") + else: # PSBTv0 (version is None or 0) + if b"\x00" not in global_kvs: + raise PSBTError("PSBT_GLOBAL_UNSIGNED_TX (0x00) is required for PSBTv0") + + # Check for forbidden v2-only global keys in v0 context. + # PSBT_GLOBAL_VERSION (0xfb) with value 0 is explicitly permitted by BIP174 + # and was already validated above, so it is excluded from this set. + v2_only_global_keys = {b"\x02", b"\x03", b"\x04", b"\x05", b"\x06"} + for k_v2_only in v2_only_global_keys: + if k_v2_only in global_kvs: raise PSBTError( - "Failed to parse PSBT - duplicated transaction field" + "Global key %s is not allowed in PSBTv0" + % hexlify(k_v2_only).decode() ) - elif key == b"\xfb": - if version is not None: - raise PSBTError("Duplicated global version") - if len(value) != 4: - raise PSBTError("Global version must be 4 bytes") - version = int.from_bytes(value, "little") - if version not in [0, 2]: - raise PSBTError("Unsupported PSBT version %d" % version) - else: - if key in unknown: - raise PSBTError("Duplicated key") - unknown[key] = value - - _validate_global_fields(version, tx is not None, unknown) - psbt = cls(tx, unknown, version=version) - # input scopes - for i, vin in enumerate(psbt.tx.vin): - psbt.inputs[i] = cls.PSBTIN_CLS.read_from( - stream, compress=compress, vin=vin, version=version - ) - # output scopes - for i, vout in enumerate(psbt.tx.vout): - psbt.outputs[i] = cls.PSBTOUT_CLS.read_from( - stream, compress=compress, vout=vout, version=version - ) + + tx_bytes = global_kvs.pop(b"\x00") # Remove so it's not in unknown + tx_for_v0 = cls.TX_CLS.parse(tx_bytes) + # BIP-174: the global transaction must be unsigned + for inp in tx_for_v0.vin: + if len(inp.script_sig.data) > 0: + raise PSBTError( + "Global transaction input has a non-empty scriptSig" + ) + psbt = cls(tx=tx_for_v0, unknown=global_kvs, version=version) + + if version == 2: + num_inputs = psbt._raw_input_count_from_global + num_outputs = psbt._raw_output_count_from_global + + parsed_inputs = [] + for _ in range(num_inputs): + parsed_inputs.append( + cls.PSBTIN_CLS.read_from( + stream, + compress=compress, + vin=None, + version=version, + ) + ) + psbt.inputs = parsed_inputs + + for i, inp in enumerate(psbt.inputs): + cls._validate_v2_input(inp, i) + + parsed_outputs = [] + for _ in range(num_outputs): + parsed_outputs.append( + cls.PSBTOUT_CLS.read_from( + stream, + compress=compress, + vout=None, + version=version, + ) + ) + psbt.outputs = parsed_outputs + + for i, out in enumerate(psbt.outputs): + cls._validate_v2_output(out, i) + else: + temp_inputs = [] + for i in range(len(psbt.inputs)): + temp_inputs.append( + cls.PSBTIN_CLS.read_from( + stream, + compress=compress, + vin=psbt.inputs[i].vin, + version=version, + ) + ) + psbt.inputs = temp_inputs + + temp_outputs = [] + for i in range(len(psbt.outputs)): + temp_outputs.append( + cls.PSBTOUT_CLS.read_from( + stream, + compress=compress, + vout=psbt.outputs[i].vout, + version=version, + ) + ) + psbt.outputs = temp_outputs + return psbt def parse_unknowns(self): + # Handle PSBT_GLOBAL_VERSION first + if b"\xfb" in self.unknown: + self.unknown.pop(b"\xfb", None) + + if b"\x06" in self.unknown: + flags_bytes = self.unknown.pop(b"\x06") + if len(flags_bytes) != 1: + raise PSBTError("PSBT_GLOBAL_TX_MODIFIABLE must be 1 byte") + self.tx_modifiable_flags = flags_bytes[0] + for k in list(self.unknown): # xpub field if k[0] == 0x01: xpub = bip32.HDKey.parse(k[1:]) self.xpubs[xpub] = DerivationPath.parse(self.unknown.pop(k)) elif k == b"\x02": - self.tx_version = int.from_bytes(self.unknown.pop(k), "little") + if self.version == 2: + v = self.unknown.pop(k) + if len(v) != 4: + raise PSBTError("PSBT_GLOBAL_TX_VERSION must be 4 bytes") + # BIP370: PSBT_GLOBAL_TX_VERSION is a signed int32 + self.tx_version = _signed_from_bytes(v) elif k == b"\x03": - self.locktime = int.from_bytes(self.unknown.pop(k), "little") + if self.version == 2: + v = self.unknown.pop(k) + if len(v) != 4: + raise PSBTError("PSBT_GLOBAL_FALLBACK_LOCKTIME must be 4 bytes") + self.locktime = int.from_bytes(v, "little") elif k == b"\x04": - if len(self.inputs) > 0: - raise PSBTError("Inputs already initialized") - self.inputs = [ - self.PSBTIN_CLS() - for _ in range(compact.from_bytes(self.unknown.pop(k))) - ] + if self.version == 2: + if self._raw_input_count_from_global is not None: + self.unknown.pop(k, None) + continue + self._raw_input_count_from_global = compact.from_bytes( + self.unknown.pop(k) + ) + # Store count only; do not pre-allocate objects to avoid + # attacker-controlled memory exhaustion. elif k == b"\x05": - if len(self.outputs) > 0: - raise PSBTError("Outputs already initialized") - self.outputs = [ - self.PSBTOUT_CLS() - for _ in range(compact.from_bytes(self.unknown.pop(k))) - ] + if self.version == 2: + if self._raw_output_count_from_global is not None: + self.unknown.pop(k, None) + continue + self._raw_output_count_from_global = compact.from_bytes( + self.unknown.pop(k) + ) + # Store count only; do not pre-allocate objects to avoid + # attacker-controlled memory exhaustion. def sighash(self, i, sighash=SIGHASH.ALL, **kwargs): inp = self.inputs[i] @@ -1041,19 +1478,9 @@ def sign_with(self, root, sighash=SIGHASH.DEFAULT) -> int: counter += self.sign_with(k, sighash) return counter - # if WIF - fingerprint is None - fingerprint = None - # if descriptor key - if hasattr(root, "origin"): - if not root.is_private: # pubkey can't sign - return 0 - if root.is_extended: # use fingerprint only for HDKey - fingerprint = root.fingerprint - else: - root = root.key # WIF key - # if HDKey - if not fingerprint and hasattr(root, "my_fingerprint"): - fingerprint = root.my_fingerprint + fingerprint, can_sign, root = resolve_signing_root(root) + if not can_sign: + return 0 rootpub = root.get_public_key() sec = rootpub.sec() @@ -1089,7 +1516,7 @@ def sign_with(self, root, sighash=SIGHASH.DEFAULT) -> int: if fingerprint: # if taproot derivations are present add them for pub in inp.taproot_bip32_derivations: - (_leafs, derivation) = inp.taproot_bip32_derivations[pub] + _leafs, derivation = inp.taproot_bip32_derivations[pub] if derivation.fingerprint == fingerprint: # Add only if not already present if (pub, derivation) not in bip32_derivations: @@ -1105,17 +1532,10 @@ def sign_with(self, root, sighash=SIGHASH.DEFAULT) -> int: # get derived keys for signing derived_keypairs = OrderedDict() # (prv, pub) for pub, derivation in bip32_derivations: - der = derivation.derivation - # descriptor key has origin derivation that we take into account - if hasattr(root, "origin"): - if root.origin: - if root.origin.derivation != der[: len(root.origin.derivation)]: - # derivation doesn't match - go to next input - continue - der = der[len(root.origin.derivation) :] - hdkey = root.key.derive(der) - else: - hdkey = root.derive(der) + hdkey = derive_hdkey(root, derivation) + if hdkey is None: + # derivation doesn't match - go to next candidate + continue if hdkey.xonly() != pub.xonly(): raise PSBTError("Derivation path doesn't look right") @@ -1127,7 +1547,7 @@ def sign_with(self, root, sighash=SIGHASH.DEFAULT) -> int: if inp.is_taproot: # try to sign with individual private key (WIF) # or with root without derivations - counter += self.sign_input_with_tapkey( + tap_sigs = self.sign_input_with_tapkey( root, i, inp, @@ -1135,12 +1555,15 @@ def sign_with(self, root, sighash=SIGHASH.DEFAULT) -> int: ) # sign with all derived keys for prv, pub in derived_keypairs: - counter += self.sign_input_with_tapkey( + tap_sigs += self.sign_input_with_tapkey( prv, i, inp, sighash=inp_sighash, ) + if tap_sigs > 0: + counter += tap_sigs + self._update_tx_modifiable(inp_sighash) continue # hash can be reused @@ -1150,13 +1573,99 @@ def sign_with(self, root, sighash=SIGHASH.DEFAULT) -> int: # check if root itself is included in the script if sec in sc.data or pkh in sc.data: sig = root.sign(h) - # sig plus sighash flag inp.partial_sigs[rootpub] = sig.serialize() + bytes([inp_sighash]) counter += 1 + self._update_tx_modifiable(inp_sighash) for prv, pub in derived_keypairs: sig = prv.sign(h) - # sig plus sighash flag inp.partial_sigs[pub] = sig.serialize() + bytes([inp_sighash]) counter += 1 + self._update_tx_modifiable(inp_sighash) + return counter + + def _update_tx_modifiable(self, inp_sighash: int) -> None: + """Update PSBT_GLOBAL_TX_MODIFIABLE after a signature is added.""" + + if self.version != 2: + return + + is_anyonecanpay = bool(inp_sighash & SIGHASH.ANYONECANPAY) + sighash_type = inp_sighash & 0x1F + + if self.tx_modifiable_flags is None: + self.tx_modifiable_flags = 0 + + if not is_anyonecanpay: + self.tx_modifiable_flags &= ~TxModifiable.INPUTS + if sighash_type != SIGHASH.NONE: + self.tx_modifiable_flags &= ~TxModifiable.OUTPUTS + if sighash_type == SIGHASH.SINGLE: + self.tx_modifiable_flags |= TxModifiable.SIGHASH_SINGLE + + def get_tx_modifiable(self): + return self.tx_modifiable_flags + + def set_tx_modifiable(self, flags): + if self.version != 2: + raise PSBTError("GLOBAL_TX_MODIFIABLE only supported in PSBTv2") + self.tx_modifiable_flags = flags + + def is_inputs_modifiable(self): + if self.version != 2: + return True + if self.tx_modifiable_flags is None: + return False + return bool(self.tx_modifiable_flags & TxModifiable.INPUTS) + + def is_outputs_modifiable(self): + if self.version != 2: + return True + if self.tx_modifiable_flags is None: + return False + return bool(self.tx_modifiable_flags & TxModifiable.OUTPUTS) + + def has_sighash_single(self): + if self.version != 2 or self.tx_modifiable_flags is None: + return False + return bool(self.tx_modifiable_flags & TxModifiable.SIGHASH_SINGLE) + + @classmethod + def create_v2(cls, tx_version=2, fallback_locktime=None, tx_modifiable=None): + """Creator role convenience factory: initialise an empty PSBTv2. + + BIP-370 Creator role: set PSBT_GLOBAL_VERSION=2, PSBT_GLOBAL_TX_VERSION, + and optionally PSBT_GLOBAL_FALLBACK_LOCKTIME and PSBT_GLOBAL_TX_MODIFIABLE + so the Constructor role can immediately begin adding inputs and outputs. + """ + if tx_modifiable is None: + tx_modifiable = TxModifiable.INPUTS | TxModifiable.OUTPUTS + psbt = cls(tx=None, version=2) + psbt.tx_version = tx_version + psbt.locktime = fallback_locktime + psbt.tx_modifiable_flags = tx_modifiable + return psbt + + def add_input(self, input_scope): + if not self.is_inputs_modifiable(): + raise PSBTError("Inputs are not modifiable") + if self.version == 2: + self._validate_v2_input(input_scope, len(self.inputs)) + if ( + input_scope.required_height_locktime is not None + or input_scope.required_time_locktime is not None + ): + self._validate_locktime_compatibility(self.inputs + [input_scope]) + self.inputs.append(input_scope) + if self.version == 2: + self._raw_input_count_from_global = len(self.inputs) + + def add_output(self, output_scope): + if not self.is_outputs_modifiable(): + raise PSBTError("Outputs are not modifiable") + if self.version == 2: + self._validate_v2_output(output_scope, len(self.outputs)) + self.outputs.append(output_scope) + if self.version == 2: + self._raw_output_count_from_global = len(self.outputs) diff --git a/src/embit/psbtview.py b/src/embit/psbtview.py index d641248b..5700f627 100644 --- a/src/embit/psbtview.py +++ b/src/embit/psbtview.py @@ -13,8 +13,10 @@ Makes sense to run gc.collect() after processing of each scope to free memory. """ + # TODO: refactor, a lot of code is duplicated here from transaction.py from collections import OrderedDict +from binascii import hexlify import hashlib from . import compact from . import ec @@ -26,13 +28,13 @@ CompressMode, InputScope, OutputScope, + TxModifiable, + LOCKTIME_THRESHOLD, read_string, ser_string, skip_string, _GLOBAL_COUNT_FIELDS, _GLOBAL_FIXED_VALUE_LENGTHS, - _GLOBAL_V2_FIELDS, - _validate_global_fields, _validate_global_key, ) from .transaction import ( @@ -43,6 +45,8 @@ SIGHASH, hash_amounts, hash_script_pubkeys, + _signed_from_bytes, + _signed_to_bytes, ) @@ -154,7 +158,8 @@ def __init__(self, stream, offset, length=None): def version(self): if self._version is None: self.stream.seek(self.offset) - self._version = int.from_bytes(_read_exact(self.stream, 4), "little") + # tx version is a signed int32, matching Transaction.read_from + self._version = _signed_from_bytes(_read_exact(self.stream, 4)) return self._version @property @@ -259,6 +264,7 @@ def __init__( tx_offset=None, compress=CompressMode.KEEP_ALL, tx_length=None, + global_kvs=None, ): if version != 2 and tx_offset is None: raise PSBTError("Global tx is not found, but PSBT version is %d" % version) @@ -275,8 +281,35 @@ def __init__( self.compress = compress self._tx_version = self.tx.version if self.tx else None self._locktime = self.tx.locktime if self.tx else None + # For PSBTv2: dict of all global key→value pairs (excluding the \x00 global tx). + # This is the single source of truth for every global field, including + # PSBT_GLOBAL_TX_MODIFIABLE (\x06). None for PSBTv0 where the global + # scope is streamed verbatim. + self._global_kvs = global_kvs self.clear_cache() + @property + def tx_modifiable_flags(self): + """PSBT_GLOBAL_TX_MODIFIABLE byte (PSBTv2 only). None when absent or for PSBTv0.""" + if self._global_kvs is None: + return None + v = self._global_kvs.get(b"\x06") + if v is None: + return None + if len(v) != 1: + raise PSBTError("Invalid PSBT_GLOBAL_TX_MODIFIABLE length") + return int.from_bytes(v, "little") + + @tx_modifiable_flags.setter + def tx_modifiable_flags(self, value): + """Setting to None removes the field; ignored for PSBTv0.""" + if self._global_kvs is None: + return + if value is None: + self._global_kvs.pop(b"\x06", None) + else: + self._global_kvs[b"\x06"] = bytes([value]) + def clear_cache(self): # cache for digests self._hash_prevouts = None @@ -302,7 +335,9 @@ def view(cls, stream, offset=None, compress=CompressMode.KEEP_ALL): num_outputs = None tx_offset = None tx_len = None - global_fields = {} + # Collect all non-global-tx key-value pairs for PSBTv2 global scope rewriting. + # The global scope is small, so materialising it avoids byte-level injection. + global_kvs = {} while True: # read key and update cursor key = read_string(stream) @@ -311,28 +346,13 @@ def view(cls, stream, offset=None, compress=CompressMode.KEEP_ALL): if len(key) == 0: break _validate_global_key(key) - if key == b"\xfb" or key in _GLOBAL_V2_FIELDS: - value, value_size = _read_global_value(stream, key) - cur += value_size - if key == b"\xfb": - if version is not None: - raise PSBTError("Duplicated global version") - if len(value) != 4: - raise PSBTError("Global version must be 4 bytes") - version = int.from_bytes(value, "little") - if version not in [0, 2]: - raise PSBTError("Unsupported PSBT version %d" % version) - else: - if key in global_fields: - raise PSBTError("Duplicated global field") - global_fields[key] = value - elif key == b"\x00": - # we found global transaction + if key == b"\x00": + # we found global transaction; defer version==2 check until after the loop + # so that PSBT_GLOBAL_UNSIGNED_TX is rejected even when it appears before + # PSBT_GLOBAL_VERSION (key order is not guaranteed). if tx_offset is not None: - raise PSBTError("Duplicated global transaction") - if version == 2: - raise PSBTError("Global transaction with version 2 PSBT") - if b"\x04" in global_fields or b"\x05" in global_fields: + raise PSBTError("Duplicate global transaction") + if (num_inputs is not None) or (num_outputs is not None): raise PSBTError("Invalid global transaction") tx_len = compact.read_from(stream) cur += len(compact.to_bytes(tx_len)) @@ -344,16 +364,31 @@ def view(cls, stream, offset=None, compress=CompressMode.KEEP_ALL): stream.seek(tx_offset + tx_len) cur += tx_len else: - cur += skip_string(stream) + value, value_size = _read_global_value(stream, key) + cur += value_size + if key in global_kvs: + raise PSBTError("Duplicate global key: %s" % hexlify(key).decode()) + global_kvs[key] = value + if key == b"\xfb": + version = int.from_bytes(value, "little") + elif key == b"\x04": + num_inputs = compact.from_bytes(value) + elif key == b"\x05": + num_outputs = compact.from_bytes(value) first_scope = cur - # the check inside the loop only fires if 0xfb was seen before 0x00, - # so repeat it here to stay independent of the global map key order + if version not in (None, 0, 2): + raise PSBTError("Unsupported PSBT_GLOBAL_VERSION value: %d" % version) + if version == 0: + version = None + # PSBTv2 must not have a global unsigned transaction, regardless of key order if tx_offset is not None and version == 2: raise PSBTError("Global transaction with version 2 PSBT") - _validate_global_fields(version, tx_offset is not None, global_fields) - if tx_offset is None: - num_inputs = compact.from_bytes(global_fields[b"\x04"]) - num_outputs = compact.from_bytes(global_fields[b"\x05"]) + if version != 2: + for k in {b"\x02", b"\x03", b"\x04", b"\x05", b"\x06"}: + if k in global_kvs: + raise PSBTError( + "Global key %s is not allowed in PSBTv0" % hexlify(k).decode() + ) if None in [version or tx_offset, num_inputs, num_outputs]: raise PSBTError("Missing something important in PSBT") return cls( @@ -366,6 +401,7 @@ def view(cls, stream, offset=None, compress=CompressMode.KEEP_ALL): tx_offset, compress, tx_len, + global_kvs=global_kvs if version == 2 else None, ) def _skip_scope(self): @@ -437,14 +473,22 @@ def vin(self, i, compress=None): self.seek_to_scope(i) v = self.get_value(b"\x0e", from_current=True) + if v is None: + raise PSBTError( + "PSBTv2 input %d missing required PSBT_IN_PREVIOUS_TXID (0x0e)" % i + ) txid = bytes(reversed(v)) self.seek_to_scope(i) v = self.get_value(b"\x0f", from_current=True) + if v is None: + raise PSBTError( + "PSBTv2 input %d missing required PSBT_IN_OUTPUT_INDEX (0x0f)" % i + ) vout = int.from_bytes(v, "little") self.seek_to_scope(i) - v = self.get_value(b"\x10", from_current=True) or b"\xFF\xFF\xFF\xFF" + v = self.get_value(b"\x10", from_current=True) or b"\xff\xff\xff\xff" sequence = int.from_bytes(v, "little") return TransactionInput(txid, vout, sequence=sequence) @@ -458,10 +502,18 @@ def vout(self, i, compress=None): self.seek_to_scope(self.num_inputs + i) v = self.get_value(b"\x03", from_current=True) - value = int.from_bytes(v, "little") + if v is None: + raise PSBTError( + "PSBTv2 output %d missing required PSBT_OUT_AMOUNT (0x03)" % i + ) + value = _signed_from_bytes(v) self.seek_to_scope(self.num_inputs + i) v = self.get_value(b"\x04", from_current=True) + if v is None: + raise PSBTError( + "PSBTv2 output %d missing required PSBT_OUT_SCRIPT (0x04)" % i + ) script_pubkey = Script(v) return TransactionOutput(value, script_pubkey) @@ -469,15 +521,93 @@ def vout(self, i, compress=None): @property def locktime(self): if self._locktime is None: - v = self.get_value(b"\x03") - self._locktime = int.from_bytes(v, "little") if v is not None else 0 + if self.version == 2: + self._locktime = self._determine_locktime_v2() + else: + v = self.get_value(b"\x03") + self._locktime = int.from_bytes(v, "little") if v is not None else 0 return self._locktime + def _determine_locktime_v2(self): + """BIP370 locktime determination for PSBTv2. + + Derives the transaction locktime from per-input required locktime fields. + Falls back to PSBT_GLOBAL_FALLBACK_LOCKTIME (or 0) when no input imposes + a requirement. + """ + v = self.get_value(b"\x03") + if v is not None: + if len(v) != 4: + raise PSBTError("PSBT_GLOBAL_FALLBACK_LOCKTIME must be 4 bytes") + fallback = int.from_bytes(v, "little") + else: + fallback = 0 + + height_locktimes = [] + time_locktimes = [] + inputs_with_requirements = 0 + + for i in range(self.num_inputs): + self.seek_to_scope(i) + v_height = self.get_value(b"\x12", from_current=True) + self.seek_to_scope(i) + v_time = self.get_value(b"\x11", from_current=True) + + has_requirement = False + if v_height is not None: + if len(v_height) != 4: + raise PSBTError("PSBT_IN_REQUIRED_HEIGHT_LOCKTIME must be 4 bytes") + height_locktime = int.from_bytes(v_height, "little") + if height_locktime == 0 or height_locktime >= LOCKTIME_THRESHOLD: + raise PSBTError( + "Height-based locktime must be > 0 and < %d" + % LOCKTIME_THRESHOLD + ) + height_locktimes.append(height_locktime) + has_requirement = True + if v_time is not None: + if len(v_time) != 4: + raise PSBTError("PSBT_IN_REQUIRED_TIME_LOCKTIME must be 4 bytes") + time_locktime = int.from_bytes(v_time, "little") + if time_locktime < LOCKTIME_THRESHOLD: + raise PSBTError( + "Time-based locktime must be >= %d" % LOCKTIME_THRESHOLD + ) + time_locktimes.append(time_locktime) + has_requirement = True + if has_requirement: + inputs_with_requirements += 1 + + if inputs_with_requirements == 0: + return fallback + + inputs_supporting_height = len(height_locktimes) + inputs_supporting_time = len(time_locktimes) + + # Prefer height-based locktime if every input with requirements supports it + if inputs_supporting_height == inputs_with_requirements: + return max(height_locktimes) + # Fall back to time-based locktime if every input with requirements supports it + if inputs_supporting_time == inputs_with_requirements: + return max(time_locktimes) + + raise PSBTError( + "Cannot determine locktime: inputs have conflicting height and time locktime requirements" + ) + @property def tx_version(self): if self._tx_version is None: v = self.get_value(b"\x02") - self._tx_version = int.from_bytes(v, "little") if v is not None else 0 + if v is None: + if self.version == 2: + raise PSBTError("Missing PSBT_GLOBAL_TX_VERSION in PSBTv2") + self._tx_version = 0 + else: + if len(v) != 4: + raise PSBTError("Invalid PSBT_GLOBAL_TX_VERSION length") + # BIP370: PSBT_GLOBAL_TX_VERSION is a signed int32 + self._tx_version = _signed_from_bytes(v) return self._tx_version def seek_to_value(self, key_start, from_current=False): @@ -567,7 +697,7 @@ def sighash_taproot( sh, anyonecanpay = SIGHASH.check(sighash) h = hashes.tagged_hash_init("TapSighash", b"\x00") h.update(bytes([sighash])) - h.update(self.tx_version.to_bytes(4, "little")) + h.update(_signed_to_bytes(self.tx_version, 4)) h.update(self.locktime.to_bytes(4, "little")) if not anyonecanpay: h.update(self.hash_prevouts()) @@ -615,7 +745,7 @@ def sighash_segwit(self, input_index, script_pubkey, value, sighash=SIGHASH.ALL) inp = self.vin(input_index) zero = b"\x00" * 32 # for sighashes h = hashlib.sha256() - h.update(self.tx_version.to_bytes(4, "little")) + h.update(_signed_to_bytes(self.tx_version, 4)) if anyonecanpay: h.update(zero) else: @@ -655,7 +785,7 @@ def sighash_legacy(self, input_index, script_pubkey, sighash=SIGHASH.ALL): return b"\x00" * 31 + b"\x01" h = hashlib.sha256() - h.update(self.tx_version.to_bytes(4, "little")) + h.update(_signed_to_bytes(self.tx_version, 4)) # ANYONECANPAY - only one input is serialized if anyonecanpay: h.update(compact.to_bytes(1)) @@ -789,6 +919,23 @@ def sign_input_with_tapkey( counter += 1 return counter + def _update_tx_modifiable(self, inp_sighash: int) -> None: + if self.version != 2: + return + + is_anyonecanpay = bool(inp_sighash & SIGHASH.ANYONECANPAY) + sighash_type = inp_sighash & 0x1F + + if self.tx_modifiable_flags is None: + self.tx_modifiable_flags = 0 + + if not is_anyonecanpay: + self.tx_modifiable_flags &= ~TxModifiable.INPUTS + if sighash_type != SIGHASH.NONE: + self.tx_modifiable_flags &= ~TxModifiable.OUTPUTS + if sighash_type == SIGHASH.SINGLE: + self.tx_modifiable_flags |= TxModifiable.SIGHASH_SINGLE + def sign_input( self, i, root, sig_stream, sighash=SIGHASH.DEFAULT, extra_scope_data=None ) -> int: @@ -852,7 +999,7 @@ def sign_input( if fingerprint: # if taproot derivations are present add them for pub in inp.taproot_bip32_derivations: - (_leafs, derivation) = inp.taproot_bip32_derivations[pub] + _leafs, derivation = inp.taproot_bip32_derivations[pub] if derivation.fingerprint == fingerprint: # Add only if not already present if (pub, derivation) not in bip32_derivations: @@ -912,6 +1059,8 @@ def sign_input( for pub, leaf in inp.taproot_sigs: ser_string(sig_stream, b"\x14" + pub.xonly() + leaf) ser_string(sig_stream, inp.taproot_sigs[(pub, leaf)]) + if counter > 0: + self._update_tx_modifiable(inp_sighash) return counter h = self.sighash(i, sighash=inp_sighash, input_scope=inp) @@ -923,11 +1072,13 @@ def sign_input( # sig plus sighash flag inp.partial_sigs[rootpub] = sig.serialize() + bytes([inp_sighash]) counter += 1 + self._update_tx_modifiable(inp_sighash) for prv, pub in derived_keypairs: sig = prv.sign(h) # sig plus sighash flag inp.partial_sigs[pub] = sig.serialize() + bytes([inp_sighash]) counter += 1 + self._update_tx_modifiable(inp_sighash) for pub in inp.partial_sigs: ser_string(sig_stream, b"\x02" + pub.serialize()) ser_string(sig_stream, inp.partial_sigs[pub]) @@ -955,6 +1106,11 @@ def sign_with(self, root, sig_stream, sighash=SIGHASH.DEFAULT) -> int: counter += self.sign_input(i, root, sig_stream, sighash=sighash) # add separator sig_stream.write(b"\x00") + # BIP-370: signer must set PSBT_GLOBAL_TX_MODIFIABLE for PSBTv2. + # Default to 0 (nothing modifiable) when no signing occurred and the + # field was absent — avoids leaving the field missing after a sign pass. + if self.version == 2 and self.tx_modifiable_flags is None: + self.tx_modifiable_flags = 0 return counter def write_to( @@ -978,15 +1134,29 @@ def write_to( compress = self.compress # first we write global scope - self.stream.seek(self.offset) - res = read_write(self.stream, writable_stream, self.first_scope - self.offset) + if self._global_kvs is not None: + # PSBTv2: reconstruct global scope from the materialised key-value dict. + # This sidesteps byte-level injection and gives deterministic key ordering. + writable_stream.write(self.MAGIC) + res = len(self.MAGIC) + for k in sorted(self._global_kvs.keys()): + res += ser_string(writable_stream, k) + res += ser_string(writable_stream, self._global_kvs[k]) + writable_stream.write(b"\x00") # global scope separator + res += 1 + else: + # PSBTv0: stream global scope directly from source (includes global tx). + self.stream.seek(self.offset) + res = read_write( + self.stream, writable_stream, self.first_scope - self.offset + ) # write all inputs for i in range(self.num_inputs): inp = self.input(i) # add extra data from extra input streams for s in extra_input_streams: - extra = InputScope.read_from(s) + extra = InputScope.read_from(s, version=self.version) inp.update(extra) if compress: inp.clear_metadata(compress=compress) @@ -997,7 +1167,7 @@ def write_to( out = self.output(i) # add extra data from extra input streams for s in extra_output_streams: - extra = OutputScope.read_from(s) + extra = OutputScope.read_from(s, version=self.version) out.update(extra) if compress: out.clear_metadata(compress=compress) diff --git a/src/embit/transaction.py b/src/embit/transaction.py index 2301021f..56aad0d7 100644 --- a/src/embit/transaction.py +++ b/src/embit/transaction.py @@ -40,6 +40,23 @@ def check(cls, sighash: int): # util functions +def _signed_from_bytes(b): + res = int.from_bytes(b, "little") + bits = 8 * len(b) + if bits and res >= (1 << (bits - 1)): + res -= 1 << bits + return res + + +def _signed_to_bytes(i, length): + bits = 8 * length + if i < -(1 << (bits - 1)) or i >= (1 << (bits - 1)): + raise OverflowError("int too big to convert") + if i < 0: + i += 1 << bits + return i.to_bytes(length, "little") + + def hash_amounts(amounts): h = hashlib.sha256() for a in amounts: @@ -83,7 +100,7 @@ def is_segwit(self): def write_to(self, stream): """Returns the byte serialization of the transaction""" - res = stream.write(self.version.to_bytes(4, "little")) + res = stream.write(_signed_to_bytes(self.version, 4)) if self.is_segwit: res += stream.write(b"\x00\x01") # segwit marker and flag res += stream.write(compact.to_bytes(len(self.vin))) @@ -100,7 +117,7 @@ def write_to(self, stream): def hash(self): h = hashlib.sha256() - h.update(self.version.to_bytes(4, "little")) + h.update(_signed_to_bytes(self.version, 4)) h.update(compact.to_bytes(len(self.vin))) for inp in self.vin: h.update(inp.serialize()) @@ -151,7 +168,7 @@ def read_vout(cls, stream, idx): @classmethod def read_from(cls, stream): - ver = int.from_bytes(_read_exact(stream, 4), "little") + ver = _signed_from_bytes(_read_exact(stream, 4)) num_vin = compact.read_from(stream) # if num_vin is zero it is a segwit transaction is_segwit = num_vin == 0 @@ -228,7 +245,7 @@ def sighash_taproot( sh, anyonecanpay = SIGHASH.check(sighash) h = hashes.tagged_hash_init("TapSighash", b"\x00") h.update(bytes([sighash])) - h.update(self.version.to_bytes(4, "little")) + h.update(_signed_to_bytes(self.version, 4)) h.update(self.locktime.to_bytes(4, "little")) if not anyonecanpay: h.update(self.hash_prevouts()) @@ -275,7 +292,7 @@ def sighash_segwit(self, input_index, script_pubkey, value, sighash=SIGHASH.ALL) inp = self.vin[input_index] zero = b"\x00" * 32 # for sighashes h = hashlib.sha256() - h.update(self.version.to_bytes(4, "little")) + h.update(_signed_to_bytes(self.version, 4)) if anyonecanpay: h.update(zero) else: @@ -314,7 +331,7 @@ def sighash_legacy(self, input_index, script_pubkey, sighash=SIGHASH.ALL): return b"\x00" * 31 + b"\x01" h = hashlib.sha256() - h.update(self.version.to_bytes(4, "little")) + h.update(_signed_to_bytes(self.version, 4)) # ANYONECANPAY - only one input is serialized if anyonecanpay: h.update(compact.to_bytes(1)) diff --git a/tests/tests/test_psbtV2.py b/tests/tests/test_psbtV2.py new file mode 100644 index 00000000..2830728e --- /dev/null +++ b/tests/tests/test_psbtV2.py @@ -0,0 +1,656 @@ +""" +BIP 370 Test Vectors for PSBTv2 +https://github.com/bitcoin/bips/blob/master/bip-0370.mediawiki#user-content-Test_Vectors +""" + +import pytest +from binascii import unhexlify +from io import BytesIO +from embit.psbt import ( + PSBT, + PSBTError, + TxModifiable, + InputScope, + OutputScope, + CompressMode, +) +from embit.psbtview import PSBTView +from embit.script import Script +from embit.transaction import SIGHASH, Transaction, TransactionInput, TransactionOutput + + +class TestPSBTVectors: + """Test vectors for PSBT parsing and validation""" + + def test_invalid_psbtv0_with_version_field(self): + """PSBTv0 but with PSBT_GLOBAL_VERSION set to 2 should be invalid""" + hex_data = "70736274ff01007102000000010b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc80000000000feffffff020008af2f00000000160014c430f64c4756da310dbd1a085572ef299926272c8bbdeb0b00000000160014a07dac8ab6ca942d379ed795f835ba71c9cc68850000000001fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e01086b02473044022005275a485734e0ae1f3b971237586f0e72dc85833d278c0e474cd23112c0fa5e02206b048c83cebc3c41d0b93cc7da76185cedbd030d005b08018be2b98bbacbdf7b012103760dcca05f3997dc65b293060f7f29f1514c8c527048e12802b041d4fc340a2700220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a000000002202036efe2c255621986553ba9d65c3ddc64165ca1436e05aa35a4c6eb02451cf796d18f69d873e540000800100008000000080010000006200000000" + + with pytest.raises(PSBTError): + PSBT.parse(unhexlify(hex_data)) + + def test_invalid_psbtv0_with_tx_version(self): + """PSBTv0 but with PSBT_GLOBAL_TX_VERSION should be invalid""" + hex_data = "70736274ff01007102000000010b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc80000000000feffffff020008af2f00000000160014c430f64c4756da310dbd1a085572ef299926272c8bbdeb0b00000000160014a07dac8ab6ca942d379ed795f835ba71c9cc68850000000001020402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e01086b02473044022005275a485734e0ae1f3b971237586f0e72dc85833d278c0e474cd23112c0fa5e02206b048c83cebc3c41d0b93cc7da76185cedbd030d005b08018be2b98bbacbdf7b012103760dcca05f3997dc65b293060f7f29f1514c8c527048e12802b041d4fc340a2700220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a000000002202036efe2c255621986553ba9d65c3ddc64165ca1436e05aa35a4c6eb02451cf796d18f69d873e540000800100008000000080010000006200000000" + + with pytest.raises(PSBTError): + PSBT.parse(unhexlify(hex_data)) + + def test_invalid_psbtv0_with_fallback_locktime(self): + """PSBTv0 but with PSBT_GLOBAL_FALLBACK_LOCKTIME should be invalid""" + hex_data = "70736274ff01007102000000010b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc80000000000feffffff020008af2f00000000160014c430f64c4756da310dbd1a085572ef299926272c8bbdeb0b00000000160014a07dac8ab6ca942d379ed795f835ba71c9cc68850000000001030402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e01086b02473044022005275a485734e0ae1f3b971237586f0e72dc85833d278c0e474cd23112c0fa5e02206b048c83cebc3c41d0b93cc7da76185cedbd030d005b08018be2b98bbacbdf7b012103760dcca05f3997dc65b293060f7f29f1514c8c527048e12802b041d4fc340a2700220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a000000002202036efe2c255621986553ba9d65c3ddc64165ca1436e05aa35a4c6eb02451cf796d18f69d873e540000800100008000000080010000006200000000" + + with pytest.raises(PSBTError): + PSBT.parse(unhexlify(hex_data)) + + def test_invalid_psbtv0_with_input_count(self): + """PSBTv0 but with PSBT_GLOBAL_INPUT_COUNT should be invalid""" + hex_data = "70736274ff01007102000000010b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc80000000000feffffff020008af2f00000000160014c430f64c4756da310dbd1a085572ef299926272c8bbdeb0b00000000160014a07dac8ab6ca942d379ed795f835ba71c9cc68850000000001040102000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e01086b02473044022005275a485734e0ae1f3b971237586f0e72dc85833d278c0e474cd23112c0fa5e02206b048c83cebc3c41d0b93cc7da76185cedbd030d005b08018be2b98bbacbdf7b012103760dcca05f3997dc65b293060f7f29f1514c8c527048e12802b041d4fc340a2700220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a000000002202036efe2c255621986553ba9d65c3ddc64165ca1436e05aa35a4c6eb02451cf796d18f69d873e540000800100008000000080010000006200000000" + + with pytest.raises(PSBTError): + PSBT.parse(unhexlify(hex_data)) + + def test_invalid_psbtv0_with_output_count(self): + """PSBTv0 but with PSBT_GLOBAL_OUTPUT_COUNT should be invalid""" + hex_data = "70736274ff01007102000000010b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc80000000000feffffff020008af2f00000000160014c430f64c4756da310dbd1a085572ef299926272c8bbdeb0b00000000160014a07dac8ab6ca942d379ed795f835ba71c9cc68850000000001050102000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e01086b02473044022005275a485734e0ae1f3b971237586f0e72dc85833d278c0e474cd23112c0fa5e02206b048c83cebc3c41d0b93cc7da76185cedbd030d005b08018be2b98bbacbdf7b012103760dcca05f3997dc65b293060f7f29f1514c8c527048e12802b041d4fc340a2700220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a000000002202036efe2c255621986553ba9d65c3ddc64165ca1436e05aa35a4c6eb02451cf796d18f69d873e540000800100008000000080010000006200000000" + + with pytest.raises(PSBTError): + PSBT.parse(unhexlify(hex_data)) + + def test_invalid_psbtv0_with_tx_modifiable(self): + """PSBTv0 but with PSBT_GLOBAL_TX_MODIFIABLE should be invalid""" + hex_data = "70736274ff01007102000000010b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc80000000000feffffff020008af2f00000000160014c430f64c4756da310dbd1a085572ef299926272c8bbdeb0b00000000160014a07dac8ab6ca942d379ed795f835ba71c9cc68850000000001060100000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e01086b02473044022005275a485734e0ae1f3b971237586f0e72dc85833d278c0e474cd23112c0fa5e02206b048c83cebc3c41d0b93cc7da76185cedbd030d005b08018be2b98bbacbdf7b012103760dcca05f3997dc65b293060f7f29f1514c8c527048e12802b041d4fc340a2700220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a000000002202036efe2c255621986553ba9d65c3ddc64165ca1436e05aa35a4c6eb02451cf796d18f69d873e540000800100008000000080010000006200000000" + + with pytest.raises(PSBTError): + PSBT.parse(unhexlify(hex_data)) + + def test_invalid_psbtv0_with_previous_txid(self): + """PSBTv0 but with PSBT_IN_PREVIOUS_TXID should be invalid""" + hex_data = "70736274ff01007102000000010b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc80000000000feffffff020008af2f00000000160014c430f64c4756da310dbd1a085572ef299926272c8bbdeb0b00000000160014a07dac8ab6ca942d379ed795f835ba71c9cc688500000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e01086b02473044022005275a485734e0ae1f3b971237586f0e72dc85833d278c0e474cd23112c0fa5e02206b048c83cebc3c41d0b93cc7da76185cedbd030d005b08018be2b98bbacbdf7b012103760dcca05f3997dc65b293060f7f29f1514c8c527048e12802b041d4fc340a27010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc800220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a000000002202036efe2c255621986553ba9d65c3ddc64165ca1436e05aa35a4c6eb02451cf796d18f69d873e540000800100008000000080010000006200000000" + + with pytest.raises(PSBTError): + PSBT.parse(unhexlify(hex_data)) + + def test_invalid_psbtv0_with_output_index(self): + """PSBTv0 but with PSBT_IN_OUTPUT_INDEX should be invalid""" + hex_data = "70736274ff01007102000000010b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc80000000000feffffff020008af2f00000000160014c430f64c4756da310dbd1a085572ef299926272c8bbdeb0b00000000160014a07dac8ab6ca942d379ed795f835ba71c9cc688500000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e01086b02473044022005275a485734e0ae1f3b971237586f0e72dc85833d278c0e474cd23112c0fa5e02206b048c83cebc3c41d0b93cc7da76185cedbd030d005b08018be2b98bbacbdf7b012103760dcca05f3997dc65b293060f7f29f1514c8c527048e12802b041d4fc340a27010f040000000000220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a000000002202036efe2c255621986553ba9d65c3ddc64165ca1436e05aa35a4c6eb02451cf796d18f69d873e540000800100008000000080010000006200000000" + + with pytest.raises(PSBTError): + PSBT.parse(unhexlify(hex_data)) + + def test_invalid_psbtv0_with_sequence(self): + """PSBTv0 but with PSBT_IN_SEQUENCE should be invalid""" + hex_data = "70736274ff01007102000000010b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc80000000000feffffff020008af2f00000000160014c430f64c4756da310dbd1a085572ef299926272c8bbdeb0b00000000160014a07dac8ab6ca942d379ed795f835ba71c9cc688500000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e01086b02473044022005275a485734e0ae1f3b971237586f0e72dc85833d278c0e474cd23112c0fa5e02206b048c83cebc3c41d0b93cc7da76185cedbd030d005b08018be2b98bbacbdf7b012103760dcca05f3997dc65b293060f7f29f1514c8c527048e12802b041d4fc340a27011004ffffffff00220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a000000002202036efe2c255621986553ba9d65c3ddc64165ca1436e05aa35a4c6eb02451cf796d18f69d873e540000800100008000000080010000006200000000" + + with pytest.raises(PSBTError): + PSBT.parse(unhexlify(hex_data)) + + def test_invalid_psbtv0_with_required_time_locktime(self): + """PSBTv0 but with PSBT_IN_REQUIRED_TIME_LOCKTIME should be invalid""" + hex_data = "70736274ff01007102000000010b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc80000000000feffffff020008af2f00000000160014c430f64c4756da310dbd1a085572ef299926272c8bbdeb0b00000000160014a07dac8ab6ca942d379ed795f835ba71c9cc688500000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e01086b02473044022005275a485734e0ae1f3b971237586f0e72dc85833d278c0e474cd23112c0fa5e02206b048c83cebc3c41d0b93cc7da76185cedbd030d005b08018be2b98bbacbdf7b012103760dcca05f3997dc65b293060f7f29f1514c8c527048e12802b041d4fc340a270111048c8dc46200220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a000000002202036efe2c255621986553ba9d65c3ddc64165ca1436e05aa35a4c6eb02451cf796d18f69d873e540000800100008000000080010000006200000000" + + with pytest.raises(PSBTError): + PSBT.parse(unhexlify(hex_data)) + + def test_invalid_psbtv0_with_required_height_locktime(self): + """PSBTv0 but with PSBT_IN_REQUIRED_HEIGHT_LOCKTIME should be invalid""" + hex_data = "70736274ff01007102000000010b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc80000000000feffffff020008af2f00000000160014c430f64c4756da310dbd1a085572ef299926272c8bbdeb0b00000000160014a07dac8ab6ca942d379ed795f835ba71c9cc688500000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e01086b02473044022005275a485734e0ae1f3b971237586f0e72dc85833d278c0e474cd23112c0fa5e02206b048c83cebc3c41d0b93cc7da76185cedbd030d005b08018be2b98bbacbdf7b012103760dcca05f3997dc65b293060f7f29f1514c8c527048e12802b041d4fc340a270112041027000000220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a000000002202036efe2c255621986553ba9d65c3ddc64165ca1436e05aa35a4c6eb02451cf796d18f69d873e540000800100008000000080010000006200000000" + + with pytest.raises(PSBTError): + PSBT.parse(unhexlify(hex_data)) + + def test_invalid_psbtv0_with_output_amount(self): + """PSBTv0 but with PSBT_OUT_AMOUNT should be invalid""" + hex_data = "70736274ff01007102000000010b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc80000000000feffffff020008af2f00000000160014c430f64c4756da310dbd1a085572ef299926272c8bbdeb0b00000000160014a07dac8ab6ca942d379ed795f835ba71c9cc688500000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e01086b02473044022005275a485734e0ae1f3b971237586f0e72dc85833d278c0e474cd23112c0fa5e02206b048c83cebc3c41d0b93cc7da76185cedbd030d005b08018be2b98bbacbdf7b012103760dcca05f3997dc65b293060f7f29f1514c8c527048e12802b041d4fc340a2700220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f00000000002202036efe2c255621986553ba9d65c3ddc64165ca1436e05aa35a4c6eb02451cf796d18f69d873e540000800100008000000080010000006200000000" + + with pytest.raises(PSBTError): + PSBT.parse(unhexlify(hex_data)) + + def test_invalid_psbtv0_with_output_script(self): + """PSBTv0 but with PSBT_OUT_SCRIPT should be invalid""" + hex_data = "70736274ff01007102000000010b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc80000000000feffffff020008af2f00000000160014c430f64c4756da310dbd1a085572ef299926272c8bbdeb0b00000000160014a07dac8ab6ca942d379ed795f835ba71c9cc688500000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e01086b02473044022005275a485734e0ae1f3b971237586f0e72dc85833d278c0e474cd23112c0fa5e02206b048c83cebc3c41d0b93cc7da76185cedbd030d005b08018be2b98bbacbdf7b012103760dcca05f3997dc65b293060f7f29f1514c8c527048e12802b041d4fc340a2700220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000104160014a07dac8ab6ca942d379ed795f835ba71c9cc6885002202036efe2c255621986553ba9d65c3ddc64165ca1436e05aa35a4c6eb02451cf796d18f69d873e540000800100008000000080010000006200000000" + + with pytest.raises(PSBTError): + PSBT.parse(unhexlify(hex_data)) + + def test_invalid_psbtv2_global_unsigned_tx(self): + """PSBTv2 with PSBT_GLOBAL_UNSIGNED_TX should be invalid""" + hex_data = "70736274ff0100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e00000000010204020000000103040000000001040101010501020106010701fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f0400000000011004feffffff0111048c8dc4620112041027000000220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c00220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" + + with pytest.raises(PSBTError): + PSBT.parse(unhexlify(hex_data)) + + def test_invalid_psbtv2_missing_input_count(self): + """PSBTv2 missing PSBT_GLOBAL_INPUT_COUNT should be invalid""" + hex_data = "70736274ff01020402000000010304000000000105010201fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f0400000000011004feffffff00220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c00220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" + + with pytest.raises(PSBTError): + PSBT.parse(unhexlify(hex_data)) + + def test_invalid_psbtv2_missing_output_count(self): + """PSBTv2 missing PSBT_GLOBAL_OUTPUT_COUNT should be invalid""" + hex_data = "70736274ff01020402000000010304000000000104010101fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f0400000000011004feffffff00220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c00220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" + + with pytest.raises(PSBTError): + PSBT.parse(unhexlify(hex_data)) + + def test_invalid_psbtv2_missing_tx_version(self): + """PSBTv2 missing PSBT_GLOBAL_TX_VERSION should be invalid""" + hex_data = "70736274ff010401010105010201fb040200000000010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f0400000000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" + + with pytest.raises(PSBTError): + PSBT.parse(unhexlify(hex_data)) + + def test_invalid_psbtv2_missing_previous_txid(self): + """PSBTv2 input missing PSBT_IN_PREVIOUS_TXID should be invalid""" + hex_data = "70736274ff0102040200000001030400000000010401010105010201fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010f0400000000011004feffffff00220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c00220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" + + with pytest.raises(PSBTError): + PSBT.parse(unhexlify(hex_data)) + + def test_invalid_psbtv2_missing_output_index(self): + """PSBTv2 input missing PSBT_IN_OUTPUT_INDEX should be invalid""" + hex_data = "70736274ff0102040200000001030400000000010401010105010201fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8011004feffffff00220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c00220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" + + with pytest.raises(PSBTError): + PSBT.parse(unhexlify(hex_data)) + + def test_invalid_psbtv2_missing_output_amount(self): + """PSBTv2 output missing PSBT_OUT_AMOUNT should be invalid""" + hex_data = "70736274ff0102040200000001030400000000010401010105010201fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f0400000000011004feffffff00220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000104160014c430f64c4756da310dbd1a085572ef299926272c00220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" + + with pytest.raises(PSBTError): + PSBT.parse(unhexlify(hex_data)) + + def test_invalid_psbtv2_missing_output_script(self): + """PSBTv2 output missing PSBT_OUT_SCRIPT should be invalid""" + hex_data = "70736274ff0102040200000001030400000000010401010105010201fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f0400000000011004feffffff00220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f0000000000220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" + + with pytest.raises(PSBTError): + PSBT.parse(unhexlify(hex_data)) + + def test_invalid_psbtv2_invalid_time_locktime(self): + """PSBTv2 with PSBT_IN_REQUIRED_TIME_LOCKTIME less than 500000000 should be invalid""" + hex_data = "70736274ff01020402000000010401010105010201fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f0400000000011104ff64cd1d00220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c00220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" + + with pytest.raises(PSBTError): + PSBT.parse(unhexlify(hex_data)) + + def test_invalid_psbtv2_invalid_height_locktime(self): + """PSBTv2 with PSBT_IN_REQUIRED_HEIGHT_LOCKTIME >= 500000000 should be invalid""" + hex_data = "70736274ff01020402000000010401010105010201fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f04000000000112040065cd1d00220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c00220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" + + with pytest.raises(PSBTError): + PSBT.parse(unhexlify(hex_data)) + + def test_invalid_psbtv2_height_locktime_zero(self): + """PSBTv2 with PSBT_IN_REQUIRED_HEIGHT_LOCKTIME == 0 should be invalid""" + hex_data = "70736274ff010204020000000103040000000001040101010501020106010701fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f0400000000011004feffffff0111048c8dc4620112040000000000220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c00220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" + + with pytest.raises(PSBTError): + PSBT.parse(unhexlify(hex_data)) + + # Valid PSBTs + def test_valid_psbtv2_minimal(self): + """1 input, 2 output PSBTv2, required fields only should be valid""" + hex_data = "70736274ff01020402000000010401010105010201fb040200000000010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f0400000000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" + + psbt = PSBT.parse(unhexlify(hex_data)) + assert psbt.version == 2 + assert len(psbt.inputs) == 1 + assert len(psbt.outputs) == 2 + + def test_valid_psbtv2_updated(self): + """1 input, 2 output updated PSBTv2 should be valid""" + hex_data = "70736274ff01020402000000010401010105010201fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f040000000000220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c00220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" + + psbt = PSBT.parse(unhexlify(hex_data)) + assert psbt.version == 2 + assert len(psbt.inputs) == 1 + assert len(psbt.outputs) == 2 + + def test_valid_psbtv2_with_sequence(self): + """1 input, 2 output PSBTv2 with sequence should be valid""" + hex_data = "70736274ff01020402000000010401010105010201fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f0400000000011004feffffff00220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c00220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" + + psbt = PSBT.parse(unhexlify(hex_data)) + assert psbt.version == 2 + assert psbt.inputs[0].sequence == 0xFFFFFFFE + + def test_valid_psbtv2_with_sequence_and_locktime(self): + """PSBTv2 with all possible fields should be valid, including sequence""" + hex_data = "70736274ff0102040200000001030400000000010401010105010201fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f0400000000011004feffffff0111048c8dc4620112041027000000220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c00220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" + + psbt = PSBT.parse(unhexlify(hex_data)) + assert psbt.version == 2 + assert psbt.inputs[0].sequence == 0xFFFFFFFE + assert psbt.inputs[0].required_time_locktime == 1657048460 + assert psbt.inputs[0].required_height_locktime == 10000 + + def test_inputs_modifiable_flag(self): + """Inputs Modifiable Flag (bit 0) only""" + hex_data = "70736274ff0102040200000001040101010501020106010101fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f040000000000220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c00220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" + + psbt = PSBT.parse(unhexlify(hex_data)) + assert psbt.version == 2 + assert len(psbt.inputs) == 1 + assert len(psbt.outputs) == 2 + assert psbt.tx_modifiable_flags == 0x01 + assert psbt.is_inputs_modifiable() + assert not psbt.is_outputs_modifiable() + assert not psbt.has_sighash_single() + + def test_outputs_modifiable_flag(self): + """Outputs Modifiable Flag (bit 1) only""" + hex_data = "70736274ff0102040200000001040101010501020106010201fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f040000000000220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c00220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" + + psbt = PSBT.parse(unhexlify(hex_data)) + assert psbt.version == 2 + assert len(psbt.inputs) == 1 + assert len(psbt.outputs) == 2 + assert psbt.tx_modifiable_flags == 0x02 + assert not psbt.is_inputs_modifiable() + assert psbt.is_outputs_modifiable() + assert not psbt.has_sighash_single() + + def test_sighash_single_flag(self): + """Has SIGHASH_SINGLE Flag (bit 2) only""" + hex_data = "70736274ff0102040200000001040101010501020106010401fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f040000000000220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c00220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" + + psbt = PSBT.parse(unhexlify(hex_data)) + assert psbt.version == 2 + assert len(psbt.inputs) == 1 + assert len(psbt.outputs) == 2 + assert psbt.tx_modifiable_flags == 0x04 + assert not psbt.is_inputs_modifiable() + assert not psbt.is_outputs_modifiable() + assert psbt.has_sighash_single() + + def test_undefined_flag_bit3(self): + """Undefined flag (bit 3) — parser must not reject it""" + hex_data = "70736274ff0102040200000001040101010501020106010801fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f040000000000220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c00220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" + + psbt = PSBT.parse(unhexlify(hex_data)) + assert psbt.version == 2 + assert len(psbt.inputs) == 1 + assert len(psbt.outputs) == 2 + assert psbt.tx_modifiable_flags == 0x08 + # bits 0/1/2 are all clear + assert not psbt.is_inputs_modifiable() + assert not psbt.is_outputs_modifiable() + assert not psbt.has_sighash_single() + + def test_inputs_and_outputs_modifiable(self): + """Inputs Modifiable (bit 0) + Outputs Modifiable (bit 1)""" + hex_data = "70736274ff0102040200000001040101010501020106010301fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f040000000000220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c00220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" + + psbt = PSBT.parse(unhexlify(hex_data)) + assert psbt.version == 2 + assert len(psbt.inputs) == 1 + assert len(psbt.outputs) == 2 + assert psbt.tx_modifiable_flags == 0x03 + assert psbt.is_inputs_modifiable() + assert psbt.is_outputs_modifiable() + assert not psbt.has_sighash_single() + + def test_inputs_modifiable_and_sighash_single(self): + """Inputs Modifiable (bit 0) + Has SIGHASH_SINGLE (bit 2)""" + hex_data = "70736274ff0102040200000001040101010501020106010501fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f040000000000220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c00220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" + + psbt = PSBT.parse(unhexlify(hex_data)) + assert psbt.version == 2 + assert len(psbt.inputs) == 1 + assert len(psbt.outputs) == 2 + assert psbt.tx_modifiable_flags == 0x05 + assert psbt.is_inputs_modifiable() + assert not psbt.is_outputs_modifiable() + assert psbt.has_sighash_single() + + def test_outputs_modifiable_and_sighash_single(self): + """Outputs Modifiable (bit 1) + Has SIGHASH_SINGLE (bit 2)""" + hex_data = "70736274ff0102040200000001040101010501020106010601fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f040000000000220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c00220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" + + psbt = PSBT.parse(unhexlify(hex_data)) + assert psbt.version == 2 + assert len(psbt.inputs) == 1 + assert len(psbt.outputs) == 2 + assert psbt.tx_modifiable_flags == 0x06 + assert not psbt.is_inputs_modifiable() + assert psbt.is_outputs_modifiable() + assert psbt.has_sighash_single() + + def test_all_defined_flags(self): + """All defined flags set: Inputs (bit 0) + Outputs (bit 1) + SIGHASH_SINGLE (bit 2)""" + hex_data = "70736274ff0102040200000001040101010501020106010701fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f040000000000220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c00220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" + + psbt = PSBT.parse(unhexlify(hex_data)) + assert psbt.version == 2 + assert len(psbt.inputs) == 1 + assert len(psbt.outputs) == 2 + assert psbt.tx_modifiable_flags == 0x07 + assert psbt.is_inputs_modifiable() + assert psbt.is_outputs_modifiable() + assert psbt.has_sighash_single() + + def test_all_possible_flags(self): + """All 8 bits set (0xFF) — defined + undefined flags""" + hex_data = "70736274ff010204020000000104010101050102010601ff01fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f040000000000220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c00220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" + + psbt = PSBT.parse(unhexlify(hex_data)) + assert psbt.version == 2 + assert len(psbt.inputs) == 1 + assert len(psbt.outputs) == 2 + assert psbt.tx_modifiable_flags == 0xFF + assert psbt.is_inputs_modifiable() + assert psbt.is_outputs_modifiable() + assert psbt.has_sighash_single() + # undefined bits preserved as-is + assert psbt.tx_modifiable_flags & ~( + TxModifiable.INPUTS | TxModifiable.OUTPUTS | TxModifiable.SIGHASH_SINGLE + ) + + def test_valid_psbtv2_with_all_fields(self): + """PSBTv2 with all possible fields should be valid""" + hex_data = "70736274ff010204020000000103040000000001040101010501020106010701fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f0400000000011004feffffff0111048c8dc4620112041027000000220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c00220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" + + psbt = PSBT.parse(unhexlify(hex_data)) + assert psbt.version == 2 + assert psbt.inputs[0].required_time_locktime == 1657048460 + assert psbt.inputs[0].required_height_locktime == 10000 + + +class TestPSBTLocktime: + """Test locktime determination algorithm for PSBTv2""" + + def test_locktime_no_locktimes_specified(self): + """No locktimes specified should return 0""" + hex_data = "70736274ff01020402000000010401010105010201fb040200000000010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f0400000000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" + + psbt = PSBT.parse(unhexlify(hex_data)) + assert psbt.determine_locktime() == 0 + + def test_locktime_fallback_zero(self): + """Fallback locktime of 0 should return 0""" + hex_data = "70736274ff0102040200000001030400000000010401020105010101fb040200000000010e200f758dbfbd4da7c16c8a3309c3c81e1100f561ea646db5b01752c485e1bdde9f010f040100000000010e203a1b3b3c837d6489ea7a31d8e6c7dd503c001bef3e06958e7574808d68ca78a5010f0400000000000103084f9335770000000001041600140b1352cacd03cf6aa1b7f3c8d6388671b34a5e1100" + + psbt = PSBT.parse(unhexlify(hex_data)) + assert psbt.determine_locktime() == 0 + + def test_locktime_height_10000(self): + """Height locktime of 10000 should return 10000""" + hex_data = "70736274ff0102040200000001030400000000010401020105010101fb040200000000010e200f758dbfbd4da7c16c8a3309c3c81e1100f561ea646db5b01752c485e1bdde9f010f04010000000112041027000000010e203a1b3b3c837d6489ea7a31d8e6c7dd503c001bef3e06958e7574808d68ca78a5010f0400000000000103084f9335770000000001041600140b1352cacd03cf6aa1b7f3c8d6388671b34a5e1100" + + psbt = PSBT.parse(unhexlify(hex_data)) + assert psbt.determine_locktime() == 10000 + + def test_locktime_max_height(self): + """Multiple height locktimes should return maximum""" + hex_data = "70736274ff0102040200000001030400000000010401020105010101fb040200000000010e200f758dbfbd4da7c16c8a3309c3c81e1100f561ea646db5b01752c485e1bdde9f010f04010000000112041027000000010e203a1b3b3c837d6489ea7a31d8e6c7dd503c001bef3e06958e7574808d68ca78a5010f040000000001120428230000000103084f9335770000000001041600140b1352cacd03cf6aa1b7f3c8d6388671b34a5e1100" + + psbt = PSBT.parse(unhexlify(hex_data)) + assert len(psbt.inputs) == 2 + assert psbt.determine_locktime() == 10000 # max(10000, 9000) + + def test_locktime_height_and_time(self): + """1 input with Height and other with height and time locktimes should return max height value""" + hex_data = "70736274ff0102040200000001030400000000010401020105010101fb040200000000010e200f758dbfbd4da7c16c8a3309c3c81e1100f561ea646db5b01752c485e1bdde9f010f04010000000112041027000000010e203a1b3b3c837d6489ea7a31d8e6c7dd503c001bef3e06958e7574808d68ca78a5010f04000000000111048c8dc46201120428230000000103084f9335770000000001041600140b1352cacd03cf6aa1b7f3c8d6388671b34a5e1100" + + psbt = PSBT.parse(unhexlify(hex_data)) + assert len(psbt.inputs) == 2 + assert psbt.determine_locktime() == 10000 + + def test_locktime_height_preferred_over_time(self): + """Height locktime should be preferred when both present (BIP-370: height preferred on tie)""" + hex_data = "70736274ff0102040200000001030400000000010401020105010101fb040200000000010e200f758dbfbd4da7c16c8a3309c3c81e1100f561ea646db5b01752c485e1bdde9f010f04010000000111048b8dc4620112041027000000010e203a1b3b3c837d6489ea7a31d8e6c7dd503c001bef3e06958e7574808d68ca78a5010f04000000000111048c8dc46201120428230000000103084f9335770000000001041600140b1352cacd03cf6aa1b7f3c8d6388671b34a5e1100" + + psbt = PSBT.parse(unhexlify(hex_data)) + assert len(psbt.inputs) == 2 + assert psbt.determine_locktime() == 10000 # Height takes priority over time + + def test_locktime_time_only(self): + """Time locktime only should return time value""" + hex_data = "70736274ff0102040200000001030400000000010401020105010101fb040200000000010e200f758dbfbd4da7c16c8a3309c3c81e1100f561ea646db5b01752c485e1bdde9f010f04010000000111048b8dc46200010e203a1b3b3c837d6489ea7a31d8e6c7dd503c001bef3e06958e7574808d68ca78a5010f04000000000111048c8dc46201120428230000000103084f9335770000000001041600140b1352cacd03cf6aa1b7f3c8d6388671b34a5e1100" + + psbt = PSBT.parse(unhexlify(hex_data)) + assert len(psbt.inputs) == 2 + assert psbt.determine_locktime() == 1657048460 + + def test_locktime_max_time(self): + """Multiple time locktimes should return maximum""" + hex_data = "70736274ff0102040200000001030400000000010401020105010101fb040200000000010e200f758dbfbd4da7c16c8a3309c3c81e1100f561ea646db5b01752c485e1bdde9f010f040100000000010e203a1b3b3c837d6489ea7a31d8e6c7dd503c001bef3e06958e7574808d68ca78a5010f04000000000111048c8dc462000103084f9335770000000001041600140b1352cacd03cf6aa1b7f3c8d6388671b34a5e1100" + + psbt = PSBT.parse(unhexlify(hex_data)) + assert len(psbt.inputs) == 2 + assert psbt.determine_locktime() == 1657048460 + + def test_locktime_time_wins_when_not_all_support_height(self): + """Input 1 has height 10000 and time 1657048459, Input 2 has time 1657048460 only should return max time 1657048460""" + hex_data = "70736274ff0102040200000001030400000000010401020105010101fb040200000000010e200f758dbfbd4da7c16c8a3309c3c81e1100f561ea646db5b01752c485e1bdde9f010f04010000000111048b8dc4620112041027000000010e203a1b3b3c837d6489ea7a31d8e6c7dd503c001bef3e06958e7574808d68ca78a5010f04000000000111048c8dc462000103084f9335770000000001041600140b1352cacd03cf6aa1b7f3c8d6388671b34a5e1100" + + psbt = PSBT.parse(unhexlify(hex_data)) + assert len(psbt.inputs) == 2 + assert psbt.determine_locktime() == 1657048460 + + def test_locktime_uncomputable(self): + """Input with height only and input with time only should raise PSBTError""" + hex_data = "70736274ff0102040200000001030400000000010401020105010101fb040200000000010e200f758dbfbd4da7c16c8a3309c3c81e1100f561ea646db5b01752c485e1bdde9f010f04010000000112041027000000010e203a1b3b3c837d6489ea7a31d8e6c7dd503c001bef3e06958e7574808d68ca78a5010f04000000000111048c8dc462000103084f9335770000000001041600140b1352cacd03cf6aa1b7f3c8d6388671b34a5e1100" + + psbt = PSBT.parse(unhexlify(hex_data)) + assert len(psbt.inputs) == 2 + with pytest.raises(PSBTError): + psbt.determine_locktime() + + +class TestPSBTv2Constructor: + """Test BIP-370 Creator / Constructor role: create_v2(), add_input(), add_output()""" + + def test_create_v2_defaults(self): + """create_v2() produces a version-2 PSBT with sensible defaults""" + psbt = PSBT.create_v2() + assert psbt.version == 2 + assert psbt.tx_version == 2 + assert psbt.locktime is None + assert psbt.tx_modifiable_flags == TxModifiable.INPUTS | TxModifiable.OUTPUTS + assert len(psbt.inputs) == 0 + assert len(psbt.outputs) == 0 + + def test_create_v2_custom_params(self): + """create_v2() accepts custom tx_version, fallback_locktime and tx_modifiable""" + psbt = PSBT.create_v2( + tx_version=1, + fallback_locktime=600000, + tx_modifiable=TxModifiable.INPUTS, + ) + assert psbt.tx_version == 1 + assert psbt.locktime == 600000 + assert psbt.tx_modifiable_flags == TxModifiable.INPUTS + + def test_add_input_appends_and_updates_count(self): + """add_input() appends the scope and increments the global input count""" + psbt = PSBT.create_v2() + inp = InputScope() + inp.txid = bytes(32) + inp.vout = 0 + psbt.add_input(inp) + assert len(psbt.inputs) == 1 + assert psbt._raw_input_count_from_global == 1 + + def test_add_input_requires_inputs_modifiable(self): + """add_input() raises when INPUTS bit is clear""" + psbt = PSBT.create_v2(tx_modifiable=TxModifiable.OUTPUTS) + inp = InputScope() + inp.txid = bytes(32) + inp.vout = 0 + with pytest.raises(PSBTError): + psbt.add_input(inp) + + def test_add_input_requires_txid(self): + """add_input() raises when PSBT_IN_PREVIOUS_TXID is absent""" + psbt = PSBT.create_v2() + inp = InputScope() + inp.vout = 0 + with pytest.raises(PSBTError): + psbt.add_input(inp) + + def test_add_input_requires_vout(self): + """add_input() raises when PSBT_IN_OUTPUT_INDEX is absent""" + psbt = PSBT.create_v2() + inp = InputScope() + inp.txid = bytes(32) + with pytest.raises(PSBTError): + psbt.add_input(inp) + + def test_add_output_appends_and_updates_count(self): + """add_output() appends the scope and increments the global output count""" + psbt = PSBT.create_v2() + out = OutputScope() + out.value = 100000 + out.script_pubkey = Script(b"\x00\x14" + bytes(20)) + psbt.add_output(out) + assert len(psbt.outputs) == 1 + assert psbt._raw_output_count_from_global == 1 + + def test_add_output_requires_outputs_modifiable(self): + """add_output() raises when OUTPUTS bit is clear""" + psbt = PSBT.create_v2(tx_modifiable=TxModifiable.INPUTS) + out = OutputScope() + out.value = 100000 + out.script_pubkey = Script(b"\x00\x14" + bytes(20)) + with pytest.raises(PSBTError): + psbt.add_output(out) + + def test_add_output_requires_amount(self): + """add_output() raises when PSBT_OUT_AMOUNT is absent""" + psbt = PSBT.create_v2() + out = OutputScope() + out.script_pubkey = Script(b"\x00\x14" + bytes(20)) + with pytest.raises(PSBTError): + psbt.add_output(out) + + def test_add_output_requires_script(self): + """add_output() raises when PSBT_OUT_SCRIPT is absent""" + psbt = PSBT.create_v2() + out = OutputScope() + out.value = 100000 + with pytest.raises(PSBTError): + psbt.add_output(out) + + +class TestTxModifiableUpdate: + """Test BIP-370 Signer role: _update_tx_modifiable() correctly restricts future modifications""" + + def test_sighash_all_clears_inputs_and_outputs(self): + """SIGHASH_ALL (not ANYONECANPAY) must clear both INPUTS and OUTPUTS bits""" + psbt = PSBT.create_v2() + psbt.tx_modifiable_flags = TxModifiable.INPUTS | TxModifiable.OUTPUTS + psbt._update_tx_modifiable(SIGHASH.ALL) + assert not psbt.is_inputs_modifiable() + assert not psbt.is_outputs_modifiable() + assert not psbt.has_sighash_single() + + def test_sighash_none_clears_inputs_preserves_outputs(self): + """SIGHASH_NONE clears INPUTS bit but must not touch the OUTPUTS bit""" + psbt = PSBT.create_v2() + psbt.tx_modifiable_flags = TxModifiable.INPUTS | TxModifiable.OUTPUTS + psbt._update_tx_modifiable(SIGHASH.NONE) + assert not psbt.is_inputs_modifiable() + assert psbt.is_outputs_modifiable() + + def test_sighash_single_sets_flag_and_clears_inputs(self): + """SIGHASH_SINGLE sets SIGHASH_SINGLE flag and clears INPUTS and OUTPUTS bits""" + psbt = PSBT.create_v2() + psbt.tx_modifiable_flags = TxModifiable.INPUTS | TxModifiable.OUTPUTS + psbt._update_tx_modifiable(SIGHASH.SINGLE) + assert not psbt.is_inputs_modifiable() + assert not psbt.is_outputs_modifiable() + assert psbt.has_sighash_single() + + def test_sighash_all_anyonecanpay_preserves_inputs_clears_outputs(self): + """SIGHASH_ALL|ANYONECANPAY preserves INPUTS bit and clears OUTPUTS bit""" + psbt = PSBT.create_v2() + psbt.tx_modifiable_flags = TxModifiable.INPUTS | TxModifiable.OUTPUTS + psbt._update_tx_modifiable(SIGHASH.ALL | SIGHASH.ANYONECANPAY) + assert psbt.is_inputs_modifiable() + assert not psbt.is_outputs_modifiable() + + def test_update_creates_flags_when_absent(self): + """_update_tx_modifiable initialises the field to 0 when it was absent""" + psbt = PSBT.create_v2() + psbt.tx_modifiable_flags = None + psbt._update_tx_modifiable(SIGHASH.ALL) + assert psbt.tx_modifiable_flags == 0 + + def test_no_op_for_psbtv0(self): + """_update_tx_modifiable is a no-op for PSBTv0 (PSBT_GLOBAL_TX_MODIFIABLE is v2-only)""" + psbt = PSBT.create_v2() + psbt.version = None # downgrade to v0 + psbt.tx_modifiable_flags = TxModifiable.INPUTS | TxModifiable.OUTPUTS + psbt._update_tx_modifiable(SIGHASH.ALL) + # must remain untouched because version != 2 + assert psbt.tx_modifiable_flags == TxModifiable.INPUTS | TxModifiable.OUTPUTS + + +class TestPSBTViewV2Validation: + def test_psbtview_rejects_v2_only_global_in_v0(self): + """PSBTView should enforce the same v0/v2 global separation as PSBT""" + psbt = PSBT(Transaction()) + raw = psbt.serialize() + stream = BytesIO(raw) + stream.read(len(PSBT.MAGIC)) + while True: + pos = stream.tell() + key_len = stream.read(1)[0] + if key_len == 0: + separator = pos + break + stream.seek(key_len, 1) + value_len = stream.read(1)[0] + stream.seek(value_len, 1) + + tx_version_kv = b"\x01\x02\x04\x02\x00\x00\x00" + invalid = raw[:separator] + tx_version_kv + raw[separator:] + with pytest.raises(PSBTError): + PSBTView.view(BytesIO(invalid)) + + +class TestPSBTv2TransactionVersion: + def test_psbt_tx_preserves_zero_and_negative_versions(self): + """PSBT_GLOBAL_TX_VERSION is signed and zero must not fall back to 2""" + psbt = PSBT.create_v2(tx_version=0) + inp = InputScope() + inp.txid = bytes(32) + inp.vout = 0 + psbt.add_input(inp) + out = OutputScope() + out.value = 1 + out.script_pubkey = Script(b"") + psbt.add_output(out) + assert psbt.tx.serialize()[:4] == b"\x00\x00\x00\x00" + + psbt.tx_version = -1 + assert psbt.tx.serialize()[:4] == b"\xff\xff\xff\xff" + + +class TestPSBTv2Compression: + def test_psbtv2_compress_non_witness_utxo_is_key_order_independent(self): + """PSBTv2 compress should not load the full prev tx when 0x00 precedes 0x0f""" + prev = Transaction( + vin=[TransactionInput(bytes([1]) * 32, 0)], + vout=[TransactionOutput(1234, Script(b"\x51"))], + ) + psbt = PSBT.create_v2() + inp = InputScope() + inp.txid = prev.txid() + inp.vout = 0 + # InputScope serializes non_witness_utxo before PSBTv2 prevout fields. + inp.non_witness_utxo = prev + psbt.add_input(inp) + out = OutputScope() + out.value = 1000 + out.script_pubkey = Script(b"\x51") + psbt.add_output(out) + + parsed = PSBT.parse(psbt.serialize(), compress=CompressMode.PARTIAL) + assert parsed.inputs[0].non_witness_utxo is None + assert parsed.inputs[0]._utxo.value == 1234 + assert parsed.verify() From 0b81ab3159d878dfdaf5a41a2b2e8e2c5b544aac Mon Sep 17 00:00:00 2001 From: notTanveer Date: Wed, 2 Sep 2026 10:33:47 -0300 Subject: [PATCH 02/44] fix(psbt): dedupe the BIP-370 TX_MODIFIABLE update between PSBT and PSBTView Extract next_tx_modifiable() in psbt.py. PSBT and PSBTView each carried a byte-identical copy of the BIP-370 sighash -> flag rules, so a later correction to one would silently leave the constrained-device path wrong. Going through a pure function also removes a None &= -2 TypeError on a PSBTView built with version=2 and no global_kvs, whose flags setter no-ops. Drop the trailing tx_modifiable_flags block in PSBTView.sign_with(). Every path in sign_input() that adds a signature already updates the flags, so the block could only fire after signing nothing, stamping TX_MODIFIABLE=0 onto a PSBT this pass never touched. PSBT.sign_with() does not do this. (PSBT-only half of 59908f5 on feat/silent-payments-V2.) --- src/embit/psbt.py | 31 +++++++++++++++++-------------- src/embit/psbtview.py | 23 ++++------------------- 2 files changed, 21 insertions(+), 33 deletions(-) diff --git a/src/embit/psbt.py b/src/embit/psbt.py index 835b3c8a..f72651bd 100644 --- a/src/embit/psbt.py +++ b/src/embit/psbt.py @@ -917,6 +917,20 @@ class TxModifiable: SIGHASH_SINGLE = 0b00000100 +def next_tx_modifiable(flags, inp_sighash: int) -> int: + """BIP-370: the PSBT_GLOBAL_TX_MODIFIABLE value after a signature using + ``inp_sighash`` is added. ``flags`` may be None (field absent).""" + flags = flags or 0 + sighash_type = inp_sighash & 0x1F + if not inp_sighash & SIGHASH.ANYONECANPAY: + flags &= ~TxModifiable.INPUTS + if sighash_type != SIGHASH.NONE: + flags &= ~TxModifiable.OUTPUTS + if sighash_type == SIGHASH.SINGLE: + flags |= TxModifiable.SIGHASH_SINGLE + return flags + + class PSBT(EmbitBase): MAGIC = b"psbt\xff" # for subclasses @@ -1587,22 +1601,11 @@ def sign_with(self, root, sighash=SIGHASH.DEFAULT) -> int: def _update_tx_modifiable(self, inp_sighash: int) -> None: """Update PSBT_GLOBAL_TX_MODIFIABLE after a signature is added.""" - if self.version != 2: return - - is_anyonecanpay = bool(inp_sighash & SIGHASH.ANYONECANPAY) - sighash_type = inp_sighash & 0x1F - - if self.tx_modifiable_flags is None: - self.tx_modifiable_flags = 0 - - if not is_anyonecanpay: - self.tx_modifiable_flags &= ~TxModifiable.INPUTS - if sighash_type != SIGHASH.NONE: - self.tx_modifiable_flags &= ~TxModifiable.OUTPUTS - if sighash_type == SIGHASH.SINGLE: - self.tx_modifiable_flags |= TxModifiable.SIGHASH_SINGLE + self.tx_modifiable_flags = next_tx_modifiable( + self.tx_modifiable_flags, inp_sighash + ) def get_tx_modifiable(self): return self.tx_modifiable_flags diff --git a/src/embit/psbtview.py b/src/embit/psbtview.py index 5700f627..ce4a881c 100644 --- a/src/embit/psbtview.py +++ b/src/embit/psbtview.py @@ -28,8 +28,8 @@ CompressMode, InputScope, OutputScope, - TxModifiable, LOCKTIME_THRESHOLD, + next_tx_modifiable, read_string, ser_string, skip_string, @@ -922,19 +922,9 @@ def sign_input_with_tapkey( def _update_tx_modifiable(self, inp_sighash: int) -> None: if self.version != 2: return - - is_anyonecanpay = bool(inp_sighash & SIGHASH.ANYONECANPAY) - sighash_type = inp_sighash & 0x1F - - if self.tx_modifiable_flags is None: - self.tx_modifiable_flags = 0 - - if not is_anyonecanpay: - self.tx_modifiable_flags &= ~TxModifiable.INPUTS - if sighash_type != SIGHASH.NONE: - self.tx_modifiable_flags &= ~TxModifiable.OUTPUTS - if sighash_type == SIGHASH.SINGLE: - self.tx_modifiable_flags |= TxModifiable.SIGHASH_SINGLE + self.tx_modifiable_flags = next_tx_modifiable( + self.tx_modifiable_flags, inp_sighash + ) def sign_input( self, i, root, sig_stream, sighash=SIGHASH.DEFAULT, extra_scope_data=None @@ -1106,11 +1096,6 @@ def sign_with(self, root, sig_stream, sighash=SIGHASH.DEFAULT) -> int: counter += self.sign_input(i, root, sig_stream, sighash=sighash) # add separator sig_stream.write(b"\x00") - # BIP-370: signer must set PSBT_GLOBAL_TX_MODIFIABLE for PSBTv2. - # Default to 0 (nothing modifiable) when no signing occurred and the - # field was absent — avoids leaving the field missing after a sign pass. - if self.version == 2 and self.tx_modifiable_flags is None: - self.tx_modifiable_flags = 0 return counter def write_to( From 9577f0cab270ccd35c7784ef4475541b0d8c48ae Mon Sep 17 00:00:00 2001 From: notTanveer Date: Wed, 19 Aug 2026 18:08:17 +0530 Subject: [PATCH 03/44] fix(psbt): close PSBTv2 global-field validation gaps left by upstream rebase Rebasing feat/silent-payments-V2 onto upstream/master's PSBTv2 hardening series (eb6104f) merged in upstream's declarative _validate_global_key, V2_FIELDS/_validate_key scope checks, and _read_global_value eager-shape validation, but left _validate_global_fields (canonical count-field encoding, fixed-length checks, required-field presence) unwired in both PSBT.read_from and PSBTView.view. Malformed PSBT_GLOBAL_INPUT_COUNT / PSBT_GLOBAL_OUTPUT_COUNT values crashed with RuntimeError/ValueError instead of PSBTError, non-canonical compact encodings went unrejected, and PSBTView accepted a v2 PSBT missing PSBT_GLOBAL_TX_VERSION until something later touched the lazy .tx_version property. Wire _validate_global_fields into both parse paths, replacing the now-duplicate manual v0/v2 field-exclusion checks it subsumes. All 25 of upstream's tests/tests/test_psbt_versions.py cases now pass. --- src/embit/psbt.py | 20 ++++---------------- src/embit/psbtview.py | 19 ++++++++----------- 2 files changed, 12 insertions(+), 27 deletions(-) diff --git a/src/embit/psbt.py b/src/embit/psbt.py index f72651bd..2886f93f 100644 --- a/src/embit/psbt.py +++ b/src/embit/psbt.py @@ -1231,9 +1231,11 @@ def read_from(cls, stream, compress=CompressMode.KEEP_ALL): "Unsupported PSBT_GLOBAL_VERSION value: %d" % parsed_version ) + # Canonical-encoding, fixed-length, and required-field checks for every + # global key, shared with PSBTView.view()'s streaming parse. + _validate_global_fields(version, b"\x00" in global_kvs, global_kvs) + if version == 2: # PSBTv2 - if b"\x00" in global_kvs: # PSBT_GLOBAL_UNSIGNED_TX - raise PSBTError("PSBT_GLOBAL_UNSIGNED_TX is not allowed in PSBTv2") # Pass all global KVs to unknown; __init__ calls parse_unknowns. psbt = cls(tx=None, unknown=global_kvs, version=version) @@ -1249,20 +1251,6 @@ def read_from(cls, stream, compress=CompressMode.KEEP_ALL): if psbt.tx_version is None: raise PSBTError("PSBTv2 missing required PSBT_GLOBAL_TX_VERSION (0x02)") else: # PSBTv0 (version is None or 0) - if b"\x00" not in global_kvs: - raise PSBTError("PSBT_GLOBAL_UNSIGNED_TX (0x00) is required for PSBTv0") - - # Check for forbidden v2-only global keys in v0 context. - # PSBT_GLOBAL_VERSION (0xfb) with value 0 is explicitly permitted by BIP174 - # and was already validated above, so it is excluded from this set. - v2_only_global_keys = {b"\x02", b"\x03", b"\x04", b"\x05", b"\x06"} - for k_v2_only in v2_only_global_keys: - if k_v2_only in global_kvs: - raise PSBTError( - "Global key %s is not allowed in PSBTv0" - % hexlify(k_v2_only).decode() - ) - tx_bytes = global_kvs.pop(b"\x00") # Remove so it's not in unknown tx_for_v0 = cls.TX_CLS.parse(tx_bytes) # BIP-174: the global transaction must be unsigned diff --git a/src/embit/psbtview.py b/src/embit/psbtview.py index ce4a881c..735c573e 100644 --- a/src/embit/psbtview.py +++ b/src/embit/psbtview.py @@ -35,6 +35,7 @@ skip_string, _GLOBAL_COUNT_FIELDS, _GLOBAL_FIXED_VALUE_LENGTHS, + _validate_global_fields, _validate_global_key, ) from .transaction import ( @@ -352,7 +353,7 @@ def view(cls, stream, offset=None, compress=CompressMode.KEEP_ALL): # PSBT_GLOBAL_VERSION (key order is not guaranteed). if tx_offset is not None: raise PSBTError("Duplicate global transaction") - if (num_inputs is not None) or (num_outputs is not None): + if b"\x04" in global_kvs or b"\x05" in global_kvs: raise PSBTError("Invalid global transaction") tx_len = compact.read_from(stream) cur += len(compact.to_bytes(tx_len)) @@ -371,10 +372,6 @@ def view(cls, stream, offset=None, compress=CompressMode.KEEP_ALL): global_kvs[key] = value if key == b"\xfb": version = int.from_bytes(value, "little") - elif key == b"\x04": - num_inputs = compact.from_bytes(value) - elif key == b"\x05": - num_outputs = compact.from_bytes(value) first_scope = cur if version not in (None, 0, 2): raise PSBTError("Unsupported PSBT_GLOBAL_VERSION value: %d" % version) @@ -383,12 +380,12 @@ def view(cls, stream, offset=None, compress=CompressMode.KEEP_ALL): # PSBTv2 must not have a global unsigned transaction, regardless of key order if tx_offset is not None and version == 2: raise PSBTError("Global transaction with version 2 PSBT") - if version != 2: - for k in {b"\x02", b"\x03", b"\x04", b"\x05", b"\x06"}: - if k in global_kvs: - raise PSBTError( - "Global key %s is not allowed in PSBTv0" % hexlify(k).decode() - ) + # Canonical-encoding, fixed-length, and required-field checks for every + # global key, shared with PSBT.read_from's two-pass parse. + _validate_global_fields(version, tx_offset is not None, global_kvs) + if tx_offset is None: + num_inputs = compact.from_bytes(global_kvs[b"\x04"]) + num_outputs = compact.from_bytes(global_kvs[b"\x05"]) if None in [version or tx_offset, num_inputs, num_outputs]: raise PSBTError("Missing something important in PSBT") return cls( From 24d4b592071dec30ef8c2819c45b96a3c441e57f Mon Sep 17 00:00:00 2001 From: notTanveer Date: Sat, 22 Aug 2026 23:40:23 +0530 Subject: [PATCH 04/44] fix(psbt): correctness, RAM fixes in PSBTv2 parsing paths and revert signed byte helpers Parsing tx_version unsigned closes the same Transaction(version=tx_version) overflow the helpers were added for. --- src/embit/liquid/pset.py | 15 +--- src/embit/psbt.py | 132 ++++++++++++------------------ src/embit/psbtview.py | 107 +++++++++++++++--------- src/embit/transaction.py | 29 ++----- tests/tests/test_psbtV2.py | 6 +- tests/tests/test_psbt_versions.py | 35 +++++--- 6 files changed, 160 insertions(+), 164 deletions(-) diff --git a/src/embit/liquid/pset.py b/src/embit/liquid/pset.py index 6ce50587..1f0d62ae 100644 --- a/src/embit/liquid/pset.py +++ b/src/embit/liquid/pset.py @@ -27,7 +27,7 @@ class LInputScope(InputScope): TX_CLS = LTransaction TXOUT_CLS = LTransactionOutput - def __init__(self, unknown: dict = {}, **kwargs): + def __init__(self, unknown: dict = None, **kwargs): # liquid-specific fields: self.value = None self.value_blinding_factor = None @@ -244,7 +244,7 @@ def write_to(self, stream, skip_separator=False, version=None, **kwargs) -> int: class LOutputScope(OutputScope): - def __init__(self, unknown: dict = {}, vout=None, **kwargs): + def __init__(self, unknown: dict = None, vout=None, **kwargs): # liquid stuff self.value_commitment = None self.value_blinding_factor = None @@ -502,16 +502,9 @@ class PSET(PSBT): TX_CLS = LTransaction @classmethod - def _validate_v2_output(cls, out, i): + def _v2_output_has_amount(cls, out): """PSET allows value_commitment as an alternative to value (blinded outputs).""" - if out.value is None and not getattr(out, "value_commitment", None): - raise PSBTError( - "PSBTv2 output %d missing required PSBT_OUT_AMOUNT (0x03)" % i - ) - if out.script_pubkey is None: - raise PSBTError( - "PSBTv2 output %d missing required PSBT_OUT_SCRIPT (0x04)" % i - ) + return out.value is not None or out.value_commitment is not None def unblind(self, blinding_key): for inp in self.inputs: diff --git a/src/embit/psbt.py b/src/embit/psbt.py index 2886f93f..317e8291 100644 --- a/src/embit/psbt.py +++ b/src/embit/psbt.py @@ -4,8 +4,6 @@ TransactionOutput, TransactionInput, SIGHASH, - _signed_from_bytes, - _signed_to_bytes, ) from . import compact from . import bip32 @@ -99,7 +97,10 @@ def ser_string(stream, s: bytes) -> int: def read_string(stream) -> bytes: - l = compact.read_from(stream) + try: + l = compact.read_from(stream) + except (RuntimeError, TypeError) as e: + raise PSBTError("Failed to read key/value length: %s" % e) s = stream.read(l) if len(s) != l: raise PSBTError("Failed to read %d bytes" % l) @@ -148,6 +149,26 @@ def derive_hdkey(root, derivation): return root.derive(der) +def choose_locktime( + height_locktimes, time_locktimes, num_with_requirements, fallback=0 +): + """BIP-370 locktime determination. + + ``height_locktimes``/``time_locktimes`` are the required locktimes collected + from the inputs and ``num_with_requirements`` is how many inputs expressed at + least one requirement. Height is preferred when every such input supports it. + """ + if num_with_requirements == 0: + return fallback + if len(height_locktimes) == num_with_requirements: + return max(height_locktimes) + if len(time_locktimes) == num_with_requirements: + return max(time_locktimes) + raise PSBTError( + "Cannot determine locktime: inputs have conflicting height and time locktime requirements" + ) + + class DerivationPath(EmbitBase): def __init__(self, fingerprint: bytes, derivation: list): self.fingerprint = fingerprint @@ -176,8 +197,8 @@ def read_from(cls, stream): class PSBTScope(EmbitBase): V2_FIELDS = () - def __init__(self, unknown: dict = {}): - self.unknown = unknown + def __init__(self, unknown: dict = None): + self.unknown = {} if unknown is None else unknown self.parse_unknowns() def write_to(self, stream, skip_separator=False, **kwargs) -> int: @@ -236,7 +257,7 @@ class InputScope(PSBTScope): TXOUT_CLS = TransactionOutput V2_FIELDS = (b"\x0e", b"\x0f", b"\x10", b"\x11", b"\x12") - def __init__(self, unknown: dict = {}, vin=None, compress=CompressMode.KEEP_ALL): + def __init__(self, unknown: dict = None, vin=None, compress=CompressMode.KEEP_ALL): self.compress = compress self.txid = None self.vout = None @@ -245,7 +266,6 @@ def __init__(self, unknown: dict = {}, vin=None, compress=CompressMode.KEEP_ALL) self.txid = vin.txid self.vout = vin.vout self.sequence = vin.sequence - self.unknown = unknown self.non_witness_utxo = None self.witness_utxo = None self._utxo = None @@ -269,7 +289,7 @@ def __init__(self, unknown: dict = {}, vin=None, compress=CompressMode.KEEP_ALL) self.final_scriptwitness = None self.required_time_locktime = None self.required_height_locktime = None - self.parse_unknowns() + super().__init__(unknown) def clear_metadata(self, compress=CompressMode.CLEAR_ALL): """Removes metadata like derivations, utxos etc except final or partial sigs""" @@ -708,9 +728,15 @@ def read_from(cls, stream, compress=CompressMode.KEEP_ALL, vin=None, version=Non # index after the non-witness UTXO. Peek only at compact-sized keys and # skip values to keep compressed parsing independent of key order. res._prescan_vout = None - if version == 2 and compress and res.vout is None: + if version == 2 and compress: try: + # Probe seekability BEFORE consuming anything: if we cannot rewind, + # a half-finished prescan would leave the cursor mid-map and corrupt + # the real parse below. start_pos = stream.seek(0, 1) + except (AttributeError, OSError): + start_pos = None # not seekable; OOM protection unavailable + if start_pos is not None: while True: k = read_string(stream) if len(k) == 0: @@ -719,14 +745,10 @@ def read_from(cls, stream, compress=CompressMode.KEEP_ALL, vin=None, version=Non v = read_string(stream) if len(v) == 4: res._prescan_vout = int.from_bytes(v, "little") - else: - # Let the real parse raise the precise validation error. - pass + # otherwise let the real parse raise the precise error break skip_string(stream) stream.seek(start_pos) - except (AttributeError, OSError): - pass # stream not seekable; OOM protection may be unavailable while True: key = read_string(stream) # separator @@ -740,20 +762,19 @@ def read_from(cls, stream, compress=CompressMode.KEEP_ALL, vin=None, version=Non class OutputScope(PSBTScope): V2_FIELDS = (b"\x03", b"\x04") - def __init__(self, unknown: dict = {}, vout=None, compress=CompressMode.KEEP_ALL): + def __init__(self, unknown: dict = None, vout=None, compress=CompressMode.KEEP_ALL): self.compress = compress self.value = None self.script_pubkey = None if vout is not None: self.value = vout.value self.script_pubkey = vout.script_pubkey - self.unknown = unknown self.redeem_script = None self.witness_script = None self.bip32_derivations = OrderedDict() self.taproot_bip32_derivations = OrderedDict() self.taproot_internal_key = None - self.parse_unknowns() + super().__init__(unknown) def clear_metadata(self, compress=CompressMode.CLEAR_ALL): """Removes metadata like derivations, utxos etc except final or partial sigs""" @@ -822,8 +843,9 @@ def read_value(self, stream, k, version=None): raise PSBTError("PSBT_OUT_AMOUNT must be 8 bytes") if self.value is not None: raise PSBTError("Duplicated PSBT_OUT_AMOUNT") - self.value = _signed_from_bytes(v) - if self.value < 0: + # BIP370: PSBT_OUT_AMOUNT is a signed int64, so the top half is negative + self.value = int.from_bytes(v, "little") + if self.value >= 2**63: raise PSBTError("PSBT_OUT_AMOUNT must be non-negative") elif k == b"\x04": if version != 2: @@ -868,8 +890,7 @@ def write_to(self, stream, skip_separator=False, version=None, **kwargs) -> int: if version == 2: if self.value is not None: r += ser_string(stream, b"\x03") - # BIP370: PSBT_OUT_AMOUNT is a signed 64-bit integer - r += ser_string(stream, _signed_to_bytes(self.value, 8)) + r += ser_string(stream, self.value.to_bytes(8, "little")) if self.script_pubkey is not None: r += ser_string(stream, b"\x04") r += self.script_pubkey.write_to(stream) @@ -951,11 +972,15 @@ def _validate_v2_input(cls, inp, i): "PSBTv2 input %d missing required PSBT_IN_OUTPUT_INDEX (0x0f)" % i ) + @classmethod + def _v2_output_has_amount(cls, out): + """Subclasses (e.g. PSET) may override to accept alternative amount fields.""" + return out.value is not None + @classmethod def _validate_v2_output(cls, out, i): - """Check that a PSBTv2 output has all required fields. - Subclasses (e.g. PSET) may override this to accept alternative fields.""" - if out.value is None: + """Check that a PSBTv2 output has all required fields.""" + if not cls._v2_output_has_amount(out): raise PSBTError( "PSBTv2 output %d missing required PSBT_OUT_AMOUNT (0x03)" % i ) @@ -987,24 +1012,6 @@ def parse_tx(self, tx): self.inputs = [self.PSBTIN_CLS(vin=vin) for vin in tx.vin] self.outputs = [self.PSBTOUT_CLS(vout=vout) for vout in tx.vout] - def __eq__(self, other): - if type(self) is not type(other): - return False - return ( - self.version == other.version - and self.tx_version == other.tx_version - and self.locktime == other.locktime - and self.tx_modifiable_flags == other.tx_modifiable_flags - and self.inputs == other.inputs - and self.outputs == other.outputs - and self.xpubs == other.xpubs - and self.unknown == other.unknown - ) - - def __hash__(self): - # defining __eq__ sets __hash__ to None, so restore EmbitBase behaviour - return hash(self.serialize()) - @staticmethod def _classify_locktimes(inputs): """Classify inputs by locktime requirement type. @@ -1035,42 +1042,13 @@ def determine_locktime(self): """ if self.version != 2: return self.locktime or 0 - - height_locktimes, time_locktimes, inputs_with_locktime_requirements = ( - self._classify_locktimes(self.inputs) - ) - - # If no inputs have locktime requirements, use fallback - if inputs_with_locktime_requirements == 0: - return self.locktime or 0 - - # height preferred, if ALL inputs with requirements support it - if len(height_locktimes) == inputs_with_locktime_requirements: - return max(height_locktimes) - - # fall back to time if ALL inputs with requirements support it - if len(time_locktimes) == inputs_with_locktime_requirements: - return max(time_locktimes) - - raise PSBTError( - "Cannot determine locktime: inputs have conflicting height and time locktime requirements" + return choose_locktime( + *self._classify_locktimes(self.inputs), fallback=self.locktime or 0 ) def _validate_locktime_compatibility(self, inputs): """Verify that the given input list has mutually compatible locktime requirements.""" - height_locktimes, time_locktimes, inputs_with_requirements = ( - self._classify_locktimes(inputs) - ) - if inputs_with_requirements == 0: - return - if ( - len(height_locktimes) == inputs_with_requirements - or len(time_locktimes) == inputs_with_requirements - ): - return - raise PSBTError( - "Input locktime requirements are incompatible with existing inputs" - ) + choose_locktime(*self._classify_locktimes(inputs), fallback=0) @property def tx(self): @@ -1142,8 +1120,7 @@ def write_to(self, stream) -> int: if self.version == 2: tx_version = self.tx_version if self.tx_version is not None else 2 r += ser_string(stream, b"\x02") - # BIP370: PSBT_GLOBAL_TX_VERSION is a signed int32 - r += ser_string(stream, _signed_to_bytes(tx_version, 4)) + r += ser_string(stream, tx_version.to_bytes(4, "little")) if self.locktime is not None: r += ser_string(stream, b"\x03") r += ser_string(stream, self.locktime.to_bytes(4, "little")) @@ -1342,8 +1319,7 @@ def parse_unknowns(self): v = self.unknown.pop(k) if len(v) != 4: raise PSBTError("PSBT_GLOBAL_TX_VERSION must be 4 bytes") - # BIP370: PSBT_GLOBAL_TX_VERSION is a signed int32 - self.tx_version = _signed_from_bytes(v) + self.tx_version = int.from_bytes(v, "little") elif k == b"\x03": if self.version == 2: v = self.unknown.pop(k) diff --git a/src/embit/psbtview.py b/src/embit/psbtview.py index 735c573e..d650264c 100644 --- a/src/embit/psbtview.py +++ b/src/embit/psbtview.py @@ -29,6 +29,7 @@ InputScope, OutputScope, LOCKTIME_THRESHOLD, + choose_locktime, next_tx_modifiable, read_string, ser_string, @@ -46,8 +47,6 @@ SIGHASH, hash_amounts, hash_script_pubkeys, - _signed_from_bytes, - _signed_to_bytes, ) @@ -159,8 +158,7 @@ def __init__(self, stream, offset, length=None): def version(self): if self._version is None: self.stream.seek(self.offset) - # tx version is a signed int32, matching Transaction.read_from - self._version = _signed_from_bytes(_read_exact(self.stream, 4)) + self._version = int.from_bytes(_read_exact(self.stream, 4), "little") return self._version @property @@ -339,6 +337,10 @@ def view(cls, stream, offset=None, compress=CompressMode.KEEP_ALL): # Collect all non-global-tx key-value pairs for PSBTv2 global scope rewriting. # The global scope is small, so materialising it avoids byte-level injection. global_kvs = {} + # key -> (value_offset, value_len) for globals whose value is only needed + # if this turns out to be a PSBTv2. PSBTView is RAM-constrained, so we + # skip them on the first pass and read them back below only when used. + deferred_kvs = {} while True: # read key and update cursor key = read_string(stream) @@ -365,13 +367,20 @@ def view(cls, stream, offset=None, compress=CompressMode.KEEP_ALL): stream.seek(tx_offset + tx_len) cur += tx_len else: - value, value_size = _read_global_value(stream, key) - cur += value_size - if key in global_kvs: + if key in global_kvs or key in deferred_kvs: raise PSBTError("Duplicate global key: %s" % hexlify(key).decode()) - global_kvs[key] = value - if key == b"\xfb": - version = int.from_bytes(value, "little") + if key in _GLOBAL_FIXED_VALUE_LENGTHS or key in _GLOBAL_COUNT_FIELDS: + value, value_size = _read_global_value(stream, key) + cur += value_size + global_kvs[key] = value + if key == b"\xfb": + version = int.from_bytes(value, "little") + else: + value_len = compact.read_from(stream) + header = len(compact.to_bytes(value_len)) + deferred_kvs[key] = (cur + header, value_len) + stream.seek(value_len, 1) + cur += header + value_len first_scope = cur if version not in (None, 0, 2): raise PSBTError("Unsupported PSBT_GLOBAL_VERSION value: %d" % version) @@ -388,6 +397,15 @@ def view(cls, stream, offset=None, compress=CompressMode.KEEP_ALL): num_outputs = compact.from_bytes(global_kvs[b"\x05"]) if None in [version or tx_offset, num_inputs, num_outputs]: raise PSBTError("Missing something important in PSBT") + if version == 2: + # PSBTv2 rebuilds its global scope from _global_kvs on write, so now + # (and only now) we need the values we skipped over above. + for k, (v_off, v_len) in deferred_kvs.items(): + stream.seek(v_off) + v = stream.read(v_len) + if len(v) != v_len: + raise PSBTError("Failed to read %d bytes" % v_len) + global_kvs[k] = v return cls( stream, num_inputs, @@ -414,6 +432,20 @@ def _skip_scope(self): off += skip_string(self.stream) return off + def _scan_scope_values(self, keys): + """Reads the scope at the current cursor and returns {key: value} for the + requested (exact-match) keys. Leaves the cursor at the next scope.""" + found = {} + while True: + key = read_string(self.stream) + # separator - end of scope + if len(key) == 0: + return found + if key in keys and key not in found: + found[key] = read_string(self.stream) + else: + skip_string(self.stream) + def seek_to_scope(self, n): """ Moves the stream cursor to n'th scope. @@ -474,6 +506,8 @@ def vin(self, i, compress=None): raise PSBTError( "PSBTv2 input %d missing required PSBT_IN_PREVIOUS_TXID (0x0e)" % i ) + if len(v) != 32: + raise PSBTError("PSBT_IN_PREVIOUS_TXID must be 32 bytes") txid = bytes(reversed(v)) self.seek_to_scope(i) @@ -482,10 +516,16 @@ def vin(self, i, compress=None): raise PSBTError( "PSBTv2 input %d missing required PSBT_IN_OUTPUT_INDEX (0x0f)" % i ) + if len(v) != 4: + raise PSBTError("PSBT_IN_OUTPUT_INDEX must be 4 bytes") vout = int.from_bytes(v, "little") self.seek_to_scope(i) - v = self.get_value(b"\x10", from_current=True) or b"\xff\xff\xff\xff" + v = self.get_value(b"\x10", from_current=True) + if v is None: + v = b"\xff\xff\xff\xff" + elif len(v) != 4: + raise PSBTError("PSBT_IN_SEQUENCE must be 4 bytes") sequence = int.from_bytes(v, "little") return TransactionInput(txid, vout, sequence=sequence) @@ -503,7 +543,12 @@ def vout(self, i, compress=None): raise PSBTError( "PSBTv2 output %d missing required PSBT_OUT_AMOUNT (0x03)" % i ) - value = _signed_from_bytes(v) + if len(v) != 8: + raise PSBTError("PSBT_OUT_AMOUNT must be 8 bytes") + # BIP370: PSBT_OUT_AMOUNT is a signed int64, so the top half is negative + value = int.from_bytes(v, "little") + if value >= 2**63: + raise PSBTError("PSBT_OUT_AMOUNT must be non-negative") self.seek_to_scope(self.num_inputs + i) v = self.get_value(b"\x04", from_current=True) @@ -544,11 +589,13 @@ def _determine_locktime_v2(self): time_locktimes = [] inputs_with_requirements = 0 - for i in range(self.num_inputs): - self.seek_to_scope(i) - v_height = self.get_value(b"\x12", from_current=True) - self.seek_to_scope(i) - v_time = self.get_value(b"\x11", from_current=True) + # One sequential pass: _scan_scope_values() stops on the scope separator, + # which is where the next scope begins, so no O(n^2) re-seeking per input. + self.seek_to_scope(0) + for _ in range(self.num_inputs): + found = self._scan_scope_values((b"\x11", b"\x12")) + v_height = found.get(b"\x12") + v_time = found.get(b"\x11") has_requirement = False if v_height is not None: @@ -575,21 +622,8 @@ def _determine_locktime_v2(self): if has_requirement: inputs_with_requirements += 1 - if inputs_with_requirements == 0: - return fallback - - inputs_supporting_height = len(height_locktimes) - inputs_supporting_time = len(time_locktimes) - - # Prefer height-based locktime if every input with requirements supports it - if inputs_supporting_height == inputs_with_requirements: - return max(height_locktimes) - # Fall back to time-based locktime if every input with requirements supports it - if inputs_supporting_time == inputs_with_requirements: - return max(time_locktimes) - - raise PSBTError( - "Cannot determine locktime: inputs have conflicting height and time locktime requirements" + return choose_locktime( + height_locktimes, time_locktimes, inputs_with_requirements, fallback ) @property @@ -603,8 +637,7 @@ def tx_version(self): else: if len(v) != 4: raise PSBTError("Invalid PSBT_GLOBAL_TX_VERSION length") - # BIP370: PSBT_GLOBAL_TX_VERSION is a signed int32 - self._tx_version = _signed_from_bytes(v) + self._tx_version = int.from_bytes(v, "little") return self._tx_version def seek_to_value(self, key_start, from_current=False): @@ -694,7 +727,7 @@ def sighash_taproot( sh, anyonecanpay = SIGHASH.check(sighash) h = hashes.tagged_hash_init("TapSighash", b"\x00") h.update(bytes([sighash])) - h.update(_signed_to_bytes(self.tx_version, 4)) + h.update(self.tx_version.to_bytes(4, "little")) h.update(self.locktime.to_bytes(4, "little")) if not anyonecanpay: h.update(self.hash_prevouts()) @@ -742,7 +775,7 @@ def sighash_segwit(self, input_index, script_pubkey, value, sighash=SIGHASH.ALL) inp = self.vin(input_index) zero = b"\x00" * 32 # for sighashes h = hashlib.sha256() - h.update(_signed_to_bytes(self.tx_version, 4)) + h.update(self.tx_version.to_bytes(4, "little")) if anyonecanpay: h.update(zero) else: @@ -782,7 +815,7 @@ def sighash_legacy(self, input_index, script_pubkey, sighash=SIGHASH.ALL): return b"\x00" * 31 + b"\x01" h = hashlib.sha256() - h.update(_signed_to_bytes(self.tx_version, 4)) + h.update(self.tx_version.to_bytes(4, "little")) # ANYONECANPAY - only one input is serialized if anyonecanpay: h.update(compact.to_bytes(1)) diff --git a/src/embit/transaction.py b/src/embit/transaction.py index 56aad0d7..2301021f 100644 --- a/src/embit/transaction.py +++ b/src/embit/transaction.py @@ -40,23 +40,6 @@ def check(cls, sighash: int): # util functions -def _signed_from_bytes(b): - res = int.from_bytes(b, "little") - bits = 8 * len(b) - if bits and res >= (1 << (bits - 1)): - res -= 1 << bits - return res - - -def _signed_to_bytes(i, length): - bits = 8 * length - if i < -(1 << (bits - 1)) or i >= (1 << (bits - 1)): - raise OverflowError("int too big to convert") - if i < 0: - i += 1 << bits - return i.to_bytes(length, "little") - - def hash_amounts(amounts): h = hashlib.sha256() for a in amounts: @@ -100,7 +83,7 @@ def is_segwit(self): def write_to(self, stream): """Returns the byte serialization of the transaction""" - res = stream.write(_signed_to_bytes(self.version, 4)) + res = stream.write(self.version.to_bytes(4, "little")) if self.is_segwit: res += stream.write(b"\x00\x01") # segwit marker and flag res += stream.write(compact.to_bytes(len(self.vin))) @@ -117,7 +100,7 @@ def write_to(self, stream): def hash(self): h = hashlib.sha256() - h.update(_signed_to_bytes(self.version, 4)) + h.update(self.version.to_bytes(4, "little")) h.update(compact.to_bytes(len(self.vin))) for inp in self.vin: h.update(inp.serialize()) @@ -168,7 +151,7 @@ def read_vout(cls, stream, idx): @classmethod def read_from(cls, stream): - ver = _signed_from_bytes(_read_exact(stream, 4)) + ver = int.from_bytes(_read_exact(stream, 4), "little") num_vin = compact.read_from(stream) # if num_vin is zero it is a segwit transaction is_segwit = num_vin == 0 @@ -245,7 +228,7 @@ def sighash_taproot( sh, anyonecanpay = SIGHASH.check(sighash) h = hashes.tagged_hash_init("TapSighash", b"\x00") h.update(bytes([sighash])) - h.update(_signed_to_bytes(self.version, 4)) + h.update(self.version.to_bytes(4, "little")) h.update(self.locktime.to_bytes(4, "little")) if not anyonecanpay: h.update(self.hash_prevouts()) @@ -292,7 +275,7 @@ def sighash_segwit(self, input_index, script_pubkey, value, sighash=SIGHASH.ALL) inp = self.vin[input_index] zero = b"\x00" * 32 # for sighashes h = hashlib.sha256() - h.update(_signed_to_bytes(self.version, 4)) + h.update(self.version.to_bytes(4, "little")) if anyonecanpay: h.update(zero) else: @@ -331,7 +314,7 @@ def sighash_legacy(self, input_index, script_pubkey, sighash=SIGHASH.ALL): return b"\x00" * 31 + b"\x01" h = hashlib.sha256() - h.update(_signed_to_bytes(self.version, 4)) + h.update(self.version.to_bytes(4, "little")) # ANYONECANPAY - only one input is serialized if anyonecanpay: h.update(compact.to_bytes(1)) diff --git a/tests/tests/test_psbtV2.py b/tests/tests/test_psbtV2.py index 2830728e..2b636379 100644 --- a/tests/tests/test_psbtV2.py +++ b/tests/tests/test_psbtV2.py @@ -614,8 +614,8 @@ def test_psbtview_rejects_v2_only_global_in_v0(self): class TestPSBTv2TransactionVersion: - def test_psbt_tx_preserves_zero_and_negative_versions(self): - """PSBT_GLOBAL_TX_VERSION is signed and zero must not fall back to 2""" + def test_psbt_tx_preserves_zero_and_high_bit_versions(self): + """PSBT_GLOBAL_TX_VERSION is copied through verbatim, zero must not fall back to 2""" psbt = PSBT.create_v2(tx_version=0) inp = InputScope() inp.txid = bytes(32) @@ -627,7 +627,7 @@ def test_psbt_tx_preserves_zero_and_negative_versions(self): psbt.add_output(out) assert psbt.tx.serialize()[:4] == b"\x00\x00\x00\x00" - psbt.tx_version = -1 + psbt.tx_version = 0xFFFFFFFF assert psbt.tx.serialize()[:4] == b"\xff\xff\xff\xff" diff --git a/tests/tests/test_psbt_versions.py b/tests/tests/test_psbt_versions.py index 50ad2c30..b942349f 100644 --- a/tests/tests/test_psbt_versions.py +++ b/tests/tests/test_psbt_versions.py @@ -9,12 +9,7 @@ def key_value(key, value): - return ( - compact.to_bytes(len(key)) - + key - + compact.to_bytes(len(value)) - + value - ) + return compact.to_bytes(len(key)) + key + compact.to_bytes(len(value)) + value def unsigned_tx(value=1000): @@ -253,9 +248,9 @@ def test_unknown_global_field_with_key_data_remains_compatible(self): def test_v0_rejects_v2_output_scope(self): raw = psbt_v0() - output_data = key_value( - b"\x03", (1000).to_bytes(8, "little") - ) + key_value(b"\x04", b"\x52") + output_data = key_value(b"\x03", (1000).to_bytes(8, "little")) + key_value( + b"\x04", b"\x52" + ) invalid = raw[:-1] + output_data + raw[-1:] with self.assertRaises(PSBTError): @@ -270,9 +265,9 @@ def test_v0_rejects_v2_prevout_metadata(self): vin=[TransactionInput(bytes([1]) * 32, 0)], vout=[TransactionOutput(2000, Script(b"\x51"))], ) - input_data = key_value( - b"\x00", previous_tx.serialize() - ) + key_value(b"\x0e", bytes(reversed(previous_tx.txid()))) + input_data = key_value(b"\x00", previous_tx.serialize()) + key_value( + b"\x0e", bytes(reversed(previous_tx.txid())) + ) invalid = raw[:-2] + input_data + raw[-2:] with self.assertRaises(PSBTError): @@ -302,6 +297,22 @@ def test_v2_scope_fields_remain_compatible(self): self.assertEqual(out.value, 1000) self.assertEqual(out.script_pubkey, Script(b"\x51")) + def test_v2_out_amount_rejects_negative_int64(self): + """BIP370 PSBT_OUT_AMOUNT is a signed int64 - the top half of the range is negative""" + + def parse(amount): + data = ( + key_value(b"\x03", amount.to_bytes(8, "little")) + + key_value(b"\x04", b"\x51") + + b"\x00" + ) + return OutputScope.read_from(BytesIO(data), version=2) + + self.assertEqual(parse(2**63 - 1).value, 2**63 - 1) + for amount in (2**63, 2**64 - 1): + with self.assertRaises(PSBTError): + parse(amount) + def test_v2_fields_reject_key_data(self): fields = [ (InputScope, b"\x0e\x00", bytes(32)), From d9828a2a884b5078ff6482c0c1092cf91eeab8d8 Mon Sep 17 00:00:00 2001 From: notTanveer Date: Wed, 2 Sep 2026 10:33:56 -0300 Subject: [PATCH 05/44] ref(psbt): extract taproot key-path signature storage into _sign_taproot_keypath Subclasses that tweak the key differently (BIP-376 silent payment spends) encode and store the BIP-341 key-path signature identically, so share that step instead of duplicating it. (PSBT-only half of 533cd85 on feat/silent-payments-V2.) --- src/embit/psbt.py | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/embit/psbt.py b/src/embit/psbt.py index 317e8291..2d0e1b05 100644 --- a/src/embit/psbt.py +++ b/src/embit/psbt.py @@ -1384,6 +1384,21 @@ def sighash(self, i, sighash=SIGHASH.ALL, **kwargs): h = self.sighash_legacy(i, sc, sighash=sighash) return h + def _sign_taproot_keypath(self, pk, input_index: int, inp, sighash) -> int: + """Store a BIP-341 key-path signature made with the already-tweaked pk. + + Shared with subclasses that tweak the key differently (BIP-376 SP + spends) but encode and store the signature identically. + """ + sig = pk.schnorr_sign(self.sighash(input_index, sighash=sighash)) + sigdata = sig.serialize() + # append sighash if necessary + if sighash != SIGHASH.DEFAULT: + sigdata += bytes([sighash]) + inp.taproot_key_sig = sigdata + inp.final_scriptwitness = Witness([sigdata]) + return 1 + def sign_input_with_tapkey( self, key: ec.PrivateKey, @@ -1399,18 +1414,8 @@ def sign_input_with_tapkey( # check if key is internal key pk = key.taproot_tweak(inp.taproot_merkle_root or b"") if pk.xonly() in inp.utxo.script_pubkey.data: - h = self.sighash( - input_index, - sighash=sighash, - ) - sig = pk.schnorr_sign(h) - sigdata = sig.serialize() - if sighash != SIGHASH.DEFAULT: - sigdata += bytes([sighash]) - inp.taproot_key_sig = sigdata - inp.final_scriptwitness = Witness([sigdata]) # no need to sign anything else - return 1 + return self._sign_taproot_keypath(pk, input_index, inp, sighash) counter = 0 # negate if necessary pub = ec.PublicKey.from_xonly(key.xonly()) From 8a2f626e5b3df0de3b5c46525efd86b21b089a91 Mon Sep 17 00:00:00 2001 From: odudex Date: Wed, 2 Sep 2026 10:35:51 -0300 Subject: [PATCH 06/44] changelog: add PSBTv2 support entry --- CHANGELOG.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b11788d..c67e8042 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,20 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] + +### Added + +- Add PSBT version 2 (BIP 370) support to `PSBT` and `PSBTView`: parsing, + validation and serialization of the v2 global, input and output fields, + unsigned transaction reconstruction, and `PSBT_GLOBAL_TX_MODIFIABLE` + updates on signing. + +### Fixed + +- Validate PSBTv2 count fields canonically and raise `PSBTError` instead of + `RuntimeError`/`ValueError` on malformed values in both parse paths. + ## [0.8.2] - 2026-08-08 ### Changed From 8af21133248564569a2359a567e2f06ac7469289 Mon Sep 17 00:00:00 2001 From: odudex Date: Wed, 2 Sep 2026 10:59:07 -0300 Subject: [PATCH 07/44] fix(psbtview): match PSBTv2 scope fields exactly and validate scopes like PSBT PSBTView.vin()/vout() looked up PSBT_IN_PREVIOUS_TXID, OUTPUT_INDEX, SEQUENCE, PSBT_OUT_AMOUNT and SCRIPT with seek_to_value(), which matches by key prefix and returns the first hit. A keyless BIP-370 field carrying keydata (0x0e||xx) or a duplicate of the field was therefore accepted, and its value fed hashPrevouts/hashSequence/hashOutputs. PSBT.parse rejects both. On a two-input PSBT with the bogus key in input 1, PSBTView.sign_with() computed input 0's sighash over the wrong prevout and wrote its signature to the sig stream before input 1 was ever parsed. vin()/vout() now go through _scan_scope_values(), which matches keys exactly, rejects duplicates of the requested keys and runs the scope class's _validate_key() on every key it skips over, so a malformed map is rejected while hashing, before any signature is emitted. The same walk serves _determine_locktime_v2(). input()/output() now run the PSBT_CLS._validate_v2_input/_output hooks (PSET overrides them), so a v2 map missing a required field fails in input(i) instead of only in vin(i). tx_version and the fallback locktime are read from the materialised _global_kvs instead of re-walking the global map with a prefix match. --- src/embit/liquid/psetview.py | 1 + src/embit/psbt.py | 5 +- src/embit/psbtview.py | 111 ++++++++++++++++++++--------------- tests/tests/test_psbtV2.py | 100 +++++++++++++++++++++++++++++++ 4 files changed, 167 insertions(+), 50 deletions(-) diff --git a/src/embit/liquid/psetview.py b/src/embit/liquid/psetview.py index 7d692304..606a11dc 100644 --- a/src/embit/liquid/psetview.py +++ b/src/embit/liquid/psetview.py @@ -123,6 +123,7 @@ class PSETView(PSBTView): PSBTIN_CLS = LInputScope PSBTOUT_CLS = LOutputScope TX_CLS = GlobalLTransactionView + PSBT_CLS = PSET def clear_cache(self): # cache for digests diff --git a/src/embit/psbt.py b/src/embit/psbt.py index 2d0e1b05..a1b229f6 100644 --- a/src/embit/psbt.py +++ b/src/embit/psbt.py @@ -220,8 +220,9 @@ def parse_unknowns(self): s.seek(0) self.read_value(s, k) - def _validate_key(self, key): - if len(key) != 1 and key[:1] in self.V2_FIELDS: + @classmethod + def _validate_key(cls, key): + if len(key) != 1 and key[:1] in cls.V2_FIELDS: raise PSBTError("Invalid PSBTv2 field key") def read_value(self, stream, key, version=None): diff --git a/src/embit/psbtview.py b/src/embit/psbtview.py index d650264c..a7f55945 100644 --- a/src/embit/psbtview.py +++ b/src/embit/psbtview.py @@ -24,6 +24,7 @@ from .script import Script, Witness from . import hashes from .psbt import ( + PSBT, PSBTError, CompressMode, InputScope, @@ -251,6 +252,11 @@ class PSBTView: PSBTIN_CLS = InputScope PSBTOUT_CLS = OutputScope TX_CLS = GlobalTransactionView + # provides the per-scope PSBTv2 validation hooks shared with PSBT + PSBT_CLS = PSBT + # PSBTv2 fields needed to rebuild a TransactionInput / TransactionOutput + _V2_VIN_KEYS = (b"\x0e", b"\x0f", b"\x10") + _V2_VOUT_KEYS = (b"\x03", b"\x04") def __init__( self, @@ -432,16 +438,26 @@ def _skip_scope(self): off += skip_string(self.stream) return off - def _scan_scope_values(self, keys): + def _scan_scope_values(self, keys, scope_cls=None): """Reads the scope at the current cursor and returns {key: value} for the - requested (exact-match) keys. Leaves the cursor at the next scope.""" + requested (exact-match) keys. Leaves the cursor at the next scope. + + Keys are matched exactly, so a keyless BIP-370 field carrying keydata + (e.g. 0x0e||xx) never stands in for the real one, a duplicate of a + requested key is rejected, and when scope_cls is given every key in the + scope is checked with its _validate_key() - the same checks the full + scope parser applies.""" found = {} while True: key = read_string(self.stream) # separator - end of scope if len(key) == 0: return found - if key in keys and key not in found: + if scope_cls is not None: + scope_cls._validate_key(key) + if key in keys: + if key in found: + raise PSBTError("Duplicated key: %s" % hexlify(key).decode()) found[key] = read_string(self.stream) else: skip_string(self.stream) @@ -477,9 +493,12 @@ def input(self, i, compress=None): raise PSBTError("Invalid input index") vin = self.tx.vin(i) if self.tx else None self.seek_to_scope(i) - return self.PSBTIN_CLS.read_from( + inp = self.PSBTIN_CLS.read_from( self.stream, vin=vin, compress=compress, version=self.version ) + if self.version == 2: + self.PSBT_CLS._validate_v2_input(inp, i) + return inp def output(self, i, compress=None): """Reads, parses and returns PSBT OutputScope #i""" @@ -489,19 +508,16 @@ def output(self, i, compress=None): raise PSBTError("Invalid output index") vout = self.tx.vout(i) if self.tx else None self.seek_to_scope(self.num_inputs + i) - return self.PSBTOUT_CLS.read_from( + out = self.PSBTOUT_CLS.read_from( self.stream, vout=vout, compress=compress, version=self.version ) + if self.version == 2: + self.PSBT_CLS._validate_v2_output(out, i) + return out - # compress is not used here, but may be used by subclasses (liquid) - def vin(self, i, compress=None): - if i < 0 or i >= self.num_inputs: - raise PSBTError("Invalid input index") - if self.tx: - return self.tx.vin(i) - - self.seek_to_scope(i) - v = self.get_value(b"\x0e", from_current=True) + def _v2_vin(self, found, i): + """Builds a TransactionInput from the PSBTv2 fields of input scope i.""" + v = found.get(b"\x0e") if v is None: raise PSBTError( "PSBTv2 input %d missing required PSBT_IN_PREVIOUS_TXID (0x0e)" % i @@ -510,8 +526,7 @@ def vin(self, i, compress=None): raise PSBTError("PSBT_IN_PREVIOUS_TXID must be 32 bytes") txid = bytes(reversed(v)) - self.seek_to_scope(i) - v = self.get_value(b"\x0f", from_current=True) + v = found.get(b"\x0f") if v is None: raise PSBTError( "PSBTv2 input %d missing required PSBT_IN_OUTPUT_INDEX (0x0f)" % i @@ -520,8 +535,7 @@ def vin(self, i, compress=None): raise PSBTError("PSBT_IN_OUTPUT_INDEX must be 4 bytes") vout = int.from_bytes(v, "little") - self.seek_to_scope(i) - v = self.get_value(b"\x10", from_current=True) + v = found.get(b"\x10") if v is None: v = b"\xff\xff\xff\xff" elif len(v) != 4: @@ -530,15 +544,9 @@ def vin(self, i, compress=None): return TransactionInput(txid, vout, sequence=sequence) - # compress is not used here, but may be used by subclasses (liquid) - def vout(self, i, compress=None): - if i < 0 or i >= self.num_outputs: - raise PSBTError("Invalid output index") - if self.tx: - return self.tx.vout(i) - - self.seek_to_scope(self.num_inputs + i) - v = self.get_value(b"\x03", from_current=True) + def _v2_vout(self, found, i): + """Builds a TransactionOutput from the PSBTv2 fields of output scope i.""" + v = found.get(b"\x03") if v is None: raise PSBTError( "PSBTv2 output %d missing required PSBT_OUT_AMOUNT (0x03)" % i @@ -550,15 +558,32 @@ def vout(self, i, compress=None): if value >= 2**63: raise PSBTError("PSBT_OUT_AMOUNT must be non-negative") - self.seek_to_scope(self.num_inputs + i) - v = self.get_value(b"\x04", from_current=True) + v = found.get(b"\x04") if v is None: raise PSBTError( "PSBTv2 output %d missing required PSBT_OUT_SCRIPT (0x04)" % i ) - script_pubkey = Script(v) + return TransactionOutput(value, Script(v)) + + # compress is not used here, but may be used by subclasses (liquid) + def vin(self, i, compress=None): + if i < 0 or i >= self.num_inputs: + raise PSBTError("Invalid input index") + if self.tx: + return self.tx.vin(i) + self.seek_to_scope(i) + found = self._scan_scope_values(self._V2_VIN_KEYS, self.PSBTIN_CLS) + return self._v2_vin(found, i) - return TransactionOutput(value, script_pubkey) + # compress is not used here, but may be used by subclasses (liquid) + def vout(self, i, compress=None): + if i < 0 or i >= self.num_outputs: + raise PSBTError("Invalid output index") + if self.tx: + return self.tx.vout(i) + self.seek_to_scope(self.num_inputs + i) + found = self._scan_scope_values(self._V2_VOUT_KEYS, self.PSBTOUT_CLS) + return self._v2_vout(found, i) @property def locktime(self): @@ -577,13 +602,9 @@ def _determine_locktime_v2(self): Falls back to PSBT_GLOBAL_FALLBACK_LOCKTIME (or 0) when no input imposes a requirement. """ - v = self.get_value(b"\x03") - if v is not None: - if len(v) != 4: - raise PSBTError("PSBT_GLOBAL_FALLBACK_LOCKTIME must be 4 bytes") - fallback = int.from_bytes(v, "little") - else: - fallback = 0 + # lengths of the v2 globals were already checked by _validate_global_fields + v = self._global_kvs.get(b"\x03") + fallback = int.from_bytes(v, "little") if v is not None else 0 height_locktimes = [] time_locktimes = [] @@ -593,7 +614,7 @@ def _determine_locktime_v2(self): # which is where the next scope begins, so no O(n^2) re-seeking per input. self.seek_to_scope(0) for _ in range(self.num_inputs): - found = self._scan_scope_values((b"\x11", b"\x12")) + found = self._scan_scope_values((b"\x11", b"\x12"), self.PSBTIN_CLS) v_height = found.get(b"\x12") v_time = found.get(b"\x11") @@ -629,15 +650,9 @@ def _determine_locktime_v2(self): @property def tx_version(self): if self._tx_version is None: - v = self.get_value(b"\x02") - if v is None: - if self.version == 2: - raise PSBTError("Missing PSBT_GLOBAL_TX_VERSION in PSBTv2") - self._tx_version = 0 - else: - if len(v) != 4: - raise PSBTError("Invalid PSBT_GLOBAL_TX_VERSION length") - self._tx_version = int.from_bytes(v, "little") + # only reached for PSBTv2 - v0 takes it from the global tx. + # _validate_global_fields already checked presence and length. + self._tx_version = int.from_bytes(self._global_kvs[b"\x02"], "little") return self._tx_version def seek_to_value(self, key_start, from_current=False): diff --git a/tests/tests/test_psbtV2.py b/tests/tests/test_psbtV2.py index 2b636379..58a1cc2d 100644 --- a/tests/tests/test_psbtV2.py +++ b/tests/tests/test_psbtV2.py @@ -16,6 +16,11 @@ ) from embit.psbtview import PSBTView from embit.script import Script +from embit import compact +from .test_psbtview import PSBTS as VIEW_PSBTS, ROOT as SIGNING_ROOT + +# native segwit single key, 3 inputs 4 outputs, PSBTv2, signable with SIGNING_ROOT +SIGNABLE_V2_B64 = VIEW_PSBTS[1] from embit.transaction import SIGHASH, Transaction, TransactionInput, TransactionOutput @@ -654,3 +659,98 @@ def test_psbtv2_compress_non_witness_utxo_is_key_order_independent(self): assert parsed.inputs[0].non_witness_utxo is None assert parsed.inputs[0]._utxo.value == 1234 assert parsed.verify() + + +def kv(key, value): + return compact.to_bytes(len(key)) + key + compact.to_bytes(len(value)) + value + + +TXID_A = bytes(range(32)) +TXID_B = bytes(range(0x20, 0x40)) +V2_IN = kv(b"\x0e", TXID_A) + kv(b"\x0f", (0).to_bytes(4, "little")) +V2_OUT = kv(b"\x03", (1000).to_bytes(8, "little")) + kv(b"\x04", b"\x51") + + +def raw_v2(inputs, outputs, extra_globals=b""): + """A raw PSBTv2 from already serialized input and output maps (no separators).""" + g = ( + kv(b"\x02", (2).to_bytes(4, "little")) + + kv(b"\x04", compact.to_bytes(len(inputs))) + + kv(b"\x05", compact.to_bytes(len(outputs))) + + extra_globals + + kv(b"\xfb", (2).to_bytes(4, "little")) + ) + return ( + PSBT.MAGIC + + g + + b"\x00" + + b"".join(m + b"\x00" for m in inputs) + + b"".join(m + b"\x00" for m in outputs) + ) + + +class TestPSBTViewV2ScopeFields: + """PSBTView must reject the same malformed v2 scope fields as PSBT""" + + def test_baseline_valid(self): + raw = raw_v2([V2_IN], [V2_OUT]) + psbt = PSBT.parse(raw) + view = PSBTView.view(BytesIO(raw)) + assert view.vin(0).serialize() == psbt.inputs[0].vin.serialize() + assert view.vout(0).serialize() == psbt.outputs[0].vout.serialize() + + @pytest.mark.parametrize( + "bogus_in, bogus_out", + [ + (kv(b"\x0e\xaa", TXID_B), b""), + (kv(b"\x0f\xaa", (7).to_bytes(4, "little")), b""), + (kv(b"\x10\xaa", (7).to_bytes(4, "little")), b""), + (b"", kv(b"\x03\xaa", (999999).to_bytes(8, "little"))), + (b"", kv(b"\x04\xaa", b"\x52")), + ], + ) + def test_keyed_variants_of_keyless_fields_rejected(self, bogus_in, bogus_out): + # the bogus key precedes the real one so a prefix match would pick it up + raw = raw_v2([bogus_in + V2_IN], [bogus_out + V2_OUT]) + with pytest.raises(PSBTError): + PSBT.parse(raw) + view = PSBTView.view(BytesIO(raw)) + with pytest.raises(PSBTError): + view.vin(0) + view.vout(0) + with pytest.raises(PSBTError): + view.input(0) + view.output(0) + + def test_duplicate_prevout_fields_rejected(self): + raw = raw_v2([kv(b"\x0e", TXID_B) + V2_IN], [V2_OUT]) + with pytest.raises(PSBTError): + PSBT.parse(raw) + with pytest.raises(PSBTError): + PSBTView.view(BytesIO(raw)).vin(0) + raw = raw_v2([V2_IN], [kv(b"\x03", bytes(8)) + V2_OUT]) + with pytest.raises(PSBTError): + PSBTView.view(BytesIO(raw)).vout(0) + + def test_input_and_output_validate_required_fields(self): + raw = raw_v2([kv(b"\x0f", bytes(4))], [V2_OUT]) + with pytest.raises(PSBTError): + PSBTView.view(BytesIO(raw)).input(0) + raw = raw_v2([V2_IN], [kv(b"\x04", b"\x51")]) + with pytest.raises(PSBTError): + PSBTView.view(BytesIO(raw)).output(0) + + def test_sign_does_not_emit_sigs_before_rejecting_a_later_input(self): + """A malformed input 1 must be caught while hashing input 0, not after.""" + psbt = PSBT.from_string(SIGNABLE_V2_B64) + raw_inputs = [inp.serialize(version=2)[:-1] for inp in psbt.inputs] + raw_outputs = [out.serialize(version=2)[:-1] for out in psbt.outputs] + raw_inputs[1] = kv(b"\x0e\xaa", TXID_B) + raw_inputs[1] + raw = raw_v2(raw_inputs, raw_outputs) + with pytest.raises(PSBTError): + PSBT.parse(raw) + view = PSBTView.view(BytesIO(raw)) + sigs = BytesIO() + with pytest.raises(PSBTError): + view.sign_with(SIGNING_ROOT, sigs) + assert sigs.getvalue() == b"" From 7cc5ce2f653a13e8dd33a21af0068a5cb13d9fb3 Mon Sep 17 00:00:00 2001 From: odudex Date: Wed, 2 Sep 2026 11:00:21 -0300 Subject: [PATCH 08/44] fix(pset): sign PSETv2 with the BIP-370 locktime and keep a zero sequence PSET.blinded_tx, which PSET.sighash_segwit/sighash_legacy hash against, still built the transaction with the raw fallback locktime while PSBT.tx went through determine_locktime(). A PSETv2 with a required height or time locktime therefore signed one transaction and extracted another, and PSET.sign_with() disagreed with PSETView.sign_with(), which already used the derived value. LInputScope.vin/blinded_vin also rewrote sequence == 0 to 0xffffffff via `self.sequence or 0xFFFFFFFF`, the bug the base InputScope.vin fixed on this branch, so the same zero-sequence input hashed and serialized differently depending on which scope class held it. --- src/embit/liquid/pset.py | 17 ++++++++------- tests/tests/test_liquid.py | 43 +++++++++++++++++++++++++++++++------- 2 files changed, 46 insertions(+), 14 deletions(-) diff --git a/src/embit/liquid/pset.py b/src/embit/liquid/pset.py index 1f0d62ae..cd17f209 100644 --- a/src/embit/liquid/pset.py +++ b/src/embit/liquid/pset.py @@ -124,7 +124,7 @@ def vin(self): return LTransactionInput( self.txid, self.vout, - sequence=(self.sequence or 0xFFFFFFFF), + sequence=(self.sequence if self.sequence is not None else 0xFFFFFFFF), asset_issuance=self.asset_issuance, ) @@ -133,7 +133,7 @@ def blinded_vin(self): return LTransactionInput( self.txid, self.vout, - sequence=(self.sequence or 0xFFFFFFFF), + sequence=(self.sequence if self.sequence is not None else 0xFFFFFFFF), asset_issuance=self.asset_issuance, witness=TxInWitness(self.issue_rangeproof, self.token_rangeproof), ) @@ -349,10 +349,12 @@ def blinded_vout(self): self.value_commitment or self.value, self.script_pubkey, self.ecdh_pubkey, - None - if not self.surjection_proof - else TxOutWitness( - Proof(self.surjection_proof), RangeProof(self.range_proof) + ( + None + if not self.surjection_proof + else TxOutWitness( + Proof(self.surjection_proof), RangeProof(self.range_proof) + ) ), ) @@ -641,7 +643,8 @@ def fee(self): def blinded_tx(self): return self.TX_CLS( version=2 if self.tx_version is None else self.tx_version, - locktime=self.locktime or 0, + # BIP-370: PSBTv2 derives the locktime from the per-input requirements + locktime=self.determine_locktime(), vin=[inp.blinded_vin for inp in self.inputs], vout=[out.blinded_vout for out in self.outputs], ) diff --git a/tests/tests/test_liquid.py b/tests/tests/test_liquid.py index cc8a290b..c4b9c963 100644 --- a/tests/tests/test_liquid.py +++ b/tests/tests/test_liquid.py @@ -2,7 +2,9 @@ from unittest import TestCase, skipUnless from embit.util import secp256k1 from binascii import unhexlify -from embit.liquid.pset import PSET +from embit.liquid.pset import PSET, LInputScope +from embit.liquid.psetview import PSETView +from io import BytesIO from embit.liquid.transaction import LTransaction from embit.liquid import slip77 from embit.script import Script @@ -28,6 +30,11 @@ ) +SIGN_TPRV = "tprv8ZgxMBicQKsPf27gmh4DbQqN2K6xnXA7m7AeceqQVGkRYny3X49sgcufzbJcq4k5eaGZDMijccdDzvQga2Saqd78dKqN52QwLyqgY8apX3j" +# 1 input, 3 outputs PSETv2 signable with SIGN_TPRV +SIGN_PSET_V2_B64 = "cHNldP8BAgQCAAAAAQMEAAAAAAEEAQEBBQEDAfsEAgAAAAABAOQCAAAAAAFK0vGV0PugDPRBFiCV3/0N1nGBWE0U4wgegC2qmHSnRwAAAAAA/f///wMBGGls8j4yCaboRHnQHxP+eckBNWrlOQfMiCz/0+JkASMBAAAAAACYloAAFgAUisfI8pzbq6cwbzmkzqfy3Ks4y2QBGGls8j4yCaboRHnQHxP+eckBNWrlOQfMiCz/0+JkASMBAAd18FluqWoAFgAUA2V92wLjWqu3hH/ptYAJmo6/MUQBGGls8j4yCaboRHnQHxP+eckBNWrlOQfMiCz/0+JkASMBAAAAAAAAABYAAAoAAAABAUIBGGls8j4yCaboRHnQHxP+eckBNWrlOQfMiCz/0+JkASMBAAAAAACYloAAFgAUisfI8pzbq6cwbzmkzqfy3Ks4y2QiBgOhnuyHYV5cZ5bX/jWEgTl4SyWP3oVJy8Pp95KqiRmsChjRKMetVAAAgAEAAIAAAACAAAAAAAIAAAABDiB7ALW7kD6wSBz+JHoahpFXiVehkjlkCgG9IUZQP80H4wEPBAAAAAABEAT9////ACICA4u6NSSv6aHUr4gObKCSK+Ej17xHjEeyVcoEcIN11l0tGNEox61UAACAAQAAgAAAAIABAAAAAwAAAAEEFgAUlI/NhKVhaPasthkSL1rf+Mu50z0BAwgniZUAAAAAAAf8BHBzZXQCIBhpbPI+Mgmm6ER50B8T/nnJATVq5TkHzIgs/9PiZAEjB/wEcHNldAgEAAAAAAABBBYAFASJ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u6ANh4jDSLE9huhpIMMUv9nMpOGXxgdGOJaAM3jrCkrmutyNeJ/7i6d7qomlY1rt7nZTZ9AsLQqn19sOqgxm/OrCVwJRD9dBxvlpI0cGo0n7kPSQ74gD24AawSukdh6CYtxIKjo19BRIz4R9aJBktUgxsDPrvb3LxCd0s0prRvoqs/KhPZh+RmmlEoFbXXtrQZNt3CbKFa91shs89zqEeoOoUTw2udiekZhf47tORf5NfLiim31ssglpFShlHch4RCYRvvB0s9bfnZZzpMRyXwivR07CSTSkZEQYr3ZaQrv+8Ttv6iJWosJEAEQDLkEeR9JxuTNB/wEcHNldAVDAQABpo4NtvKS8tTm+XBSMxVgJ9n0/PyoNzYupvwN/jTxowK7wpU9wq3zG8ZjXuNP9iBDA16miq7iyqE/agp1CQmujwf8BHBzZXQGIQO76oHHphQNAVUtYNp3LMyLbHDhacNdtHz6sq+P0SLD6Qf8BHBzZXQHIQPZdPeFpD4dsI+NNMLI3lyrpSyc+MFBJgY1S/B2IJJxHwf8BHBzZXQIBAAAAAAAAQQAAQMIGQAAAAAAAAAH/ARwc2V0AiAYaWzyPjIJpuhEedAfE/55yQE1auU5B8yILP/T4mQBIwf8BHBzZXQIBAAAAAAA" + + @skipUnless( _LIQUID_AVAILABLE, "Liquid/ZKP backend unavailable; pure-Python fallback in use", @@ -92,18 +99,40 @@ def test_pset(self): self.assertTrue(str(tx), b64) def test_sign(self): - root = HDKey.from_string( - "tprv8ZgxMBicQKsPf27gmh4DbQqN2K6xnXA7m7AeceqQVGkRYny3X49sgcufzbJcq4k5eaGZDMijccdDzvQga2Saqd78dKqN52QwLyqgY8apX3j" - ) - psets = [ - "cHNldP8BAgQCAAAAAQMEAAAAAAEEAQEBBQEDAfsEAgAAAAABAOQCAAAAAAFK0vGV0PugDPRBFiCV3/0N1nGBWE0U4wgegC2qmHSnRwAAAAAA/f///wMBGGls8j4yCaboRHnQHxP+eckBNWrlOQfMiCz/0+JkASMBAAAAAACYloAAFgAUisfI8pzbq6cwbzmkz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qf8BHBzZXQHIQPZdPeFpD4dsI+NNMLI3lyrpSyc+MFBJgY1S/B2IJJxHwf8BHBzZXQIBAAAAAAAAQQAAQMIGQAAAAAAAAAH/ARwc2V0AiAYaWzyPjIJpuhEedAfE/55yQE1auU5B8yILP/T4mQBIwf8BHBzZXQIBAAAAAAA" - ] + root = HDKey.from_string(SIGN_TPRV) + psets = [SIGN_PSET_V2_B64] for b64 in psets: tx = PSET.from_string(b64) self.assertEqual(len(tx.to_string()), len(b64)) tx.sign_with(root) LTransaction.parse(tx.tx.serialize()) + def test_v2_locktime_and_sequence(self): + """PSET signs with the BIP-370 locktime and keeps a zero sequence""" + root = HDKey.from_string(SIGN_TPRV) + pset = PSET.from_string(SIGN_PSET_V2_B64) + self.assertEqual(pset.version, 2) + pset.inputs[0].required_height_locktime = 12345 + pset.inputs[0].sequence = 0 + # both the extracted and the sighash transaction use the derived locktime + self.assertEqual(pset.tx.locktime, 12345) + self.assertEqual(pset.blinded_tx.locktime, 12345) + self.assertEqual(pset.tx.vin[0].sequence, 0) + self.assertEqual(pset.blinded_tx.vin[0].sequence, 0) + # survives a round trip + pset = PSET.parse(pset.serialize()) + self.assertEqual(pset.inputs[0].sequence, 0) + self.assertEqual(pset.blinded_tx.locktime, 12345) + # PSET and PSETView agree on the signature + raw = pset.serialize() + self.assertEqual(pset.sign_with(root), 1) + view = PSETView.view(BytesIO(raw)) + sigs = BytesIO() + self.assertEqual(view.sign_with(root, sigs), 1) + sigs.seek(0) + signed = LInputScope.read_from(sigs, version=2) + self.assertEqual(signed.partial_sigs, pset.inputs[0].partial_sigs) + def test_slip77(self): seed = b"5" * 32 mbk = slip77.master_blinding_from_seed(seed) From e92934c1abed3b09eb9f3682a6d6dd21d7d9104f Mon Sep 17 00:00:00 2001 From: odudex Date: Wed, 2 Sep 2026 11:05:34 -0300 Subject: [PATCH 09/44] fix(psbt): report every malformed PSBT as PSBTError Truncated or corrupted input reached callers as RuntimeError from compact.read_from, ValueError/TransactionError/ECError/HDError from the nested transaction, script and key parsers, IndexError from a one-byte PSBT_GLOBAL_XPUB key, OverflowError from an absurd length reaching BytesIO.read/seek, and the base-class EmbitError for trailing bytes. Only read_string() wrapped its length read; the prescan added on this branch, PSBTView's deferred-global and scope walks, and GlobalTransactionView all called compact.read_from directly. - compact.read_from now fails on a truncated multi-byte prefix instead of returning int.from_bytes(b"") == 0, and rejects non-canonical encodings the way Bitcoin Core's ReadCompactSize does. A separator written as fd0000 was accepted as an empty key by both parsers, and PSBTView's offset bookkeeping assumes canonical lengths. - read_compact/read_exact/skip_exact wrap the low-level reads; every scope and global read goes through them and through _read_value_checked(), which reports nested parser errors as PSBTError naming the key. - PSBT.parse raises PSBTError on trailing bytes; PSBT_GLOBAL_XPUB keys are length-checked in the shared _validate_global_key; PSBTView bounds the global tx input/output counts by the declared tx length. - Serialization of out-of-range v2 fields (tx_modifiable_flags=300, locktime=2**32, negative sequence, 31-byte txid, amount >= 2**63) raises PSBTError instead of OverflowError/ValueError. Tests truncate and bit-flip three sample PSBTs at every byte and assert both parsers only ever raise PSBTError. --- src/embit/compact.py | 16 +++- src/embit/liquid/psetview.py | 10 +- src/embit/psbt.py | 173 ++++++++++++++++++++++++++--------- src/embit/psbtview.py | 54 +++++++---- tests/tests/test_parsing.py | 29 ++++-- tests/tests/test_psbtV2.py | 149 +++++++++++++++++++++++++++++- 6 files changed, 347 insertions(+), 84 deletions(-) diff --git a/src/embit/compact.py b/src/embit/compact.py index 67185d27..bb2d1bee 100644 --- a/src/embit/compact.py +++ b/src/embit/compact.py @@ -1,4 +1,5 @@ -""" Compact Int parsing / serialization """ +"""Compact Int parsing / serialization""" + import io @@ -36,9 +37,14 @@ def read_from(stream) -> int: i = c[0] if i >= 0xFD: bytes_to_read = 2 ** (i - 0xFC) - data = stream.read(bytes_to_read) - if len(data) != bytes_to_read: - raise RuntimeError("Incomplete compact integer") - return int.from_bytes(data, "little") + b = stream.read(bytes_to_read) + if len(b) != bytes_to_read: + raise RuntimeError("Can't read %d bytes from the stream" % bytes_to_read) + res = int.from_bytes(b, "little") + # reject non-canonical encodings like Bitcoin Core does: the value must + # not fit in the next shorter encoding + if res < (0xFD if i == 0xFD else 1 << (4 * bytes_to_read)): + raise ValueError("Non-canonical compact int encoding") + return res else: return i diff --git a/src/embit/liquid/psetview.py b/src/embit/liquid/psetview.py index 606a11dc..6a8aa7cf 100644 --- a/src/embit/liquid/psetview.py +++ b/src/embit/liquid/psetview.py @@ -45,7 +45,7 @@ def num_vout_offset(self): def num_vout(self): if self._num_vout is None: self.stream.seek(self.num_vout_offset) - self._num_vout = compact.read_from(self.stream) + self._num_vout = read_compact(self.stream) return self._num_vout @property @@ -99,8 +99,8 @@ def _skip_output(self): c = self.stream.read(1) if c != b"\x00": self.stream.seek(32, 1) # ecdh_pubkey - l = compact.read_from(self.stream) - self.stream.seek(l, 1) # scriptpubkey + l = read_compact(self.stream) + skip_exact(self.stream, l) # scriptpubkey def vout(self, i): if i < 0 or i >= self.num_vout: @@ -177,7 +177,7 @@ def hash_rangeproofs(self): if not rangeproof_offset: h.update(b"\x00") else: - l = compact.read_from(self.stream) + l = read_compact(self.stream) h.update(compact.to_bytes(l)) self._hash_to(h, l) @@ -193,7 +193,7 @@ def hash_rangeproofs(self): if not surj_proof_offset: h.update(b"\x00") else: - l = compact.read_from(self.stream) + l = read_compact(self.stream) h.update(compact.to_bytes(l)) self._hash_to(h, l) self._hash_rangeproofs = h.digest() diff --git a/src/embit/psbt.py b/src/embit/psbt.py index a1b229f6..11d1ef42 100644 --- a/src/embit/psbt.py +++ b/src/embit/psbt.py @@ -64,6 +64,9 @@ class CompressMode: def _validate_global_key(key): if len(key) != 1 and key[:1] in _GLOBAL_SINGLETON_FIELDS: raise PSBTError("Invalid global field key") + # PSBT_GLOBAL_XPUB: keydata is a 78-byte serialized extended public key + if key[:1] == b"\x01" and len(key) != 79: + raise PSBTError("Invalid PSBT_GLOBAL_XPUB key length") def _validate_global_fields(version, has_tx, fields): @@ -92,24 +95,63 @@ def _validate_global_fields(version, has_tx, fields): raise PSBTError("Invalid global count") +# exceptions nested parsers (Transaction, Script, keys, compact) raise on +# malformed input; PSBT parse paths convert them to PSBTError +_PARSE_ERRORS = (EmbitError, ValueError, IndexError, OverflowError, RuntimeError) + + def ser_string(stream, s: bytes) -> int: return stream.write(compact.to_bytes(len(s))) + stream.write(s) -def read_string(stream) -> bytes: +def _ser_uint(stream, key: bytes, value, length: int, name: str) -> int: + """Serializes a keyless field holding a fixed-width little-endian unsigned int.""" + if ( + not isinstance(value, int) + or isinstance(value, bool) + or value < 0 + or value >> (8 * length) + ): + raise PSBTError("%s must be an unsigned %d-byte integer" % (name, length)) + return ser_string(stream, key) + ser_string( + stream, value.to_bytes(length, "little") + ) + + +def read_compact(stream) -> int: + """compact.read_from() that raises PSBTError on truncated or non-canonical input""" + try: + return compact.read_from(stream) + except (RuntimeError, ValueError) as e: + raise PSBTError("Failed to read compact int: %s" % e) + + +def read_exact(stream, l: int) -> bytes: + """Reads exactly l bytes or raises PSBTError (also on absurd lengths).""" try: - l = compact.read_from(stream) - except (RuntimeError, TypeError) as e: - raise PSBTError("Failed to read key/value length: %s" % e) - s = stream.read(l) + s = stream.read(l) + except (OverflowError, MemoryError): + raise PSBTError("Failed to read %d bytes" % l) if len(s) != l: raise PSBTError("Failed to read %d bytes" % l) return s +def skip_exact(stream, l: int) -> None: + """Moves the cursor l bytes forward or raises PSBTError on absurd lengths.""" + try: + stream.seek(l, 1) + except (OverflowError, MemoryError, OSError, ValueError): + raise PSBTError("Failed to skip %d bytes" % l) + + +def read_string(stream) -> bytes: + return read_exact(stream, read_compact(stream)) + + def skip_string(stream) -> int: - l = compact.read_from(stream) - stream.seek(l, 1) + l = read_compact(stream) + skip_exact(stream, l) return len(compact.to_bytes(l)) + l @@ -218,7 +260,16 @@ def parse_unknowns(self): s = BytesIO() ser_string(s, self.unknown[k]) s.seek(0) - self.read_value(s, k) + self._read_value_checked(s, k) + + def _read_value_checked(self, stream, key, version=None): + """read_value() that reports malformed nested data as PSBTError.""" + try: + self.read_value(stream, key, version=version) + except PSBTError: + raise + except _PARSE_ERRORS as e: + raise PSBTError("Invalid value for key %s: %s" % (hexlify(key).decode(), e)) @classmethod def _validate_key(cls, key): @@ -249,7 +300,7 @@ def read_from(cls, stream, *args, **kwargs): break if version != 2 and key in res.V2_FIELDS: raise PSBTError("PSBTv2 field is not allowed in PSBTv0") - res.read_value(stream, key) + res._read_value_checked(stream, key) return res @@ -413,7 +464,7 @@ def read_value(self, stream, k, version=None): elif self.non_witness_utxo is not None: raise PSBTError("Duplicated utxo value") else: - length = compact.read_from(stream) + length = read_compact(stream) value_stream = _BoundedReader(stream, length) # For PSBTv2, PSBT_IN_OUTPUT_INDEX may follow PSBT_IN_NON_WITNESS_UTXO; # use the pre-scanned vout (set by read_from) when the field hasn't @@ -602,7 +653,7 @@ def read_value(self, stream, k, version=None): pub = ec.PublicKey.from_xonly(k[1:]) if pub not in self.taproot_bip32_derivations: b = BytesIO(v) - num_leaf_hashes = compact.read_from(b) + num_leaf_hashes = read_compact(b) leaf_hashes = [b.read(32) for i in range(num_leaf_hashes)] if not all([len(leaf) == 32 for leaf in leaf_hashes]): raise PSBTError("Invalid length of taproot leaf hashes") @@ -634,8 +685,9 @@ def write_to(self, stream, skip_separator=False, version=None, **kwargs) -> int: r += ser_string(stream, b"\x02" + pub.serialize()) r += ser_string(stream, self.partial_sigs[pub]) if self.sighash_type is not None: - r += stream.write(b"\x01\x03") - r += ser_string(stream, self.sighash_type.to_bytes(4, "little")) + r += _ser_uint( + stream, b"\x03", self.sighash_type, 4, "PSBT_IN_SIGHASH_TYPE" + ) if self.redeem_script is not None: r += stream.write(b"\x01\x04") r += self.redeem_script.write_to(stream) # script serialization has length @@ -654,27 +706,29 @@ def write_to(self, stream, skip_separator=False, version=None, **kwargs) -> int: if version == 2: if self.txid is not None: + if len(self.txid) != 32: + raise PSBTError("PSBT_IN_PREVIOUS_TXID must be 32 bytes") r += ser_string(stream, b"\x0e") r += ser_string(stream, bytes(reversed(self.txid))) if self.vout is not None: - r += ser_string(stream, b"\x0f") - r += ser_string(stream, self.vout.to_bytes(4, "little")) + r += _ser_uint(stream, b"\x0f", self.vout, 4, "PSBT_IN_OUTPUT_INDEX") if self.sequence is not None: - r += ser_string(stream, b"\x10") - r += ser_string(stream, self.sequence.to_bytes(4, "little")) - - # Add required time locktime if present + r += _ser_uint(stream, b"\x10", self.sequence, 4, "PSBT_IN_SEQUENCE") if self.required_time_locktime is not None: - r += ser_string(stream, b"\x11") - r += ser_string( - stream, self.required_time_locktime.to_bytes(4, "little") + r += _ser_uint( + stream, + b"\x11", + self.required_time_locktime, + 4, + "PSBT_IN_REQUIRED_TIME_LOCKTIME", ) - - # Add required height locktime if present if self.required_height_locktime is not None: - r += ser_string(stream, b"\x12") - r += ser_string( - stream, self.required_height_locktime.to_bytes(4, "little") + r += _ser_uint( + stream, + b"\x12", + self.required_height_locktime, + 4, + "PSBT_IN_REQUIRED_HEIGHT_LOCKTIME", ) # PSBT_IN_TAP_KEY_SIG @@ -755,7 +809,7 @@ def read_from(cls, stream, compress=CompressMode.KEEP_ALL, vin=None, version=Non # separator if len(key) == 0: break - res.read_value(stream, key, version=version) + res._read_value_checked(stream, key, version=version) del res._prescan_vout return res @@ -864,7 +918,7 @@ def read_value(self, stream, k, version=None): pub = ec.PublicKey.from_xonly(k[1:]) if pub not in self.taproot_bip32_derivations: b = BytesIO(v) - num_leaf_hashes = compact.read_from(b) + num_leaf_hashes = read_compact(b) leaf_hashes = [b.read(32) for i in range(num_leaf_hashes)] if not all([len(leaf) == 32 for leaf in leaf_hashes]): raise PSBTError("Invalid length of taproot leaf hashes") @@ -890,8 +944,10 @@ def write_to(self, stream, skip_separator=False, version=None, **kwargs) -> int: if version == 2: if self.value is not None: - r += ser_string(stream, b"\x03") - r += ser_string(stream, self.value.to_bytes(8, "little")) + # BIP-370: signed int64, the top half is not representable + if self.value >= 2**63: + raise PSBTError("PSBT_OUT_AMOUNT must be non-negative") + r += _ser_uint(stream, b"\x03", self.value, 8, "PSBT_OUT_AMOUNT") if self.script_pubkey is not None: r += ser_string(stream, b"\x04") r += self.script_pubkey.write_to(stream) @@ -929,7 +985,7 @@ def read_from(cls, stream, compress=CompressMode.KEEP_ALL, vout=None, version=No # separator if len(key) == 0: break - res.read_value(stream, key, version=version) + res._read_value_checked(stream, key, version=version) return res @@ -1120,20 +1176,24 @@ def write_to(self, stream) -> int: if self.version == 2: tx_version = self.tx_version if self.tx_version is not None else 2 - r += ser_string(stream, b"\x02") - r += ser_string(stream, tx_version.to_bytes(4, "little")) + r += _ser_uint(stream, b"\x02", tx_version, 4, "PSBT_GLOBAL_TX_VERSION") if self.locktime is not None: - r += ser_string(stream, b"\x03") - r += ser_string(stream, self.locktime.to_bytes(4, "little")) + r += _ser_uint( + stream, b"\x03", self.locktime, 4, "PSBT_GLOBAL_FALLBACK_LOCKTIME" + ) r += ser_string(stream, b"\x04") r += ser_string(stream, compact.to_bytes(len(self.inputs))) r += ser_string(stream, b"\x05") r += ser_string(stream, compact.to_bytes(len(self.outputs))) if self.tx_modifiable_flags is not None: - r += ser_string(stream, b"\x06") - r += ser_string(stream, bytes([self.tx_modifiable_flags])) - r += ser_string(stream, b"\xfb") - r += ser_string(stream, self.version.to_bytes(4, "little")) + r += _ser_uint( + stream, + b"\x06", + self.tx_modifiable_flags, + 1, + "PSBT_GLOBAL_TX_MODIFIABLE", + ) + r += _ser_uint(stream, b"\xfb", self.version, 4, "PSBT_GLOBAL_VERSION") r += self._write_extra_globals(stream) # unknown @@ -1150,6 +1210,14 @@ def write_to(self, stream) -> int: r += out.write_to(stream, version=self.version) return r + @classmethod + def parse(cls, s: bytes, *args, **kwargs): + stream = BytesIO(s) + res = cls.read_from(stream, *args, **kwargs) + if len(stream.read(1)) > 0: + raise PSBTError("Unexpected extra bytes") + return res + @classmethod def from_base64(cls, b64, compress=CompressMode.KEEP_ALL): raw = a2b_base64(b64) @@ -1230,7 +1298,10 @@ def read_from(cls, stream, compress=CompressMode.KEEP_ALL): raise PSBTError("PSBTv2 missing required PSBT_GLOBAL_TX_VERSION (0x02)") else: # PSBTv0 (version is None or 0) tx_bytes = global_kvs.pop(b"\x00") # Remove so it's not in unknown - tx_for_v0 = cls.TX_CLS.parse(tx_bytes) + try: + tx_for_v0 = cls.TX_CLS.parse(tx_bytes) + except _PARSE_ERRORS as e: + raise PSBTError("Invalid global transaction: %s" % e) # BIP-174: the global transaction must be unsigned for inp in tx_for_v0.vin: if len(inp.script_sig.data) > 0: @@ -1299,6 +1370,13 @@ def read_from(cls, stream, compress=CompressMode.KEEP_ALL): return psbt + @staticmethod + def _parse_count(value): + try: + return compact.from_bytes(value) + except (ValueError, RuntimeError) as e: + raise PSBTError("Invalid global count: %s" % e) + def parse_unknowns(self): # Handle PSBT_GLOBAL_VERSION first if b"\xfb" in self.unknown: @@ -1313,8 +1391,13 @@ def parse_unknowns(self): for k in list(self.unknown): # xpub field if k[0] == 0x01: - xpub = bip32.HDKey.parse(k[1:]) - self.xpubs[xpub] = DerivationPath.parse(self.unknown.pop(k)) + try: + xpub = bip32.HDKey.parse(k[1:]) + self.xpubs[xpub] = DerivationPath.parse(self.unknown.pop(k)) + except PSBTError: + raise + except _PARSE_ERRORS as e: + raise PSBTError("Invalid PSBT_GLOBAL_XPUB: %s" % e) elif k == b"\x02": if self.version == 2: v = self.unknown.pop(k) @@ -1332,7 +1415,7 @@ def parse_unknowns(self): if self._raw_input_count_from_global is not None: self.unknown.pop(k, None) continue - self._raw_input_count_from_global = compact.from_bytes( + self._raw_input_count_from_global = self._parse_count( self.unknown.pop(k) ) # Store count only; do not pre-allocate objects to avoid @@ -1342,7 +1425,7 @@ def parse_unknowns(self): if self._raw_output_count_from_global is not None: self.unknown.pop(k, None) continue - self._raw_output_count_from_global = compact.from_bytes( + self._raw_output_count_from_global = self._parse_count( self.unknown.pop(k) ) # Store count only; do not pre-allocate objects to avoid diff --git a/src/embit/psbtview.py b/src/embit/psbtview.py index a7f55945..9b5a0bbf 100644 --- a/src/embit/psbtview.py +++ b/src/embit/psbtview.py @@ -35,6 +35,10 @@ read_string, ser_string, skip_string, + read_compact, + read_exact, + skip_exact, + _PARSE_ERRORS, _GLOBAL_COUNT_FIELDS, _GLOBAL_FIXED_VALUE_LENGTHS, _validate_global_fields, @@ -52,15 +56,13 @@ def _read_global_value(stream, key): - value_len = compact.read_from(stream) + value_len = read_compact(stream) if key in _GLOBAL_FIXED_VALUE_LENGTHS: if value_len != _GLOBAL_FIXED_VALUE_LENGTHS[key]: raise PSBTError("Invalid global field length") elif key in _GLOBAL_COUNT_FIELDS and value_len > 9: raise PSBTError("Invalid global count length") - value = stream.read(value_len) - if len(value) != value_len: - raise PSBTError("Failed to read %d bytes" % value_len) + value = read_exact(stream, value_len) return value, len(compact.to_bytes(value_len)) + value_len @@ -166,7 +168,7 @@ def version(self): def num_vin(self): if self._num_vin is None: self.stream.seek(self.offset + self.NUM_VIN_OFFSET) - self._num_vin = compact.read_from(self.stream) + self._num_vin = read_compact(self.stream) self._vin0_offset = self.stream.tell() return self._num_vin @@ -176,7 +178,7 @@ def num_vout(self): self._check_inputs() # version, n_vin, n_vin * len(vin) self.stream.seek(self.vin0_offset + self.LEN_VIN * self.num_vin) - self._num_vout = compact.read_from(self.stream) + self._num_vout = read_compact(self.stream) self._vout0_offset = self.stream.tell() return self._num_vout @@ -222,13 +224,15 @@ def vin(self, i): raise PSBTError("Invalid input index") self._check_inputs() self.stream.seek(self.vin0_offset + self.LEN_VIN * i) - return TransactionInput.read_from(self.stream) + try: + return TransactionInput.read_from(self.stream) + except _PARSE_ERRORS as e: + raise PSBTError("Invalid global transaction input: %s" % e) def _skip_output(self): """Seeks over one output""" self.stream.seek(8, 1) - l = compact.read_from(self.stream) - self.stream.seek(l, 1) + skip_exact(self.stream, read_compact(self.stream)) def vout(self, i): if i < 0 or i >= self.num_vout: @@ -238,7 +242,10 @@ def vout(self, i): while n: self._skip_output() n -= 1 - return TransactionOutput.read_from(self.stream) + try: + return TransactionOutput.read_from(self.stream) + except _PARSE_ERRORS as e: + raise PSBTError("Invalid global transaction output: %s" % e) class PSBTView: @@ -363,14 +370,24 @@ def view(cls, stream, offset=None, compress=CompressMode.KEEP_ALL): raise PSBTError("Duplicate global transaction") if b"\x04" in global_kvs or b"\x05" in global_kvs: raise PSBTError("Invalid global transaction") - tx_len = compact.read_from(stream) + tx_len = read_compact(stream) cur += len(compact.to_bytes(tx_len)) tx_offset = cur tx = cls.TX_CLS(stream, tx_offset, tx_len) - num_inputs = tx.num_vin - num_outputs = tx.num_vout + try: + num_inputs = tx.num_vin + num_outputs = tx.num_vout + except (OverflowError, ValueError, OSError): + raise PSBTError("Invalid global transaction") + # every input takes at least 41 bytes and every output 9, + # so the counts must fit in the declared transaction length + if tx.vout0_offset + 9 * num_outputs > tx_offset + tx_len: + raise PSBTError("Invalid global transaction") # seek to the end of transaction - stream.seek(tx_offset + tx_len) + try: + stream.seek(tx_offset + tx_len) + except (OverflowError, ValueError, OSError): + raise PSBTError("Invalid global transaction length") cur += tx_len else: if key in global_kvs or key in deferred_kvs: @@ -382,10 +399,10 @@ def view(cls, stream, offset=None, compress=CompressMode.KEEP_ALL): if key == b"\xfb": version = int.from_bytes(value, "little") else: - value_len = compact.read_from(stream) + value_len = read_compact(stream) header = len(compact.to_bytes(value_len)) deferred_kvs[key] = (cur + header, value_len) - stream.seek(value_len, 1) + skip_exact(stream, value_len) cur += header + value_len first_scope = cur if version not in (None, 0, 2): @@ -408,10 +425,7 @@ def view(cls, stream, offset=None, compress=CompressMode.KEEP_ALL): # (and only now) we need the values we skipped over above. for k, (v_off, v_len) in deferred_kvs.items(): stream.seek(v_off) - v = stream.read(v_len) - if len(v) != v_len: - raise PSBTError("Failed to read %d bytes" % v_len) - global_kvs[k] = v + global_kvs[k] = read_exact(stream, v_len) return cls( stream, num_inputs, diff --git a/tests/tests/test_parsing.py b/tests/tests/test_parsing.py index b09d26c1..22dd229c 100644 --- a/tests/tests/test_parsing.py +++ b/tests/tests/test_parsing.py @@ -109,8 +109,11 @@ def test_compact(self): with self.assertRaises(RuntimeError): compact.read_from(BytesIO(b"")) for prefix, width in ((253, 2), (254, 4), (255, 8)): - raw = bytes([prefix]) + b"\x01" + bytes(width - 1) - self.assertEqual(compact.from_bytes(raw), 1) # Minimality is unchanged. + # non-minimal encodings are rejected like Bitcoin Core does + with self.assertRaises(ValueError): + compact.from_bytes(bytes([prefix]) + b"\x01" + bytes(width - 1)) + raw = bytes([prefix]) + bytes(width - 1) + b"\x01" + self.assertEqual(compact.from_bytes(raw), 1 << (8 * (width - 1))) for count in range(width): with self.assertRaises(RuntimeError): compact.from_bytes(raw[: 1 + count]) @@ -250,12 +253,23 @@ def test_view_skips_are_checked_and_chunked(self): stream = TrackingStream(raw[:100]) with self.assertRaises(PSBTError): _ = GlobalTransactionView(stream, 0, len(raw)).locktime - # Counts with extended encodings retain their actual offsets. + # Non-minimal counts are rejected rather than re-encoded to find offsets. raw = self.transaction().serialize() - extended = raw[:4] + b"\xfd\x01\x00" + raw[5:46] + b"\xfd\x01\x00" + raw[47:] - view = GlobalTransactionView(BytesIO(extended), 0, len(extended)) + for extended in ( + raw[:4] + b"\xfd\x01\x00" + raw[5:], + raw[:46] + b"\xfd\x01\x00" + raw[47:], + ): + with self.assertRaises(PSBTError): + _ = GlobalTransactionView(BytesIO(extended), 0, len(extended)).locktime + # Counts with extended encodings retain their actual offsets. + tx = self.transaction() + tx.vin = [TransactionInput(bytes(range(32)), i) for i in range(253)] + tx.vout = [TransactionOutput(i, Script(b"\x51")) for i in range(253)] + raw = tx.serialize() + view = GlobalTransactionView(BytesIO(raw), 0, len(raw)) self.assertEqual(view.locktime, 0) - self.assertEqual(view.vout(0).value, 42) + self.assertEqual(view.vin(252).vout, 252) + self.assertEqual(view.vout(252).value, 252) def test_scope_reads_do_not_rescan_inputs(self): tx = self.transaction() @@ -284,8 +298,7 @@ def test_view_reuses_checked_script_ranges(self): self.assertTrue(stream.max_read <= 32) # Accessed fields still require complete reads after traversal. stream.short_at = view.vin0_offset - error = TransactionError if length is None else PSBTError - with self.assertRaises(error): + with self.assertRaises(PSBTError): view.vin(0) def test_view_checks_unvisited_ranges_after_external_seek(self): diff --git a/tests/tests/test_psbtV2.py b/tests/tests/test_psbtV2.py index 58a1cc2d..84cba0a2 100644 --- a/tests/tests/test_psbtV2.py +++ b/tests/tests/test_psbtV2.py @@ -4,7 +4,7 @@ """ import pytest -from binascii import unhexlify +from binascii import unhexlify, a2b_base64 from io import BytesIO from embit.psbt import ( PSBT, @@ -754,3 +754,150 @@ def test_sign_does_not_emit_sigs_before_rejecting_a_later_input(self): with pytest.raises(PSBTError): view.sign_with(SIGNING_ROOT, sigs) assert sigs.getvalue() == b"" + + +def _exercise_view(raw, compress=CompressMode.KEEP_ALL): + """Parses with PSBTView and touches every lazily-read field.""" + view = PSBTView.view(BytesIO(raw), compress=compress) + view.tx_version + view.locktime + for i in range(view.num_inputs): + view.input(i) + view.vin(i) + for i in range(view.num_outputs): + view.output(i) + view.vout(i) + view.write_to(BytesIO()) + return view + + +class TestPSBTErrorHygiene: + """Malformed input must surface as PSBTError from every parse path""" + + V2_UPDATED = unhexlify( + "70736274ff01020402000000010401010105010201fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f040000000000220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c00220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" + ) + SAMPLES = [V2_UPDATED, a2b_base64(VIEW_PSBTS[0]), a2b_base64(VIEW_PSBTS[1])] + + @pytest.mark.parametrize("compress", [CompressMode.KEEP_ALL, CompressMode.PARTIAL]) + def test_truncated_input(self, compress): + for raw in self.SAMPLES: + for n in range(len(raw)): + with pytest.raises(PSBTError): + PSBT.parse(raw[:n], compress=compress) + try: + _exercise_view(raw[:n], compress=compress) + except PSBTError: + pass + + @pytest.mark.parametrize("compress", [CompressMode.KEEP_ALL, CompressMode.PARTIAL]) + def test_corrupted_byte(self, compress): + for raw in self.SAMPLES: + for n in range(len(raw)): + flipped = raw[:n] + bytes([raw[n] ^ 0xFF]) + raw[n + 1 :] + try: + PSBT.parse(flipped, compress=compress) + except PSBTError: + pass + try: + _exercise_view(flipped, compress=compress) + except PSBTError: + pass + + @pytest.mark.parametrize("separator", [b"\xfd\x00\x00", b"\xfd", b"\xfe\x00\x00"]) + def test_non_canonical_or_truncated_separator(self, separator): + raw = raw_v2([V2_IN], [V2_OUT]) + assert raw.endswith(b"\x00") + raw = raw[:-1] + separator + with pytest.raises(PSBTError): + PSBT.parse(raw) + with pytest.raises(PSBTError): + _exercise_view(raw) + + def test_compact_rejects_non_canonical_and_truncated(self): + with pytest.raises(ValueError): + compact.from_bytes(b"\xfd\x00\x00") + with pytest.raises(ValueError): + compact.from_bytes(b"\xfe\xff\xff\x00\x00") + with pytest.raises(RuntimeError): + compact.from_bytes(b"\xfd\x00") + assert compact.from_bytes(b"\xfd\xfd\x00") == 0xFD + assert compact.from_bytes(b"\xfe\x00\x00\x01\x00") == 0x10000 + + def test_malformed_xpub_global(self): + raw = raw_v2([V2_IN], [V2_OUT], extra_globals=kv(b"\x01\x00", bytes(4))) + with pytest.raises(PSBTError): + PSBT.parse(raw) + with pytest.raises(PSBTError): + _exercise_view(raw) + # right length, garbage content + raw = raw_v2([V2_IN], [V2_OUT], extra_globals=kv(b"\x01" + bytes(78), bytes(4))) + with pytest.raises(PSBTError): + PSBT.parse(raw) + + def test_corrupted_global_transaction(self): + raw = a2b_base64(VIEW_PSBTS[0]) + # global tx value starts right after magic + key(00) + value length + assert raw[5:7] == b"\x01\x00" + tx_len_size = len(compact.to_bytes(len(raw))) # >= 1 + corrupt = raw[: 7 + tx_len_size + 4] + b"\xff" * 8 + raw[7 + tx_len_size + 12 :] + with pytest.raises(PSBTError): + PSBT.parse(corrupt) + with pytest.raises(PSBTError): + _exercise_view(corrupt) + + def test_parse_rejects_trailing_bytes(self): + raw = raw_v2([V2_IN], [V2_OUT]) + with pytest.raises(PSBTError): + PSBT.parse(raw + b"\x00") + + def test_corrupted_nested_value_reports_key(self): + # bip32 derivation with a 1-byte pubkey in the key + raw = raw_v2([V2_IN + kv(b"\x06\x02", bytes(8))], [V2_OUT]) + with pytest.raises(PSBTError, match="0602"): + PSBT.parse(raw) + with pytest.raises(PSBTError): + _exercise_view(raw) + + def test_bad_global_count_in_constructor(self): + with pytest.raises(PSBTError): + PSBT(unknown={b"\x04": b"\xfd"}, version=2) + + def test_serialize_rejects_out_of_range_fields(self): + def v2(): + psbt = PSBT.create_v2() + inp = InputScope() + inp.txid = bytes(32) + inp.vout = 0 + psbt.add_input(inp) + out = OutputScope() + out.value = 1 + out.script_pubkey = Script(b"\x51") + psbt.add_output(out) + return psbt + + psbt = v2() + psbt.serialize() + for attr, value in [ + ("tx_modifiable_flags", 300), + ("locktime", 2**32), + ("tx_version", -1), + ]: + psbt = v2() + setattr(psbt, attr, value) + with pytest.raises(PSBTError): + psbt.serialize() + for attr, value in [ + ("sequence", -1), + ("vout", 2**32), + ("required_height_locktime", 2**40), + ("txid", bytes(31)), + ]: + psbt = v2() + setattr(psbt.inputs[0], attr, value) + with pytest.raises(PSBTError): + psbt.serialize() + psbt = v2() + psbt.outputs[0].value = 2**63 + with pytest.raises(PSBTError): + psbt.serialize() From ddadb23f1a64782d60bf3c68aa130e55f9379935 Mon Sep 17 00:00:00 2001 From: odudex Date: Wed, 2 Sep 2026 11:06:30 -0300 Subject: [PATCH 10/44] fix(psbtview): reject trailing data and count/map mismatches when viewing PSBT.parse fails on anything after the last output map (as Bitcoin Core's DecodeRawPSBT does) and on a PSBT_GLOBAL_INPUT_COUNT larger than the maps present, but PSBTView accepted both silently: it only ever walked as far as the scope being asked for, so an extra map, trailing garbage or a missing map surfaced late, in whatever accessor happened to reach it, or not at all. PSBTView.view() now walks all the input and output maps once, reading only the key and value lengths, and fails if the maps run out or bytes remain. Every proper prefix of a PSBT now fails in view() instead of in a later lazy read. --- src/embit/psbtview.py | 9 ++++++++- tests/tests/test_psbtV2.py | 25 +++++++++++++++++++------ 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/embit/psbtview.py b/src/embit/psbtview.py index 9b5a0bbf..4d086b90 100644 --- a/src/embit/psbtview.py +++ b/src/embit/psbtview.py @@ -426,7 +426,7 @@ def view(cls, stream, offset=None, compress=CompressMode.KEEP_ALL): for k, (v_off, v_len) in deferred_kvs.items(): stream.seek(v_off) global_kvs[k] = read_exact(stream, v_len) - return cls( + res = cls( stream, num_inputs, num_outputs, @@ -438,6 +438,13 @@ def view(cls, stream, offset=None, compress=CompressMode.KEEP_ALL): tx_len, global_kvs=global_kvs if version == 2 else None, ) + # Walk every input and output map once, reading only the key/value + # lengths: the declared counts must match the maps actually present + # and nothing may follow the last one, like PSBT.parse enforces. + res.seek_to_scope(num_inputs + num_outputs) + if len(stream.read(1)) > 0: + raise PSBTError("Unexpected extra bytes") + return res def _skip_scope(self): off = 0 diff --git a/tests/tests/test_psbtV2.py b/tests/tests/test_psbtV2.py index 84cba0a2..85d89809 100644 --- a/tests/tests/test_psbtV2.py +++ b/tests/tests/test_psbtV2.py @@ -785,10 +785,8 @@ def test_truncated_input(self, compress): for n in range(len(raw)): with pytest.raises(PSBTError): PSBT.parse(raw[:n], compress=compress) - try: - _exercise_view(raw[:n], compress=compress) - except PSBTError: - pass + with pytest.raises(PSBTError): + PSBTView.view(BytesIO(raw[:n]), compress=compress) @pytest.mark.parametrize("compress", [CompressMode.KEEP_ALL, CompressMode.PARTIAL]) def test_corrupted_byte(self, compress): @@ -846,10 +844,25 @@ def test_corrupted_global_transaction(self): with pytest.raises(PSBTError): _exercise_view(corrupt) - def test_parse_rejects_trailing_bytes(self): + @pytest.mark.parametrize("trailing", [b"\x00", b"\xff", V2_OUT + b"\x00"]) + def test_rejects_trailing_bytes(self, trailing): raw = raw_v2([V2_IN], [V2_OUT]) with pytest.raises(PSBTError): - PSBT.parse(raw + b"\x00") + PSBT.parse(raw + trailing) + with pytest.raises(PSBTError): + PSBTView.view(BytesIO(raw + trailing)) + raw = a2b_base64(VIEW_PSBTS[0]) + with pytest.raises(PSBTError): + PSBTView.view(BytesIO(raw + trailing)) + + def test_view_rejects_count_larger_than_maps(self): + raw = raw_v2([V2_IN, V2_IN], [V2_OUT]) + assert raw[raw.index(b"\x01\x04\x01") + 3] == 2 + raw = raw.replace(b"\x01\x04\x01\x02", b"\x01\x04\x01\x03", 1) + with pytest.raises(PSBTError): + PSBT.parse(raw) + with pytest.raises(PSBTError): + PSBTView.view(BytesIO(raw)) def test_corrupted_nested_value_reports_key(self): # bip32 derivation with a 1-byte pubkey in the key From c2b83c24ab9baf665e701751dcdbd96ff4cd414d Mon Sep 17 00:00:00 2001 From: odudex Date: Wed, 2 Sep 2026 11:10:47 -0300 Subject: [PATCH 11/44] fix(psbt): align PSBT and PSBTView on locktime, key order, key-path sigs and errors Several places where the two implementations disagreed on the same PSBT: - PSBTView.locktime returned the BIP-370 derived locktime while PSBT.locktime is the raw PSBT_GLOBAL_FALLBACK_LOCKTIME field, so test_psbtview's equality assertion only held for vectors without per-input requirements. PSBTView now mirrors PSBT: .locktime is the raw field and determine_locktime() is the transaction locktime, which is what every sighash uses. - PSBTView wrote the v2 global map in sorted key order, PSBT in xpubs / BIP-370 fields / unknowns order, so a PSBT with an unknown global field round-tripped to different bytes through each. PSBTView now uses PSBT's order and keeps the source order of unknowns with an OrderedDict (MicroPython dicts are unordered). - PSBTView's taproot key-path signer filled only final_scriptwitness while PSBT also stores PSBT_IN_TAP_KEY_SIG, so the signed PSBTs differed. Both now fill both fields and the sig stream carries 0x13 as well. - PSBTScope(unknown={...}) could not carry PSBTv2 fields because parse_unknowns() never passed a version, and any unknown key raised "Duplicated key" because the entry was never popped before read_value() re-added it. Scopes take version= and consume the dict as they parse. - An input without a utxo raised AttributeError from is_taproot when signing; it is now PSBTError("Missing previous utxo on input N") from sighash() in both PSBT and PSBTView, matching PSBT.fee(). - Documented that PSBTView.sign_with() keeps the TX_MODIFIABLE update on the view, not in the sig stream, so the same view must write the result. Parity tests sign taproot key-path and script-path PSBTs, in v0 and v2 and with KEEP_ALL and PARTIAL compression, through PSBT and PSBTView and compare the resulting bytes. --- src/embit/liquid/psetview.py | 2 +- src/embit/psbt.py | 47 ++++++++---- src/embit/psbtview.py | 68 ++++++++++++----- tests/tests/test_psbtV2.py | 142 +++++++++++++++++++++++++++++++++++ 4 files changed, 228 insertions(+), 31 deletions(-) diff --git a/src/embit/liquid/psetview.py b/src/embit/liquid/psetview.py index 6a8aa7cf..e1ab778a 100644 --- a/src/embit/liquid/psetview.py +++ b/src/embit/liquid/psetview.py @@ -261,7 +261,7 @@ def sighash_segwit( ) else: h.update(zero) - h.update(self.locktime.to_bytes(4, "little")) + h.update(self.determine_locktime().to_bytes(4, "little")) h.update(sighash.to_bytes(4, "little")) return hashlib.sha256(h.digest()).digest() diff --git a/src/embit/psbt.py b/src/embit/psbt.py index 11d1ef42..c6e69a25 100644 --- a/src/embit/psbt.py +++ b/src/embit/psbt.py @@ -239,9 +239,9 @@ def read_from(cls, stream): class PSBTScope(EmbitBase): V2_FIELDS = () - def __init__(self, unknown: dict = None): + def __init__(self, unknown: dict = None, version=None): self.unknown = {} if unknown is None else unknown - self.parse_unknowns() + self.parse_unknowns(version) def write_to(self, stream, skip_separator=False, **kwargs) -> int: # unknown @@ -254,13 +254,14 @@ def write_to(self, stream, skip_separator=False, **kwargs) -> int: r += stream.write(b"\x00") return r - def parse_unknowns(self): - # go through all the unknowns and parse them + def parse_unknowns(self, version=None): + # go through all the unknowns and parse them: known keys land in their + # fields, the rest is put back into self.unknown by read_value() for k in list(self.unknown): s = BytesIO() - ser_string(s, self.unknown[k]) + ser_string(s, self.unknown.pop(k)) s.seek(0) - self._read_value_checked(s, k) + self._read_value_checked(s, k, version=version) def _read_value_checked(self, stream, key, version=None): """read_value() that reports malformed nested data as PSBTError.""" @@ -309,7 +310,13 @@ class InputScope(PSBTScope): TXOUT_CLS = TransactionOutput V2_FIELDS = (b"\x0e", b"\x0f", b"\x10", b"\x11", b"\x12") - def __init__(self, unknown: dict = None, vin=None, compress=CompressMode.KEEP_ALL): + def __init__( + self, + unknown: dict = None, + vin=None, + compress=CompressMode.KEEP_ALL, + version=None, + ): self.compress = compress self.txid = None self.vout = None @@ -341,7 +348,7 @@ def __init__(self, unknown: dict = None, vin=None, compress=CompressMode.KEEP_AL self.final_scriptwitness = None self.required_time_locktime = None self.required_height_locktime = None - super().__init__(unknown) + super().__init__(unknown, version=version) def clear_metadata(self, compress=CompressMode.CLEAR_ALL): """Removes metadata like derivations, utxos etc except final or partial sigs""" @@ -428,7 +435,8 @@ def is_verified(self): @property def is_taproot(self): - return self.utxo.script_pubkey.script_type() == "p2tr" + utxo = self.utxo + return utxo is not None and utxo.script_pubkey.script_type() == "p2tr" def verify(self, ignore_missing=False): """Verifies the hash of previous transaction provided in non_witness_utxo. @@ -817,7 +825,13 @@ def read_from(cls, stream, compress=CompressMode.KEEP_ALL, vin=None, version=Non class OutputScope(PSBTScope): V2_FIELDS = (b"\x03", b"\x04") - def __init__(self, unknown: dict = None, vout=None, compress=CompressMode.KEEP_ALL): + def __init__( + self, + unknown: dict = None, + vout=None, + compress=CompressMode.KEEP_ALL, + version=None, + ): self.compress = compress self.value = None self.script_pubkey = None @@ -829,7 +843,7 @@ def __init__(self, unknown: dict = None, vout=None, compress=CompressMode.KEEP_A self.bip32_derivations = OrderedDict() self.taproot_bip32_derivations = OrderedDict() self.taproot_internal_key = None - super().__init__(unknown) + super().__init__(unknown, version=version) def clear_metadata(self, compress=CompressMode.CLEAR_ALL): """Removes metadata like derivations, utxos etc except final or partial sigs""" @@ -1433,10 +1447,17 @@ def parse_unknowns(self): def sighash(self, i, sighash=SIGHASH.ALL, **kwargs): inp = self.inputs[i] + if inp.utxo is None: + raise PSBTError("Missing previous utxo on input %d" % i) if inp.is_taproot: - values = [inp.utxo.value for inp in self.inputs] - scripts = [inp.utxo.script_pubkey for inp in self.inputs] + values = [] + scripts = [] + for j, other in enumerate(self.inputs): + if other.utxo is None: + raise PSBTError("Missing previous utxo on input %d" % j) + values.append(other.utxo.value) + scripts.append(other.utxo.script_pubkey) return self.sighash_taproot( i, script_pubkeys=scripts, diff --git a/src/embit/psbtview.py b/src/embit/psbtview.py index 4d086b90..25b38862 100644 --- a/src/embit/psbtview.py +++ b/src/embit/psbtview.py @@ -54,6 +54,9 @@ hash_script_pubkeys, ) +# global key order used by PSBT.write_to after the xpubs +_GLOBAL_V2_ORDER = (b"\x02", b"\x03", b"\x04", b"\x05", b"\x06", b"\xfb") + def _read_global_value(stream, key): value_len = read_compact(stream) @@ -324,6 +327,7 @@ def tx_modifiable_flags(self, value): def clear_cache(self): # cache for digests + self._tx_locktime = None self._hash_prevouts = None self._hash_sequence = None self._hash_outputs = None @@ -349,11 +353,11 @@ def view(cls, stream, offset=None, compress=CompressMode.KEEP_ALL): tx_len = None # Collect all non-global-tx key-value pairs for PSBTv2 global scope rewriting. # The global scope is small, so materialising it avoids byte-level injection. - global_kvs = {} + global_kvs = OrderedDict() # key -> (value_offset, value_len) for globals whose value is only needed # if this turns out to be a PSBTv2. PSBTView is RAM-constrained, so we # skip them on the first pass and read them back below only when used. - deferred_kvs = {} + deferred_kvs = OrderedDict() while True: # read key and update cursor key = read_string(stream) @@ -608,13 +612,22 @@ def vout(self, i, compress=None): @property def locktime(self): - if self._locktime is None: + """Same meaning as PSBT.locktime: the global transaction locktime for + PSBTv0 and PSBT_GLOBAL_FALLBACK_LOCKTIME (or None) for PSBTv2. + The locktime of the transaction being signed is determine_locktime().""" + if self.version == 2: + v = self._global_kvs.get(b"\x03") + return int.from_bytes(v, "little") if v is not None else None + return self._locktime + + def determine_locktime(self): + """The transaction locktime, derived per BIP-370 for PSBTv2.""" + if self._tx_locktime is None: if self.version == 2: - self._locktime = self._determine_locktime_v2() + self._tx_locktime = self._determine_locktime_v2() else: - v = self.get_value(b"\x03") - self._locktime = int.from_bytes(v, "little") if v is not None else 0 - return self._locktime + self._tx_locktime = self._locktime or 0 + return self._tx_locktime def _determine_locktime_v2(self): """BIP370 locktime determination for PSBTv2. @@ -764,7 +777,7 @@ def sighash_taproot( h = hashes.tagged_hash_init("TapSighash", b"\x00") h.update(bytes([sighash])) h.update(self.tx_version.to_bytes(4, "little")) - h.update(self.locktime.to_bytes(4, "little")) + h.update(self.determine_locktime().to_bytes(4, "little")) if not anyonecanpay: h.update(self.hash_prevouts()) h.update(self.hash_amounts(values)) @@ -835,7 +848,7 @@ def sighash_segwit(self, input_index, script_pubkey, value, sighash=SIGHASH.ALL) ) else: h.update(zero) - h.update(self.locktime.to_bytes(4, "little")) + h.update(self.determine_locktime().to_bytes(4, "little")) h.update(sighash.to_bytes(4, "little")) return hashlib.sha256(h.digest()).digest() @@ -884,12 +897,14 @@ def sighash_legacy(self, input_index, script_pubkey, sighash=SIGHASH.ALL): else: # shouldn't happen raise PSBTError("Invalid sighash") - h.update(self.locktime.to_bytes(4, "little")) + h.update(self.determine_locktime().to_bytes(4, "little")) h.update(sighash.to_bytes(4, "little")) return hashlib.sha256(h.digest()).digest() def sighash(self, i, sighash=SIGHASH.ALL, input_scope=None, **kwargs): inp = self.input(i) if input_scope is None else input_scope + if inp.utxo is None: + raise PSBTError("Missing previous utxo on input %d" % i) if inp.is_taproot: values = [] @@ -897,6 +912,8 @@ def sighash(self, i, sighash=SIGHASH.ALL, input_scope=None, **kwargs): # TODO: not very optimal. Maybe use build_cache() or something? for idx in range(self.num_inputs): inp = self.input(idx) + if inp.utxo is None: + raise PSBTError("Missing previous utxo on input %d" % idx) values.append(inp.utxo.value) scripts.append(inp.utxo.script_pubkey) return self.sighash_taproot( @@ -950,11 +967,12 @@ def sign_input_with_tapkey( sighash=sighash, ) sig = pk.schnorr_sign(h) - wit = sig.serialize() + sigdata = sig.serialize() if sighash != SIGHASH.DEFAULT: - wit += bytes([sighash]) - # TODO: maybe better to put into internal key sig field - inp.final_scriptwitness = Witness([wit]) + sigdata += bytes([sighash]) + # same fields PSBT._sign_taproot_keypath fills + inp.taproot_key_sig = sigdata + inp.final_scriptwitness = Witness([sigdata]) # no need to sign anything else return 1 counter = 0 @@ -1111,6 +1129,9 @@ def sign_input( if inp.final_scriptwitness: ser_string(sig_stream, b"\x08") ser_string(sig_stream, inp.final_scriptwitness.serialize()) + if inp.taproot_key_sig: + ser_string(sig_stream, b"\x13") + ser_string(sig_stream, inp.taproot_key_sig) for pub, leaf in inp.taproot_sigs: ser_string(sig_stream, b"\x14" + pub.xonly() + leaf) @@ -1148,6 +1169,11 @@ def sign_with(self, root, sig_stream, sighash=SIGHASH.DEFAULT) -> int: Sighash kwarg is set to SIGHASH.DEFAULT, for segwit and legacy it's replaced to SIGHASH.ALL so if PSBT is asking to sign with a different sighash this function won't sign. If you want to sign with sighashes provided in the PSBT - set sighash=None. + + The sig_stream only carries per-input fields. For PSBTv2 the signer + also has to update PSBT_GLOBAL_TX_MODIFIABLE (BIP-370); that update is + kept on this view (tx_modifiable_flags) and emitted by write_to(), so + write the signed PSBT with the same view that produced the signatures. """ counter = 0 for i in range(self.num_inputs): @@ -1186,11 +1212,19 @@ def write_to( # first we write global scope if self._global_kvs is not None: - # PSBTv2: reconstruct global scope from the materialised key-value dict. - # This sidesteps byte-level injection and gives deterministic key ordering. + # PSBTv2: reconstruct global scope from the materialised key-value dict + # in the same order PSBT.write_to uses: xpubs, the BIP-370 fields, + # then the unknown fields as they appeared in the source. writable_stream.write(self.MAGIC) res = len(self.MAGIC) - for k in sorted(self._global_kvs.keys()): + keys = [k for k in self._global_kvs if k[:1] == b"\x01"] + keys += [k for k in _GLOBAL_V2_ORDER if k in self._global_kvs] + keys += [ + k + for k in self._global_kvs + if k[:1] != b"\x01" and k not in _GLOBAL_V2_ORDER + ] + for k in keys: res += ser_string(writable_stream, k) res += ser_string(writable_stream, self._global_kvs[k]) writable_stream.write(b"\x00") # global scope separator diff --git a/tests/tests/test_psbtV2.py b/tests/tests/test_psbtV2.py index 85d89809..e7cf52e7 100644 --- a/tests/tests/test_psbtV2.py +++ b/tests/tests/test_psbtV2.py @@ -914,3 +914,145 @@ def v2(): psbt.outputs[0].value = 2**63 with pytest.raises(PSBTError): psbt.serialize() + + +def to_v2(psbt): + """Rebuilds a parsed PSBTv0 as an equivalent PSBTv2.""" + v2 = PSBT.create_v2(tx_version=psbt.tx.version, fallback_locktime=psbt.locktime) + for inp in psbt.inputs: + v2.add_input(inp) + for out in psbt.outputs: + v2.add_output(out) + v2.tx_modifiable_flags = None + return PSBT.parse(v2.serialize()) + + +def sign_both(raw, root, sighash=SIGHASH.DEFAULT, compress=CompressMode.KEEP_ALL): + """Signs raw with PSBT and PSBTView, returns (psbt, view, psbt bytes, view bytes).""" + psbt = PSBT.parse(raw, compress=compress) + view = PSBTView.view(BytesIO(raw), compress=compress) + n1 = psbt.sign_with(root, sighash) + sigs = BytesIO() + n2 = view.sign_with(root, sigs, sighash) + assert n1 == n2 + sigs.seek(0) + out = BytesIO() + # write_to() applies the view's compress mode to every scope + view.write_to(out, extra_input_streams=[sigs]) + for sc in psbt.inputs + psbt.outputs: + sc.clear_metadata(compress=compress) + return psbt, view, psbt.serialize(), out.getvalue() + + +class TestPSBTViewParity: + """PSBT and PSBTView expose the same values and produce the same bytes""" + + LOCKTIME_VECTORS = [ + # (hex, fallback field, determined locktime) + ( + "70736274ff0102040200000001030400000000010401020105010101fb040200000000010e200f758dbfbd4da7c16c8a3309c3c81e1100f561ea646db5b01752c485e1bdde9f010f04010000000112041027000000010e203a1b3b3c837d6489ea7a31d8e6c7dd503c001bef3e06958e7574808d68ca78a5010f0400000000000103084f9335770000000001041600140b1352cacd03cf6aa1b7f3c8d6388671b34a5e1100", + 0, + 10000, + ), + ( + "70736274ff01020402000000010401010105010201fb040200000000010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f0400000000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300", + None, + 0, + ), + ] + + @pytest.mark.parametrize("hex_data, fallback, determined", LOCKTIME_VECTORS) + def test_locktime_api(self, hex_data, fallback, determined): + raw = unhexlify(hex_data) + psbt = PSBT.parse(raw) + view = PSBTView.view(BytesIO(raw)) + assert psbt.locktime == view.locktime == fallback + assert psbt.determine_locktime() == view.determine_locktime() == determined + assert psbt.tx.locktime == determined + # v0: both are the global tx locktime + raw = a2b_base64(VIEW_PSBTS[0]) + psbt = PSBT.parse(raw) + view = PSBTView.view(BytesIO(raw)) + assert psbt.locktime == view.locktime == psbt.tx.locktime + assert psbt.determine_locktime() == view.determine_locktime() == psbt.locktime + + def test_unknown_and_xpub_globals_keep_order(self): + xpub = SIGNING_ROOT.to_public().serialize() + extra = ( + kv(b"\xf0", b"first") + + kv(b"\x01" + xpub, SIGNING_ROOT.my_fingerprint + bytes(4)) + + kv(b"\x07", b"second") + ) + raw = raw_v2([V2_IN], [V2_OUT], extra_globals=extra) + psbt = PSBT.parse(raw) + assert list(psbt.unknown) == [b"\xf0", b"\x07"] + assert len(psbt.xpubs) == 1 + out = BytesIO() + PSBTView.view(BytesIO(raw)).write_to(out) + assert psbt.serialize() == out.getvalue() + # PSBT and PSBTView agree even when the source order differs from theirs + assert PSBT.parse(out.getvalue()).serialize() == out.getvalue() + + def test_scope_constructor_accepts_v2_unknowns(self): + txid = bytes(range(32)) + inp = InputScope( + {b"\x0e": txid[::-1], b"\x0f": (3).to_bytes(4, "little")}, version=2 + ) + assert inp.txid == txid and inp.vout == 3 and inp.unknown == {} + with pytest.raises(PSBTError): + InputScope({b"\x0e": txid[::-1]}) + out = OutputScope({b"\x03": (5).to_bytes(8, "little")}, version=2) + assert out.value == 5 + with pytest.raises(PSBTError): + OutputScope({b"\x03": (5).to_bytes(8, "little")}) + + @pytest.mark.parametrize("compress", [CompressMode.KEEP_ALL, CompressMode.PARTIAL]) + def test_taproot_keypath_signing_matches(self, compress): + from .test_taproot import TAP_PSBTS, KEY_A + + for raw in [ + a2b_base64(TAP_PSBTS[0]), + to_v2(PSBT.from_string(TAP_PSBTS[0])).serialize(), + ]: + psbt, view, ser1, ser2 = sign_both(raw, KEY_A, compress=compress) + assert psbt.inputs[0].taproot_key_sig is not None + assert ser1 == ser2 + if psbt.version == 2: + assert psbt.tx_modifiable_flags == view.tx_modifiable_flags == 0 + + def test_taproot_scriptpath_signing_matches(self): + from .test_taproot import TAPTREE_PSBT, TAPTREE_KEYS + + for raw in [ + a2b_base64(TAPTREE_PSBT), + to_v2(PSBT.from_string(TAPTREE_PSBT)).serialize(), + ]: + psbt = PSBT.parse(raw) + view = PSBTView.view(BytesIO(raw)) + sigs = BytesIO() + for key in TAPTREE_KEYS: + assert psbt.sign_with(key) == view.sign_with(key, sigs) + # one scope per key per input in the stream; merge them all + sigs.seek(0) + out = BytesIO() + streams = [BytesIO(sigs.getvalue()) for _ in TAPTREE_KEYS] + # each stream must be positioned at its own signing pass + for n, s in enumerate(streams): + for _ in range(n * view.num_inputs): + InputScope.read_from(s, version=view.version) + view.write_to(out, extra_input_streams=streams) + assert psbt.serialize() == out.getvalue() + + def test_missing_utxo_is_psbterror(self): + raw = a2b_base64(VIEW_PSBTS[1]) + psbt = PSBT.parse(raw) + psbt.inputs[1].witness_utxo = None + psbt.inputs[1].non_witness_utxo = None + raw = psbt.serialize() + assert not psbt.inputs[1].is_taproot + with pytest.raises(PSBTError): + PSBT.parse(raw).sign_with(SIGNING_ROOT) + with pytest.raises(PSBTError): + PSBTView.view(BytesIO(raw)).sign_with(SIGNING_ROOT, BytesIO()) + with pytest.raises(PSBTError): + PSBT.parse(raw).fee() From 99b69fc8adc3ff136dd393250eed9aa1f67aa45f Mon Sep 17 00:00:00 2001 From: odudex Date: Wed, 2 Sep 2026 11:11:52 -0300 Subject: [PATCH 12/44] test(psbt): hold PSBT_IN_NON_WITNESS_UTXO to its declared length A declared length that did not match the transaction used to shift the key/value framing of the rest of the input map, so PSBT.parse and PSBTView disagreed on the same bytes. The value is already parsed through _BoundedReader; add regression coverage for a short and a long length in both the streaming (compress) and the full parse. --- src/embit/psbt.py | 3 +++ tests/tests/test_psbtV2.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/embit/psbt.py b/src/embit/psbt.py index c6e69a25..b235b7e0 100644 --- a/src/embit/psbt.py +++ b/src/embit/psbt.py @@ -472,6 +472,9 @@ def read_value(self, stream, k, version=None): elif self.non_witness_utxo is not None: raise PSBTError("Duplicated utxo value") else: + # parse the transaction straight from the stream, but hold it to + # the declared value length so a wrong length cannot shift the + # key/value framing of the rest of the map length = read_compact(stream) value_stream = _BoundedReader(stream, length) # For PSBTv2, PSBT_IN_OUTPUT_INDEX may follow PSBT_IN_NON_WITNESS_UTXO; diff --git a/tests/tests/test_psbtV2.py b/tests/tests/test_psbtV2.py index e7cf52e7..f3e39e21 100644 --- a/tests/tests/test_psbtV2.py +++ b/tests/tests/test_psbtV2.py @@ -1056,3 +1056,31 @@ def test_missing_utxo_is_psbterror(self): PSBTView.view(BytesIO(raw)).sign_with(SIGNING_ROOT, BytesIO()) with pytest.raises(PSBTError): PSBT.parse(raw).fee() + + +class TestNonWitnessUtxoLength: + """The declared PSBT_IN_NON_WITNESS_UTXO length must match the transaction""" + + @pytest.mark.parametrize("delta", [-2, -1, 1, 2, 40]) + @pytest.mark.parametrize("compress", [CompressMode.KEEP_ALL, CompressMode.PARTIAL]) + def test_length_mismatch_rejected(self, delta, compress): + for b64 in [VIEW_PSBTS[0], VIEW_PSBTS[1]]: + psbt = PSBT.from_string(b64) + tx = psbt.inputs[0].non_witness_utxo.serialize() + raw = psbt.serialize() + good = kv(b"\x00", tx) + assert raw.count(good) == 1 + bad = compact.to_bytes(len(tx) + delta) + tx + if delta > 0: + # pad so the value itself is still present, the framing is off + bad += b"\x00" * delta + raw = raw.replace(good, b"\x01" + bad, 1) + with pytest.raises(PSBTError): + PSBT.parse(raw, compress=compress) + # the shifted framing is caught by the scope walk in view() or, + # when the framing happens to line up, by the value parse + with pytest.raises(PSBTError): + view = PSBTView.view(BytesIO(raw), compress=compress) + view.input(0) + view.vin(0) + view.sighash(0) From 12ce2e06c8228914f29be8f3bb5c2db5b9c42dfb Mon Sep 17 00:00:00 2001 From: odudex Date: Wed, 2 Sep 2026 11:16:35 -0300 Subject: [PATCH 13/44] perf(psbtview): hash prevouts, sequences and outputs in one pass per view Every PSBTView.vin(i)/vout(i) for a PSBTv2 seeks from the first scope, so hash_prevouts/hash_sequence were O(N^2) scope skips and sighash_legacy, which walks all inputs for every input it signs, O(N^3). The taproot path re-parsed every full input scope, utxo included, for each signed input in both v0 and v2, and then parsed the signed input once more for its sighash. - _iter_vins()/_iter_vouts() walk the scopes (or the global tx) once; hash_prevouts and hash_sequence share a single walk and sighash_legacy iterates them instead of calling vin(i)/vout(i) in a loop. - _utxo_values_and_scripts() parses each input's utxo once per view and caches the amounts and scriptPubKeys the BIP-341 sighash commits to. - sign_input_with_tapkey passes the already parsed scope to sighash() instead of parsing it a second time. PSETView keeps going through its full input/output scopes, since Liquid inputs carry issuance data the plain prevout scan does not read. Signing 40 inputs, scope skips / input scope parses, before -> after: p2wpkh v2 3160 / 40 -> 1600 / 40 p2pkh v2 33580 / 40 -> 2380 / 40 p2tr v0 32760 / 1680 -> 780 / 80 p2tr v2 34360 / 1680 -> 820 / 80 Co-authored-by: notTanveer --- src/embit/liquid/psetview.py | 26 ++++++- src/embit/psbtview.py | 144 ++++++++++++++++++++++++++--------- 2 files changed, 130 insertions(+), 40 deletions(-) diff --git a/src/embit/liquid/psetview.py b/src/embit/liquid/psetview.py index e1ab778a..80fdbd5a 100644 --- a/src/embit/liquid/psetview.py +++ b/src/embit/liquid/psetview.py @@ -67,7 +67,19 @@ def vin(self, i): self.stream.seek(self.vin0_offset) for j in range(i): self._skip_input() - return LTransactionInput.read_from(self.stream) + return self._read_vin() + + def _read_vin(self): + try: + return LTransactionInput.read_from(self.stream) + except _PARSE_ERRORS as e: + raise PSBTError("Invalid global transaction input: %s" % e) + + def _read_vout(self): + try: + return LTransactionOutput.read_from(self.stream) + except _PARSE_ERRORS as e: + raise PSBTError("Invalid global transaction output: %s" % e) def _skip_input(self): off = 32 + 4 + 5 @@ -110,7 +122,7 @@ def vout(self, i): while n: self._skip_output() n -= 1 - return LTransactionOutput.read_from(self.stream) + return self._read_vout() class PSETView(PSBTView): @@ -134,6 +146,16 @@ def clear_cache(self): def vin(self, i, compress=None): return self.input(i, True).vin + # Liquid inputs carry issuance data the plain v2 prevout scan lacks, + # so keep going through the full input scope. + def _iter_vins(self): + for i in range(self.num_inputs): + yield self.vin(i) + + def _iter_vouts(self): + for i in range(self.num_outputs): + yield self.vout(i) + def blinded_vin(self, i, compress=None): return self.input(i, compress).blinded_vin diff --git a/src/embit/psbtview.py b/src/embit/psbtview.py index 25b38862..5a63a0e7 100644 --- a/src/embit/psbtview.py +++ b/src/embit/psbtview.py @@ -222,15 +222,37 @@ def _check_inputs(self): raise PSBTError("Global transaction input has a non-empty scriptSig") self._inputs_checked = True + def _read_vin(self): + try: + return TransactionInput.read_from(self.stream) + except _PARSE_ERRORS as e: + raise PSBTError("Invalid global transaction input: %s" % e) + + def _read_vout(self): + try: + return TransactionOutput.read_from(self.stream) + except _PARSE_ERRORS as e: + raise PSBTError("Invalid global transaction output: %s" % e) + def vin(self, i): if i < 0 or i >= self.num_vin: raise PSBTError("Invalid input index") self._check_inputs() self.stream.seek(self.vin0_offset + self.LEN_VIN * i) - try: - return TransactionInput.read_from(self.stream) - except _PARSE_ERRORS as e: - raise PSBTError("Invalid global transaction input: %s" % e) + return self._read_vin() + + def iter_vin(self): + """Yields every input in one sequential pass over the stream.""" + self._check_inputs() + self.stream.seek(self.vin0_offset) + for _ in range(self.num_vin): + yield self._read_vin() + + def iter_vout(self): + """Yields every output in one sequential pass over the stream.""" + self.stream.seek(self.vout0_offset) + for _ in range(self.num_vout): + yield self._read_vout() def _skip_output(self): """Seeks over one output""" @@ -245,10 +267,7 @@ def vout(self, i): while n: self._skip_output() n -= 1 - try: - return TransactionOutput.read_from(self.stream) - except _PARSE_ERRORS as e: - raise PSBTError("Invalid global transaction output: %s" % e) + return self._read_vout() class PSBTView: @@ -328,6 +347,7 @@ def tx_modifiable_flags(self, value): def clear_cache(self): # cache for digests self._tx_locktime = None + self._utxo_cache = None self._hash_prevouts = None self._hash_sequence = None self._hash_outputs = None @@ -600,6 +620,65 @@ def vin(self, i, compress=None): found = self._scan_scope_values(self._V2_VIN_KEYS, self.PSBTIN_CLS) return self._v2_vin(found, i) + def _iter_vins(self): + """Yields the TransactionInput of every input in one sequential pass, + instead of seeking from the first scope for each of them.""" + if self.tx: + for vin in self.tx.iter_vin(): + yield vin + return + self.seek_to_scope(0) + for i in range(self.num_inputs): + found = self._scan_scope_values(self._V2_VIN_KEYS, self.PSBTIN_CLS) + yield self._v2_vin(found, i) + + def _iter_vouts(self): + """Yields the TransactionOutput of every output in one sequential pass.""" + if self.tx: + for vout in self.tx.iter_vout(): + yield vout + return + self.seek_to_scope(self.num_inputs) + for i in range(self.num_outputs): + found = self._scan_scope_values(self._V2_VOUT_KEYS, self.PSBTOUT_CLS) + yield self._v2_vout(found, i) + + def _utxo_values_and_scripts(self): + """(values, script_pubkeys) of every input's utxo, parsed once per view. + + Taproot sighashes commit to all of them, so without the cache every + signed input re-parsed every input scope.""" + if self._utxo_cache is None: + values = [] + scripts = [] + # walk the input scopes sequentially when the stream can tell() + # where a parsed scope ended; otherwise seek to each one + sequential = hasattr(self.stream, "tell") + off = self.seek_to_scope(0) if sequential else None + for i in range(self.num_inputs): + if sequential: + # tx.vin(i) moves the cursor, so restore it before parsing + vin = self.tx.vin(i) if self.tx else None + self.stream.seek(off) + # parse with the view's mode: it decides whether utxo is + # the witness utxo or the non-witness output, and the + # amounts signed must be the ones input(i) shows + inp = self.PSBTIN_CLS.read_from( + self.stream, + vin=vin, + compress=self.compress, + version=self.version, + ) + off = self.stream.seek(0, 1) + else: + inp = self.input(i) + if inp.utxo is None: + raise PSBTError("Missing previous utxo on input %d" % i) + values.append(inp.utxo.value) + scripts.append(inp.utxo.script_pubkey) + self._utxo_cache = (values, scripts) + return self._utxo_cache + # compress is not used here, but may be used by subclasses (liquid) def vout(self, i, compress=None): if i < 0 or i >= self.num_outputs: @@ -717,30 +796,31 @@ def get_value(self, key_start, from_current=False): if off: return read_string(self.stream) + def _hash_prevouts_and_sequence(self): + """Both digests need the same walk over the inputs, so do it once.""" + hp = hashlib.sha256() + hs = hashlib.sha256() + for inp in self._iter_vins(): + hp.update(bytes(reversed(inp.txid))) + hp.update(inp.vout.to_bytes(4, "little")) + hs.update(inp.sequence.to_bytes(4, "little")) + self._hash_prevouts = hp.digest() + self._hash_sequence = hs.digest() + def hash_prevouts(self): if self._hash_prevouts is None: - h = hashlib.sha256() - for i in range(self.num_inputs): - inp = self.vin(i) - h.update(bytes(reversed(inp.txid))) - h.update(inp.vout.to_bytes(4, "little")) - self._hash_prevouts = h.digest() + self._hash_prevouts_and_sequence() return self._hash_prevouts def hash_sequence(self): if self._hash_sequence is None: - h = hashlib.sha256() - for i in range(self.num_inputs): - inp = self.vin(i) - h.update(inp.sequence.to_bytes(4, "little")) - self._hash_sequence = h.digest() + self._hash_prevouts_and_sequence() return self._hash_sequence def hash_outputs(self): if self._hash_outputs is None: h = hashlib.sha256() - for i in range(self.num_outputs): - out = self.vout(i) + for out in self._iter_vouts(): h.update(out.serialize()) self._hash_outputs = h.digest() return self._hash_outputs @@ -871,8 +951,7 @@ def sighash_legacy(self, input_index, script_pubkey, sighash=SIGHASH.ALL): h.update(self.vin(input_index).serialize(script_pubkey)) else: h.update(compact.to_bytes(self.num_inputs)) - for i in range(self.num_inputs): - inp = self.vin(i) + for i, inp in enumerate(self._iter_vins()): if input_index == i: h.update(inp.serialize(script_pubkey)) else: @@ -891,8 +970,7 @@ def sighash_legacy(self, input_index, script_pubkey, sighash=SIGHASH.ALL): h.update(self.vout(input_index).serialize()) elif sh == SIGHASH.ALL: h.update(compact.to_bytes(self.num_outputs)) - for i in range(self.num_outputs): - out = self.vout(i) + for out in self._iter_vouts(): h.update(out.serialize()) else: # shouldn't happen @@ -907,15 +985,7 @@ def sighash(self, i, sighash=SIGHASH.ALL, input_scope=None, **kwargs): raise PSBTError("Missing previous utxo on input %d" % i) if inp.is_taproot: - values = [] - scripts = [] - # TODO: not very optimal. Maybe use build_cache() or something? - for idx in range(self.num_inputs): - inp = self.input(idx) - if inp.utxo is None: - raise PSBTError("Missing previous utxo on input %d" % idx) - values.append(inp.utxo.value) - scripts.append(inp.utxo.script_pubkey) + values, scripts = self._utxo_values_and_scripts() return self.sighash_taproot( i, script_pubkeys=scripts, @@ -962,10 +1032,7 @@ def sign_input_with_tapkey( # check if key is internal key pk = key.taproot_tweak(inp.taproot_merkle_root or b"") if pk.xonly() in inp.utxo.script_pubkey.data: - h = self.sighash( - input_index, - sighash=sighash, - ) + h = self.sighash(input_index, sighash=sighash, input_scope=inp) sig = pk.schnorr_sign(h) sigdata = sig.serialize() if sighash != SIGHASH.DEFAULT: @@ -987,6 +1054,7 @@ def sign_input_with_tapkey( h = self.sighash( input_index, sighash=sighash, + input_scope=inp, ext_flag=1, script=script, leaf_version=leaf_version, From 4a36c622bd44ac615490cf4d00d535b71c092f0d Mon Sep 17 00:00:00 2001 From: odudex Date: Wed, 2 Sep 2026 11:18:42 -0300 Subject: [PATCH 14/44] test(psbt): run the BIP-370 vectors through PSBTView and compare signers by bytes - Every BIP-370 vector in test_psbtV2 now goes through both PSBT.parse and PSBTView.view: invalid ones must raise PSBTError from both, valid ones must round-trip byte for byte through PSBT.serialize() and PSBTView.write_to(), agree on version, counts, tx_version, locktime and the reconstructed unsigned transaction. - PSBT_GLOBAL_TX_MODIFIABLE is asserted after a real sign_with() for every sighash type, when the field is absent, when nothing is signed and for v0, in PSBT and PSBTView alike. - Legacy and segwit inputs are signed as PSBTv2 and must produce the same partial signatures as the v0 original; a required height locktime must be what the sighash commits to. - test_psbtview compares the PSBT and PSBTView outputs by bytes, not length, so field and ordering differences are caught. - The bit-flip fuzz test steps through every byte of the small vector and every third byte of the larger ones to keep the suite fast. --- tests/tests/test_psbt.py | 11 +- tests/tests/test_psbtV2.py | 255 +++++++++++++++++++++++++---------- tests/tests/test_psbtview.py | 2 +- 3 files changed, 190 insertions(+), 78 deletions(-) diff --git a/tests/tests/test_psbt.py b/tests/tests/test_psbt.py index 2f7a26d7..d78300bf 100644 --- a/tests/tests/test_psbt.py +++ b/tests/tests/test_psbt.py @@ -66,6 +66,11 @@ ] +SIGN_XKEY = "tprv8ZgxMBicQKsPd9TeAdPADNnSyH9SSUUbTVeFszDE23Ki6TBB5nCefAdHkK8Fm3qMQR6sHwA56zqRmKmxnHk37JkiFzvncDqoKmPWubu7hDF" +# 1 segwit and 1 legacy input, signable with SIGN_XKEY +SIGN_PSBT_HEX = "70736274ff01009a020000000258e87a21b56daf0c23be8e7070456c336f7cbaa5c8757924f545887bb2abdd750000000000ffffffff838d0427d0ec650a68aa46bb0b098aea4422c071b2ca78352a077959d07cea1d0100000000ffffffff0270aaf00800000000160014d85c2b71d0060b09c9886aeb815e50991dda124d00e1f5050000000016001400aea9a2e5f0f876a588df5546e8742d1d87008f00000000000100bb0200000001aad73931018bd25f84ae400b68848be09db706eac2ac18298babee71ab656f8b0000000048473044022058f6fc7c6a33e1b31548d481c826c015bd30135aad42cd67790dab66d2ad243b02204a1ced2604c6735b6393e5b41691dd78b00f0c5942fb9f751856faa938157dba01feffffff0280f0fa020000000017a9140fb9463421696b82c833af241c78c17ddbde493487d0f20a270100000017a91429ca74f8a08f81999428185c97b5d852e4063f618765000000010304010000000104475221029583bf39ae0a609747ad199addd634fa6108559d6c5cd39b4c2183f1ab96e07f2102dab61ff49a14db6a7d02b0cd1fbb78fc4b18312b5b4e54dae4dba2fbfef536d752ae2206029583bf39ae0a609747ad199addd634fa6108559d6c5cd39b4c2183f1ab96e07f10d90c6a4f000000800000008000000080220602dab61ff49a14db6a7d02b0cd1fbb78fc4b18312b5b4e54dae4dba2fbfef536d710d90c6a4f0000008000000080010000800001012000c2eb0b0000000017a914b7f5faf40e3d40a5a459b1db3535f2b72fa921e8870103040100000001042200208c2353173743b595dfb4a07b72ba8e42e3797da74e87fe7d9d7497e3b2028903010547522103089dc10c7ac6db54f91329af617333db388cead0c231f723379d1b99030b02dc21023add904f3d6dcf59ddb906b0dee23529b7ffb9ed50e5e86151926860221f0e7352ae2206023add904f3d6dcf59ddb906b0dee23529b7ffb9ed50e5e86151926860221f0e7310d90c6a4f000000800000008003000080220603089dc10c7ac6db54f91329af617333db388cead0c231f723379d1b99030b02dc10d90c6a4f00000080000000800200008000220203a9a4c37f5996d3aa25dbac6b570af0650394492942460b354753ed9eeca5877110d90c6a4f000000800000008004000080002202027f6399757d2eff55a136ad02c684b1838b6556e5f1b6b34282a94b6b5005109610d90c6a4f00000080000000800500008000" + + class PSBTTest(TestCase): def test_invalid_psbts(self): for psbt_str in INVALID_VECTORS: @@ -88,10 +93,8 @@ def test_valid_psbts(self): def test_sign(self): """Parses a PSBT, signs both inputs (1 segwit and 1 legacy), and verifies each signature is correct""" - xkey = HDKey.from_base58( - "tprv8ZgxMBicQKsPd9TeAdPADNnSyH9SSUUbTVeFszDE23Ki6TBB5nCefAdHkK8Fm3qMQR6sHwA56zqRmKmxnHk37JkiFzvncDqoKmPWubu7hDF" - ) - psbt_str = "70736274ff01009a020000000258e87a21b56daf0c23be8e7070456c336f7cbaa5c8757924f545887bb2abdd750000000000ffffffff838d0427d0ec650a68aa46bb0b098aea4422c071b2ca78352a077959d07cea1d0100000000ffffffff0270aaf00800000000160014d85c2b71d0060b09c9886aeb815e50991dda124d00e1f5050000000016001400aea9a2e5f0f876a588df5546e8742d1d87008f00000000000100bb0200000001aad73931018bd25f84ae400b68848be09db706eac2ac18298babee71ab656f8b0000000048473044022058f6fc7c6a33e1b31548d481c826c015bd30135aad42cd67790dab66d2ad243b02204a1ced2604c6735b6393e5b41691dd78b00f0c5942fb9f751856faa938157dba01feffffff0280f0fa020000000017a9140fb9463421696b82c833af241c78c17ddbde493487d0f20a270100000017a91429ca74f8a08f81999428185c97b5d852e4063f618765000000010304010000000104475221029583bf39ae0a609747ad199addd634fa6108559d6c5cd39b4c2183f1ab96e07f2102dab61ff49a14db6a7d02b0cd1fbb78fc4b18312b5b4e54dae4dba2fbfef536d752ae2206029583bf39ae0a609747ad199addd634fa6108559d6c5cd39b4c2183f1ab96e07f10d90c6a4f000000800000008000000080220602dab61ff49a14db6a7d02b0cd1fbb78fc4b18312b5b4e54dae4dba2fbfef536d710d90c6a4f0000008000000080010000800001012000c2eb0b0000000017a914b7f5faf40e3d40a5a459b1db3535f2b72fa921e8870103040100000001042200208c2353173743b595dfb4a07b72ba8e42e3797da74e87fe7d9d7497e3b2028903010547522103089dc10c7ac6db54f91329af617333db388cead0c231f723379d1b99030b02dc21023add904f3d6dcf59ddb906b0dee23529b7ffb9ed50e5e86151926860221f0e7352ae2206023add904f3d6dcf59ddb906b0dee23529b7ffb9ed50e5e86151926860221f0e7310d90c6a4f000000800000008003000080220603089dc10c7ac6db54f91329af617333db388cead0c231f723379d1b99030b02dc10d90c6a4f00000080000000800200008000220203a9a4c37f5996d3aa25dbac6b570af0650394492942460b354753ed9eeca5877110d90c6a4f000000800000008004000080002202027f6399757d2eff55a136ad02c684b1838b6556e5f1b6b34282a94b6b5005109610d90c6a4f00000080000000800500008000" + xkey = HDKey.from_base58(SIGN_XKEY) + psbt_str = SIGN_PSBT_HEX exp_partial_sigs = [ { "029583bf39ae0a609747ad199addd634fa6108559d6c5cd39b4c2183f1ab96e07f": "3044022074018ad4180097b873323c0015720b3684cc8123891048e7dbcd9b55ad679c99022073d369b740e3eb53dcefa33823c8070514ca55a7dd9544f157c167913261118c01", diff --git a/tests/tests/test_psbtV2.py b/tests/tests/test_psbtV2.py index f3e39e21..09ec73fa 100644 --- a/tests/tests/test_psbtV2.py +++ b/tests/tests/test_psbtV2.py @@ -24,6 +24,50 @@ from embit.transaction import SIGHASH, Transaction, TransactionInput, TransactionOutput +def assert_invalid(hex_data): + """Both parsers must reject the vector with PSBTError.""" + raw = unhexlify(hex_data) + with pytest.raises(PSBTError): + PSBT.parse(raw) + with pytest.raises(PSBTError): + _exercise_view(raw) + + +def parse_valid(hex_data): + """Both parsers must accept the vector, agree, and round-trip it byte for byte.""" + raw = unhexlify(hex_data) + psbt = PSBT.parse(raw) + assert psbt.serialize() == raw + view = _exercise_view(raw) + assert psbt.version == view.version + assert len(psbt.inputs) == view.num_inputs + assert len(psbt.outputs) == view.num_outputs + assert psbt.tx_version == view.tx_version + assert psbt.locktime == view.locktime + try: + tx = psbt.tx + except PSBTError: + # uncomputable locktime: a valid PSBT that no signer can use yet + with pytest.raises(PSBTError): + view.determine_locktime() + else: + assert tx.serialize() == view_tx(view).serialize() + out = BytesIO() + view.write_to(out) + assert out.getvalue() == raw + return psbt + + +def view_tx(view): + """The unsigned transaction as PSBTView sees it.""" + return Transaction( + version=view.tx_version, + vin=[view.vin(i) for i in range(view.num_inputs)], + vout=[view.vout(i) for i in range(view.num_outputs)], + locktime=view.determine_locktime(), + ) + + class TestPSBTVectors: """Test vectors for PSBT parsing and validation""" @@ -31,176 +75,152 @@ def test_invalid_psbtv0_with_version_field(self): """PSBTv0 but with PSBT_GLOBAL_VERSION set to 2 should be invalid""" hex_data = "70736274ff01007102000000010b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc80000000000feffffff020008af2f00000000160014c430f64c4756da310dbd1a085572ef299926272c8bbdeb0b00000000160014a07dac8ab6ca942d379ed795f835ba71c9cc68850000000001fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e01086b02473044022005275a485734e0ae1f3b971237586f0e72dc85833d278c0e474cd23112c0fa5e02206b048c83cebc3c41d0b93cc7da76185cedbd030d005b08018be2b98bbacbdf7b012103760dcca05f3997dc65b293060f7f29f1514c8c527048e12802b041d4fc340a2700220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a000000002202036efe2c255621986553ba9d65c3ddc64165ca1436e05aa35a4c6eb02451cf796d18f69d873e540000800100008000000080010000006200000000" - with pytest.raises(PSBTError): - PSBT.parse(unhexlify(hex_data)) + assert_invalid(hex_data) def test_invalid_psbtv0_with_tx_version(self): """PSBTv0 but with PSBT_GLOBAL_TX_VERSION should be invalid""" hex_data = "70736274ff01007102000000010b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc80000000000feffffff020008af2f00000000160014c430f64c4756da310dbd1a085572ef299926272c8bbdeb0b00000000160014a07dac8ab6ca942d379ed795f835ba71c9cc68850000000001020402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e01086b02473044022005275a485734e0ae1f3b971237586f0e72dc85833d278c0e474cd23112c0fa5e02206b048c83cebc3c41d0b93cc7da76185cedbd030d005b08018be2b98bbacbdf7b012103760dcca05f3997dc65b293060f7f29f1514c8c527048e12802b041d4fc340a2700220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a000000002202036efe2c255621986553ba9d65c3ddc64165ca1436e05aa35a4c6eb02451cf796d18f69d873e540000800100008000000080010000006200000000" - with pytest.raises(PSBTError): - PSBT.parse(unhexlify(hex_data)) + assert_invalid(hex_data) def test_invalid_psbtv0_with_fallback_locktime(self): """PSBTv0 but with PSBT_GLOBAL_FALLBACK_LOCKTIME should be invalid""" hex_data = "70736274ff01007102000000010b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc80000000000feffffff020008af2f00000000160014c430f64c4756da310dbd1a085572ef299926272c8bbdeb0b00000000160014a07dac8ab6ca942d379ed795f835ba71c9cc68850000000001030402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e01086b02473044022005275a485734e0ae1f3b971237586f0e72dc85833d278c0e474cd23112c0fa5e02206b048c83cebc3c41d0b93cc7da76185cedbd030d005b08018be2b98bbacbdf7b012103760dcca05f3997dc65b293060f7f29f1514c8c527048e12802b041d4fc340a2700220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a000000002202036efe2c255621986553ba9d65c3ddc64165ca1436e05aa35a4c6eb02451cf796d18f69d873e540000800100008000000080010000006200000000" - with pytest.raises(PSBTError): - PSBT.parse(unhexlify(hex_data)) + assert_invalid(hex_data) def test_invalid_psbtv0_with_input_count(self): """PSBTv0 but with PSBT_GLOBAL_INPUT_COUNT should be invalid""" hex_data = "70736274ff01007102000000010b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc80000000000feffffff020008af2f00000000160014c430f64c4756da310dbd1a085572ef299926272c8bbdeb0b00000000160014a07dac8ab6ca942d379ed795f835ba71c9cc68850000000001040102000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e01086b02473044022005275a485734e0ae1f3b971237586f0e72dc85833d278c0e474cd23112c0fa5e02206b048c83cebc3c41d0b93cc7da76185cedbd030d005b08018be2b98bbacbdf7b012103760dcca05f3997dc65b293060f7f29f1514c8c527048e12802b041d4fc340a2700220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a000000002202036efe2c255621986553ba9d65c3ddc64165ca1436e05aa35a4c6eb02451cf796d18f69d873e540000800100008000000080010000006200000000" - with pytest.raises(PSBTError): - PSBT.parse(unhexlify(hex_data)) + assert_invalid(hex_data) def test_invalid_psbtv0_with_output_count(self): """PSBTv0 but with PSBT_GLOBAL_OUTPUT_COUNT should be invalid""" hex_data = "70736274ff01007102000000010b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc80000000000feffffff020008af2f00000000160014c430f64c4756da310dbd1a085572ef299926272c8bbdeb0b00000000160014a07dac8ab6ca942d379ed795f835ba71c9cc68850000000001050102000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e01086b02473044022005275a485734e0ae1f3b971237586f0e72dc85833d278c0e474cd23112c0fa5e02206b048c83cebc3c41d0b93cc7da76185cedbd030d005b08018be2b98bbacbdf7b012103760dcca05f3997dc65b293060f7f29f1514c8c527048e12802b041d4fc340a2700220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a000000002202036efe2c255621986553ba9d65c3ddc64165ca1436e05aa35a4c6eb02451cf796d18f69d873e540000800100008000000080010000006200000000" - with pytest.raises(PSBTError): - PSBT.parse(unhexlify(hex_data)) + assert_invalid(hex_data) def test_invalid_psbtv0_with_tx_modifiable(self): """PSBTv0 but with PSBT_GLOBAL_TX_MODIFIABLE should be invalid""" hex_data = "70736274ff01007102000000010b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc80000000000feffffff020008af2f00000000160014c430f64c4756da310dbd1a085572ef299926272c8bbdeb0b00000000160014a07dac8ab6ca942d379ed795f835ba71c9cc68850000000001060100000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e01086b02473044022005275a485734e0ae1f3b971237586f0e72dc85833d278c0e474cd23112c0fa5e02206b048c83cebc3c41d0b93cc7da76185cedbd030d005b08018be2b98bbacbdf7b012103760dcca05f3997dc65b293060f7f29f1514c8c527048e12802b041d4fc340a2700220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a000000002202036efe2c255621986553ba9d65c3ddc64165ca1436e05aa35a4c6eb02451cf796d18f69d873e540000800100008000000080010000006200000000" - with pytest.raises(PSBTError): - PSBT.parse(unhexlify(hex_data)) + assert_invalid(hex_data) def test_invalid_psbtv0_with_previous_txid(self): """PSBTv0 but with PSBT_IN_PREVIOUS_TXID should be invalid""" hex_data = "70736274ff01007102000000010b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc80000000000feffffff020008af2f00000000160014c430f64c4756da310dbd1a085572ef299926272c8bbdeb0b00000000160014a07dac8ab6ca942d379ed795f835ba71c9cc688500000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e01086b02473044022005275a485734e0ae1f3b971237586f0e72dc85833d278c0e474cd23112c0fa5e02206b048c83cebc3c41d0b93cc7da76185cedbd030d005b08018be2b98bbacbdf7b012103760dcca05f3997dc65b293060f7f29f1514c8c527048e12802b041d4fc340a27010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc800220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a000000002202036efe2c255621986553ba9d65c3ddc64165ca1436e05aa35a4c6eb02451cf796d18f69d873e540000800100008000000080010000006200000000" - with pytest.raises(PSBTError): - PSBT.parse(unhexlify(hex_data)) + assert_invalid(hex_data) def test_invalid_psbtv0_with_output_index(self): """PSBTv0 but with PSBT_IN_OUTPUT_INDEX should be invalid""" hex_data = "70736274ff01007102000000010b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc80000000000feffffff020008af2f00000000160014c430f64c4756da310dbd1a085572ef299926272c8bbdeb0b00000000160014a07dac8ab6ca942d379ed795f835ba71c9cc688500000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e01086b02473044022005275a485734e0ae1f3b971237586f0e72dc85833d278c0e474cd23112c0fa5e02206b048c83cebc3c41d0b93cc7da76185cedbd030d005b08018be2b98bbacbdf7b012103760dcca05f3997dc65b293060f7f29f1514c8c527048e12802b041d4fc340a27010f040000000000220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a000000002202036efe2c255621986553ba9d65c3ddc64165ca1436e05aa35a4c6eb02451cf796d18f69d873e540000800100008000000080010000006200000000" - with pytest.raises(PSBTError): - PSBT.parse(unhexlify(hex_data)) + assert_invalid(hex_data) def test_invalid_psbtv0_with_sequence(self): """PSBTv0 but with PSBT_IN_SEQUENCE should be invalid""" hex_data = "70736274ff01007102000000010b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc80000000000feffffff020008af2f00000000160014c430f64c4756da310dbd1a085572ef299926272c8bbdeb0b00000000160014a07dac8ab6ca942d379ed795f835ba71c9cc688500000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e01086b02473044022005275a485734e0ae1f3b971237586f0e72dc85833d278c0e474cd23112c0fa5e02206b048c83cebc3c41d0b93cc7da76185cedbd030d005b08018be2b98bbacbdf7b012103760dcca05f3997dc65b293060f7f29f1514c8c527048e12802b041d4fc340a27011004ffffffff00220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a000000002202036efe2c255621986553ba9d65c3ddc64165ca1436e05aa35a4c6eb02451cf796d18f69d873e540000800100008000000080010000006200000000" - with pytest.raises(PSBTError): - PSBT.parse(unhexlify(hex_data)) + assert_invalid(hex_data) def test_invalid_psbtv0_with_required_time_locktime(self): """PSBTv0 but with PSBT_IN_REQUIRED_TIME_LOCKTIME should be invalid""" hex_data = "70736274ff01007102000000010b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc80000000000feffffff020008af2f00000000160014c430f64c4756da310dbd1a085572ef299926272c8bbdeb0b00000000160014a07dac8ab6ca942d379ed795f835ba71c9cc688500000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e01086b02473044022005275a485734e0ae1f3b971237586f0e72dc85833d278c0e474cd23112c0fa5e02206b048c83cebc3c41d0b93cc7da76185cedbd030d005b08018be2b98bbacbdf7b012103760dcca05f3997dc65b293060f7f29f1514c8c527048e12802b041d4fc340a270111048c8dc46200220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a000000002202036efe2c255621986553ba9d65c3ddc64165ca1436e05aa35a4c6eb02451cf796d18f69d873e540000800100008000000080010000006200000000" - with pytest.raises(PSBTError): - PSBT.parse(unhexlify(hex_data)) + assert_invalid(hex_data) def test_invalid_psbtv0_with_required_height_locktime(self): """PSBTv0 but with PSBT_IN_REQUIRED_HEIGHT_LOCKTIME should be invalid""" hex_data = "70736274ff01007102000000010b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc80000000000feffffff020008af2f00000000160014c430f64c4756da310dbd1a085572ef299926272c8bbdeb0b00000000160014a07dac8ab6ca942d379ed795f835ba71c9cc688500000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e01086b02473044022005275a485734e0ae1f3b971237586f0e72dc85833d278c0e474cd23112c0fa5e02206b048c83cebc3c41d0b93cc7da76185cedbd030d005b08018be2b98bbacbdf7b012103760dcca05f3997dc65b293060f7f29f1514c8c527048e12802b041d4fc340a270112041027000000220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a000000002202036efe2c255621986553ba9d65c3ddc64165ca1436e05aa35a4c6eb02451cf796d18f69d873e540000800100008000000080010000006200000000" - with pytest.raises(PSBTError): - PSBT.parse(unhexlify(hex_data)) + assert_invalid(hex_data) def test_invalid_psbtv0_with_output_amount(self): """PSBTv0 but with PSBT_OUT_AMOUNT should be invalid""" hex_data = "70736274ff01007102000000010b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc80000000000feffffff020008af2f00000000160014c430f64c4756da310dbd1a085572ef299926272c8bbdeb0b00000000160014a07dac8ab6ca942d379ed795f835ba71c9cc688500000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e01086b02473044022005275a485734e0ae1f3b971237586f0e72dc85833d278c0e474cd23112c0fa5e02206b048c83cebc3c41d0b93cc7da76185cedbd030d005b08018be2b98bbacbdf7b012103760dcca05f3997dc65b293060f7f29f1514c8c527048e12802b041d4fc340a2700220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f00000000002202036efe2c255621986553ba9d65c3ddc64165ca1436e05aa35a4c6eb02451cf796d18f69d873e540000800100008000000080010000006200000000" - with pytest.raises(PSBTError): - PSBT.parse(unhexlify(hex_data)) + assert_invalid(hex_data) def test_invalid_psbtv0_with_output_script(self): """PSBTv0 but with PSBT_OUT_SCRIPT should be invalid""" hex_data = "70736274ff01007102000000010b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc80000000000feffffff020008af2f00000000160014c430f64c4756da310dbd1a085572ef299926272c8bbdeb0b00000000160014a07dac8ab6ca942d379ed795f835ba71c9cc688500000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e01086b02473044022005275a485734e0ae1f3b971237586f0e72dc85833d278c0e474cd23112c0fa5e02206b048c83cebc3c41d0b93cc7da76185cedbd030d005b08018be2b98bbacbdf7b012103760dcca05f3997dc65b293060f7f29f1514c8c527048e12802b041d4fc340a2700220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000104160014a07dac8ab6ca942d379ed795f835ba71c9cc6885002202036efe2c255621986553ba9d65c3ddc64165ca1436e05aa35a4c6eb02451cf796d18f69d873e540000800100008000000080010000006200000000" - with pytest.raises(PSBTError): - PSBT.parse(unhexlify(hex_data)) + assert_invalid(hex_data) def test_invalid_psbtv2_global_unsigned_tx(self): """PSBTv2 with PSBT_GLOBAL_UNSIGNED_TX should be invalid""" hex_data = "70736274ff0100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e00000000010204020000000103040000000001040101010501020106010701fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f0400000000011004feffffff0111048c8dc4620112041027000000220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c00220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" - with pytest.raises(PSBTError): - PSBT.parse(unhexlify(hex_data)) + assert_invalid(hex_data) def test_invalid_psbtv2_missing_input_count(self): """PSBTv2 missing PSBT_GLOBAL_INPUT_COUNT should be invalid""" hex_data = "70736274ff01020402000000010304000000000105010201fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f0400000000011004feffffff00220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c00220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" - with pytest.raises(PSBTError): - PSBT.parse(unhexlify(hex_data)) + assert_invalid(hex_data) def test_invalid_psbtv2_missing_output_count(self): """PSBTv2 missing PSBT_GLOBAL_OUTPUT_COUNT should be invalid""" hex_data = "70736274ff01020402000000010304000000000104010101fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f0400000000011004feffffff00220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c00220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" - with pytest.raises(PSBTError): - PSBT.parse(unhexlify(hex_data)) + assert_invalid(hex_data) def test_invalid_psbtv2_missing_tx_version(self): """PSBTv2 missing PSBT_GLOBAL_TX_VERSION should be invalid""" hex_data = "70736274ff010401010105010201fb040200000000010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f0400000000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" - with pytest.raises(PSBTError): - PSBT.parse(unhexlify(hex_data)) + assert_invalid(hex_data) def test_invalid_psbtv2_missing_previous_txid(self): """PSBTv2 input missing PSBT_IN_PREVIOUS_TXID should be invalid""" hex_data = "70736274ff0102040200000001030400000000010401010105010201fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010f0400000000011004feffffff00220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c00220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" - with pytest.raises(PSBTError): - PSBT.parse(unhexlify(hex_data)) + assert_invalid(hex_data) def test_invalid_psbtv2_missing_output_index(self): """PSBTv2 input missing PSBT_IN_OUTPUT_INDEX should be invalid""" hex_data = "70736274ff0102040200000001030400000000010401010105010201fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8011004feffffff00220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c00220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" - with pytest.raises(PSBTError): - PSBT.parse(unhexlify(hex_data)) + assert_invalid(hex_data) def test_invalid_psbtv2_missing_output_amount(self): """PSBTv2 output missing PSBT_OUT_AMOUNT should be invalid""" hex_data = "70736274ff0102040200000001030400000000010401010105010201fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f0400000000011004feffffff00220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000104160014c430f64c4756da310dbd1a085572ef299926272c00220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" - with pytest.raises(PSBTError): - PSBT.parse(unhexlify(hex_data)) + assert_invalid(hex_data) def test_invalid_psbtv2_missing_output_script(self): """PSBTv2 output missing PSBT_OUT_SCRIPT should be invalid""" hex_data = "70736274ff0102040200000001030400000000010401010105010201fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f0400000000011004feffffff00220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f0000000000220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" - with pytest.raises(PSBTError): - PSBT.parse(unhexlify(hex_data)) + assert_invalid(hex_data) def test_invalid_psbtv2_invalid_time_locktime(self): """PSBTv2 with PSBT_IN_REQUIRED_TIME_LOCKTIME less than 500000000 should be invalid""" hex_data = "70736274ff01020402000000010401010105010201fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f0400000000011104ff64cd1d00220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c00220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" - with pytest.raises(PSBTError): - PSBT.parse(unhexlify(hex_data)) + assert_invalid(hex_data) def test_invalid_psbtv2_invalid_height_locktime(self): """PSBTv2 with PSBT_IN_REQUIRED_HEIGHT_LOCKTIME >= 500000000 should be invalid""" hex_data = "70736274ff01020402000000010401010105010201fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f04000000000112040065cd1d00220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c00220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" - with pytest.raises(PSBTError): - PSBT.parse(unhexlify(hex_data)) + assert_invalid(hex_data) def test_invalid_psbtv2_height_locktime_zero(self): """PSBTv2 with PSBT_IN_REQUIRED_HEIGHT_LOCKTIME == 0 should be invalid""" hex_data = "70736274ff010204020000000103040000000001040101010501020106010701fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f0400000000011004feffffff0111048c8dc4620112040000000000220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c00220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" - with pytest.raises(PSBTError): - PSBT.parse(unhexlify(hex_data)) + assert_invalid(hex_data) # Valid PSBTs def test_valid_psbtv2_minimal(self): """1 input, 2 output PSBTv2, required fields only should be valid""" hex_data = "70736274ff01020402000000010401010105010201fb040200000000010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f0400000000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" - psbt = PSBT.parse(unhexlify(hex_data)) + psbt = parse_valid(hex_data) assert psbt.version == 2 assert len(psbt.inputs) == 1 assert len(psbt.outputs) == 2 @@ -209,7 +229,7 @@ def test_valid_psbtv2_updated(self): """1 input, 2 output updated PSBTv2 should be valid""" hex_data = "70736274ff01020402000000010401010105010201fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f040000000000220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c00220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" - psbt = PSBT.parse(unhexlify(hex_data)) + psbt = parse_valid(hex_data) assert psbt.version == 2 assert len(psbt.inputs) == 1 assert len(psbt.outputs) == 2 @@ -218,7 +238,7 @@ def test_valid_psbtv2_with_sequence(self): """1 input, 2 output PSBTv2 with sequence should be valid""" hex_data = "70736274ff01020402000000010401010105010201fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f0400000000011004feffffff00220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c00220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" - psbt = PSBT.parse(unhexlify(hex_data)) + psbt = parse_valid(hex_data) assert psbt.version == 2 assert psbt.inputs[0].sequence == 0xFFFFFFFE @@ -226,7 +246,7 @@ def test_valid_psbtv2_with_sequence_and_locktime(self): """PSBTv2 with all possible fields should be valid, including sequence""" hex_data = "70736274ff0102040200000001030400000000010401010105010201fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f0400000000011004feffffff0111048c8dc4620112041027000000220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c00220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" - psbt = PSBT.parse(unhexlify(hex_data)) + psbt = parse_valid(hex_data) assert psbt.version == 2 assert psbt.inputs[0].sequence == 0xFFFFFFFE assert psbt.inputs[0].required_time_locktime == 1657048460 @@ -236,7 +256,7 @@ def test_inputs_modifiable_flag(self): """Inputs Modifiable Flag (bit 0) only""" hex_data = "70736274ff0102040200000001040101010501020106010101fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f040000000000220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c00220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" - psbt = PSBT.parse(unhexlify(hex_data)) + psbt = parse_valid(hex_data) assert psbt.version == 2 assert len(psbt.inputs) == 1 assert len(psbt.outputs) == 2 @@ -249,7 +269,7 @@ def test_outputs_modifiable_flag(self): """Outputs Modifiable Flag (bit 1) only""" hex_data = "70736274ff0102040200000001040101010501020106010201fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f040000000000220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c00220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" - psbt = PSBT.parse(unhexlify(hex_data)) + psbt = parse_valid(hex_data) assert psbt.version == 2 assert len(psbt.inputs) == 1 assert len(psbt.outputs) == 2 @@ -262,7 +282,7 @@ def test_sighash_single_flag(self): """Has SIGHASH_SINGLE Flag (bit 2) only""" hex_data = "70736274ff0102040200000001040101010501020106010401fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f040000000000220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c00220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" - psbt = PSBT.parse(unhexlify(hex_data)) + psbt = parse_valid(hex_data) assert psbt.version == 2 assert len(psbt.inputs) == 1 assert len(psbt.outputs) == 2 @@ -275,7 +295,7 @@ def test_undefined_flag_bit3(self): """Undefined flag (bit 3) — parser must not reject it""" hex_data = "70736274ff0102040200000001040101010501020106010801fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f040000000000220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c00220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" - psbt = PSBT.parse(unhexlify(hex_data)) + psbt = parse_valid(hex_data) assert psbt.version == 2 assert len(psbt.inputs) == 1 assert len(psbt.outputs) == 2 @@ -289,7 +309,7 @@ def test_inputs_and_outputs_modifiable(self): """Inputs Modifiable (bit 0) + Outputs Modifiable (bit 1)""" hex_data = "70736274ff0102040200000001040101010501020106010301fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f040000000000220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c00220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" - psbt = PSBT.parse(unhexlify(hex_data)) + psbt = parse_valid(hex_data) assert psbt.version == 2 assert len(psbt.inputs) == 1 assert len(psbt.outputs) == 2 @@ -302,7 +322,7 @@ def test_inputs_modifiable_and_sighash_single(self): """Inputs Modifiable (bit 0) + Has SIGHASH_SINGLE (bit 2)""" hex_data = "70736274ff0102040200000001040101010501020106010501fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f040000000000220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c00220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" - psbt = PSBT.parse(unhexlify(hex_data)) + psbt = parse_valid(hex_data) assert psbt.version == 2 assert len(psbt.inputs) == 1 assert len(psbt.outputs) == 2 @@ -315,7 +335,7 @@ def test_outputs_modifiable_and_sighash_single(self): """Outputs Modifiable (bit 1) + Has SIGHASH_SINGLE (bit 2)""" hex_data = "70736274ff0102040200000001040101010501020106010601fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f040000000000220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c00220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" - psbt = PSBT.parse(unhexlify(hex_data)) + psbt = parse_valid(hex_data) assert psbt.version == 2 assert len(psbt.inputs) == 1 assert len(psbt.outputs) == 2 @@ -328,7 +348,7 @@ def test_all_defined_flags(self): """All defined flags set: Inputs (bit 0) + Outputs (bit 1) + SIGHASH_SINGLE (bit 2)""" hex_data = "70736274ff0102040200000001040101010501020106010701fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f040000000000220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c00220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" - psbt = PSBT.parse(unhexlify(hex_data)) + psbt = parse_valid(hex_data) assert psbt.version == 2 assert len(psbt.inputs) == 1 assert len(psbt.outputs) == 2 @@ -341,7 +361,7 @@ def test_all_possible_flags(self): """All 8 bits set (0xFF) — defined + undefined flags""" hex_data = "70736274ff010204020000000104010101050102010601ff01fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f040000000000220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c00220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" - psbt = PSBT.parse(unhexlify(hex_data)) + psbt = parse_valid(hex_data) assert psbt.version == 2 assert len(psbt.inputs) == 1 assert len(psbt.outputs) == 2 @@ -358,7 +378,7 @@ def test_valid_psbtv2_with_all_fields(self): """PSBTv2 with all possible fields should be valid""" hex_data = "70736274ff010204020000000103040000000001040101010501020106010701fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f0400000000011004feffffff0111048c8dc4620112041027000000220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c00220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" - psbt = PSBT.parse(unhexlify(hex_data)) + psbt = parse_valid(hex_data) assert psbt.version == 2 assert psbt.inputs[0].required_time_locktime == 1657048460 assert psbt.inputs[0].required_height_locktime == 10000 @@ -371,28 +391,28 @@ def test_locktime_no_locktimes_specified(self): """No locktimes specified should return 0""" hex_data = "70736274ff01020402000000010401010105010201fb040200000000010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f0400000000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" - psbt = PSBT.parse(unhexlify(hex_data)) + psbt = parse_valid(hex_data) assert psbt.determine_locktime() == 0 def test_locktime_fallback_zero(self): """Fallback locktime of 0 should return 0""" hex_data = "70736274ff0102040200000001030400000000010401020105010101fb040200000000010e200f758dbfbd4da7c16c8a3309c3c81e1100f561ea646db5b01752c485e1bdde9f010f040100000000010e203a1b3b3c837d6489ea7a31d8e6c7dd503c001bef3e06958e7574808d68ca78a5010f0400000000000103084f9335770000000001041600140b1352cacd03cf6aa1b7f3c8d6388671b34a5e1100" - psbt = PSBT.parse(unhexlify(hex_data)) + psbt = parse_valid(hex_data) assert psbt.determine_locktime() == 0 def test_locktime_height_10000(self): """Height locktime of 10000 should return 10000""" hex_data = "70736274ff0102040200000001030400000000010401020105010101fb040200000000010e200f758dbfbd4da7c16c8a3309c3c81e1100f561ea646db5b01752c485e1bdde9f010f04010000000112041027000000010e203a1b3b3c837d6489ea7a31d8e6c7dd503c001bef3e06958e7574808d68ca78a5010f0400000000000103084f9335770000000001041600140b1352cacd03cf6aa1b7f3c8d6388671b34a5e1100" - psbt = PSBT.parse(unhexlify(hex_data)) + psbt = parse_valid(hex_data) assert psbt.determine_locktime() == 10000 def test_locktime_max_height(self): """Multiple height locktimes should return maximum""" hex_data = "70736274ff0102040200000001030400000000010401020105010101fb040200000000010e200f758dbfbd4da7c16c8a3309c3c81e1100f561ea646db5b01752c485e1bdde9f010f04010000000112041027000000010e203a1b3b3c837d6489ea7a31d8e6c7dd503c001bef3e06958e7574808d68ca78a5010f040000000001120428230000000103084f9335770000000001041600140b1352cacd03cf6aa1b7f3c8d6388671b34a5e1100" - psbt = PSBT.parse(unhexlify(hex_data)) + psbt = parse_valid(hex_data) assert len(psbt.inputs) == 2 assert psbt.determine_locktime() == 10000 # max(10000, 9000) @@ -400,7 +420,7 @@ def test_locktime_height_and_time(self): """1 input with Height and other with height and time locktimes should return max height value""" hex_data = "70736274ff0102040200000001030400000000010401020105010101fb040200000000010e200f758dbfbd4da7c16c8a3309c3c81e1100f561ea646db5b01752c485e1bdde9f010f04010000000112041027000000010e203a1b3b3c837d6489ea7a31d8e6c7dd503c001bef3e06958e7574808d68ca78a5010f04000000000111048c8dc46201120428230000000103084f9335770000000001041600140b1352cacd03cf6aa1b7f3c8d6388671b34a5e1100" - psbt = PSBT.parse(unhexlify(hex_data)) + psbt = parse_valid(hex_data) assert len(psbt.inputs) == 2 assert psbt.determine_locktime() == 10000 @@ -408,7 +428,7 @@ def test_locktime_height_preferred_over_time(self): """Height locktime should be preferred when both present (BIP-370: height preferred on tie)""" hex_data = "70736274ff0102040200000001030400000000010401020105010101fb040200000000010e200f758dbfbd4da7c16c8a3309c3c81e1100f561ea646db5b01752c485e1bdde9f010f04010000000111048b8dc4620112041027000000010e203a1b3b3c837d6489ea7a31d8e6c7dd503c001bef3e06958e7574808d68ca78a5010f04000000000111048c8dc46201120428230000000103084f9335770000000001041600140b1352cacd03cf6aa1b7f3c8d6388671b34a5e1100" - psbt = PSBT.parse(unhexlify(hex_data)) + psbt = parse_valid(hex_data) assert len(psbt.inputs) == 2 assert psbt.determine_locktime() == 10000 # Height takes priority over time @@ -416,7 +436,7 @@ def test_locktime_time_only(self): """Time locktime only should return time value""" hex_data = "70736274ff0102040200000001030400000000010401020105010101fb040200000000010e200f758dbfbd4da7c16c8a3309c3c81e1100f561ea646db5b01752c485e1bdde9f010f04010000000111048b8dc46200010e203a1b3b3c837d6489ea7a31d8e6c7dd503c001bef3e06958e7574808d68ca78a5010f04000000000111048c8dc46201120428230000000103084f9335770000000001041600140b1352cacd03cf6aa1b7f3c8d6388671b34a5e1100" - psbt = PSBT.parse(unhexlify(hex_data)) + psbt = parse_valid(hex_data) assert len(psbt.inputs) == 2 assert psbt.determine_locktime() == 1657048460 @@ -424,7 +444,7 @@ def test_locktime_max_time(self): """Multiple time locktimes should return maximum""" hex_data = "70736274ff0102040200000001030400000000010401020105010101fb040200000000010e200f758dbfbd4da7c16c8a3309c3c81e1100f561ea646db5b01752c485e1bdde9f010f040100000000010e203a1b3b3c837d6489ea7a31d8e6c7dd503c001bef3e06958e7574808d68ca78a5010f04000000000111048c8dc462000103084f9335770000000001041600140b1352cacd03cf6aa1b7f3c8d6388671b34a5e1100" - psbt = PSBT.parse(unhexlify(hex_data)) + psbt = parse_valid(hex_data) assert len(psbt.inputs) == 2 assert psbt.determine_locktime() == 1657048460 @@ -432,7 +452,7 @@ def test_locktime_time_wins_when_not_all_support_height(self): """Input 1 has height 10000 and time 1657048459, Input 2 has time 1657048460 only should return max time 1657048460""" hex_data = "70736274ff0102040200000001030400000000010401020105010101fb040200000000010e200f758dbfbd4da7c16c8a3309c3c81e1100f561ea646db5b01752c485e1bdde9f010f04010000000111048b8dc4620112041027000000010e203a1b3b3c837d6489ea7a31d8e6c7dd503c001bef3e06958e7574808d68ca78a5010f04000000000111048c8dc462000103084f9335770000000001041600140b1352cacd03cf6aa1b7f3c8d6388671b34a5e1100" - psbt = PSBT.parse(unhexlify(hex_data)) + psbt = parse_valid(hex_data) assert len(psbt.inputs) == 2 assert psbt.determine_locktime() == 1657048460 @@ -440,7 +460,7 @@ def test_locktime_uncomputable(self): """Input with height only and input with time only should raise PSBTError""" hex_data = "70736274ff0102040200000001030400000000010401020105010101fb040200000000010e200f758dbfbd4da7c16c8a3309c3c81e1100f561ea646db5b01752c485e1bdde9f010f04010000000112041027000000010e203a1b3b3c837d6489ea7a31d8e6c7dd503c001bef3e06958e7574808d68ca78a5010f04000000000111048c8dc462000103084f9335770000000001041600140b1352cacd03cf6aa1b7f3c8d6388671b34a5e1100" - psbt = PSBT.parse(unhexlify(hex_data)) + psbt = parse_valid(hex_data) assert len(psbt.inputs) == 2 with pytest.raises(PSBTError): psbt.determine_locktime() @@ -791,7 +811,8 @@ def test_truncated_input(self, compress): @pytest.mark.parametrize("compress", [CompressMode.KEEP_ALL, CompressMode.PARTIAL]) def test_corrupted_byte(self, compress): for raw in self.SAMPLES: - for n in range(len(raw)): + # every byte of the small vector, every third of the larger ones + for n in range(0, len(raw), 1 if len(raw) < 600 else 3): flipped = raw[:n] + bytes([raw[n] ^ 0xFF]) + raw[n + 1 :] try: PSBT.parse(flipped, compress=compress) @@ -1084,3 +1105,91 @@ def test_length_mismatch_rejected(self, delta, compress): view.input(0) view.vin(0) view.sighash(0) + + +class TestTxModifiableAfterSigning: + """PSBT_GLOBAL_TX_MODIFIABLE after a real sign_with, in PSBT and PSBTView""" + + @pytest.mark.parametrize( + "sighash, expected", + [ + (SIGHASH.ALL, 0), + (SIGHASH.ALL | SIGHASH.ANYONECANPAY, TxModifiable.INPUTS), + (SIGHASH.NONE, TxModifiable.OUTPUTS), + ( + SIGHASH.NONE | SIGHASH.ANYONECANPAY, + TxModifiable.INPUTS | TxModifiable.OUTPUTS, + ), + (SIGHASH.SINGLE, TxModifiable.SIGHASH_SINGLE), + ( + SIGHASH.SINGLE | SIGHASH.ANYONECANPAY, + TxModifiable.INPUTS | TxModifiable.SIGHASH_SINGLE, + ), + ], + ) + def test_flags_after_signing(self, sighash, expected): + psbt = PSBT.from_string(SIGNABLE_V2_B64) + psbt.tx_modifiable_flags = TxModifiable.INPUTS | TxModifiable.OUTPUTS + raw = psbt.serialize() + psbt, view, ser1, ser2 = sign_both(raw, SIGNING_ROOT, sighash) + assert psbt.tx_modifiable_flags == expected + assert view.tx_modifiable_flags == expected + assert PSBT.parse(ser2).tx_modifiable_flags == expected + assert ser1 == ser2 + + def test_absent_field_is_set_by_signer(self): + psbt, view, ser1, ser2 = sign_both(a2b_base64(SIGNABLE_V2_B64), SIGNING_ROOT) + assert psbt.tx_modifiable_flags == view.tx_modifiable_flags == 0 + assert ser1 == ser2 + + def test_untouched_when_nothing_signed(self): + psbt = PSBT.from_string(SIGNABLE_V2_B64) + raw = psbt.serialize() + other = SIGNING_ROOT.derive([1]) + psbt, view, ser1, ser2 = sign_both(raw, other) + assert psbt.tx_modifiable_flags is None + assert view.tx_modifiable_flags is None + assert ser1 == ser2 == raw + + def test_v0_never_gets_the_field(self): + raw = a2b_base64(VIEW_PSBTS[0]) + psbt, view, ser1, ser2 = sign_both(raw, SIGNING_ROOT) + assert psbt.tx_modifiable_flags is None + assert view.tx_modifiable_flags is None + assert ser1 == ser2 + + +class TestPSBTv2Signing: + """Legacy, segwit and taproot inputs sign the same in v0 and v2""" + + @pytest.mark.parametrize("compress", [CompressMode.KEEP_ALL, CompressMode.PARTIAL]) + def test_legacy_and_segwit(self, compress): + from .test_psbt import SIGN_PSBT_HEX, SIGN_XKEY + from embit.bip32 import HDKey + + xkey = HDKey.from_base58(SIGN_XKEY) + v0 = PSBT.parse(unhexlify(SIGN_PSBT_HEX)) + raw2 = to_v2(v0).serialize() + v0.sign_with(xkey) + assert all(inp.partial_sigs for inp in v0.inputs) + psbt, view, ser1, ser2 = sign_both(raw2, xkey, compress=compress) + assert ser1 == ser2 + for a, b in zip(v0.inputs, psbt.inputs): + assert a.partial_sigs == b.partial_sigs + # the finalized transaction is the same one + assert psbt.tx.txid() == v0.tx.txid() + + def test_v2_sighash_uses_derived_locktime(self): + v0 = PSBT.from_string(SIGNABLE_V2_B64) + v0.inputs[0].required_height_locktime = 200 + raw = v0.serialize() + psbt, view, ser1, ser2 = sign_both(raw, SIGNING_ROOT) + assert ser1 == ser2 + assert psbt.tx.locktime == 200 + # signing a copy with the locktime baked into the fallback field and + # no per-input requirement yields the same signatures + v0 = PSBT.from_string(SIGNABLE_V2_B64) + v0.locktime = 200 + v0.sign_with(SIGNING_ROOT) + for a, b in zip(v0.inputs, psbt.inputs): + assert a.partial_sigs == b.partial_sigs diff --git a/tests/tests/test_psbtview.py b/tests/tests/test_psbtview.py index 6eac6f88..5c7ab7b8 100644 --- a/tests/tests/test_psbtview.py +++ b/tests/tests/test_psbtview.py @@ -132,7 +132,7 @@ def test_sign(self): psbtv.write_to(ser, extra_input_streams=[sigs_stream2]) for sc in psbt.inputs + psbt.outputs: sc.clear_metadata(compress=compress) - self.assertEqual(len(ser.getvalue()), len(psbt.serialize())) + self.assertEqual(ser.getvalue(), psbt.serialize()) # check compress reduces the size of psbt if compress != CompressMode.CLEAR_ALL: sigs_stream2.seek(0) From 1d7166df58332710046b220e4a42801f7e755f84 Mon Sep 17 00:00:00 2001 From: odudex Date: Wed, 2 Sep 2026 11:18:42 -0300 Subject: [PATCH 15/44] changelog: PSBT hardening follow-ups --- CHANGELOG.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c67e8042..0f241589 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,10 +14,36 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 unsigned transaction reconstruction, and `PSBT_GLOBAL_TX_MODIFIABLE` updates on signing. +### Changed + +- `PSBTView.locktime` now has the same meaning as `PSBT.locktime` (the raw + fallback field for PSBTv2); the derived transaction locktime is + `PSBTView.determine_locktime()`. +- `PSBTView.view()` walks every input and output map once and rejects a PSBT + with trailing data or fewer maps than the global counts declare. +- `compact.read_from` rejects non-canonical and truncated compact-size + encodings, as Bitcoin Core does. +- `PSBTView` hashes prevouts, sequences and outputs in a single pass and + caches the per-input utxo data used by taproot sighashes. + ### Fixed - Validate PSBTv2 count fields canonically and raise `PSBTError` instead of `RuntimeError`/`ValueError` on malformed values in both parse paths. +- `PSBTView` matched PSBTv2 prevout and output fields by key prefix, so a + keyed or duplicated field could feed a wrong value into the sighash. +- Every malformed PSBT now raises `PSBTError` from both `PSBT` and + `PSBTView`, including truncated input, corrupted nested transactions, + keys and scripts, and out-of-range values on serialization. +- `PSBT_IN_NON_WITNESS_UTXO` is held to its declared length. +- `PSET` signed PSETv2 with the fallback locktime instead of the BIP-370 + derived one, and rewrote a zero input sequence to `0xffffffff`. +- `PSBT` and `PSBTView` now write the PSBTv2 global map in the same order and + both store `PSBT_IN_TAP_KEY_SIG` for taproot key-path signatures. +- `InputScope`/`OutputScope` accept PSBTv2 fields through the `unknown` + constructor argument with `version=2`. +- Signing an input without a utxo raises `PSBTError` instead of + `AttributeError`. ## [0.8.2] - 2026-08-08 From 76bbf90fcb415db546b16c7dbef2686d7b2ca711 Mon Sep 17 00:00:00 2001 From: odudex Date: Wed, 2 Sep 2026 11:20:41 -0300 Subject: [PATCH 16/44] chore(psbtview): keep the global-tx bound check and scope walk mypy-clean --- src/embit/psbtview.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/embit/psbtview.py b/src/embit/psbtview.py index 5a63a0e7..db657e1f 100644 --- a/src/embit/psbtview.py +++ b/src/embit/psbtview.py @@ -405,7 +405,7 @@ def view(cls, stream, offset=None, compress=CompressMode.KEEP_ALL): raise PSBTError("Invalid global transaction") # every input takes at least 41 bytes and every output 9, # so the counts must fit in the declared transaction length - if tx.vout0_offset + 9 * num_outputs > tx_offset + tx_len: + if tx.vout0_offset + 9 * num_outputs > cur + tx_len: raise PSBTError("Invalid global transaction") # seek to the end of transaction try: @@ -465,7 +465,7 @@ def view(cls, stream, offset=None, compress=CompressMode.KEEP_ALL): # Walk every input and output map once, reading only the key/value # lengths: the declared counts must match the maps actually present # and nothing may follow the last one, like PSBT.parse enforces. - res.seek_to_scope(num_inputs + num_outputs) + res.seek_to_scope(res.num_inputs + res.num_outputs) if len(stream.read(1)) > 0: raise PSBTError("Unexpected extra bytes") return res From d972fc557cdeba07f18af925613edb713ea3af64 Mon Sep 17 00:00:00 2001 From: odudex Date: Wed, 2 Sep 2026 11:50:44 -0300 Subject: [PATCH 17/44] fix(psbt): close gaps found reviewing the hardening series - psetview imported PSBTError-wrapping helpers through `from ..psbtview import *`, which skips underscored names, so GlobalLTransactionView's `except _PARSE_ERRORS` raised NameError on the first corrupted global transaction instead of PSBTError. Import it explicitly. - Cover a PSBT_IN_NON_WITNESS_UTXO declared 1-4 bytes short: a bounded reader serving short reads would let Transaction.read_from finish on a truncated transaction and hand the cut bytes to the next key, so with a crafted unknown key PSBT.parse accepted the input with locktime 0 while the PARTIAL parse rejected it. _BoundedReader makes a read past the declared length a PSBTError. - PSET._validate_v2_output requires PSBT_ELEMENTS_OUT_ASSET or its commitment; without either the output could be parsed but hashing or extracting it raised TypeError from LTransactionOutput.write_to. - PSBTView._scan_scope_values rejected duplicates only of the keys it was asked for, so a lone sign_input(0) signed a PSBT whose other input repeated e.g. PSBT_IN_WITNESS_UTXO, which PSBT.parse rejects. Every repeated key in the map is now rejected. Co-authored-by: notTanveer --- src/embit/liquid/pset.py | 18 ++++++++++++ src/embit/liquid/psetview.py | 1 + src/embit/psbtview.py | 14 +++++---- tests/tests/test_liquid.py | 29 +++++++++++++++++++ tests/tests/test_psbtV2.py | 55 ++++++++++++++++++++++++++++++++++++ 5 files changed, 112 insertions(+), 5 deletions(-) diff --git a/src/embit/liquid/pset.py b/src/embit/liquid/pset.py index cd17f209..7e5c319c 100644 --- a/src/embit/liquid/pset.py +++ b/src/embit/liquid/pset.py @@ -508,6 +508,24 @@ def _v2_output_has_amount(cls, out): """PSET allows value_commitment as an alternative to value (blinded outputs).""" return out.value is not None or out.value_commitment is not None + @classmethod + def _validate_v2_output(cls, out, i): + # not super(): MicroPython < 1.24 binds cls to type in that call + if not cls._v2_output_has_amount(out): + raise PSBTError( + "PSBTv2 output %d missing required PSBT_OUT_AMOUNT (0x03)" % i + ) + if out.script_pubkey is None: + raise PSBTError( + "PSBTv2 output %d missing required PSBT_OUT_SCRIPT (0x04)" % i + ) + # LOutputScope.vout serializes asset_commitment or asset; without + # either the output cannot be hashed or extracted + if out.asset is None and out.asset_commitment is None: + raise PSBTError( + "PSETv2 output %d missing required PSBT_ELEMENTS_OUT_ASSET" % i + ) + def unblind(self, blinding_key): for inp in self.inputs: inp.unblind(blinding_key) diff --git a/src/embit/liquid/psetview.py b/src/embit/liquid/psetview.py index 80fdbd5a..3362d151 100644 --- a/src/embit/liquid/psetview.py +++ b/src/embit/liquid/psetview.py @@ -1,5 +1,6 @@ from ..psbtview import * from .pset import * +from ..psbt import _PARSE_ERRORS import hashlib diff --git a/src/embit/psbtview.py b/src/embit/psbtview.py index db657e1f..169e1395 100644 --- a/src/embit/psbtview.py +++ b/src/embit/psbtview.py @@ -489,10 +489,11 @@ def _scan_scope_values(self, keys, scope_cls=None): Keys are matched exactly, so a keyless BIP-370 field carrying keydata (e.g. 0x0e||xx) never stands in for the real one, a duplicate of a - requested key is rejected, and when scope_cls is given every key in the - scope is checked with its _validate_key() - the same checks the full - scope parser applies.""" + key in the map is rejected, and when scope_cls is given every key in + the scope is checked with its _validate_key() - the same checks the + full scope parser applies.""" found = {} + seen = set() while True: key = read_string(self.stream) # separator - end of scope @@ -500,9 +501,12 @@ def _scan_scope_values(self, keys, scope_cls=None): return found if scope_cls is not None: scope_cls._validate_key(key) + # PSBT maps must not repeat a key; the scope parser rejects that + # for every key, so do the same here + if key in seen: + raise PSBTError("Duplicated key: %s" % hexlify(key).decode()) + seen.add(key) if key in keys: - if key in found: - raise PSBTError("Duplicated key: %s" % hexlify(key).decode()) found[key] = read_string(self.stream) else: skip_string(self.stream) diff --git a/tests/tests/test_liquid.py b/tests/tests/test_liquid.py index c4b9c963..1d6c80a3 100644 --- a/tests/tests/test_liquid.py +++ b/tests/tests/test_liquid.py @@ -3,6 +3,7 @@ from embit.util import secp256k1 from binascii import unhexlify from embit.liquid.pset import PSET, LInputScope +from embit.psbt import PSBTError from embit.liquid.psetview import PSETView from io import BytesIO from embit.liquid.transaction import LTransaction @@ -31,6 +32,8 @@ SIGN_TPRV = "tprv8ZgxMBicQKsPf27gmh4DbQqN2K6xnXA7m7AeceqQVGkRYny3X49sgcufzbJcq4k5eaGZDMijccdDzvQga2Saqd78dKqN52QwLyqgY8apX3j" +# unsigned PSETv0 with one input +PSET_V0_B64 = "cHNldP8BAOUCAAAAAAGuRyPZXPN6wSbiWqD1H2SPAc71iny/ypyV8WCEVan99wAAAAAA/f///wMBbVIcOOweoVc0riK3xGBkQSgpwNBXnwpxPRwE7el5Am8BAAAAAAAAFX4AFgAU0f9GbzioopmlUxwIEtw2A7bBoqcBbVIcOOweoVc0riK3xGBkQSgpwNBXnwpxPRwE7el5Am8BAAAAAAAAB9AAF6kUhbF7AcbaCecOBk/Gxt9QgPuv5WmHAW1SHDjsHqFXNK4it8RgZEEoKcDQV58KcT0cBO3peQJvAQAAAAAAAAD5AAAAAAAAAAEBegoqZjByOhLSsE8bxsVLenapedhf/F6WMSVZzaoBDjblOAlSBNqO+LXzdyamQjl5k1HwTxTXTd3QLwyzkkQ/8T9niAJMPezwMquHK0Odb63wj5PaR47dnbGnucPWb8mFS8J4OBYAFNH/Rm84qKKZpVMcCBLcNgO2waKnIgYDj4XtoWwNAcJbWUu77jhntiKOc30ej6kuRE9kkuHtxTgYXMlGDVQAAIAAAACAAAAAgAAAAAABAAAAC/wIZWxlbWVudHMACEceAAAAAAAAC/wIZWxlbWVudHMBICPcWgmTLwN0EzYgqtgN0xN0wE4UmJ4bno3ORIivJklUC/wIZWxlbWVudHMCIG1SHDjsHqFXNK4it8RgZEEoKcDQV58KcT0cBO3peQJvC/wIZWxlbWVudHMDIOsb0DNA8Zt98kSd2TPa4Fd8Oi+CbLFybitdRzizP+qWACICA4+F7aFsDQHCW1lLu+44Z7Y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Rkos5AVV7Np+RK6FMsIriFhlEwadmxw++i7WEFHnShvcu6ANh4jDSLE9huhpIMMUv9nMpOGXxgdGOJaAM3jrCkrmutyNeJ/7i6d7qomlY1rt7nZTZ9AsLQqn19sOqgxm/OrCVwJRD9dBxvlpI0cGo0n7kPSQ74gD24AawSukdh6CYtxIKjo19BRIz4R9aJBktUgxsDPrvb3LxCd0s0prRvoqs/KhPZh+RmmlEoFbXXtrQZNt3CbKFa91shs89zqEeoOoUTw2udiekZhf47tORf5NfLiim31ssglpFShlHch4RCYRvvB0s9bfnZZzpMRyXwivR07CSTSkZEQYr3ZaQrv+8Ttv6iJWosJEAEQDLkEeR9JxuTNB/wEcHNldAVDAQABpo4NtvKS8tTm+XBSMxVgJ9n0/PyoNzYupvwN/jTxowK7wpU9wq3zG8ZjXuNP9iBDA16miq7iyqE/agp1CQmujwf8BHBzZXQGIQO76oHHphQNAVUtYNp3LMyLbHDhacNdtHz6sq+P0SLD6Qf8BHBzZXQHIQPZdPeFpD4dsI+NNMLI3lyrpSyc+MFBJgY1S/B2IJJxHwf8BHBzZXQIBAAAAAAAAQQAAQMIGQAAAAAAAAAH/ARwc2V0AiAYaWzyPjIJpuhEedAfE/55yQE1auU5B8yILP/T4mQBIwf8BHBzZXQIBAAAAAAA" @@ -133,6 +136,32 @@ def test_v2_locktime_and_sequence(self): signed = LInputScope.read_from(sigs, version=2) self.assertEqual(signed.partial_sigs, pset.inputs[0].partial_sigs) + def test_v2_output_requires_asset(self): + pset = PSET.from_string(SIGN_PSET_V2_B64) + pset.outputs[0].asset = None + raw = pset.serialize() + with self.assertRaises(PSBTError): + PSET.parse(raw) + view = PSETView.view(BytesIO(raw)) + with self.assertRaises(PSBTError): + view.output(0) + with self.assertRaises(PSBTError): + view.hash_outputs() + + def test_corrupted_global_transaction_is_psbterror(self): + pset = PSET.from_string(PSET_V0_B64) + raw = pset.serialize() + # oversize the scriptPubKey length of a global tx output + spk = max((out.script_pubkey.serialize() for out in pset.tx.vout), key=len) + i = raw.index(spk) + corrupt = raw[:i] + b"\xff" + raw[i + 1 :] + with self.assertRaises(PSBTError): + PSET.parse(corrupt) + with self.assertRaises(PSBTError): + view = PSETView.view(BytesIO(corrupt)) + view.vout(view.num_outputs - 1) + view.hash_outputs() + def test_slip77(self): seed = b"5" * 32 mbk = slip77.master_blinding_from_seed(seed) diff --git a/tests/tests/test_psbtV2.py b/tests/tests/test_psbtV2.py index 09ec73fa..4719b97e 100644 --- a/tests/tests/test_psbtV2.py +++ b/tests/tests/test_psbtV2.py @@ -1193,3 +1193,58 @@ def test_v2_sighash_uses_derived_locktime(self): v0.sign_with(SIGNING_ROOT) for a, b in zip(v0.inputs, psbt.inputs): assert a.partial_sigs == b.partial_sigs + + +class TestNonWitnessUtxoResync: + def test_short_length_cannot_resync_on_a_following_key(self): + """A utxo length one byte short must not let the trailing locktime byte + be re-read as the next key length and land on a valid framing.""" + from embit.psbt import ser_string + + spk = Script(unhexlify("76a914" + "22" * 20 + "88ac")) + prev = Transaction( + version=1, + vin=[TransactionInput(b"\x11" * 32, 0)], + vout=[TransactionOutput(100000, spk)], + locktime=0x32000000, + ) + trunc = Transaction(version=1, vin=prev.vin, vout=prev.vout, locktime=0) + raw_tx = prev.serialize() + assert raw_tx[-1] == 0x32 + s = BytesIO() + ser_string(s, b"\x00") + s.write(compact.to_bytes(len(raw_tx) - 1)) # declared one short + s.write(raw_tx) + # an unknown entry the leftover 0x32 byte swallows as a 50-byte key + s.write(b"\x30" + b"\x30" + b"K" * 47 + b"\x01" + b"\x00") + ser_string(s, b"\x0e") + ser_string(s, bytes(reversed(trunc.txid()))) + ser_string(s, b"\x0f") + ser_string(s, (0).to_bytes(4, "little")) + raw = raw_v2([s.getvalue()], [V2_OUT]) + for compress in [CompressMode.KEEP_ALL, CompressMode.PARTIAL]: + with pytest.raises(PSBTError): + PSBT.parse(raw, compress=compress) + with pytest.raises(PSBTError): + view = PSBTView.view(BytesIO(raw), compress=compress) + view.input(0) + view.vin(0) + + +class TestViewDuplicateKeys: + def test_duplicate_non_prevout_key_rejected_when_hashing(self): + """A duplicated key in another input must fail even when only one + input is signed, like PSBT.parse does.""" + psbt = PSBT.from_string(SIGNABLE_V2_B64) + raw_inputs = [inp.serialize(version=2)[:-1] for inp in psbt.inputs] + raw_outputs = [out.serialize(version=2)[:-1] for out in psbt.outputs] + dup = kv(b"\x01", psbt.inputs[1].witness_utxo.serialize()) + raw_inputs[1] = dup + raw_inputs[1] + raw = raw_v2(raw_inputs, raw_outputs) + with pytest.raises(PSBTError): + PSBT.parse(raw) + view = PSBTView.view(BytesIO(raw)) + sigs = BytesIO() + with pytest.raises(PSBTError): + view.sign_input(0, SIGNING_ROOT, sigs) + assert sigs.getvalue() == b"" From 3cd810c32220cc97154dfbc8728cfa1a11645f51 Mon Sep 17 00:00:00 2001 From: odudex Date: Wed, 2 Sep 2026 11:52:08 -0300 Subject: [PATCH 18/44] fix(transaction): compute BIP-341 sighashes correctly for ANYONECANPAY and SINGLE Two deviations from the BIP-341 SigMsg, present in both Transaction and PSBTView.sighash_taproot, reported in #65 and fixed the same way as #66: - with ANYONECANPAY the message hashed the whole serialized input (41 bytes, scriptSig length included) instead of the 36-byte outpoint; - with SIGHASH_SINGLE it hashed the corresponding output itself instead of sha_single_output, its SHA256. Any taproot input signed with a non-default sighash therefore produced a signature over the wrong message. SINGLE with no corresponding output now raises instead of an IndexError. Every hash type in the BIP-341 wallet test vectors (0x00, 0x01, 0x02, 0x03, 0x81, 0x82, 0x83) is now checked against Transaction, PSBT and PSBTView, in PSBTv0 and PSBTv2. Co-authored-by: Marcel Hernandez --- src/embit/psbtview.py | 9 +++-- src/embit/transaction.py | 9 +++-- tests/tests/test_psbtV2.py | 74 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 4 deletions(-) diff --git a/src/embit/psbtview.py b/src/embit/psbtview.py index 169e1395..3cbbb823 100644 --- a/src/embit/psbtview.py +++ b/src/embit/psbtview.py @@ -873,7 +873,9 @@ def sighash_taproot( h.update(bytes([2 * ext_flag + int(annex is not None)])) if anyonecanpay: vin = self.vin(input_index) - h.update(vin.serialize()) + # BIP-341: outpoint (36 bytes), not the whole serialized input + h.update(bytes(reversed(vin.txid))) + h.update(vin.vout.to_bytes(4, "little")) h.update(values[input_index].to_bytes(8, "little")) h.update(script_pubkeys[input_index].serialize()) h.update(vin.sequence.to_bytes(4, "little")) @@ -882,7 +884,10 @@ def sighash_taproot( if annex is not None: h.update(hashes.sha256(compact.to_bytes(len(annex)) + annex)) if sh == SIGHASH.SINGLE: - h.update(self.vout(input_index).serialize()) + if input_index >= self.num_outputs: + raise PSBTError("No corresponding output for SIGHASH_SINGLE") + # BIP-341: sha_single_output, the SHA256 of the output + h.update(hashes.sha256(self.vout(input_index).serialize())) if script is not None: h.update( hashes.tagged_hash( diff --git a/src/embit/transaction.py b/src/embit/transaction.py index 2301021f..8061dfc2 100644 --- a/src/embit/transaction.py +++ b/src/embit/transaction.py @@ -240,7 +240,9 @@ def sighash_taproot( # data about this input h.update(bytes([2 * ext_flag + int(annex is not None)])) if anyonecanpay: - h.update(self.vin[input_index].serialize()) + # BIP-341: outpoint (36 bytes), not the whole serialized input + h.update(bytes(reversed(self.vin[input_index].txid))) + h.update(self.vin[input_index].vout.to_bytes(4, "little")) h.update(values[input_index].to_bytes(8, "little")) h.update(script_pubkeys[input_index].serialize()) h.update(self.vin[input_index].sequence.to_bytes(4, "little")) @@ -249,7 +251,10 @@ def sighash_taproot( if annex is not None: h.update(hashes.sha256(compact.to_bytes(len(annex)) + annex)) if sh == SIGHASH.SINGLE: - h.update(self.vout[input_index].serialize()) + if input_index >= len(self.vout): + raise TransactionError("No corresponding output for SIGHASH_SINGLE") + # BIP-341: sha_single_output, the SHA256 of the output + h.update(hashes.sha256(self.vout[input_index].serialize())) if script is not None: h.update( hashes.tagged_hash( diff --git a/tests/tests/test_psbtV2.py b/tests/tests/test_psbtV2.py index 4719b97e..f0dfa65e 100644 --- a/tests/tests/test_psbtV2.py +++ b/tests/tests/test_psbtV2.py @@ -17,6 +17,7 @@ from embit.psbtview import PSBTView from embit.script import Script from embit import compact +from embit.base import EmbitError from .test_psbtview import PSBTS as VIEW_PSBTS, ROOT as SIGNING_ROOT # native segwit single key, 3 inputs 4 outputs, PSBTv2, signable with SIGNING_ROOT @@ -1248,3 +1249,76 @@ def test_duplicate_non_prevout_key_rejected_when_hashing(self): with pytest.raises(PSBTError): view.sign_input(0, SIGNING_ROOT, sigs) assert sigs.getvalue() == b"" + + +# BIP-341 wallet-test-vectors.json, keyPathSpending[0] +BIP341_RAW_TX = "02000000097de20cbff686da83a54981d2b9bab3586f4ca7e48f57f5b55963115f3b334e9c010000000000000000d7b7cab57b1393ace2d064f4d4a2cb8af6def61273e127517d44759b6dafdd990000000000fffffffff8e1f583384333689228c5d28eac13366be082dc57441760d957275419a418420000000000fffffffff0689180aa63b30cb162a73c6d2a38b7eeda2a83ece74310fda0843ad604853b0100000000feffffffaa5202bdf6d8ccd2ee0f0202afbbb7461d9264a25e5bfd3c5a52ee1239e0ba6c0000000000feffffff956149bdc66faa968eb2be2d2faa29718acbfe3941215893a2a3446d32acd050000000000000000000e664b9773b88c09c32cb70a2a3e4da0ced63b7ba3b22f848531bbb1d5d5f4c94010000000000000000e9aa6b8e6c9de67619e6a3924ae25696bb7b694bb677a632a74ef7eadfd4eabf0000000000ffffffffa778eb6a263dc090464cd125c466b5a99667720b1c110468831d058aa1b82af10100000000ffffffff0200ca9a3b000000001976a91406afd46bcdfd22ef94ac122aa11f241244a37ecc88ac807840cb0000000020ac9a87f5594be208f8532db38cff670c450ed2fea8fcdefcc9a663f78bab962b0065cd1d" +BIP341_UTXOS = [ + ("512053a1f6e454df1aa2776a2814a721372d6258050de330b3c6d10ee8f4e0dda343", 420000000), + ("5120147c9c57132f6e7ecddba9800bb0c4449251c92a1e60371ee77557b6620f3ea3", 462000000), + ("76a914751e76e8199196d454941c45d1b3a323f1433bd688ac", 294000000), + ("5120e4d810fd50586274face62b8a807eb9719cef49c04177cc6b76a9a4251d5450e", 504000000), + ("512091b64d5324723a985170e4dc5a0f84c041804f2cd12660fa5dec09fc21783605", 630000000), + ("00147dd65592d0ab2fe0d0257d571abf032cd9db93dc", 378000000), + ("512075169f4001aa68f15bbed28b218df1d0a62cbbcf1188c6665110c293c907b831", 672000000), + ("5120712447206d7a5238acc7ff53fbe94a3b64539ad291c7cdbc490b7577e4b17df5", 546000000), + ("512077e30a5522dd9f894c3f8b8bd4c4b2cf82ca7da8a3ea6a239655c39c050ab220", 588000000), +] +# (txinIndex, hashType, sigHash) +BIP341_SIGHASHES = [ + (0, 0x03, "2514a6272f85cfa0f45eb907fcb0d121b808ed37c6ea160a5a9046ed5526d555"), + (1, 0x83, "325a644af47e8a5a2591cda0ab0723978537318f10e6a63d4eed783b96a71a4d"), + (3, 0x01, "bf013ea93474aa67815b1b6cc441d23b64fa310911d991e713cd34c7f5d46669"), + (4, 0x00, "4f900a0bae3f1446fd48490c2958b5a023228f01661cda3496a11da502a7f7ef"), + (6, 0x02, "15f25c298eb5cdc7eb1d638dd2d45c97c4c59dcaec6679cfc16ad84f30876b85"), + (7, 0x82, "cd292de50313804dabe4685e83f923d2969577191a3e1d2882220dca88cbeb10"), + (8, 0x81, "cccb739eca6c13a8a89e6e5cd317ffe55669bbda23f2fd37b0f18755e008edd2"), +] + + +class TestBIP341Sighash: + """Taproot sighash for every hash type against the BIP-341 vectors""" + + def _psbt(self): + tx = Transaction.parse(unhexlify(BIP341_RAW_TX)) + psbt = PSBT(tx) + for inp, (spk, amount) in zip(psbt.inputs, BIP341_UTXOS): + inp.witness_utxo = TransactionOutput(amount, Script(unhexlify(spk))) + return psbt + + @pytest.mark.parametrize("index, hash_type, expected", BIP341_SIGHASHES) + def test_transaction(self, index, hash_type, expected): + tx = Transaction.parse(unhexlify(BIP341_RAW_TX)) + scripts = [Script(unhexlify(spk)) for spk, _ in BIP341_UTXOS] + values = [amount for _, amount in BIP341_UTXOS] + assert tx.sighash_taproot( + index, scripts, values, sighash=hash_type + ) == unhexlify(expected) + + @pytest.mark.parametrize("index, hash_type, expected", BIP341_SIGHASHES) + def test_psbt_and_psbtview(self, index, hash_type, expected): + psbt = self._psbt() + assert psbt.inputs[index].is_taproot + raw = psbt.serialize() + assert psbt.sighash(index, sighash=hash_type) == unhexlify(expected) + view = PSBTView.view(BytesIO(raw)) + assert view.sighash(index, sighash=hash_type) == unhexlify(expected) + v2 = to_v2(psbt) + assert v2.sighash(index, sighash=hash_type) == unhexlify(expected) + view = PSBTView.view(BytesIO(v2.serialize())) + assert view.sighash(index, sighash=hash_type) == unhexlify(expected) + + def test_single_without_matching_output(self): + psbt = self._psbt() + # first taproot input with no output at the same index + index = next( + i + for i, inp in enumerate(psbt.inputs) + if inp.is_taproot and i >= len(psbt.tx.vout) + ) + with pytest.raises(EmbitError): + psbt.sighash(index, sighash=SIGHASH.SINGLE) + with pytest.raises(PSBTError): + PSBTView.view(BytesIO(psbt.serialize())).sighash( + index, sighash=SIGHASH.SINGLE + ) From e5e69965af6da97a9dcef58c3e3584807ad50944 Mon Sep 17 00:00:00 2001 From: odudex Date: Wed, 2 Sep 2026 11:52:25 -0300 Subject: [PATCH 19/44] fix(psbt): parse PSBTv0 unsigned transactions with no inputs BIP-174 serializes PSBT_GLOBAL_UNSIGNED_TX without witness data, so a transaction with no inputs yet (a Creator-stage PSBT) starts with a plain 0x00 input count. Transaction.read_from took that byte for the BIP-144 segwit marker and failed on the flag, so PSBT.parse rejected such PSBTs while PSBTView, which never looks for a marker, accepted them. Transaction.read_from gains segwit=True; PSBT passes segwit=False for the global transaction, as Bitcoin Core does. Network transactions keep the marker detection. Same approach as #117. Co-authored-by: Sinan --- src/embit/liquid/transaction.py | 4 ++- src/embit/psbt.py | 4 ++- src/embit/transaction.py | 7 +++-- tests/tests/test_psbtV2.py | 53 ++++++++++++++++++++++++++++++++- 4 files changed, 63 insertions(+), 5 deletions(-) diff --git a/src/embit/liquid/transaction.py b/src/embit/liquid/transaction.py index ca7af0a5..a3c544fa 100644 --- a/src/embit/liquid/transaction.py +++ b/src/embit/liquid/transaction.py @@ -287,7 +287,9 @@ def read_vout(cls, stream, idx): return res, hashlib.sha256(h.digest()).digest() @classmethod - def read_from(cls, stream): + def read_from(cls, stream, segwit=True): + # segwit= only mirrors Transaction.read_from: Liquid transactions + # carry an explicit witness flag byte, so nothing is ambiguous here ver = int.from_bytes(stream.read(4), "little") has_witness = False if stream.read(1) == b"\x01": diff --git a/src/embit/psbt.py b/src/embit/psbt.py index b235b7e0..b83a83e2 100644 --- a/src/embit/psbt.py +++ b/src/embit/psbt.py @@ -1316,7 +1316,9 @@ def read_from(cls, stream, compress=CompressMode.KEEP_ALL): else: # PSBTv0 (version is None or 0) tx_bytes = global_kvs.pop(b"\x00") # Remove so it's not in unknown try: - tx_for_v0 = cls.TX_CLS.parse(tx_bytes) + # BIP-174: the unsigned tx is serialized without witness data, + # so a leading zero is an empty input list, not a segwit marker + tx_for_v0 = cls.TX_CLS.parse(tx_bytes, segwit=False) except _PARSE_ERRORS as e: raise PSBTError("Invalid global transaction: %s" % e) # BIP-174: the global transaction must be unsigned diff --git a/src/embit/transaction.py b/src/embit/transaction.py index 8061dfc2..9de9aca6 100644 --- a/src/embit/transaction.py +++ b/src/embit/transaction.py @@ -150,11 +150,14 @@ def read_vout(cls, stream, idx): return res, hashlib.sha256(h.digest()).digest() @classmethod - def read_from(cls, stream): + def read_from(cls, stream, segwit=True): + """segwit=False parses a transaction known to be serialized without + witness data (BIP-174's unsigned tx), where a leading zero is a real + input count and not the BIP-144 marker.""" ver = int.from_bytes(_read_exact(stream, 4), "little") num_vin = compact.read_from(stream) # if num_vin is zero it is a segwit transaction - is_segwit = num_vin == 0 + is_segwit = segwit and num_vin == 0 if is_segwit: marker = stream.read(1) if marker != b"\x01": diff --git a/tests/tests/test_psbtV2.py b/tests/tests/test_psbtV2.py index f0dfa65e..f22e2fd9 100644 --- a/tests/tests/test_psbtV2.py +++ b/tests/tests/test_psbtV2.py @@ -15,7 +15,7 @@ CompressMode, ) from embit.psbtview import PSBTView -from embit.script import Script +from embit.script import Script, Witness from embit import compact from embit.base import EmbitError from .test_psbtview import PSBTS as VIEW_PSBTS, ROOT as SIGNING_ROOT @@ -1322,3 +1322,54 @@ def test_single_without_matching_output(self): PSBTView.view(BytesIO(psbt.serialize())).sighash( index, sighash=SIGHASH.SINGLE ) + + +class TestZeroInputUnsignedTx: + """BIP-174: the unsigned tx is non-witness serialized, so a PSBT whose + transaction has no inputs yet starts its input count with a plain 0x00""" + + def test_parses_and_round_trips(self): + tx = Transaction(vin=[], vout=[TransactionOutput(1000, Script(b"\x51"))]) + raw = PSBT(tx).serialize() + psbt = PSBT.parse(raw) + assert len(psbt.inputs) == 0 and len(psbt.outputs) == 1 + assert psbt.serialize() == raw + assert psbt.tx.serialize() == tx.serialize() + view = PSBTView.view(BytesIO(raw)) + assert view.num_inputs == 0 and view.num_outputs == 1 + assert view.vout(0).serialize() == tx.vout[0].serialize() + out = BytesIO() + view.write_to(out) + assert out.getvalue() == raw + + def test_witness_serialized_unsigned_tx_rejected(self): + tx = Transaction( + vin=[TransactionInput(bytes(32), 0)], + vout=[TransactionOutput(1000, Script(b"\x51"))], + ) + tx.vin[0].witness = Witness([b"\x01"]) + assert tx.serialize()[4:6] == b"\x00\x01" + raw = PSBT.MAGIC + kv(b"\x00", tx.serialize()) + b"\x00" + b"\x00" + b"\x00" + with pytest.raises(PSBTError): + PSBT.parse(raw) + # a network transaction still auto-detects the marker + assert Transaction.parse(tx.serialize()).serialize() == tx.serialize() + + +class TestSequenceZero: + """#146: nSequence=0 must survive into the signed transaction""" + + def test_sequence_zero_is_signed_as_is(self): + psbt = PSBT.from_string(SIGNABLE_V2_B64) + v0 = PSBT.parse(a2b_base64(VIEW_PSBTS[0])) + for p in (psbt, v0): + p.inputs[0].sequence = 0 + raw = p.serialize() + parsed = PSBT.parse(raw) + assert parsed.inputs[0].sequence == 0 + assert parsed.tx.vin[0].sequence == 0 + view = PSBTView.view(BytesIO(raw)) + assert view.vin(0).sequence == 0 + _, _, ser1, ser2 = sign_both(raw, SIGNING_ROOT) + assert ser1 == ser2 + assert PSBT.parse(ser1).tx.vin[0].sequence == 0 From 52883cc64fd13397dee1f2f4e9068606072d5c16 Mon Sep 17 00:00:00 2001 From: odudex Date: Wed, 2 Sep 2026 11:52:45 -0300 Subject: [PATCH 20/44] changelog: taproot sighash, zero-input PSBT and PSET asset fixes --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f241589..3f72784e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 constructor argument with `version=2`. - Signing an input without a utxo raises `PSBTError` instead of `AttributeError`. +- Taproot sighashes with `ANYONECANPAY` hashed the whole serialized input + instead of the outpoint, and `SIGHASH_SINGLE` hashed the output instead of + its SHA256, in both `Transaction` and `PSBTView` (#65). +- `PSBT.parse` rejected a PSBTv0 whose unsigned transaction has no inputs, + taking the zero input count for a segwit marker (#117). +- `PSET` v2 outputs without an asset or asset commitment are rejected at parse + time instead of failing with `TypeError` when hashed. ## [0.8.2] - 2026-08-08 From bdcb788d02c6a7c264f0db9e00b303d50cda6342 Mon Sep 17 00:00:00 2001 From: odudex Date: Wed, 2 Sep 2026 14:06:23 -0300 Subject: [PATCH 21/44] fix(psbt): pass the PSBT version through PSBTScope.read_from The base PSBTScope.read_from popped the version keyword to apply the v0/v2 field-scope check but called read_value() without it, so any scope class relying on the base implementation parsed versioned fields as if the PSBT were v0. InputScope and OutputScope override read_from and were not affected; found by the CI review on the PR. --- src/embit/psbt.py | 2 +- tests/tests/test_psbtV2.py | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/embit/psbt.py b/src/embit/psbt.py index b83a83e2..606144c2 100644 --- a/src/embit/psbt.py +++ b/src/embit/psbt.py @@ -301,7 +301,7 @@ def read_from(cls, stream, *args, **kwargs): break if version != 2 and key in res.V2_FIELDS: raise PSBTError("PSBTv2 field is not allowed in PSBTv0") - res._read_value_checked(stream, key) + res._read_value_checked(stream, key, version=version) return res diff --git a/tests/tests/test_psbtV2.py b/tests/tests/test_psbtV2.py index f22e2fd9..4332e735 100644 --- a/tests/tests/test_psbtV2.py +++ b/tests/tests/test_psbtV2.py @@ -1373,3 +1373,28 @@ def test_sequence_zero_is_signed_as_is(self): _, _, ser1, ser2 = sign_both(raw, SIGNING_ROOT) assert ser1 == ser2 assert PSBT.parse(ser1).tx.vin[0].sequence == 0 + + +class TestBaseScopeVersion: + def test_base_read_from_passes_version_to_read_value(self): + """A scope built on PSBTScope.read_from must see the PSBT version.""" + from embit.psbt import PSBTScope + + seen = [] + + class Scope(PSBTScope): + V2_FIELDS = (b"\x0e",) + + def read_value(self, stream, key, version=None): + seen.append((key, version)) + super().read_value(stream, key, version=version) + + raw = kv(b"\x0e", bytes(32)) + kv(b"\xf0", b"x") + b"\x00" + scope = Scope.read_from(BytesIO(raw), version=2) + assert seen == [(b"\x0e", 2), (b"\xf0", 2)] + assert list(scope.unknown) == [b"\x0e", b"\xf0"] + seen.clear() + Scope.read_from(BytesIO(kv(b"\xf0", b"x") + b"\x00")) + assert seen == [(b"\xf0", None)] + with pytest.raises(PSBTError): + Scope.read_from(BytesIO(raw)) From 3937ff07c36ba57de7a09b5205674ca82c9a7901 Mon Sep 17 00:00:00 2001 From: odudex Date: Thu, 3 Sep 2026 18:25:39 -0300 Subject: [PATCH 22/44] fix(psbt): bound the taproot leaf hash count before reading leaves A crafted PSBT_IN/OUT_TAP_BIP32_DERIVATION value with a huge compact count made the parser build a list of up to 2^64 empty reads before the length check ran, exhausting memory on constrained devices. Co-authored-by: notTanveer --- CHANGELOG.md | 3 +++ src/embit/psbt.py | 8 ++++---- tests/tests/test_taproot.py | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f72784e..04315072 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -51,6 +51,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 taking the zero input count for a segwit marker (#117). - `PSET` v2 outputs without an asset or asset commitment are rejected at parse time instead of failing with `TypeError` when hashed. +- `PSBT_IN_TAP_BIP32_DERIVATION` and `PSBT_OUT_TAP_BIP32_DERIVATION` check + the declared leaf hash count against the value length before building the + list, so a crafted count cannot exhaust memory. ## [0.8.2] - 2026-08-08 diff --git a/src/embit/psbt.py b/src/embit/psbt.py index 606144c2..4c8a6ab1 100644 --- a/src/embit/psbt.py +++ b/src/embit/psbt.py @@ -665,9 +665,9 @@ def read_value(self, stream, k, version=None): if pub not in self.taproot_bip32_derivations: b = BytesIO(v) num_leaf_hashes = read_compact(b) - leaf_hashes = [b.read(32) for i in range(num_leaf_hashes)] - if not all([len(leaf) == 32 for leaf in leaf_hashes]): + if num_leaf_hashes * 32 > len(v) - b.seek(0, 1): raise PSBTError("Invalid length of taproot leaf hashes") + leaf_hashes = [b.read(32) for i in range(num_leaf_hashes)] der = DerivationPath.read_from(b) self.taproot_bip32_derivations[pub] = (leaf_hashes, der) @@ -936,9 +936,9 @@ def read_value(self, stream, k, version=None): if pub not in self.taproot_bip32_derivations: b = BytesIO(v) num_leaf_hashes = read_compact(b) - leaf_hashes = [b.read(32) for i in range(num_leaf_hashes)] - if not all([len(leaf) == 32 for leaf in leaf_hashes]): + if num_leaf_hashes * 32 > len(v) - b.seek(0, 1): raise PSBTError("Invalid length of taproot leaf hashes") + leaf_hashes = [b.read(32) for i in range(num_leaf_hashes)] der = DerivationPath.read_from(b) self.taproot_bip32_derivations[pub] = (leaf_hashes, der) diff --git a/tests/tests/test_taproot.py b/tests/tests/test_taproot.py index 3e3ad646..390162c3 100644 --- a/tests/tests/test_taproot.py +++ b/tests/tests/test_taproot.py @@ -265,3 +265,36 @@ def test_sign_psbtview(self): self.assertEqual( inp1.final_scriptwitness, inp2.final_scriptwitness ) # test psbt and psbtview give the same results + + def test_tap_bip32_derivation_leaf_count_bounded(self): + """A crafted leaf hash count must be rejected before any list is built, + so a huge compact int cannot exhaust memory.""" + from embit.psbt import InputScope, OutputScope, PSBTError + from embit import compact + + xonly = ROOT.derive("m/86h/1h/0h/0/0").xonly() + fingerprint = b"\x00" * 4 + for cls, keytype in ((InputScope, b"\x16"), (OutputScope, b"\x07")): + for count in (2**64 - 1, 2**32, 2): + # count leaf hashes claimed, but only one 32-byte leaf plus path present + value = compact.to_bytes(count) + b"\xaa" * 32 + fingerprint + key = keytype + xonly + stream = BytesIO( + compact.to_bytes(len(key)) + key + + compact.to_bytes(len(value)) + value + + b"\x00" + ) + with self.assertRaises(PSBTError): + cls.read_from(stream) + # exact count still parses + value = compact.to_bytes(1) + b"\xaa" * 32 + fingerprint + key = keytype + xonly + stream = BytesIO( + compact.to_bytes(len(key)) + key + + compact.to_bytes(len(value)) + value + + b"\x00" + ) + scope = cls.read_from(stream) + leaf_hashes, der = list(scope.taproot_bip32_derivations.values())[0] + self.assertEqual(leaf_hashes, [b"\xaa" * 32]) + self.assertEqual(der.fingerprint, fingerprint) From 0e0e914a1811a2c36d9b63f5c7e4f23b794a7a01 Mon Sep 17 00:00:00 2001 From: odudex Date: Tue, 8 Sep 2026 08:46:32 -0300 Subject: [PATCH 23/44] fix(psbtview): stream deferred PSBTv2 global values --- src/embit/psbtview.py | 31 ++++++++++++++++----------- tests/tests/test_psbtV2.py | 43 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 12 deletions(-) diff --git a/src/embit/psbtview.py b/src/embit/psbtview.py index 3cbbb823..28425b32 100644 --- a/src/embit/psbtview.py +++ b/src/embit/psbtview.py @@ -316,6 +316,8 @@ def __init__( self._tx_version = self.tx.version if self.tx else None self._locktime = self.tx.locktime if self.tx else None # For PSBTv2: dict of all global key→value pairs (excluding the \x00 global tx). + # Validated fixed/count values are bytes; other values are stored as + # (offset, length) tuples and streamed from the source on write. # This is the single source of truth for every global field, including # PSBT_GLOBAL_TX_MODIFIABLE (\x06). None for PSBTv0 where the global # scope is streamed verbatim. @@ -371,12 +373,11 @@ def view(cls, stream, offset=None, compress=CompressMode.KEEP_ALL): num_outputs = None tx_offset = None tx_len = None - # Collect all non-global-tx key-value pairs for PSBTv2 global scope rewriting. - # The global scope is small, so materialising it avoids byte-level injection. + # Buffer only the bounded global fields needed for PSBTv2 validation + # and transaction handling. global_kvs = OrderedDict() - # key -> (value_offset, value_len) for globals whose value is only needed - # if this turns out to be a PSBTv2. PSBTView is RAM-constrained, so we - # skip them on the first pass and read them back below only when used. + # key -> (value_offset, value_len) for other globals. Keep references + # so arbitrarily large values can be copied on write without buffering. deferred_kvs = OrderedDict() while True: # read key and update cursor @@ -445,11 +446,7 @@ def view(cls, stream, offset=None, compress=CompressMode.KEEP_ALL): if None in [version or tx_offset, num_inputs, num_outputs]: raise PSBTError("Missing something important in PSBT") if version == 2: - # PSBTv2 rebuilds its global scope from _global_kvs on write, so now - # (and only now) we need the values we skipped over above. - for k, (v_off, v_len) in deferred_kvs.items(): - stream.seek(v_off) - global_kvs[k] = read_exact(stream, v_len) + global_kvs.update(deferred_kvs) res = cls( stream, num_inputs, @@ -1289,7 +1286,7 @@ def write_to( # first we write global scope if self._global_kvs is not None: - # PSBTv2: reconstruct global scope from the materialised key-value dict + # PSBTv2: reconstruct global scope from buffered fields and references # in the same order PSBT.write_to uses: xpubs, the BIP-370 fields, # then the unknown fields as they appeared in the source. writable_stream.write(self.MAGIC) @@ -1303,7 +1300,17 @@ def write_to( ] for k in keys: res += ser_string(writable_stream, k) - res += ser_string(writable_stream, self._global_kvs[k]) + value = self._global_kvs[k] + if isinstance(value, tuple): + value_offset, value_len = value + self.stream.seek(value_offset) + res += writable_stream.write(compact.to_bytes(value_len)) + copied = read_write(self.stream, writable_stream, value_len) + if copied != value_len: + raise PSBTError("Truncated global value") + res += copied + else: + res += ser_string(writable_stream, value) writable_stream.write(b"\x00") # global scope separator res += 1 else: diff --git a/tests/tests/test_psbtV2.py b/tests/tests/test_psbtV2.py index 4332e735..89bbea15 100644 --- a/tests/tests/test_psbtV2.py +++ b/tests/tests/test_psbtV2.py @@ -1015,6 +1015,49 @@ def test_unknown_and_xpub_globals_keep_order(self): # PSBT and PSBTView agree even when the source order differs from theirs assert PSBT.parse(out.getvalue()).serialize() == out.getvalue() + @pytest.mark.parametrize("prefix", [b"", b"embedded PSBT"]) + def test_large_unknown_globals_are_streamed(self, prefix): + class BoundedReads(BytesIO): + def read(self, size=-1): + assert 0 <= size <= 128, "Oversized payload read" + return super().read(size) + + def readinto(self, buffer): + assert len(buffer) <= 128, "Oversized copy buffer" + return super().readinto(buffer) + + payload = bytes(range(256)) * 1024 + b"tail" + xpub = SIGNING_ROOT.to_public().serialize() + extra = ( + kv(b"\xf0", payload) + + kv(b"\x01" + xpub, SIGNING_ROOT.my_fingerprint + bytes(4)) + + kv(b"\xfc\x01x\x00", payload[::-1]) + + kv(b"\xf1", b"") + ) + raw = raw_v2([V2_IN], [V2_OUT], extra_globals=extra) + source = BoundedReads(prefix + raw) + source.seek(len(prefix)) + view = PSBTView.view(source) + psbt = PSBT.parse(raw) + # Rewriting mutable globals must preserve every streamed value, even + # across repeated writes and changes in the source cursor. + for flags in (3, 0, None): + view.tx_modifiable_flags = psbt.tx_modifiable_flags = flags + source.seek(0) + out = BytesIO() + written = view.write_to(out) + assert written == len(out.getvalue()) + assert out.getvalue() == psbt.serialize() + + def test_streamed_global_rejects_truncated_source_on_write(self): + payload = b"large unknown value" * 100 + raw = raw_v2([], [], extra_globals=kv(b"\xf0", payload)) + source = BytesIO(raw) + view = PSBTView.view(source) + source.truncate(raw.index(payload) + 1) + with pytest.raises(PSBTError, match="Truncated global value"): + view.write_to(BytesIO()) + def test_scope_constructor_accepts_v2_unknowns(self): txid = bytes(range(32)) inp = InputScope( From 6bc88c2d792fde06737b519c427084d5251d917b Mon Sep 17 00:00:00 2001 From: odudex Date: Fri, 18 Sep 2026 13:56:30 -0300 Subject: [PATCH 24/44] fix(psbt): verify witness UTXOs against authenticated outputs --- CHANGELOG.md | 3 + src/embit/psbt.py | 17 ++++ tests/tests/test_psbt_verification.py | 111 ++++++++++++++++++++++++++ 3 files changed, 131 insertions(+) create mode 100644 tests/tests/test_psbt_verification.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 04315072..a59eb479 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- Reject conflicting witness and non-witness UTXOs during verification, in + both full and compressed parsing, so verified fee calculations cannot use + an unauthenticated amount or script. - Validate PSBTv2 count fields canonically and raise `PSBTError` instead of `RuntimeError`/`ValueError` on malformed values in both parse paths. - `PSBTView` matched PSBTv2 prevout and output fields by key prefix, so a diff --git a/src/embit/psbt.py b/src/embit/psbt.py index 4c8a6ab1..529e669d 100644 --- a/src/embit/psbt.py +++ b/src/embit/psbt.py @@ -445,6 +445,7 @@ def verify(self, ignore_missing=False): https://blog.trezor.io/details-of-firmware-updates-for-trezor-one-version-1-9-1-and-trezor-model-t-version-2-3-1-1eba8f60f2dd For legacy txs we need to verify it to calculate fee. """ + self._verified = False if self.non_witness_utxo or self._txhash: txid = ( bytes(reversed(self._txhash)) @@ -452,6 +453,22 @@ def verify(self, ignore_missing=False): else self.non_witness_utxo.txid() ) if self.txid == txid: + if self._txhash: + utxo = self._utxo + else: + if ( + self.vout is None + or self.vout < 0 + or self.vout >= len(self.non_witness_utxo.vout) + ): + raise PSBTError("Invalid non_witness_utxo output index") + utxo = self.non_witness_utxo.vout[self.vout] + if utxo is None: + raise PSBTError("Missing non_witness_utxo output") + # Fee calculation and signing may use witness_utxo instead of + # the authenticated output, so both representations must agree. + if self.witness_utxo is not None and self.witness_utxo != utxo: + raise PSBTError("Witness utxo doesn't match non_witness_utxo") self._verified = True return True else: diff --git a/tests/tests/test_psbt_verification.py b/tests/tests/test_psbt_verification.py new file mode 100644 index 00000000..6860e83f --- /dev/null +++ b/tests/tests/test_psbt_verification.py @@ -0,0 +1,111 @@ +from io import BytesIO + +import pytest + +from embit import ec, script +from embit.psbt import PSBT, PSBTError, CompressMode +from embit.psbtview import PSBTView +from embit.transaction import ( + SIGHASH, + Transaction, + TransactionInput, + TransactionOutput, +) + + +def funded_psbt(version): + key = ec.PrivateKey((1).to_bytes(32, "big")) + spk = script.p2wpkh(key.get_public_key()) + previous = Transaction( + vin=[TransactionInput(bytes(32), 0)], + vout=[TransactionOutput(10000, spk)], + ) + psbt = PSBT( + Transaction( + vin=[TransactionInput(previous.txid(), 0)], + vout=[TransactionOutput(9000, spk)], + ), + version=version, + ) + psbt.inputs[0].non_witness_utxo = previous + psbt.inputs[0].witness_utxo = TransactionOutput(10000, spk) + return psbt, key + + +@pytest.mark.parametrize("version", [None, 2]) +@pytest.mark.parametrize( + "compress", [CompressMode.KEEP_ALL, CompressMode.CLEAR_ALL, CompressMode.PARTIAL] +) +@pytest.mark.parametrize("conflict", [None, "amount", "script"]) +def test_verify_checks_both_utxo_representations(version, compress, conflict): + psbt, key = funded_psbt(version) + if conflict == "amount": + psbt.inputs[0].witness_utxo.value = 1000 + elif conflict == "script": + psbt.inputs[0].witness_utxo.script_pubkey = script.Script(b"\x51") + raw = psbt.serialize() + parsed = PSBT.parse(raw, compress=compress) + viewed = PSBTView.view(BytesIO(raw), compress=compress).input(0) + if conflict: + for inp in (parsed.inputs[0], viewed): + with pytest.raises(PSBTError, match="Witness utxo doesn't match"): + inp.verify() + assert not inp.is_verified + else: + assert parsed.verify() + assert viewed.verify() + assert parsed.fee() == 1000 + assert parsed.sign_with(key) == 1 + signature = parsed.inputs[0].partial_sigs[key.get_public_key()] + assert key.verify(ec.Signature.parse(signature[:-1]), psbt.sighash(0)) + + +@pytest.mark.parametrize("version", [None, 2]) +@pytest.mark.parametrize( + "compress", [CompressMode.KEEP_ALL, CompressMode.CLEAR_ALL, CompressMode.PARTIAL] +) +def test_view_signs_the_taproot_amounts_it_shows(version, compress): + key = ec.PrivateKey((1).to_bytes(32, "big")) + spk = script.p2tr(key.get_public_key()) + previous = Transaction( + vin=[TransactionInput(bytes(32), 0)], + vout=[TransactionOutput(10000, spk)], + ) + psbt = PSBT( + Transaction( + vin=[TransactionInput(previous.txid(), 0)], + vout=[TransactionOutput(9000, spk)], + ), + version=version, + ) + psbt.inputs[0].non_witness_utxo = previous + # unverified witness utxo understating the amount + psbt.inputs[0].witness_utxo = TransactionOutput(1000, spk) + raw = psbt.serialize() + view = PSBTView.view(BytesIO(raw), compress=compress) + shown = view.input(0).utxo + expected = psbt.tx.sighash_taproot( + 0, [shown.script_pubkey], [shown.value], sighash=SIGHASH.DEFAULT + ) + assert view.sighash(0, sighash=SIGHASH.DEFAULT) == expected + parsed = PSBT.parse(raw, compress=compress) + assert parsed.sighash(0, sighash=SIGHASH.DEFAULT) == expected + + +def test_failed_reverification_clears_verified_state(): + psbt, _ = funded_psbt(None) + inp = psbt.inputs[0] + assert inp.verify() + inp.witness_utxo.value = 1 + with pytest.raises(PSBTError): + inp.verify() + assert not inp.is_verified + + +@pytest.mark.parametrize("index", [-1, 1, None]) +def test_verify_rejects_invalid_non_witness_output_index(index): + psbt, _ = funded_psbt(None) + psbt.inputs[0].vout = index + with pytest.raises(PSBTError, match="output index"): + psbt.verify() + assert not psbt.is_verified From bc093527b8c8d2d1b987206622cf38b351ca2494 Mon Sep 17 00:00:00 2001 From: odudex Date: Fri, 18 Sep 2026 13:58:57 -0300 Subject: [PATCH 25/44] fix(psbt): preserve signed locktime when adding v2 inputs --- CHANGELOG.md | 2 ++ src/embit/psbt.py | 22 ++++++++++-- tests/tests/test_psbtV2.py | 71 +++++++++++++++++++++++++++++++++++++- 3 files changed, 91 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a59eb479..6b173a02 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- Preserve the signed transaction locktime when adding PSBTv2 inputs, including + when existing inputs contain Taproot signatures or final scripts. - Reject conflicting witness and non-witness UTXOs during verification, in both full and compressed parsing, so verified fee calculations cannot use an unauthenticated amount or script. diff --git a/src/embit/psbt.py b/src/embit/psbt.py index 529e669d..0ebeaae1 100644 --- a/src/embit/psbt.py +++ b/src/embit/psbt.py @@ -1138,8 +1138,10 @@ def determine_locktime(self): ) def _validate_locktime_compatibility(self, inputs): - """Verify that the given input list has mutually compatible locktime requirements.""" - choose_locktime(*self._classify_locktimes(inputs), fallback=0) + """Return the locktime, rejecting incompatible input requirements.""" + return choose_locktime( + *self._classify_locktimes(inputs), fallback=self.locktime or 0 + ) @property def tx(self): @@ -1755,7 +1757,21 @@ def add_input(self, input_scope): input_scope.required_height_locktime is not None or input_scope.required_time_locktime is not None ): - self._validate_locktime_compatibility(self.inputs + [input_scope]) + locktime = self._validate_locktime_compatibility( + self.inputs + [input_scope] + ) + # ANYONECANPAY permits adding inputs, but its signatures still + # commit to nLockTime. Finalized inputs may have dropped their + # partial signatures, so check their final scripts as well. + if locktime != self.determine_locktime() and any( + inp.partial_sigs + or inp.taproot_sigs + or inp.taproot_key_sig is not None + or inp.final_scriptsig is not None + or inp.final_scriptwitness is not None + for inp in self.inputs + ): + raise PSBTError("Cannot change locktime with existing signatures") self.inputs.append(input_scope) if self.version == 2: self._raw_input_count_from_global = len(self.inputs) diff --git a/tests/tests/test_psbtV2.py b/tests/tests/test_psbtV2.py index 89bbea15..723e5bfc 100644 --- a/tests/tests/test_psbtV2.py +++ b/tests/tests/test_psbtV2.py @@ -16,7 +16,7 @@ ) from embit.psbtview import PSBTView from embit.script import Script, Witness -from embit import compact +from embit import compact, ec, script from embit.base import EmbitError from .test_psbtview import PSBTS as VIEW_PSBTS, ROOT as SIGNING_ROOT @@ -526,6 +526,75 @@ def test_add_input_requires_vout(self): with pytest.raises(PSBTError): psbt.add_input(inp) + @pytest.mark.parametrize( + "fallback, height, new_height, rejected", + [ + (0, None, 100, True), + (0, 100, 200, True), + (200, None, 100, True), + (0, 100, 100, False), + (0, 100, None, False), + ], + ) + def test_add_input_preserves_signed_locktime( + self, fallback, height, new_height, rejected + ): + key = ec.PrivateKey((1).to_bytes(32, "big")) + psbt = PSBT.create_v2(fallback_locktime=fallback) + inp = InputScope(vin=TransactionInput(bytes(32), 0, sequence=0xFFFFFFFE)) + inp.witness_utxo = TransactionOutput(10000, script.p2wpkh(key.get_public_key())) + inp.required_height_locktime = height + psbt.add_input(inp) + psbt.add_output(OutputScope(vout=TransactionOutput(9000, Script(b"\x51")))) + sighash = SIGHASH.ALL | SIGHASH.ANYONECANPAY + assert psbt.sign_with(key, sighash=sighash) == 1 + signature = ec.Signature.parse(inp.partial_sigs[key.get_public_key()][:-1]) + original = psbt.serialize() + extra = InputScope(vin=TransactionInput(b"\x01" * 32, 0)) + extra.required_height_locktime = new_height + if rejected: + with pytest.raises(PSBTError, match="existing signatures"): + psbt.add_input(extra) + assert psbt.serialize() == original + assert psbt._raw_input_count_from_global == 1 + else: + psbt.add_input(extra) + assert len(psbt.inputs) == 2 + assert key.verify(signature, psbt.sighash(0, sighash=sighash)) + + @pytest.mark.parametrize( + "signature_field, signature", + [ + ("taproot_key_sig", bytes(64)), + ("taproot_sigs", {(b"pub", b"leaf"): bytes(64)}), + ("final_scriptsig", Script(b"\x01\x01")), + ("final_scriptwitness", Witness([bytes(64)])), + ], + ) + def test_add_input_preserves_locktime_for_all_signature_fields( + self, signature_field, signature + ): + psbt = PSBT.create_v2() + inp = InputScope(vin=TransactionInput(bytes(32), 0)) + inp.required_height_locktime = 100 + inp.required_time_locktime = 500000000 + setattr(inp, signature_field, signature) + psbt.add_input(inp) + extra = InputScope(vin=TransactionInput(b"\x01" * 32, 0)) + extra.required_time_locktime = 500000000 + with pytest.raises(PSBTError, match="existing signatures"): + psbt.add_input(extra) + assert len(psbt.inputs) == 1 + assert psbt.determine_locktime() == 100 + + def test_unsigned_constructor_can_change_locktime(self): + psbt = PSBT.create_v2() + psbt.add_input(InputScope(vin=TransactionInput(bytes(32), 0))) + extra = InputScope(vin=TransactionInput(b"\x01" * 32, 0)) + extra.required_height_locktime = 100 + psbt.add_input(extra) + assert psbt.determine_locktime() == 100 + def test_add_output_appends_and_updates_count(self): """add_output() appends the scope and increments the global output count""" psbt = PSBT.create_v2() From 628154ddceacdc884916e660ed21f763c9c09365 Mon Sep 17 00:00:00 2001 From: odudex Date: Fri, 18 Sep 2026 14:01:48 -0300 Subject: [PATCH 26/44] fix(psbt): update v2 flags when signing directly with taproot keys --- CHANGELOG.md | 2 ++ src/embit/psbt.py | 2 ++ src/embit/psbtview.py | 2 ++ tests/tests/test_psbtV2.py | 61 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b173a02..3e9f53e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- Update PSBTv2 transaction modification flags when signing directly with a + Taproot key, for both key and script paths in `PSBT` and `PSBTView`. - Preserve the signed transaction locktime when adding PSBTv2 inputs, including when existing inputs contain Taproot signatures or final scripts. - Reject conflicting witness and non-witness UTXOs during verification, in diff --git a/src/embit/psbt.py b/src/embit/psbt.py index 0ebeaae1..9d40e5a1 100644 --- a/src/embit/psbt.py +++ b/src/embit/psbt.py @@ -1526,6 +1526,7 @@ def _sign_taproot_keypath(self, pk, input_index: int, inp, sighash) -> int: sigdata += bytes([sighash]) inp.taproot_key_sig = sigdata inp.final_scriptwitness = Witness([sigdata]) + self._update_tx_modifiable(sighash) return 1 def sign_input_with_tapkey( @@ -1570,6 +1571,7 @@ def sign_input_with_tapkey( if sighash != SIGHASH.DEFAULT: sigdata += bytes([sighash]) inp.taproot_sigs[(pub, leaf)] = sigdata + self._update_tx_modifiable(sighash) counter += 1 return counter diff --git a/src/embit/psbtview.py b/src/embit/psbtview.py index 28425b32..614cc07d 100644 --- a/src/embit/psbtview.py +++ b/src/embit/psbtview.py @@ -1046,6 +1046,7 @@ def sign_input_with_tapkey( # same fields PSBT._sign_taproot_keypath fills inp.taproot_key_sig = sigdata inp.final_scriptwitness = Witness([sigdata]) + self._update_tx_modifiable(sighash) # no need to sign anything else return 1 counter = 0 @@ -1074,6 +1075,7 @@ def sign_input_with_tapkey( if sighash != SIGHASH.DEFAULT: sigdata += bytes([sighash]) inp.taproot_sigs[(pub, leaf)] = sigdata + self._update_tx_modifiable(sighash) counter += 1 return counter diff --git a/tests/tests/test_psbtV2.py b/tests/tests/test_psbtV2.py index 723e5bfc..07d01ede 100644 --- a/tests/tests/test_psbtV2.py +++ b/tests/tests/test_psbtV2.py @@ -1223,6 +1223,67 @@ def test_length_mismatch_rejected(self, delta, compress): class TestTxModifiableAfterSigning: """PSBT_GLOBAL_TX_MODIFIABLE after a real sign_with, in PSBT and PSBTView""" + @pytest.mark.parametrize("version", [None, 2]) + @pytest.mark.parametrize("scriptpath", [False, True]) + @pytest.mark.parametrize( + "sighash, expected", + [(0, 0), (1, 0), (2, 2), (3, 4), (0x81, 1), (0x82, 3), (0x83, 5)], + ) + def test_direct_taproot_signing_updates_flags( + self, version, scriptpath, sighash, expected + ): + from .test_taproot import TAP_PSBTS, KEY_A, TAPTREE_PSBT, TAPTREE_KEYS + + psbt = PSBT.from_string(TAPTREE_PSBT if scriptpath else TAP_PSBTS[0]) + root = TAPTREE_KEYS[1] if scriptpath else KEY_A + if version == 2: + psbt = to_v2(psbt) + psbt.tx_modifiable_flags = TxModifiable.INPUTS | TxModifiable.OUTPUTS + key = next( + root.derive(der.derivation) + for _, der in psbt.inputs[0].taproot_bip32_derivations.values() + if der.fingerprint == root.my_fingerprint + ) + view = PSBTView.view(BytesIO(psbt.serialize())) + inp = view.input(0) + count = psbt.sign_input_with_tapkey(key, 0, sighash=sighash) + assert count > 0 + assert view.sign_input_with_tapkey(key, 0, inp, sighash=sighash) == count + if scriptpath: + assert inp.taproot_sigs + assert inp.taproot_sigs == psbt.inputs[0].taproot_sigs + else: + assert inp.taproot_key_sig == psbt.inputs[0].taproot_key_sig + pub = ec.PublicKey.from_xonly(inp.utxo.script_pubkey.data[2:]) + assert pub.schnorr_verify( + ec.SchnorrSig(inp.taproot_key_sig[:64]), psbt.sighash(0, sighash) + ) + flags = expected if version == 2 else None + assert psbt.tx_modifiable_flags == view.tx_modifiable_flags == flags + out = BytesIO() + view.write_to( + out, extra_input_streams=[BytesIO(inp.serialize(version=version))] + ) + assert PSBT.parse(out.getvalue()).tx_modifiable_flags == flags + assert out.getvalue() == psbt.serialize() + if version == 2 and not expected & TxModifiable.OUTPUTS: + with pytest.raises(PSBTError, match="Outputs are not modifiable"): + psbt.add_output(OutputScope(vout=TransactionOutput(1, Script(b"")))) + + @pytest.mark.parametrize("scriptpath", [False, True]) + def test_direct_taproot_signing_with_unrelated_key_preserves_flags( + self, scriptpath + ): + from .test_taproot import TAP_PSBTS, TAPTREE_PSBT + + psbt = to_v2(PSBT.from_string(TAPTREE_PSBT if scriptpath else TAP_PSBTS[0])) + psbt.tx_modifiable_flags = 3 + view = PSBTView.view(BytesIO(psbt.serialize())) + key = ec.PrivateKey((1).to_bytes(32, "big")) + assert psbt.sign_input_with_tapkey(key, 0) == 0 + assert view.sign_input_with_tapkey(key, 0) == 0 + assert psbt.tx_modifiable_flags == view.tx_modifiable_flags == 3 + @pytest.mark.parametrize( "sighash, expected", [ From 6ea495ac85acbdedd9e9cf54864e7614a3ff0bea Mon Sep 17 00:00:00 2001 From: odudex Date: Fri, 18 Sep 2026 14:10:45 -0300 Subject: [PATCH 27/44] fix(psbt): re-verify inputs after update() merges UTXO data InputScope.update() kept _verified set after merging a new witness_utxo, so fee() used an unauthenticated amount. It also merged a compressed _utxo without its _txhash, pairing a foreign output with our authenticated hash, so even a fresh verify() passed. Merge the pair together and clear the verified state whenever prevout or UTXO data is merged; signature-only merges keep it. --- CHANGELOG.md | 4 + src/embit/psbt.py | 28 ++++- tests/tests/test_psbt_verification.py | 142 +++++++++++++++++++++++++- 3 files changed, 172 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e9f53e8..d785e750 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- `InputScope.update()` clears the verified state when it merges prevout or + UTXO data, keeps a compressed UTXO together with its transaction hash and + output index, and discards stale caches when the prevout or full previous + transaction changes, so `verify()` cannot pass on a merged foreign output. - Update PSBTv2 transaction modification flags when signing directly with a Taproot key, for both key and script paths in `PSBT` and `PSBTView`. - Preserve the signed transaction locktime when adding PSBTv2 inputs, including diff --git a/src/embit/psbt.py b/src/embit/psbt.py index 9d40e5a1..1abbe09d 100644 --- a/src/embit/psbt.py +++ b/src/embit/psbt.py @@ -329,6 +329,7 @@ def __init__( self.witness_utxo = None self._utxo = None self._txhash = None + self._utxo_vout = None self._verified = False self.partial_sigs = OrderedDict() self.sighash_type = None @@ -371,13 +372,35 @@ def clear_metadata(self, compress=CompressMode.CLEAR_ALL): self.taproot_scripts = OrderedDict() def update(self, other): + prevout_changed = ( + other.txid is not None and other.txid != self.txid + ) or (other.vout is not None and other.vout != self.vout) self.txid = other.txid or self.txid self.vout = other.vout if other.vout is not None else self.vout self.sequence = other.sequence if other.sequence is not None else self.sequence self.unknown.update(other.unknown) self.non_witness_utxo = other.non_witness_utxo or self.non_witness_utxo self.witness_utxo = other.witness_utxo or self.witness_utxo - self._utxo = other._utxo or self._utxo + # Keep the cached output with the hash and index it was streamed with. + if other._utxo is not None: + self._utxo = other._utxo + self._txhash = other._txhash + self._utxo_vout = other._utxo_vout + elif prevout_changed or other.non_witness_utxo is not None: + # A full replacement transaction can be checked afresh. Without + # one, the old cached output cannot authenticate the new prevout. + self._utxo = None + self._txhash = None + self._utxo_vout = None + # merged prevout or utxo data has not been through verify() + if ( + other.txid + or other.vout is not None + or other.non_witness_utxo + or other.witness_utxo + or other._utxo + ): + self._verified = False self.partial_sigs.update(other.partial_sigs) self.sighash_type = ( other.sighash_type if other.sighash_type is not None else self.sighash_type @@ -454,6 +477,8 @@ def verify(self, ignore_missing=False): ) if self.txid == txid: if self._txhash: + if self.vout != self._utxo_vout: + raise PSBTError("Cached utxo doesn't match output index") utxo = self._utxo else: if ( @@ -508,6 +533,7 @@ def read_value(self, stream, k, version=None): value_stream.finish() self._txhash = txhash self._utxo = txout + self._utxo_vout = effective_vout else: tx = self.TX_CLS.read_from(value_stream) value_stream.finish() diff --git a/tests/tests/test_psbt_verification.py b/tests/tests/test_psbt_verification.py index 6860e83f..4a8ffc47 100644 --- a/tests/tests/test_psbt_verification.py +++ b/tests/tests/test_psbt_verification.py @@ -3,7 +3,7 @@ import pytest from embit import ec, script -from embit.psbt import PSBT, PSBTError, CompressMode +from embit.psbt import PSBT, PSBTError, CompressMode, InputScope from embit.psbtview import PSBTView from embit.transaction import ( SIGHASH, @@ -109,3 +109,143 @@ def test_verify_rejects_invalid_non_witness_output_index(index): with pytest.raises(PSBTError, match="output index"): psbt.verify() assert not psbt.is_verified + + +def test_update_with_utxo_data_clears_verified_state(): + psbt, _ = funded_psbt(None) + inp = psbt.inputs[0] + inp.witness_utxo = None + assert psbt.verify() + # merging signatures keeps the verification + other = InputScope() + other.partial_sigs[b"pub"] = b"sig" + inp.update(other) + assert inp.is_verified + other = InputScope() + other.witness_utxo = TransactionOutput(1000000, inp.utxo.script_pubkey) + inp.update(other) + assert not inp.is_verified + with pytest.raises(PSBTError, match="Witness utxo doesn't match"): + psbt.verify() + + +@pytest.mark.parametrize("compress", [CompressMode.CLEAR_ALL, CompressMode.PARTIAL]) +def test_update_keeps_compressed_utxo_with_its_txhash(compress): + psbt, _ = funded_psbt(None) + psbt.inputs[0].witness_utxo = None + spk = psbt.inputs[0].non_witness_utxo.vout[0].script_pubkey + good = PSBT.parse(psbt.serialize(), compress=compress) + assert good.verify() + # unrelated previous transaction with an inflated amount + psbt.inputs[0].non_witness_utxo = Transaction( + vin=[TransactionInput(b"\x07" * 32, 0)], + vout=[TransactionOutput(1000000, spk)], + ) + evil = PSBT.parse(psbt.serialize(), compress=compress) + good.inputs[0].update(evil.inputs[0]) + assert not good.is_verified + with pytest.raises(PSBTError, match="txid doesn't match"): + good.verify() + assert not good.is_verified + + +def two_output_psbt(version): + psbt, key = funded_psbt(version) + spk = script.p2pkh(key.get_public_key()) + previous = psbt.inputs[0].non_witness_utxo + previous.vout = [TransactionOutput(10000, spk), TransactionOutput(1000000, spk)] + psbt.inputs[0].txid = previous.txid() + psbt.inputs[0].witness_utxo = None + return psbt, key + + +@pytest.mark.parametrize("version", [None, 2]) +@pytest.mark.parametrize("compress", [CompressMode.CLEAR_ALL, CompressMode.PARTIAL]) +@pytest.mark.parametrize("replacement", [None, "full", "compressed"]) +def test_update_output_index_cannot_reuse_cached_amount(version, compress, replacement): + original, key = two_output_psbt(version) + previous = original.inputs[0].non_witness_utxo + psbt = PSBT.parse(original.serialize(), compress=compress) + assert psbt.verify() + assert psbt.fee() == 1000 + other = InputScope() + other.vout = 1 + if replacement == "full": + other.non_witness_utxo = previous + elif replacement == "compressed": + original.inputs[0].vout = 1 + other = PSBT.parse(original.serialize(), compress=compress).inputs[0] + psbt.inputs[0].update(other) + assert not psbt.is_verified + if replacement is None: + with pytest.raises(PSBTError, match="Missing non_witness_utxo"): + psbt.verify() + assert not psbt.is_verified + assert psbt.inputs[0].utxo is None + with pytest.raises(PSBTError, match="Missing previous utxo"): + psbt.sign_with(key) + # Supplying the full previous transaction recovers the correct output. + other.non_witness_utxo = previous + psbt.inputs[0].update(other) + assert psbt.verify() + assert psbt.inputs[0].utxo.value == 1000000 + assert psbt.fee() == 991000 + assert psbt.sign_with(key) == 1 + actual = PSBT(psbt.tx) + actual.inputs[0].non_witness_utxo = previous + signature = psbt.inputs[0].partial_sigs[key.get_public_key()] + assert key.verify(ec.Signature.parse(signature[:-1]), actual.sighash(0)) + + +@pytest.mark.parametrize("version", [None, 2]) +@pytest.mark.parametrize("compress", [CompressMode.CLEAR_ALL, CompressMode.PARTIAL]) +def test_update_unchanged_prevout_keeps_usable_cache(version, compress): + original, _ = two_output_psbt(version) + psbt = PSBT.parse(original.serialize(), compress=compress) + assert psbt.verify() + psbt.inputs[0].update(InputScope(vin=psbt.inputs[0].vin)) + assert not psbt.is_verified + assert psbt.verify() + assert psbt.fee() == 1000 + + +@pytest.mark.parametrize("version", [None, 2]) +@pytest.mark.parametrize("compress", [CompressMode.CLEAR_ALL, CompressMode.PARTIAL]) +def test_update_rejects_incoming_cache_with_changed_index(version, compress): + original, _ = two_output_psbt(version) + incoming = PSBT.parse(original.serialize(), compress=compress).inputs[0] + incoming.vout = 1 + original.inputs[0].update(incoming) + with pytest.raises(PSBTError, match="Cached utxo doesn't match output index"): + original.verify() + assert not original.is_verified + + +@pytest.mark.parametrize("compress", [CompressMode.CLEAR_ALL, CompressMode.PARTIAL]) +def test_update_full_transaction_replaces_old_cached_hash(compress): + original, _ = two_output_psbt(None) + psbt = PSBT.parse(original.serialize(), compress=compress) + assert psbt.verify() + previous = original.inputs[0].non_witness_utxo + previous.vout[0].value = 20000 + original.inputs[0].txid = previous.txid() + psbt.inputs[0].update(original.inputs[0]) + assert not psbt.is_verified + assert psbt.verify() + assert psbt.fee() == 11000 + + +@pytest.mark.parametrize("compress", [CompressMode.CLEAR_ALL, CompressMode.PARTIAL]) +def test_update_full_transaction_is_not_shadowed_by_cache(compress): + original, _ = two_output_psbt(None) + psbt = PSBT.parse(original.serialize(), compress=compress) + assert psbt.verify() + other = InputScope() + other.non_witness_utxo = Transaction( + vin=[TransactionInput(b"\x07" * 32, 0)], + vout=[TransactionOutput(5, original.inputs[0].utxo.script_pubkey)], + ) + psbt.inputs[0].update(other) + with pytest.raises(PSBTError, match="txid doesn't match"): + psbt.verify() + assert not psbt.is_verified From baf8e8d1fe8ab1b3f30c32d947e59bc83fa1b3e4 Mon Sep 17 00:00:00 2001 From: odudex Date: Fri, 18 Sep 2026 14:11:07 -0300 Subject: [PATCH 28/44] ref(psbt): drop flag updates made redundant by the taproot signers sign_input_with_tapkey and _sign_taproot_keypath now update PSBT_GLOBAL_TX_MODIFIABLE for every signature they store, so the callers in PSBT.sign_with and PSBTView.sign_input no longer need to. --- src/embit/psbt.py | 4 +--- src/embit/psbtview.py | 2 -- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/src/embit/psbt.py b/src/embit/psbt.py index 1abbe09d..d189114d 100644 --- a/src/embit/psbt.py +++ b/src/embit/psbt.py @@ -1701,9 +1701,7 @@ def sign_with(self, root, sighash=SIGHASH.DEFAULT) -> int: inp, sighash=inp_sighash, ) - if tap_sigs > 0: - counter += tap_sigs - self._update_tx_modifiable(inp_sighash) + counter += tap_sigs continue # hash can be reused diff --git a/src/embit/psbtview.py b/src/embit/psbtview.py index 614cc07d..f441eb94 100644 --- a/src/embit/psbtview.py +++ b/src/embit/psbtview.py @@ -1212,8 +1212,6 @@ def sign_input( for pub, leaf in inp.taproot_sigs: ser_string(sig_stream, b"\x14" + pub.xonly() + leaf) ser_string(sig_stream, inp.taproot_sigs[(pub, leaf)]) - if counter > 0: - self._update_tx_modifiable(inp_sighash) return counter h = self.sighash(i, sighash=inp_sighash, input_scope=inp) From 527e34a919e7df7d4dd286396eeba1075af0fbae Mon Sep 17 00:00:00 2001 From: notTanveer Date: Tue, 29 Sep 2026 20:10:04 +0530 Subject: [PATCH 29/44] fix: blank legacy SIGHASH_SINGLE outputs the way core does - Serialize the blanked outputs with nValue -1 (ff x8) in Transaction and PSBTView instead of 0xFFFFFFFF - Add Bitcoin Core LegacySignatureHash vectors for SINGLE and SINGLE|ANYONECANPAY at input indexes 1 and 2 --- src/embit/psbtview.py | 2 +- src/embit/transaction.py | 2 +- tests/tests/test_psbtV2.py | 27 +++++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/embit/psbtview.py b/src/embit/psbtview.py index f441eb94..39af5c20 100644 --- a/src/embit/psbtview.py +++ b/src/embit/psbtview.py @@ -968,7 +968,7 @@ def sighash_legacy(self, input_index, script_pubkey, sighash=SIGHASH.ALL): # one output on the same index, others are empty elif sh == SIGHASH.SINGLE: h.update(compact.to_bytes(input_index + 1)) - empty = TransactionOutput(0xFFFFFFFF, Script(b"")).serialize() + empty = TransactionOutput(0xFFFFFFFFFFFFFFFF, Script(b"")).serialize() # this way we commit to input index for i in range(input_index): h.update(empty) diff --git a/src/embit/transaction.py b/src/embit/transaction.py index 9de9aca6..deccd9cb 100644 --- a/src/embit/transaction.py +++ b/src/embit/transaction.py @@ -340,7 +340,7 @@ def sighash_legacy(self, input_index, script_pubkey, sighash=SIGHASH.ALL): # one output on the same index, others are empty elif sh == SIGHASH.SINGLE: h.update(compact.to_bytes(input_index + 1)) - empty = TransactionOutput(0xFFFFFFFF, Script(b"")).serialize() + empty = TransactionOutput(0xFFFFFFFFFFFFFFFF, Script(b"")).serialize() # this way we commit to input index for i in range(input_index): h.update(empty) diff --git a/tests/tests/test_psbtV2.py b/tests/tests/test_psbtV2.py index 07d01ede..1b84cc79 100644 --- a/tests/tests/test_psbtV2.py +++ b/tests/tests/test_psbtV2.py @@ -1497,6 +1497,33 @@ def test_single_without_matching_output(self): ) +# 3 inputs, 3 p2pkh outputs, digests from Bitcoin Core's +# test_framework.script.LegacySignatureHash +LEGACY_SINGLE_TX = "020000000333333333333333333333333333333333333333333333333333333333333333330000000000feffffff34343434343434343434343434343434343434343434343434343434343434340100000000feffffff35353535353535353535353535353535353535353535353535353535353535350200000000feffffff03409c0000000000001976a914111111111111111111111111111111111111111188ac419c0000000000001976a914111111111111111111111111111111111111111188ac429c0000000000001976a914111111111111111111111111111111111111111188ac00000000" +LEGACY_SINGLE_SPK = "76a914111111111111111111111111111111111111111188ac" +# (txinIndex, hashType, sigHash) +LEGACY_SINGLE_SIGHASHES = [ + (1, 0x03, "0920d13615abe92f406a236c982d7c8f5eead1206a96e4644be4b5f20e946db9"), + (1, 0x83, "e85a08f4d63396e3fcd3eb7f1c7249eebc3df5553d21a4abdc19d3e25a2528e2"), + (2, 0x03, "f6bd1d0591f174778887715da118a7a0b0f5f617c3b3241a98eb31d8a8406cd7"), + (2, 0x83, "72c85150138e76c6f66799b77f1cda132dcdbdd07aedd803efb3ddaf12f375a3"), +] + + +class TestLegacySighashSingle: + """SIGHASH_SINGLE blanks earlier outputs as CTxOut() with nValue = -1""" + + @pytest.mark.parametrize("index, hash_type, expected", LEGACY_SINGLE_SIGHASHES) + def test_transaction_psbt_and_psbtview(self, index, hash_type, expected): + tx = Transaction.parse(unhexlify(LEGACY_SINGLE_TX)) + spk = Script(unhexlify(LEGACY_SINGLE_SPK)) + assert tx.sighash_legacy(index, spk, hash_type) == unhexlify(expected) + psbt = PSBT(tx) + assert psbt.sighash_legacy(index, spk, hash_type) == unhexlify(expected) + view = PSBTView.view(BytesIO(psbt.serialize())) + assert view.sighash_legacy(index, spk, hash_type) == unhexlify(expected) + + class TestZeroInputUnsignedTx: """BIP-174: the unsigned tx is non-witness serialized, so a PSBT whose transaction has no inputs yet starts its input count with a plain 0x00""" From ee3807d8aa432f71c9c192da96807140cfa7554e Mon Sep 17 00:00:00 2001 From: notTanveer Date: Tue, 29 Sep 2026 20:11:48 +0530 Subject: [PATCH 30/44] fix: fail compressed PSBTv2 parsing on streams that can't seek - Raise PSBTError when PSBT_IN_NON_WITNESS_UTXO arrives before the output index is known under compress, instead of buffering the whole previous transaction - Add a test that parses the same PSBTv2 from a read-only stream with and without compress --- src/embit/psbt.py | 8 +++++++- tests/tests/test_psbtV2.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/embit/psbt.py b/src/embit/psbt.py index d189114d..b2a68ede 100644 --- a/src/embit/psbt.py +++ b/src/embit/psbt.py @@ -527,8 +527,14 @@ def read_value(self, stream, k, version=None): if self.vout is not None else getattr(self, "_prescan_vout", None) ) + if self.compress and effective_vout is None: + # don't silently buffer the whole tx when compress was asked for + raise PSBTError( + "PSBT_IN_OUTPUT_INDEX unknown before PSBT_IN_NON_WITNESS_UTXO, " + "compressed parsing of PSBTv2 needs a seekable stream" + ) # we verified and saved utxo - if self.compress and effective_vout is not None: + if self.compress: txout, txhash = self.TX_CLS.read_vout(value_stream, effective_vout) value_stream.finish() self._txhash = txhash diff --git a/tests/tests/test_psbtV2.py b/tests/tests/test_psbtV2.py index 1b84cc79..38c97b3a 100644 --- a/tests/tests/test_psbtV2.py +++ b/tests/tests/test_psbtV2.py @@ -750,6 +750,39 @@ def test_psbtv2_compress_non_witness_utxo_is_key_order_independent(self): assert parsed.inputs[0]._utxo.value == 1234 assert parsed.verify() + def test_psbtv2_compress_rejects_non_seekable_stream(self): + """Without seek the prescan can't find 0x0f, so compress must fail + instead of silently buffering the whole prev tx""" + + class NoSeek: + def __init__(self, data): + self._s = BytesIO(data) + + def read(self, n=-1): + return self._s.read(n) + + prev = Transaction( + vin=[TransactionInput(bytes([1]) * 32, 0)], + vout=[TransactionOutput(1234, Script(b"\x51"))], + ) + psbt = PSBT.create_v2() + inp = InputScope() + inp.txid = prev.txid() + inp.vout = 0 + inp.non_witness_utxo = prev + psbt.add_input(inp) + out = OutputScope() + out.value = 1000 + out.script_pubkey = Script(b"\x51") + psbt.add_output(out) + raw = psbt.serialize() + + with pytest.raises(PSBTError): + PSBT.read_from(NoSeek(raw), compress=CompressMode.PARTIAL) + # without compress a non-seekable stream is fine + parsed = PSBT.read_from(NoSeek(raw)) + assert parsed.inputs[0].non_witness_utxo.txid() == prev.txid() + def kv(key, value): return compact.to_bytes(len(key)) + key + compact.to_bytes(len(value)) + value From 19d7ffbc1da024e92620a9395968d7552b03d123 Mon Sep 17 00:00:00 2001 From: notTanveer Date: Tue, 29 Sep 2026 20:12:34 +0530 Subject: [PATCH 31/44] fix: reject an output index past the end of non_witness_utxo - Check PSBT_IN_OUTPUT_INDEX against the full non_witness_utxo in InputScope.read_from, so KEEP_ALL raises PSBTError like compress does instead of IndexError when signing - Add a v0 and v2 test that covers PSBT.parse in both modes and PSBTView signing --- src/embit/psbt.py | 7 +++++++ tests/tests/test_psbtV2.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/embit/psbt.py b/src/embit/psbt.py index b2a68ede..19a6df40 100644 --- a/src/embit/psbt.py +++ b/src/embit/psbt.py @@ -871,6 +871,13 @@ def read_from(cls, stream, compress=CompressMode.KEEP_ALL, vin=None, version=Non break res._read_value_checked(stream, key, version=version) del res._prescan_vout + # compress mode checks this in read_vout, do the same for the full tx + if ( + res.non_witness_utxo is not None + and res.vout is not None + and res.vout >= len(res.non_witness_utxo.vout) + ): + raise PSBTError("Invalid vout index %d" % res.vout) return res diff --git a/tests/tests/test_psbtV2.py b/tests/tests/test_psbtV2.py index 38c97b3a..731aac83 100644 --- a/tests/tests/test_psbtV2.py +++ b/tests/tests/test_psbtV2.py @@ -1224,6 +1224,36 @@ def test_missing_utxo_is_psbterror(self): with pytest.raises(PSBTError): PSBT.parse(raw).fee() + def test_output_index_past_non_witness_utxo_is_psbterror(self): + """KEEP_ALL used to accept it and then fail with IndexError when signing""" + prev = Transaction( + vin=[TransactionInput(bytes([1]) * 32, 0)], + vout=[TransactionOutput(1234, Script(b"\x51"))], + ) + v0 = PSBT( + Transaction( + vin=[TransactionInput(prev.txid(), 5)], + vout=[TransactionOutput(1000, Script(b"\x51"))], + ) + ) + v0.inputs[0].non_witness_utxo = prev + v2 = PSBT.create_v2() + inp = InputScope() + inp.txid = prev.txid() + inp.vout = 5 + inp.non_witness_utxo = prev + v2.add_input(inp) + out = OutputScope() + out.value = 1000 + out.script_pubkey = Script(b"\x51") + v2.add_output(out) + for raw in (v0.serialize(), v2.serialize()): + for compress in (CompressMode.KEEP_ALL, CompressMode.PARTIAL): + with pytest.raises(PSBTError): + PSBT.parse(raw, compress=compress) + with pytest.raises(PSBTError): + PSBTView.view(BytesIO(raw)).sign_with(SIGNING_ROOT, BytesIO()) + class TestNonWitnessUtxoLength: """The declared PSBT_IN_NON_WITNESS_UTXO length must match the transaction""" From 3c485cdc4b810febd4875fbd4bc8fb5cc680bb7f Mon Sep 17 00:00:00 2001 From: notTanveer Date: Tue, 29 Sep 2026 20:13:59 +0530 Subject: [PATCH 32/44] fix: reject duplicate keys in input and output maps in every mode - Track the raw keys seen in InputScope.read_from and OutputScope.read_from, since compress skips values and bypasses the per-field duplicate checks - Catch duplicated PSET rangeproof keys, which LOutputScope overwrote or skipped without a check - Add tests for duplicated input keys under KEEP_ALL and PARTIAL and for a duplicated PSET rangeproof --- src/embit/psbt.py | 10 ++++++++ tests/tests/test_psbtV2.py | 51 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/src/embit/psbt.py b/src/embit/psbt.py index 19a6df40..40868b0f 100644 --- a/src/embit/psbt.py +++ b/src/embit/psbt.py @@ -864,11 +864,17 @@ def read_from(cls, stream, compress=CompressMode.KEEP_ALL, vin=None, version=Non break skip_string(stream) stream.seek(start_pos) + # compress mode skips some values, so it can't rely on the + # per-field duplicate checks in read_value() + seen = set() while True: key = read_string(stream) # separator if len(key) == 0: break + if key in seen: + raise PSBTError("Duplicated key") + seen.add(key) res._read_value_checked(stream, key, version=version) del res._prescan_vout # compress mode checks this in read_vout, do the same for the full tx @@ -1053,11 +1059,15 @@ def write_to(self, stream, skip_separator=False, version=None, **kwargs) -> int: @classmethod def read_from(cls, stream, compress=CompressMode.KEEP_ALL, vout=None, version=None): res = cls({}, vout=vout, compress=compress) + seen = set() while True: key = read_string(stream) # separator if len(key) == 0: break + if key in seen: + raise PSBTError("Duplicated key") + seen.add(key) res._read_value_checked(stream, key, version=version) return res diff --git a/tests/tests/test_psbtV2.py b/tests/tests/test_psbtV2.py index 731aac83..c576e706 100644 --- a/tests/tests/test_psbtV2.py +++ b/tests/tests/test_psbtV2.py @@ -1486,6 +1486,57 @@ def test_duplicate_non_prevout_key_rejected_when_hashing(self): view.sign_input(0, SIGNING_ROOT, sigs) assert sigs.getvalue() == b"" + @pytest.mark.parametrize( + "key, value", + [ + (b"\x00", "prev"), + (b"\x02" + SIGNING_ROOT.get_public_key().sec(), bytes(71)), + (b"\x07", b"\x00"), + (b"\x08", b"\x00"), + ], + ids=["non_witness_utxo", "partial_sig", "final_scriptsig", "final_witness"], + ) + @pytest.mark.parametrize("compress", [CompressMode.KEEP_ALL, CompressMode.PARTIAL]) + def test_duplicate_input_key_rejected_in_every_mode(self, key, value, compress): + """compress skips some values, the duplicate check must not depend on them""" + prev = Transaction( + vin=[TransactionInput(bytes([1]) * 32, 0)], + vout=[TransactionOutput(1234, Script(b"\x51"))], + ) + if value == "prev": + value = prev.serialize() + inp = ( + kv(b"\x0e", prev.txid()) + + kv(b"\x0f", (0).to_bytes(4, "little")) + + kv(key, value) + + kv(key, value) + ) + raw = raw_v2([inp], [V2_OUT]) + with pytest.raises(PSBTError): + PSBT.parse(raw, compress=compress) + with pytest.raises(PSBTError): + PSBTView.view(BytesIO(raw)).input(0, compress=compress) + + @pytest.mark.parametrize("compress", [CompressMode.KEEP_ALL, CompressMode.PARTIAL]) + def test_duplicate_pset_rangeproof_rejected(self, compress): + """LOutputScope stores or skips rangeproofs without a duplicate check""" + from embit.liquid.pset import PSET + from embit.liquid.psetview import PSETView + from .test_liquid import SIGN_PSET_V2_B64, SIGN_TPRV + from embit.bip32 import HDKey + + raw = PSET.from_string(SIGN_PSET_V2_B64).serialize() + view = PSETView.view(BytesIO(raw)) + off = view.seek_to_scope(view.num_inputs) # first output map + dup = kv(b"\xfc\x04pset\x04", b"abc") + raw = raw[:off] + dup + dup + raw[off:] + with pytest.raises(PSBTError): + PSET.parse(raw, compress=compress) + with pytest.raises(PSBTError): + PSETView.view(BytesIO(raw)).sign_with( + HDKey.from_string(SIGN_TPRV), BytesIO() + ) + # BIP-341 wallet-test-vectors.json, keyPathSpending[0] BIP341_RAW_TX = "02000000097de20cbff686da83a54981d2b9bab3586f4ca7e48f57f5b55963115f3b334e9c010000000000000000d7b7cab57b1393ace2d064f4d4a2cb8af6def61273e127517d44759b6dafdd990000000000fffffffff8e1f583384333689228c5d28eac13366be082dc57441760d957275419a418420000000000fffffffff0689180aa63b30cb162a73c6d2a38b7eeda2a83ece74310fda0843ad604853b0100000000feffffffaa5202bdf6d8ccd2ee0f0202afbbb7461d9264a25e5bfd3c5a52ee1239e0ba6c0000000000feffffff956149bdc66faa968eb2be2d2faa29718acbfe3941215893a2a3446d32acd050000000000000000000e664b9773b88c09c32cb70a2a3e4da0ced63b7ba3b22f848531bbb1d5d5f4c94010000000000000000e9aa6b8e6c9de67619e6a3924ae25696bb7b694bb677a632a74ef7eadfd4eabf0000000000ffffffffa778eb6a263dc090464cd125c466b5a99667720b1c110468831d058aa1b82af10100000000ffffffff0200ca9a3b000000001976a91406afd46bcdfd22ef94ac122aa11f241244a37ecc88ac807840cb0000000020ac9a87f5594be208f8532db38cff670c450ed2fea8fcdefcc9a663f78bab962b0065cd1d" From 6905eb99efa69a8bca8403e3eb005818afc23bd9 Mon Sep 17 00:00:00 2001 From: notTanveer Date: Tue, 29 Sep 2026 20:14:57 +0530 Subject: [PATCH 33/44] fix: validate each PSBTv2 map right after it is parsed - Run _validate_v2_input and _validate_v2_output inside the parse loops, so a huge declared count fails on the first bad map instead of allocating a scope per input byte first - Add a test with INPUT_COUNT 2^40 followed by empty maps --- src/embit/psbt.py | 34 ++++++++++++---------------------- tests/tests/test_psbtV2.py | 13 +++++++++++++ 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/src/embit/psbt.py b/src/embit/psbt.py index 40868b0f..a9eecf3d 100644 --- a/src/embit/psbt.py +++ b/src/embit/psbt.py @@ -1401,35 +1401,25 @@ def read_from(cls, stream, compress=CompressMode.KEEP_ALL): num_inputs = psbt._raw_input_count_from_global num_outputs = psbt._raw_output_count_from_global + # validate each map as it is parsed, so a huge declared count + # fails on the first bad map instead of after allocating them all parsed_inputs = [] - for _ in range(num_inputs): - parsed_inputs.append( - cls.PSBTIN_CLS.read_from( - stream, - compress=compress, - vin=None, - version=version, - ) + for i in range(num_inputs): + inp = cls.PSBTIN_CLS.read_from( + stream, compress=compress, vin=None, version=version ) - psbt.inputs = parsed_inputs - - for i, inp in enumerate(psbt.inputs): cls._validate_v2_input(inp, i) + parsed_inputs.append(inp) + psbt.inputs = parsed_inputs parsed_outputs = [] - for _ in range(num_outputs): - parsed_outputs.append( - cls.PSBTOUT_CLS.read_from( - stream, - compress=compress, - vout=None, - version=version, - ) + for i in range(num_outputs): + out = cls.PSBTOUT_CLS.read_from( + stream, compress=compress, vout=None, version=version ) - psbt.outputs = parsed_outputs - - for i, out in enumerate(psbt.outputs): cls._validate_v2_output(out, i) + parsed_outputs.append(out) + psbt.outputs = parsed_outputs else: temp_inputs = [] for i in range(len(psbt.inputs)): diff --git a/tests/tests/test_psbtV2.py b/tests/tests/test_psbtV2.py index c576e706..171c1742 100644 --- a/tests/tests/test_psbtV2.py +++ b/tests/tests/test_psbtV2.py @@ -988,6 +988,19 @@ def test_view_rejects_count_larger_than_maps(self): with pytest.raises(PSBTError): PSBTView.view(BytesIO(raw)) + def test_huge_count_fails_on_the_first_empty_map(self): + """every 0x00 byte would become an InputScope if maps were only + validated after all of them were parsed""" + g = ( + kv(b"\x02", (2).to_bytes(4, "little")) + + kv(b"\x04", compact.to_bytes(2**40)) + + kv(b"\x05", compact.to_bytes(0)) + + kv(b"\xfb", (2).to_bytes(4, "little")) + ) + raw = PSBT.MAGIC + g + b"\x00" + b"\x00" * 100000 + with pytest.raises(PSBTError, match="input 0 missing"): + PSBT.parse(raw) + def test_corrupted_nested_value_reports_key(self): # bip32 derivation with a 1-byte pubkey in the key raw = raw_v2([V2_IN + kv(b"\x06\x02", bytes(8))], [V2_OUT]) From 7f9686f7be081b8c5bd349f6beca69a9931f6c93 Mon Sep 17 00:00:00 2001 From: notTanveer Date: Tue, 29 Sep 2026 20:16:00 +0530 Subject: [PATCH 34/44] fix: check PSBT_GLOBAL_XPUB in PSBTView like PSBT.parse does - Parse the xpub key and check the value length in PSBTView.view without reading the value - Require a full 4-byte fingerprint in DerivationPath.read_from instead of accepting a short read - Check the garbage xpub case against PSBTView and add value length tests for both parsers --- src/embit/psbt.py | 2 ++ src/embit/psbtview.py | 9 +++++++++ tests/tests/test_psbtV2.py | 19 +++++++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/src/embit/psbt.py b/src/embit/psbt.py index a9eecf3d..202c8ea6 100644 --- a/src/embit/psbt.py +++ b/src/embit/psbt.py @@ -225,6 +225,8 @@ def write_to(self, stream, **kwargs) -> int: @classmethod def read_from(cls, stream): fingerprint = stream.read(4) + if len(fingerprint) != 4: + raise PSBTError("Invalid length") derivation = [] while True: r = stream.read(4) diff --git a/src/embit/psbtview.py b/src/embit/psbtview.py index 39af5c20..9f7afaef 100644 --- a/src/embit/psbtview.py +++ b/src/embit/psbtview.py @@ -23,6 +23,7 @@ from . import script from .script import Script, Witness from . import hashes +from . import bip32 from .psbt import ( PSBT, PSBTError, @@ -425,6 +426,14 @@ def view(cls, stream, offset=None, compress=CompressMode.KEEP_ALL): version = int.from_bytes(value, "little") else: value_len = read_compact(stream) + if key[:1] == b"\x01": + # same checks PSBT.parse does, without reading the value + if value_len < 4 or value_len % 4: + raise PSBTError("Invalid PSBT_GLOBAL_XPUB value length") + try: + bip32.HDKey.parse(key[1:]) + except _PARSE_ERRORS as e: + raise PSBTError("Invalid PSBT_GLOBAL_XPUB: %s" % e) header = len(compact.to_bytes(value_len)) deferred_kvs[key] = (cur + header, value_len) skip_exact(stream, value_len) diff --git a/tests/tests/test_psbtV2.py b/tests/tests/test_psbtV2.py index 171c1742..953d14f2 100644 --- a/tests/tests/test_psbtV2.py +++ b/tests/tests/test_psbtV2.py @@ -956,6 +956,25 @@ def test_malformed_xpub_global(self): raw = raw_v2([V2_IN], [V2_OUT], extra_globals=kv(b"\x01" + bytes(78), bytes(4))) with pytest.raises(PSBTError): PSBT.parse(raw) + with pytest.raises(PSBTError): + _exercise_view(raw) + + @pytest.mark.parametrize( + "value_len, valid", [(0, 0), (3, 0), (4, 1), (5, 0), (8, 1)] + ) + def test_xpub_global_value_length(self, value_len, valid): + """fingerprint plus whole 4-byte path elements, same in both parsers""" + xpub = SIGNING_ROOT.to_public().serialize() + extra = kv(b"\x01" + xpub, bytes(value_len)) + raw = raw_v2([V2_IN], [V2_OUT], extra_globals=extra) + if valid: + PSBT.parse(raw) + _exercise_view(raw) + else: + with pytest.raises(PSBTError): + PSBT.parse(raw) + with pytest.raises(PSBTError): + _exercise_view(raw) def test_corrupted_global_transaction(self): raw = a2b_base64(VIEW_PSBTS[0]) From 91e3c105f43fb7f5b7273d94fbaf5f26f9fc133f Mon Sep 17 00:00:00 2001 From: notTanveer Date: Tue, 29 Sep 2026 20:17:02 +0530 Subject: [PATCH 35/44] fix: match PSET rangeproof keys exactly in PSETView - Add an exact flag to PSBTView.seek_to_value, keeping the prefix match as the default for existing callers - Use exact lookups for the rangeproof and surjection proof keys in PSETView.hash_rangeproofs, so a longer unknown key is not hashed as a proof - Add a test that PSET and PSETView sign the same message with ALL|RANGEPROOF when such keys are present --- src/embit/liquid/psetview.py | 8 ++++---- src/embit/psbtview.py | 6 +++--- tests/tests/test_liquid.py | 20 ++++++++++++++++++++ 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/embit/liquid/psetview.py b/src/embit/liquid/psetview.py index 3362d151..2c28f46c 100644 --- a/src/embit/liquid/psetview.py +++ b/src/embit/liquid/psetview.py @@ -190,12 +190,12 @@ def hash_rangeproofs(self): for i in range(self.num_outputs): off = self.seek_to_scope(self.num_inputs + i) rangeproof_offset = self.seek_to_value( - b"\xfc\x04pset\x04", from_current=True + b"\xfc\x04pset\x04", from_current=True, exact=True ) if not rangeproof_offset: self.stream.seek(off) rangeproof_offset = self.seek_to_value( - b"\xfc\x08elements\x04", from_current=True + b"\xfc\x08elements\x04", from_current=True, exact=True ) if not rangeproof_offset: h.update(b"\x00") @@ -206,12 +206,12 @@ def hash_rangeproofs(self): self.stream.seek(off) surj_proof_offset = self.seek_to_value( - b"\xfc\x04pset\x05", from_current=True + b"\xfc\x04pset\x05", from_current=True, exact=True ) if not surj_proof_offset: self.stream.seek(off) surj_proof_offset = self.seek_to_value( - b"\xfc\x08elements\x05", from_current=True + b"\xfc\x08elements\x05", from_current=True, exact=True ) if not surj_proof_offset: h.update(b"\x00") diff --git a/src/embit/psbtview.py b/src/embit/psbtview.py index 9f7afaef..bbd6c0d1 100644 --- a/src/embit/psbtview.py +++ b/src/embit/psbtview.py @@ -778,9 +778,9 @@ def tx_version(self): self._tx_version = int.from_bytes(self._global_kvs[b"\x02"], "little") return self._tx_version - def seek_to_value(self, key_start, from_current=False): + def seek_to_value(self, key_start, from_current=False, exact=False): """ - Seeks to value with key starting with key_start. + Seeks to value with key starting with key_start (equal to it if exact=True). Returns offset - relative if from_current=True, absolute otherwise. If key is not found - returns None. """ @@ -796,7 +796,7 @@ def seek_to_value(self, key_start, from_current=False): if len(key) == 0: return None # matches - if key.startswith(key_start): + if key == key_start if exact else key.startswith(key_start): return off # continue to the next key off += skip_string(self.stream) diff --git a/tests/tests/test_liquid.py b/tests/tests/test_liquid.py index 1d6c80a3..f2cf00e3 100644 --- a/tests/tests/test_liquid.py +++ b/tests/tests/test_liquid.py @@ -148,6 +148,26 @@ def test_v2_output_requires_asset(self): with self.assertRaises(PSBTError): view.hash_outputs() + def test_rangeproof_sighash_ignores_longer_unknown_keys(self): + """PSET keeps these keys as unknown, PSETView must not hash them as proofs""" + from embit.liquid.transaction import LSIGHASH + + root = HDKey.from_string(SIGN_TPRV) + sighash = LSIGHASH.ALL | LSIGHASH.RANGEPROOF + for key in [b"\xfc\x04pset\x04\x00", b"\xfc\x04pset\x05\x00"]: + with self.subTest(key=key): + pset = PSET.from_string(SIGN_PSET_V2_B64) + pset.outputs[0].unknown[key] = b"not a proof" + raw = pset.serialize() + pset = PSET.parse(raw) + self.assertEqual(pset.sign_with(root, sighash=sighash), 1) + sigs = BytesIO() + view = PSETView.view(BytesIO(raw)) + self.assertEqual(view.sign_with(root, sigs, sighash=sighash), 1) + sigs.seek(0) + signed = LInputScope.read_from(sigs, version=2) + self.assertEqual(signed.partial_sigs, pset.inputs[0].partial_sigs) + def test_corrupted_global_transaction_is_psbterror(self): pset = PSET.from_string(PSET_V0_B64) raw = pset.serialize() From 07a431e615839fdbc79549d0e65267911a5564c7 Mon Sep 17 00:00:00 2001 From: notTanveer Date: Tue, 29 Sep 2026 20:18:47 +0530 Subject: [PATCH 36/44] refactor: reuse the shared signing helpers in PSBTView.sign_input - Replace the inline fingerprint and origin derivation code with resolve_signing_root and derive_hdkey from psbt.py - Add a PSBT vs PSBTView signing test with descriptor keys whose origin matches, doesn't match, or is public only --- src/embit/psbtview.py | 33 +++++++++------------------------ tests/tests/test_psbtV2.py | 25 +++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 24 deletions(-) diff --git a/src/embit/psbtview.py b/src/embit/psbtview.py index bbd6c0d1..86960459 100644 --- a/src/embit/psbtview.py +++ b/src/embit/psbtview.py @@ -32,6 +32,8 @@ OutputScope, LOCKTIME_THRESHOLD, choose_locktime, + derive_hdkey, + resolve_signing_root, next_tx_modifiable, read_string, ser_string, @@ -1109,19 +1111,9 @@ def sign_input( if i < 0 or i >= self.num_inputs: raise PSBTError("Invalid input number") - # if WIF - fingerprint is None - fingerprint = None - # if descriptor key - if hasattr(root, "origin"): - if not root.is_private: # pubkey can't sign - return 0 - if root.is_extended: # use fingerprint only for HDKey - fingerprint = root.fingerprint - else: - root = root.key # WIF key - # if HDKey - if not fingerprint and hasattr(root, "my_fingerprint"): - fingerprint = root.my_fingerprint + fingerprint, can_sign, root = resolve_signing_root(root) + if not can_sign: + return 0 rootpub = root.get_public_key() sec = rootpub.sec() @@ -1174,17 +1166,10 @@ def sign_input( # get derived keys for signing derived_keypairs = OrderedDict() # (prv, pub) for pub, derivation in bip32_derivations: - der = derivation.derivation - # descriptor key has origin derivation that we take into account - if hasattr(root, "origin"): - if root.origin: - if root.origin.derivation != der[: len(root.origin.derivation)]: - # derivation doesn't match - go to next input - continue - der = der[len(root.origin.derivation) :] - hdkey = root.key.derive(der) - else: - hdkey = root.derive(der) + hdkey = derive_hdkey(root, derivation) + if hdkey is None: + # derivation doesn't match - go to next candidate + continue if hdkey.xonly() != pub.xonly(): raise PSBTError("Derivation path doesn't look right") diff --git a/tests/tests/test_psbtV2.py b/tests/tests/test_psbtV2.py index 953d14f2..20a45703 100644 --- a/tests/tests/test_psbtV2.py +++ b/tests/tests/test_psbtV2.py @@ -1242,6 +1242,31 @@ def test_taproot_scriptpath_signing_matches(self): view.write_to(out, extra_input_streams=streams) assert psbt.serialize() == out.getvalue() + def test_descriptor_key_signing_matches(self): + """origin prefix handling is shared between PSBT and PSBTView""" + from embit.descriptor.arguments import Key + + fp = SIGNING_ROOT.my_fingerprint.hex() + keys = [ + # origin matches the input derivations + "[%s/84h/1h/0h]%s" % (fp, SIGNING_ROOT.derive("m/84h/1h/0h").to_base58()), + # origin doesn't match, nothing to sign + "[%s/49h/1h/0h]%s" % (fp, SIGNING_ROOT.derive("m/49h/1h/0h").to_base58()), + # public key can't sign + SIGNING_ROOT.to_public().to_base58(), + ] + raw = a2b_base64(VIEW_PSBTS[1]) + for key, expected in zip(keys, [3, 0, 0]): + key = Key.from_string(key) + psbt = PSBT.parse(raw) + assert psbt.sign_with(key) == expected + sigs = BytesIO() + assert PSBTView.view(BytesIO(raw)).sign_with(key, sigs) == expected + sigs.seek(0) + for inp in psbt.inputs: + signed = InputScope.read_from(sigs, version=psbt.version) + assert signed.partial_sigs == inp.partial_sigs + def test_missing_utxo_is_psbterror(self): raw = a2b_base64(VIEW_PSBTS[1]) psbt = PSBT.parse(raw) From bb1dba879de451f23f78adc520b63c67afb75611 Mon Sep 17 00:00:00 2001 From: notTanveer Date: Tue, 29 Sep 2026 20:19:26 +0530 Subject: [PATCH 37/44] refactor: stop updating the parsed global counts in add_input and add_output - Drop the _raw_input_count_from_global and _raw_output_count_from_global updates, nothing reads them after parsing and write_to uses len(inputs) and len(outputs) - Check the serialized counts in the constructor tests instead of the private attributes --- src/embit/psbt.py | 4 ---- tests/tests/test_psbtV2.py | 6 +++--- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/src/embit/psbt.py b/src/embit/psbt.py index 202c8ea6..139c1669 100644 --- a/src/embit/psbt.py +++ b/src/embit/psbt.py @@ -1814,8 +1814,6 @@ def add_input(self, input_scope): ): raise PSBTError("Cannot change locktime with existing signatures") self.inputs.append(input_scope) - if self.version == 2: - self._raw_input_count_from_global = len(self.inputs) def add_output(self, output_scope): if not self.is_outputs_modifiable(): @@ -1823,5 +1821,3 @@ def add_output(self, output_scope): if self.version == 2: self._validate_v2_output(output_scope, len(self.outputs)) self.outputs.append(output_scope) - if self.version == 2: - self._raw_output_count_from_global = len(self.outputs) diff --git a/tests/tests/test_psbtV2.py b/tests/tests/test_psbtV2.py index 20a45703..3c681de8 100644 --- a/tests/tests/test_psbtV2.py +++ b/tests/tests/test_psbtV2.py @@ -499,7 +499,7 @@ def test_add_input_appends_and_updates_count(self): inp.vout = 0 psbt.add_input(inp) assert len(psbt.inputs) == 1 - assert psbt._raw_input_count_from_global == 1 + assert len(PSBT.parse(psbt.serialize()).inputs) == 1 def test_add_input_requires_inputs_modifiable(self): """add_input() raises when INPUTS bit is clear""" @@ -556,7 +556,7 @@ def test_add_input_preserves_signed_locktime( with pytest.raises(PSBTError, match="existing signatures"): psbt.add_input(extra) assert psbt.serialize() == original - assert psbt._raw_input_count_from_global == 1 + assert len(PSBT.parse(original).inputs) == 1 else: psbt.add_input(extra) assert len(psbt.inputs) == 2 @@ -603,7 +603,7 @@ def test_add_output_appends_and_updates_count(self): out.script_pubkey = Script(b"\x00\x14" + bytes(20)) psbt.add_output(out) assert len(psbt.outputs) == 1 - assert psbt._raw_output_count_from_global == 1 + assert len(PSBT.parse(psbt.serialize()).outputs) == 1 def test_add_output_requires_outputs_modifiable(self): """add_output() raises when OUTPUTS bit is clear""" From 611cf510ab6b4f817ac9504836752c299bcefaaa Mon Sep 17 00:00:00 2001 From: odudex Date: Thu, 15 Oct 2026 10:11:33 -0300 Subject: [PATCH 38/44] changelog: legacy SIGHASH_SINGLE, PSBTv2 parsing and PSETView proof key fixes --- CHANGELOG.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d785e750..f9841f07 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -65,6 +65,23 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `PSBT_IN_TAP_BIP32_DERIVATION` and `PSBT_OUT_TAP_BIP32_DERIVATION` check the declared leaf hash count against the value length before building the list, so a crafted count cannot exhaust memory. +- Legacy `SIGHASH_SINGLE` sighashes blanked the preceding outputs with a + `0xffffffff` value instead of Bitcoin Core's `-1`, in `Transaction`, `PSBT` + and `PSBTView`, so such signatures on any input past the first were invalid. +- Compressed PSBTv2 parsing raises `PSBTError` when `PSBT_IN_OUTPUT_INDEX` + cannot be found before `PSBT_IN_NON_WITNESS_UTXO`, instead of silently + buffering the whole previous transaction. +- `PSBT_IN_OUTPUT_INDEX` past the end of `PSBT_IN_NON_WITNESS_UTXO` raises + `PSBTError` at parse time without compression too, instead of `IndexError` + when signing. +- Duplicate keys in input and output maps are rejected in every compression + mode, including PSET rangeproof keys. +- PSBTv2 input and output maps are validated as soon as each is parsed, so a + huge declared count fails on the first invalid map. +- `PSBTView` validates `PSBT_GLOBAL_XPUB` like `PSBT`, and BIP32 derivation + values must hold a full 4-byte fingerprint. +- `PSETView` matched rangeproof and surjection proof keys by prefix, so a + longer unknown key was hashed as a proof under `SIGHASH_RANGEPROOF`. ## [0.8.2] - 2026-08-08 From 9b33a67e13373f84655dcc801f2147870a54af4c Mon Sep 17 00:00:00 2001 From: odudex Date: Wed, 21 Oct 2026 14:24:50 -0300 Subject: [PATCH 39/44] fix: use exact reads in the Liquid transaction parser and honor segwit=False --- src/embit/liquid/transaction.py | 51 ++++++++++++++++------------- tests/tests/test_parsing.py | 58 +++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 23 deletions(-) diff --git a/src/embit/liquid/transaction.py b/src/embit/liquid/transaction.py index a3c544fa..9f08aa50 100644 --- a/src/embit/liquid/transaction.py +++ b/src/embit/liquid/transaction.py @@ -4,6 +4,7 @@ from ..script import Script, Witness from .. import hashes from ..transaction import * +from ..transaction import _read_exact from ..base import EmbitBase import hashlib @@ -119,7 +120,7 @@ def write_to(self, stream): @classmethod def read_from(cls, stream): l = compact.read_from(stream) - data = stream.read(l) + data = _read_exact(stream, l) return cls(data) @@ -255,11 +256,8 @@ def hash(self): def read_vout(cls, stream, idx): """Returns a tuple TransactionOutput, tx_hash without storing the whole tx in memory""" h = hashlib.sha256() - h.update(stream.read(4)) - has_witness = False - flag = stream.read(1) - if flag == b"\x01": - has_witness = True + h.update(_read_exact(stream, 4)) + has_witness = cls._read_witness_flag(stream) h.update(b"\x00") num_vin = compact.read_from(stream) h.update(compact.to_bytes(num_vin)) @@ -278,7 +276,7 @@ def read_vout(cls, stream, idx): if idx == i: res = vout h.update(vout.serialize()) - h.update(stream.read(4)) + h.update(_read_exact(stream, 4)) if has_witness: for i in range(num_vin): TxInWitness.read_from(stream) @@ -286,14 +284,21 @@ def read_vout(cls, stream, idx): TxOutWitness.read_from(stream) return res, hashlib.sha256(h.digest()).digest() + @staticmethod + def _read_witness_flag(stream): + flag = _read_exact(stream, 1) + if flag not in (b"\x00", b"\x01"): + raise TransactionError("Invalid witness flag") + return flag == b"\x01" + @classmethod def read_from(cls, stream, segwit=True): - # segwit= only mirrors Transaction.read_from: Liquid transactions - # carry an explicit witness flag byte, so nothing is ambiguous here - ver = int.from_bytes(stream.read(4), "little") - has_witness = False - if stream.read(1) == b"\x01": - has_witness = True + """segwit=False parses a transaction known to be serialized without + witness data (BIP-174's unsigned tx), so a set witness flag is an error.""" + ver = int.from_bytes(_read_exact(stream, 4), "little") + has_witness = cls._read_witness_flag(stream) + if has_witness and not segwit: + raise TransactionError("Unexpected witness data") num_vin = compact.read_from(stream) vin = [] for i in range(num_vin): @@ -302,7 +307,7 @@ def read_from(cls, stream, segwit=True): vout = [] for i in range(num_vout): vout.append(LTransactionOutput.read_from(stream)) - locktime = int.from_bytes(stream.read(4), "little") + locktime = int.from_bytes(_read_exact(stream, 4), "little") # something more if has_witness: for inp in vin: @@ -463,10 +468,10 @@ def write_to(self, stream, script_sig=None): @classmethod def read_from(cls, stream): - txid = bytes(reversed(stream.read(32))) - vout = int.from_bytes(stream.read(4), "little") + txid = bytes(reversed(_read_exact(stream, 32))) + vout = int.from_bytes(_read_exact(stream, 4), "little") script_sig = Script.read_from(stream) - sequence = int.from_bytes(stream.read(4), "little") + sequence = int.from_bytes(_read_exact(stream, 4), "little") is_pegin = False asset_issuance = None if vout != 0xFFFFFFFF: @@ -541,16 +546,16 @@ def unblind(self, blinding_key, message_length=64): @classmethod def read_from(cls, stream): - asset = stream.read(33) + asset = _read_exact(stream, 33) blinded = False ecdh_pubkey = None - c = stream.read(1) + c = _read_exact(stream, 1) if c != b"\x01": - value = c + stream.read(32) + value = c + _read_exact(stream, 32) else: - value = int.from_bytes(stream.read(8), "big") - c = stream.read(1) + value = int.from_bytes(_read_exact(stream, 8), "big") + c = _read_exact(stream, 1) if c != b"\x00": - ecdh_pubkey = c + stream.read(32) + ecdh_pubkey = c + _read_exact(stream, 32) script_pubkey = Script.read_from(stream) return cls(asset, value, script_pubkey, ecdh_pubkey) diff --git a/tests/tests/test_parsing.py b/tests/tests/test_parsing.py index 22dd229c..8ff14a8b 100644 --- a/tests/tests/test_parsing.py +++ b/tests/tests/test_parsing.py @@ -9,11 +9,14 @@ from embit.base import EmbitError from embit.psbt import CompressMode, InputScope, PSBT, PSBTError from embit.psbtview import GlobalTransactionView, PSBTView +from embit.liquid.pset import PSET from embit.liquid.psetview import GlobalLTransactionView from embit.liquid.transaction import ( LTransaction, LTransactionInput, LTransactionOutput, + Proof, + TxInWitness, ) from embit.script import Script, Witness from embit.transaction import ( @@ -499,6 +502,61 @@ def test_liquid_global_tx_scriptsig_must_be_empty(self): with self.assertRaises(PSBTError): access(view) + def liquid_transaction(self): + return LTransaction( + vin=[LTransactionInput(bytes(range(32)), 1)], + vout=[ + LTransactionOutput(bytes([1]) + bytes(range(32)), 42, Script(b"\x51")), + LTransactionOutput( + b"\x0a" + bytes(32), + b"\x08" + bytes(32), + Script(b"\x51"), + b"\x02" + bytes(32), + ), + ], + ) + + def test_liquid_fixed_fields(self): + raw = self.liquid_transaction().serialize() + # Version, witness flag, input txid, output index, sequence, explicit + # asset, value and empty nonce, confidential asset, value and nonce, + # locktime. + fields = [(0, 4), (4, 1), (6, 32), (38, 4), (43, 4), (48, 33), (81, 9)] + fields += [(90, 1), (93, 33), (126, 33), (159, 33), (194, 4)] + for offset, width in fields: + for reader in ( + LTransaction.read_from, + lambda s: LTransaction.read_from(s, segwit=False), + lambda s: LTransaction.read_vout(s, 0), + ): + with self.assertRaises(TransactionError): + reader(SequentialStream(raw, offset)) + for count in range(width): + with self.assertRaises(TransactionError): + reader(BytesIO(raw[: offset + count])) + # A cut locktime used to parse as a PSET with a different locktime. + payload = b"pset" + self.global_psbt(raw[:-1])[4:] + with self.assertRaises(PSBTError): + PSET.parse(payload) + + def test_liquid_witness_flag(self): + tx = self.liquid_transaction() + raw = tx.serialize() + self.assertEqual(LTransaction.parse(raw, segwit=False).serialize(), raw) + for flag in (b"\x02", b"\xff"): + for reader in ( + LTransaction.read_from, + lambda s: LTransaction.read_vout(s, 0), + ): + with self.assertRaises(TransactionError): + reader(BytesIO(raw[:4] + flag + raw[5:])) + tx.vin[0].witness = TxInWitness(Proof(b"\x01")) + raw = tx.serialize() + self.assertEqual(LTransaction.parse(raw).serialize(), raw) + # BIP-174's unsigned transaction is serialized without witness data + with self.assertRaises(TransactionError): + LTransaction.parse(raw, segwit=False) + def test_bounded_view_short_reads_and_output_length(self): raw = self.transaction().serialize() with self.assertRaises(PSBTError): From 159e86208d456ef270b465b133345ff285030cf1 Mon Sep 17 00:00:00 2001 From: odudex Date: Wed, 11 Nov 2026 09:58:35 -0300 Subject: [PATCH 40/44] fix: bound nested reads of in-memory PSBT values by the value length MicroPython allocates the requested size before reading, so a script or witness item length from corrupted bytes raised MemoryError instead of PSBTError. The global transaction, witness UTXO and final scriptwitness now parse through _BoundedReader. --- src/embit/psbt.py | 17 ++++++++++++++--- tests/tests/test_parsing.py | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/src/embit/psbt.py b/src/embit/psbt.py index 139c1669..fb97c82b 100644 --- a/src/embit/psbt.py +++ b/src/embit/psbt.py @@ -44,6 +44,17 @@ def finish(self): raise PSBTError("Trailing bytes in PSBT value") +def _parse_bounded(cls, data, **kwargs): + """cls.parse(data) that refuses any nested read longer than what is left + of data. MicroPython allocates the requested size before it reads, so a + script or witness item length taken from corrupted bytes would otherwise + raise MemoryError instead of a parsing error.""" + stream = _BoundedReader(BytesIO(data), len(data)) + res = cls.read_from(stream, **kwargs) + stream.finish() + return res + + class CompressMode: KEEP_ALL = 0 CLEAR_ALL = 1 @@ -557,7 +568,7 @@ def read_value(self, stream, k, version=None): elif self.witness_utxo is not None: raise PSBTError("Duplicated utxo value") else: - self.witness_utxo = self.TXOUT_CLS.parse(v) + self.witness_utxo = _parse_bounded(self.TXOUT_CLS, v) # partial signature elif k[0] == 0x02: # we don't need this key for signing @@ -622,7 +633,7 @@ def read_value(self, stream, k, version=None): if len(k) != 1: raise PSBTError("Invalid final scriptwitness key") elif self.final_scriptwitness is None: - self.final_scriptwitness = Witness.parse(v) + self.final_scriptwitness = _parse_bounded(Witness, v) else: raise PSBTError("Duplicated final scriptwitness") @@ -1388,7 +1399,7 @@ def read_from(cls, stream, compress=CompressMode.KEEP_ALL): try: # BIP-174: the unsigned tx is serialized without witness data, # so a leading zero is an empty input list, not a segwit marker - tx_for_v0 = cls.TX_CLS.parse(tx_bytes, segwit=False) + tx_for_v0 = _parse_bounded(cls.TX_CLS, tx_bytes, segwit=False) except _PARSE_ERRORS as e: raise PSBTError("Invalid global transaction: %s" % e) # BIP-174: the global transaction must be unsigned diff --git a/tests/tests/test_parsing.py b/tests/tests/test_parsing.py index 8ff14a8b..572b61d6 100644 --- a/tests/tests/test_parsing.py +++ b/tests/tests/test_parsing.py @@ -5,7 +5,7 @@ from io import BytesIO from unittest import TestCase -from embit import compact +from embit import compact, psbt as psbt_module from embit.base import EmbitError from embit.psbt import CompressMode, InputScope, PSBT, PSBTError from embit.psbtview import GlobalTransactionView, PSBTView @@ -392,6 +392,41 @@ def test_scriptsig_length_bit_flips(self): with self.assertRaises(PSBTError): PSBTView.view(BytesIO(bytes(mutant))) + def test_nested_lengths_are_bounded_by_the_value(self): + # MicroPython allocates the requested size before reading, so a script + # or witness item length from corrupted bytes must be refused, never + # passed on to the stream holding the value. + huge = b"\xfe\xff\xff\xff\xff" + raw = self.transaction().serialize() + no_inputs = bytes(4) + b"\x00\x01" + bytes(8) + huge + bytes(4) + payloads = [self.global_psbt(no_inputs)] + for key, value in ((b"\x01", bytes(8) + huge), (b"\x08", b"\x01" + huge)): + payloads.append( + self.global_psbt(raw)[:-2] + + b"\x01" + + key + + compact.to_bytes(len(value)) + + value + + b"\x00\x00" + ) + streams = [] + + def tracked(data=b""): + streams.append(TrackingStream(data)) + return streams[-1] + + psbt_module.BytesIO = tracked + try: + for payload in payloads: + with self.assertRaises(PSBTError): + PSBT.read_from(BytesIO(payload)) + finally: + psbt_module.BytesIO = BytesIO + # the global transaction of every payload and the two input values + self.assertEqual(len(streams), len(payloads) + 2) + for stream in streams: + self.assertTrue(stream.max_read <= len(raw)) + def bit_flip_mutants(self, base): """Every single-bit flip, then fixed pseudo-random 2 and 3 bit flips.""" for position in range(len(base) * 8): From a21459ab572a46ab4d6f65a05579c160dd7c0a6e Mon Sep 17 00:00:00 2001 From: odudex Date: Wed, 11 Nov 2026 10:06:10 -0300 Subject: [PATCH 41/44] fix(pset): accept PSETv2 fee outputs that omit PSBT_OUT_SCRIPT The PSET spec makes the script optional for L-BTC fee outputs. An explicit output without a script now parses, serializes back without the field, and gets the empty script in the transaction, so PSET and PSETView sign it like an explicitly empty one. --- src/embit/liquid/pset.py | 22 +++++++++++++++++---- tests/tests/test_liquid.py | 39 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 4 deletions(-) diff --git a/src/embit/liquid/pset.py b/src/embit/liquid/pset.py index 7e5c319c..6c6bd774 100644 --- a/src/embit/liquid/pset.py +++ b/src/embit/liquid/pset.py @@ -333,12 +333,18 @@ def clear_metadata(self, *args, **kwargs): self.asset = None self.blinder_index = None + @property + def _tx_script_pubkey(self): + # an omitted script stays None in the scope, so the map serializes + # back as it was parsed; in the transaction it is the empty script + return Script(b"") if self.script_pubkey is None else self.script_pubkey + @property def vout(self): return LTransactionOutput( self.asset or self.asset_commitment, self.value if self.value is not None else self.value_commitment, - self.script_pubkey, + self._tx_script_pubkey, None if self.asset else self.ecdh_pubkey, ) @@ -347,7 +353,7 @@ def blinded_vout(self): return LTransactionOutput( self.asset_commitment or self.asset, self.value_commitment or self.value, - self.script_pubkey, + self._tx_script_pubkey, self.ecdh_pubkey, ( None @@ -515,7 +521,15 @@ def _validate_v2_output(cls, out, i): raise PSBTError( "PSBTv2 output %d missing required PSBT_OUT_AMOUNT (0x03)" % i ) - if out.script_pubkey is None: + # PSET: PSBT_OUT_SCRIPT is not mandatory for L-BTC fee outputs, which + # are explicit: no commitments and not marked for blinding + if out.script_pubkey is None and ( + out.value is None + or out.asset is None + or out.value_commitment is not None + or out.asset_commitment is not None + or out.blinding_pubkey is not None + ): raise PSBTError( "PSBTv2 output %d missing required PSBT_OUT_SCRIPT (0x04)" % i ) @@ -540,7 +554,7 @@ def txseed(self, seed: bytes): bytes(reversed(inp.txid)) + inp.vout.to_bytes(4, "little") for inp in self.inputs ] - ) + b"".join([out.script_pubkey.serialize() for out in self.outputs]) + ) + b"".join([out._tx_script_pubkey.serialize() for out in self.outputs]) return hashes.tagged_hash("liquid/txseed", seed + data) def blind(self, seed: bytes): diff --git a/tests/tests/test_liquid.py b/tests/tests/test_liquid.py index f2cf00e3..fbd010c2 100644 --- a/tests/tests/test_liquid.py +++ b/tests/tests/test_liquid.py @@ -136,6 +136,45 @@ def test_v2_locktime_and_sequence(self): signed = LInputScope.read_from(sigs, version=2) self.assertEqual(signed.partial_sigs, pset.inputs[0].partial_sigs) + def test_v2_fee_output_without_script(self): + """PSET lets an L-BTC fee output omit PSBT_OUT_SCRIPT""" + root = HDKey.from_string(SIGN_TPRV) + explicit = PSET.from_string(SIGN_PSET_V2_B64) + pset = PSET.from_string(SIGN_PSET_V2_B64) + self.assertEqual(pset.outputs[2].script_pubkey.data, b"") + pset.outputs[2].script_pubkey = None + raw = pset.serialize() + # key and empty value are gone + self.assertEqual(len(raw), len(explicit.serialize()) - 3) + # parses and serializes back without the field + pset = PSET.parse(raw) + self.assertEqual(pset.outputs[2].script_pubkey, None) + self.assertEqual(pset.serialize(), raw) + view = PSETView.view(BytesIO(raw)) + self.assertEqual(view.output(2).serialize(), pset.outputs[2].serialize()) + # the transaction is the one an explicitly empty script gives + self.assertEqual(pset.tx.serialize(), explicit.tx.serialize()) + self.assertEqual(pset.blinded_tx.serialize(), explicit.blinded_tx.serialize()) + self.assertEqual(pset.fee(), explicit.fee()) + self.assertEqual(pset.txseed(bytes(32)), explicit.txseed(bytes(32))) + # so PSET and PSETView sign it the same way + self.assertEqual(explicit.sign_with(root), 1) + self.assertEqual(pset.sign_with(root), 1) + self.assertEqual(pset.inputs[0].partial_sigs, explicit.inputs[0].partial_sigs) + sigs = BytesIO() + self.assertEqual(view.sign_with(root, sigs), 1) + sigs.seek(0) + signed = LInputScope.read_from(sigs, version=2) + self.assertEqual(signed.partial_sigs, explicit.inputs[0].partial_sigs) + # a blinded output is not a fee output and still needs its script + pset = PSET.from_string(SIGN_PSET_V2_B64) + pset.outputs[1].script_pubkey = None + raw = pset.serialize() + with self.assertRaises(PSBTError): + PSET.parse(raw) + with self.assertRaises(PSBTError): + PSETView.view(BytesIO(raw)).output(1) + def test_v2_output_requires_asset(self): pset = PSET.from_string(SIGN_PSET_V2_B64) pset.outputs[0].asset = None From 6dfa18c67181071801a37ff2504c9ecfcadd37bf Mon Sep 17 00:00:00 2001 From: odudex Date: Wed, 11 Nov 2026 10:10:19 -0300 Subject: [PATCH 42/44] fix(psbt): drop a stale non_witness_utxo when update() adopts another cache Updating a full input scope from a compressed scope for a different previous transaction kept the old full transaction next to the new cached hash, so verify() passed on the cache while serialization wrote the stale transaction. --- src/embit/psbt.py | 7 +++++ tests/tests/test_psbt_verification.py | 44 +++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/src/embit/psbt.py b/src/embit/psbt.py index fb97c82b..b22bac0a 100644 --- a/src/embit/psbt.py +++ b/src/embit/psbt.py @@ -399,6 +399,13 @@ def update(self, other): self._utxo = other._utxo self._txhash = other._txhash self._utxo_vout = other._utxo_vout + # verify() and utxo go by the cache from here on, so a retained + # full transaction it was not streamed from must not be written + if ( + self.non_witness_utxo is not None + and self.non_witness_utxo.hash() != self._txhash + ): + self.non_witness_utxo = None elif prevout_changed or other.non_witness_utxo is not None: # A full replacement transaction can be checked afresh. Without # one, the old cached output cannot authenticate the new prevout. diff --git a/tests/tests/test_psbt_verification.py b/tests/tests/test_psbt_verification.py index 4a8ffc47..2a2376a3 100644 --- a/tests/tests/test_psbt_verification.py +++ b/tests/tests/test_psbt_verification.py @@ -249,3 +249,47 @@ def test_update_full_transaction_is_not_shadowed_by_cache(compress): with pytest.raises(PSBTError, match="txid doesn't match"): psbt.verify() assert not psbt.is_verified + + +@pytest.mark.parametrize("compress", [CompressMode.CLEAR_ALL, CompressMode.PARTIAL]) +@pytest.mark.parametrize("witness_utxo", [False, True]) +def test_update_from_compressed_scope_drops_other_full_transaction( + compress, witness_utxo +): + psbt, _ = funded_psbt(None) + psbt.inputs[0].witness_utxo = None + stale = psbt.inputs[0].non_witness_utxo + spk = stale.vout[0].script_pubkey + # the input moved to an output of a different previous transaction + previous = Transaction( + vin=[TransactionInput(b"\x07" * 32, 0)], + vout=[TransactionOutput(20000, spk)], + ) + replacement = PSBT( + Transaction(vin=[TransactionInput(previous.txid(), 0)], vout=psbt.tx.vout) + ) + replacement.inputs[0].non_witness_utxo = previous + if witness_utxo: + replacement.inputs[0].witness_utxo = previous.vout[0] + incoming = PSBT.parse(replacement.serialize(), compress=compress).inputs[0] + assert incoming.non_witness_utxo is None + psbt.inputs[0].update(incoming) + assert psbt.verify() + assert psbt.fee() == 11000 + # the transaction the cache was not streamed from is not written back + assert psbt.inputs[0].non_witness_utxo is None + reparsed = PSBT.parse(psbt.serialize()) + assert reparsed.inputs[0].txid == previous.txid() + assert reparsed.inputs[0].non_witness_utxo is None + assert not reparsed.verify(ignore_missing=True) + + +@pytest.mark.parametrize("compress", [CompressMode.CLEAR_ALL, CompressMode.PARTIAL]) +def test_update_from_compressed_scope_keeps_its_full_transaction(compress): + psbt, _ = funded_psbt(None) + previous = psbt.inputs[0].non_witness_utxo + incoming = PSBT.parse(psbt.serialize(), compress=compress).inputs[0] + psbt.inputs[0].update(incoming) + assert psbt.inputs[0].non_witness_utxo is previous + assert psbt.verify() + assert PSBT.parse(psbt.serialize()).verify() From 829fa0aeb8e19fb895e4e3445112e20c3ca88da0 Mon Sep 17 00:00:00 2001 From: odudex Date: Wed, 11 Nov 2026 10:13:29 -0300 Subject: [PATCH 43/44] test: keep the non-witness UTXO key in the length mismatch fixture The replacement dropped the key type byte, so every case failed on unrelated framing errors and the view never reached input(0). The expected error is now pinned for each case. --- tests/tests/test_psbtV2.py | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/tests/tests/test_psbtV2.py b/tests/tests/test_psbtV2.py index 3c681de8..bce9e679 100644 --- a/tests/tests/test_psbtV2.py +++ b/tests/tests/test_psbtV2.py @@ -1328,16 +1328,28 @@ def test_length_mismatch_rejected(self, delta, compress): if delta > 0: # pad so the value itself is still present, the framing is off bad += b"\x00" * delta - raw = raw.replace(good, b"\x01" + bad, 1) - with pytest.raises(PSBTError): - PSBT.parse(raw, compress=compress) - # the shifted framing is caught by the scope walk in view() or, - # when the framing happens to line up, by the value parse - with pytest.raises(PSBTError): + raw = raw.replace(good, b"\x01\x00" + bad, 1) + if delta > 0: + # the map framing is intact, so both parsers reach the + # transaction and find the bytes left after it + with pytest.raises(PSBTError, match="Trailing bytes in PSBT value"): + PSBT.parse(raw, compress=compress) view = PSBTView.view(BytesIO(raw), compress=compress) - view.input(0) - view.vin(0) - view.sighash(0) + with pytest.raises(PSBTError, match="Trailing bytes in PSBT value"): + view.input(0) + continue + # a short length cuts the transaction and shifts the framing of the + # rest of the map, which the compressed PSBTv2 parse notices first, + # when it scans the map for the output index + if psbt.version == 2 and compress != CompressMode.KEEP_ALL: + error = "PSBT_IN_OUTPUT_INDEX unknown" + else: + error = "Read exceeds declared value length" + with pytest.raises(PSBTError, match=error): + PSBT.parse(raw, compress=compress) + # view() walks every map up front and finds the shifted framing + with pytest.raises(PSBTError, match="Unexpected extra bytes"): + PSBTView.view(BytesIO(raw), compress=compress) class TestTxModifiableAfterSigning: From 6b7b281950b22857c2eabd6d3946b844fbe1a93f Mon Sep 17 00:00:00 2001 From: odudex Date: Wed, 11 Nov 2026 10:33:25 -0300 Subject: [PATCH 44/44] fix(psbtview): write only new signatures to the signature stream sign_with() calls sign_input() once per private key of a descriptor, and each call wrote the input's existing key-path signature, final witness, leaf signatures and partial signatures again, so write_to() failed with a duplicated key. The stream now holds what the call made; existing signatures still come through the merge in write_to(). --- CHANGELOG.md | 6 ++ src/embit/psbtview.py | 11 ++++ tests/tests/test_psbtV2.py | 129 +++++++++++++++++++++++++++++++++++++ 3 files changed, 146 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f9841f07..b93db73d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 encodings, as Bitcoin Core does. - `PSBTView` hashes prevouts, sequences and outputs in a single pass and caches the per-input utxo data used by taproot sighashes. +- `PSBTView.sign_input()` and `sign_with()` write only the signatures they + make to the signature stream. Signatures the input already has are kept by + `write_to()` as before, but are no longer repeated in the stream. ### Fixed @@ -82,6 +85,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 values must hold a full 4-byte fingerprint. - `PSETView` matched rangeproof and surjection proof keys by prefix, so a longer unknown key was hashed as a proof under `SIGHASH_RANGEPROOF`. +- `PSBTView.sign_with()` with a descriptor holding several private keys + repeated an input's existing signatures in the signature stream, so + `write_to()` failed with a duplicated key. ## [0.8.2] - 2026-08-08 diff --git a/src/embit/psbtview.py b/src/embit/psbtview.py index 86960459..206e2f4a 100644 --- a/src/embit/psbtview.py +++ b/src/embit/psbtview.py @@ -1120,6 +1120,14 @@ def sign_input( pkh = hashes.hash160(sec) inp = self.input(i) + # The sig_stream gets what this call adds to the input. Signatures the + # input already has reach the output through the merge in write_to(); + # sign_with() calls this once per descriptor key, and writing them + # again each time would repeat their keys in the stream. + inp.partial_sigs = OrderedDict() + inp.taproot_sigs = OrderedDict() + inp.taproot_key_sig = None + inp.final_scriptwitness = None if extra_scope_data is not None: inp.update(extra_scope_data) @@ -1238,6 +1246,9 @@ def sign_with(self, root, sig_stream, sighash=SIGHASH.DEFAULT) -> int: so if PSBT is asking to sign with a different sighash this function won't sign. If you want to sign with sighashes provided in the PSBT - set sighash=None. + The sig_stream holds the signatures made here, not the ones the inputs + already have: write_to() keeps those when it merges the stream. + The sig_stream only carries per-input fields. For PSBTv2 the signer also has to update PSBT_GLOBAL_TX_MODIFIABLE (BIP-370); that update is kept on this view (tx_modifiable_flags) and emitted by write_to(), so diff --git a/tests/tests/test_psbtV2.py b/tests/tests/test_psbtV2.py index bce9e679..d41922dc 100644 --- a/tests/tests/test_psbtV2.py +++ b/tests/tests/test_psbtV2.py @@ -1352,6 +1352,135 @@ def test_length_mismatch_rejected(self, delta, compress): PSBTView.view(BytesIO(raw), compress=compress) +class TestViewSigningWithExistingSignatures: + """A descriptor with several private keys signs an input once per key, + and the sig_stream must not repeat what the input already has""" + + COMPRESS = [CompressMode.KEEP_ALL, CompressMode.CLEAR_ALL, CompressMode.PARTIAL] + + @pytest.mark.parametrize("version", [None, 2]) + @pytest.mark.parametrize("compress", COMPRESS) + @pytest.mark.parametrize( + "existing", ["key_sig", "final_witness", "key_path", "leaf_sig"] + ) + def test_taproot(self, version, compress, existing): + from .test_taproot import TAPTREE_PSBT, TAPTREE_KEYS + + class LeafKeys: + keys = TAPTREE_KEYS[2:] + + psbt = PSBT.from_string(TAPTREE_PSBT) + if version == 2: + psbt = to_v2(psbt) + inp = psbt.inputs[0] + if existing == "leaf_sig": + assert psbt.sign_with(TAPTREE_KEYS[1]) == 1 + else: + # key-path signing fills both fields + assert psbt.sign_with(TAPTREE_KEYS[0]) == 1 + if existing == "key_sig": + inp.final_scriptwitness = None + elif existing == "final_witness": + inp.taproot_key_sig = None + key_sig = inp.taproot_key_sig + witness = inp.final_scriptwitness + leaf_sigs = dict(inp.taproot_sigs) + raw = psbt.serialize() + assert psbt.sign_with(LeafKeys) == 2 + + view = PSBTView.view(BytesIO(raw), compress=compress) + sigs = BytesIO() + assert view.sign_with(LeafKeys, sigs) == 2 + # only what this signer made is in the stream + sigs.seek(0) + signed = InputScope.read_from(sigs, version=version) + assert signed.taproot_key_sig is None + assert signed.final_scriptwitness is None + assert len(signed.taproot_sigs) == 2 + for key in signed.taproot_sigs: + assert key not in leaf_sigs + assert signed.taproot_sigs[key] == inp.taproot_sigs[key] + # and what was there comes through the merge + sigs.seek(0) + out = BytesIO() + view.write_to(out, extra_input_streams=[sigs]) + merged = PSBT.parse(out.getvalue()).inputs[0] + assert merged.taproot_key_sig == key_sig + assert merged.taproot_sigs == inp.taproot_sigs + if compress == CompressMode.KEEP_ALL: + # a compressed view does not read final scripts + assert merged.final_scriptwitness == witness + + @pytest.mark.parametrize("version", [None, 2]) + def test_new_key_path_signature_is_emitted_once(self, version): + from .test_taproot import TAPTREE_PSBT, TAPTREE_KEYS + + class AllKeys: + keys = TAPTREE_KEYS + + psbt = PSBT.from_string(TAPTREE_PSBT) + if version == 2: + psbt = to_v2(psbt) + raw = psbt.serialize() + assert psbt.sign_with(AllKeys) == 4 + view = PSBTView.view(BytesIO(raw)) + sigs = BytesIO() + assert view.sign_with(AllKeys, sigs) == 4 + sigs.seek(0) + out = BytesIO() + view.write_to(out, extra_input_streams=[sigs]) + merged = PSBT.parse(out.getvalue()).inputs[0] + assert merged.taproot_key_sig == psbt.inputs[0].taproot_key_sig + assert merged.final_scriptwitness == psbt.inputs[0].final_scriptwitness + assert merged.taproot_sigs == psbt.inputs[0].taproot_sigs + + @pytest.mark.parametrize("version", [None, 2]) + @pytest.mark.parametrize("compress", COMPRESS) + def test_partial_sigs(self, version, compress): + signer, *others = [ec.PrivateKey(bytes([n]) * 32) for n in (1, 2, 3)] + + class Cosigners: + keys = others + + witness_script = script.multisig( + 2, [k.get_public_key() for k in [signer] + others] + ) + utxo = TransactionOutput(10000, script.p2wsh(witness_script)) + psbt = PSBT( + Transaction( + vin=[TransactionInput(bytes(range(32)), 0)], + vout=[TransactionOutput(9000, Script(b"\x51"))], + ) + ) + psbt.inputs[0].witness_utxo = utxo + psbt.inputs[0].witness_script = witness_script + if version == 2: + psbt = to_v2(psbt) + inp = psbt.inputs[0] + assert psbt.sign_with(signer) == 1 + first = signer.get_public_key() + raw = psbt.serialize() + assert psbt.sign_with(Cosigners) == 2 + + view = PSBTView.view(BytesIO(raw), compress=compress) + sigs = BytesIO() + assert view.sign_with(Cosigners, sigs) == 2 + sigs.seek(0) + signed = InputScope.read_from(sigs, version=version) + assert len(signed.partial_sigs) == 2 + assert first not in signed.partial_sigs + sigs.seek(0) + out = BytesIO() + view.write_to(out, extra_input_streams=[sigs]) + merged = PSBT.parse(out.getvalue()).inputs[0] + expected = dict(inp.partial_sigs) + if compress != CompressMode.KEEP_ALL: + # a compressed view does not read partial signatures, so its + # output only ever carries the ones it made + del expected[first] + assert merged.partial_sigs == expected + + class TestTxModifiableAfterSigning: """PSBT_GLOBAL_TX_MODIFIABLE after a real sign_with, in PSBT and PSBTView"""