diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b11788d..b93db73d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,90 @@ 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. + +### 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. +- `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 + +- `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 + 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. +- 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`. +- 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. +- `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`. +- `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 ### Changed 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/pset.py b/src/embit/liquid/pset.py index ab46d014..6c6bd774 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, @@ -29,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 @@ -126,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, ) @@ -135,15 +133,15 @@ 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), ) - 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") @@ -246,7 +244,7 @@ def write_to(self, stream, skip_separator=False, **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 @@ -335,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, ) @@ -349,12 +353,14 @@ 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 - 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) + ) ), ) @@ -386,9 +392,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 +509,37 @@ class PSET(PSBT): PSBTOUT_CLS = LOutputScope TX_CLS = LTransaction + @classmethod + 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 + ) + # 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 + ) + # 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) @@ -517,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): @@ -638,7 +675,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/src/embit/liquid/psetview.py b/src/embit/liquid/psetview.py index 7d692304..2c28f46c 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 @@ -45,7 +46,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 @@ -67,7 +68,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 @@ -99,8 +112,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: @@ -110,7 +123,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): @@ -123,6 +136,7 @@ class PSETView(PSBTView): PSBTIN_CLS = LInputScope PSBTOUT_CLS = LOutputScope TX_CLS = GlobalLTransactionView + PSBT_CLS = PSET def clear_cache(self): # cache for digests @@ -133,6 +147,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 @@ -166,33 +190,33 @@ 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") else: - l = compact.read_from(self.stream) + l = read_compact(self.stream) h.update(compact.to_bytes(l)) self._hash_to(h, l) 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") 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() @@ -260,7 +284,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/liquid/transaction.py b/src/embit/liquid/transaction.py index ca7af0a5..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,12 +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): - ver = int.from_bytes(stream.read(4), "little") - has_witness = False - if stream.read(1) == b"\x01": - has_witness = True + def read_from(cls, stream, segwit=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): @@ -300,7 +307,7 @@ def read_from(cls, stream): 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: @@ -461,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: @@ -539,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/src/embit/psbt.py b/src/embit/psbt.py index 0f374f15..b22bac0a 100644 --- a/src/embit/psbt.py +++ b/src/embit/psbt.py @@ -1,5 +1,10 @@ from collections import OrderedDict -from .transaction import Transaction, TransactionOutput, TransactionInput, SIGHASH +from .transaction import ( + Transaction, + TransactionOutput, + TransactionInput, + SIGHASH, +) from . import compact from . import bip32 from . import ec @@ -11,6 +16,8 @@ from binascii import b2a_base64, a2b_base64, hexlify, unhexlify from io import BytesIO +LOCKTIME_THRESHOLD = 500000000 + class PSBTError(EmbitError): pass @@ -37,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 @@ -57,6 +75,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): @@ -85,30 +106,128 @@ 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: - l = compact.read_from(stream) - s = stream.read(l) +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: + 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 +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) + + +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 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")) @@ -117,6 +236,8 @@ def write_to(self, stream) -> 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) @@ -131,9 +252,9 @@ def read_from(cls, stream): class PSBTScope(EmbitBase): V2_FIELDS = () - def __init__(self, unknown: dict = {}): - self.unknown = unknown - self.parse_unknowns() + def __init__(self, unknown: dict = None, version=None): + self.unknown = {} if unknown is None else unknown + self.parse_unknowns(version) def write_to(self, stream, skip_separator=False, **kwargs) -> int: # unknown @@ -146,19 +267,30 @@ 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(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.""" + 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)) - 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, *args, **kwargs): + def read_value(self, stream, key, version=None): # separator if len(key) == 0: return @@ -182,7 +314,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, version=version) return res @@ -191,7 +323,13 @@ 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, + version=None, + ): self.compress = compress self.txid = None self.vout = None @@ -200,11 +338,11 @@ 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 self._txhash = None + self._utxo_vout = None self._verified = False self.partial_sigs = OrderedDict() self.sighash_type = None @@ -222,7 +360,9 @@ def __init__(self, unknown: dict = {}, vin=None, compress=CompressMode.KEEP_ALL) self.final_scriptsig = None self.final_scriptwitness = None - self.parse_unknowns() + self.required_time_locktime = None + self.required_height_locktime = None + super().__init__(unknown, version=version) def clear_metadata(self, compress=CompressMode.CLEAR_ALL): """Removes metadata like derivations, utxos etc except final or partial sigs""" @@ -245,13 +385,42 @@ 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 + # 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. + 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 @@ -260,18 +429,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 @@ -295,7 +478,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. @@ -304,6 +488,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)) @@ -311,6 +496,24 @@ def verify(self, ignore_missing=False): else self.non_witness_utxo.txid() ) 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 ( + 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: @@ -319,7 +522,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 @@ -331,14 +534,32 @@ def read_value(self, stream, k): elif self.non_witness_utxo is not None: raise PSBTError("Duplicated utxo value") else: - length = compact.read_from(stream) + # 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; + # 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) + ) + 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 self.txid and self.vout is not None: - txout, txhash = self.TX_CLS.read_vout(value_stream, self.vout) + if self.compress: + txout, txhash = self.TX_CLS.read_vout(value_stream, effective_vout) 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() @@ -354,7 +575,7 @@ def read_value(self, stream, k): 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 @@ -419,17 +640,68 @@ def read_value(self, stream, k): 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") + # 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 @@ -461,10 +733,10 @@ def read_value(self, stream, k): 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) - leaf_hashes = [b.read(32) for i in range(num_leaf_hashes)] - if not all([len(leaf) == 32 for leaf in leaf_hashes]): + num_leaf_hashes = read_compact(b) + 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) @@ -493,8 +765,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 @@ -513,14 +786,30 @@ 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")) + r += _ser_uint(stream, b"\x10", self.sequence, 4, "PSBT_IN_SEQUENCE") + if self.required_time_locktime is not None: + r += _ser_uint( + stream, + b"\x11", + self.required_time_locktime, + 4, + "PSBT_IN_REQUIRED_TIME_LOCKTIME", + ) + if self.required_height_locktime is not None: + r += _ser_uint( + stream, + b"\x12", + self.required_height_locktime, + 4, + "PSBT_IN_REQUIRED_HEIGHT_LOCKTIME", + ) # PSBT_IN_TAP_KEY_SIG if self.taproot_key_sig is not None: @@ -567,24 +856,79 @@ 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: + 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: + break + if k == b"\x0f": + v = read_string(stream) + if len(v) == 4: + res._prescan_vout = int.from_bytes(v, "little") + # otherwise let the real parse raise the precise error + 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 + 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 + 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, + version=None, + ): 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, version=version) def clear_metadata(self, compress=CompressMode.CLEAR_ALL): """Removes metadata like derivations, utxos etc except final or partial sigs""" @@ -605,13 +949,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 +989,23 @@ def read_value(self, stream, k): else: self.bip32_derivations[pub] = DerivationPath.parse(v) + # PSBTv2 fields elif k == b"\x03": + 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") + # 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: + 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 @@ -657,10 +1017,10 @@ def read_value(self, stream, k): 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) - leaf_hashes = [b.read(32) for i in range(num_leaf_hashes)] - if not all([len(leaf) == 32 for leaf in leaf_hashes]): + num_leaf_hashes = read_compact(b) + 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) @@ -683,8 +1043,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) @@ -714,6 +1076,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, 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 + + +class TxModifiable: + INPUTS = 0b00000001 + OUTPUTS = 0b00000010 + 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" @@ -722,17 +1119,50 @@ 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 _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.""" + 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 + ) + + 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 +1172,59 @@ 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] + @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 + return choose_locktime( + *self._classify_locktimes(self.inputs), fallback=self.locktime or 0 + ) + + def _validate_locktime_compatibility(self, inputs): + """Return the locktime, rejecting incompatible input requirements.""" + return choose_locktime( + *self._classify_locktimes(inputs), fallback=self.locktime or 0 + ) + @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 +1262,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,18 +1280,27 @@ 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 - r += ser_string(stream, b"\x02") - r += ser_string(stream, tx_version.to_bytes(4, "little")) + tx_version = self.tx_version if self.tx_version is not None else 2 + 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))) - r += ser_string(stream, b"\xfb") - r += ser_string(stream, self.version.to_bytes(4, "little")) + if self.tx_modifiable_flags is not None: + 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 for key in self.unknown: r += ser_string(stream, key) @@ -825,6 +1315,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) @@ -853,91 +1351,196 @@ 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 + ) + + # 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 + # 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) + tx_bytes = global_kvs.pop(b"\x00") # Remove so it's not in unknown + 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 = _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 + for inp in tx_for_v0.vin: + if len(inp.script_sig.data) > 0: raise PSBTError( - "Failed to parse PSBT - duplicated transaction field" + "Global transaction input has a non-empty scriptSig" ) - 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 - ) + 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 + + # 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 i in range(num_inputs): + inp = cls.PSBTIN_CLS.read_from( + stream, compress=compress, vin=None, version=version + ) + cls._validate_v2_input(inp, i) + parsed_inputs.append(inp) + psbt.inputs = parsed_inputs + + parsed_outputs = [] + for i in range(num_outputs): + out = cls.PSBTOUT_CLS.read_from( + stream, compress=compress, vout=None, version=version + ) + cls._validate_v2_output(out, i) + parsed_outputs.append(out) + psbt.outputs = parsed_outputs + 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 + @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: + 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)) + 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": - 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") + self.tx_version = int.from_bytes(v, "little") 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 = self._parse_count( + 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 = self._parse_count( + 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] + 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, @@ -969,6 +1572,22 @@ 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]) + self._update_tx_modifiable(sighash) + return 1 + def sign_input_with_tapkey( self, key: ec.PrivateKey, @@ -984,18 +1603,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()) @@ -1021,6 +1630,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 @@ -1041,19 +1651,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 +1689,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 +1705,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 +1720,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 +1728,13 @@ 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, ) + counter += tap_sigs continue # hash can be reused @@ -1150,13 +1744,98 @@ 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 + self.tx_modifiable_flags = next_tx_modifiable( + self.tx_modifiable_flags, inp_sighash + ) + + 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 + ): + 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) + + 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) diff --git a/src/embit/psbtview.py b/src/embit/psbtview.py index d641248b..206e2f4a 100644 --- a/src/embit/psbtview.py +++ b/src/embit/psbtview.py @@ -13,25 +13,37 @@ 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 from . import script from .script import Script, Witness from . import hashes +from . import bip32 from .psbt import ( + PSBT, PSBTError, CompressMode, InputScope, OutputScope, + LOCKTIME_THRESHOLD, + choose_locktime, + derive_hdkey, + resolve_signing_root, + next_tx_modifiable, read_string, ser_string, skip_string, + read_compact, + read_exact, + skip_exact, + _PARSE_ERRORS, _GLOBAL_COUNT_FIELDS, _GLOBAL_FIXED_VALUE_LENGTHS, - _GLOBAL_V2_FIELDS, _validate_global_fields, _validate_global_key, ) @@ -45,17 +57,18 @@ 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 = 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 @@ -161,7 +174,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 @@ -171,7 +184,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 @@ -212,18 +225,42 @@ 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) - return TransactionInput.read_from(self.stream) + 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""" 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: @@ -233,7 +270,7 @@ def vout(self, i): while n: self._skip_output() n -= 1 - return TransactionOutput.read_from(self.stream) + return self._read_vout() class PSBTView: @@ -247,6 +284,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, @@ -259,6 +301,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,10 +318,41 @@ 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). + # 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. + 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._tx_locktime = None + self._utxo_cache = None self._hash_prevouts = None self._hash_sequence = None self._hash_outputs = None @@ -302,7 +376,12 @@ def view(cls, stream, offset=None, compress=CompressMode.KEEP_ALL): num_outputs = None tx_offset = None tx_len = None - global_fields = {} + # Buffer only the bounded global fields needed for PSBTv2 validation + # and transaction handling. + global_kvs = OrderedDict() + # 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 key = read_string(stream) @@ -311,52 +390,75 @@ 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 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 > cur + 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: - cur += skip_string(stream) + if key in global_kvs or key in deferred_kvs: + raise PSBTError("Duplicate global key: %s" % hexlify(key).decode()) + 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 = 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) + cur += header + value_len 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) + # 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_fields[b"\x04"]) - num_outputs = compact.from_bytes(global_fields[b"\x05"]) + 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( + if version == 2: + global_kvs.update(deferred_kvs) + res = cls( stream, num_inputs, num_outputs, @@ -366,7 +468,15 @@ def view(cls, stream, offset=None, compress=CompressMode.KEEP_ALL): tx_offset, compress, 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(res.num_inputs + res.num_outputs) + if len(stream.read(1)) > 0: + raise PSBTError("Unexpected extra bytes") + return res def _skip_scope(self): off = 0 @@ -381,6 +491,34 @@ def _skip_scope(self): off += skip_string(self.stream) return off + 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. + + 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 + 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 + if len(key) == 0: + 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: + 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. @@ -412,9 +550,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""" @@ -424,9 +565,62 @@ 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 + + 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 + ) + if len(v) != 32: + raise PSBTError("PSBT_IN_PREVIOUS_TXID must be 32 bytes") + txid = bytes(reversed(v)) + + v = found.get(b"\x0f") + if v is 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") + + v = found.get(b"\x10") + 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) + + 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 + ) + 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") + + v = found.get(b"\x04") + if v is None: + raise PSBTError( + "PSBTv2 output %d missing required PSBT_OUT_SCRIPT (0x04)" % i + ) + return TransactionOutput(value, Script(v)) # compress is not used here, but may be used by subclasses (liquid) def vin(self, i, compress=None): @@ -434,20 +628,68 @@ def vin(self, i, compress=None): 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) - txid = bytes(reversed(v)) + found = self._scan_scope_values(self._V2_VIN_KEYS, self.PSBTIN_CLS) + return self._v2_vin(found, i) - self.seek_to_scope(i) - v = self.get_value(b"\x0f", from_current=True) - vout = int.from_bytes(v, "little") + 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) - self.seek_to_scope(i) - v = self.get_value(b"\x10", from_current=True) or b"\xFF\xFF\xFF\xFF" - sequence = int.from_bytes(v, "little") + 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) - return TransactionInput(txid, vout, sequence=sequence) + 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): @@ -455,34 +697,92 @@ def vout(self, i, compress=None): 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) - value = int.from_bytes(v, "little") - - self.seek_to_scope(self.num_inputs + i) - v = self.get_value(b"\x04", from_current=True) - script_pubkey = Script(v) - - return TransactionOutput(value, script_pubkey) + found = self._scan_scope_values(self._V2_VOUT_KEYS, self.PSBTOUT_CLS) + return self._v2_vout(found, i) @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 + """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._tx_locktime = self._determine_locktime_v2() + else: + self._tx_locktime = self._locktime or 0 + return self._tx_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. + """ + # 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 = [] + inputs_with_requirements = 0 + + # 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"), self.PSBTIN_CLS) + v_height = found.get(b"\x12") + v_time = found.get(b"\x11") + + 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 + + return choose_locktime( + height_locktimes, time_locktimes, inputs_with_requirements, fallback + ) + @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 + # 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): + 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. """ @@ -498,7 +798,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) @@ -508,30 +808,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 @@ -568,7 +869,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)) @@ -580,7 +881,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")) @@ -589,7 +892,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( @@ -639,7 +945,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() @@ -662,8 +968,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: @@ -674,7 +979,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) @@ -682,27 +987,22 @@ 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 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 = [] - scripts = [] - # TODO: not very optimal. Maybe use build_cache() or something? - for idx in range(self.num_inputs): - inp = self.input(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, @@ -749,16 +1049,15 @@ 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) - 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]) + self._update_tx_modifiable(sighash) # no need to sign anything else return 1 counter = 0 @@ -773,6 +1072,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, @@ -786,9 +1086,17 @@ 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 + def _update_tx_modifiable(self, inp_sighash: int) -> None: + if self.version != 2: + return + 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 ) -> int: @@ -803,25 +1111,23 @@ 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() 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) @@ -852,7 +1158,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: @@ -868,17 +1174,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") @@ -908,6 +1207,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) @@ -923,11 +1225,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]) @@ -941,6 +1245,14 @@ 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 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 + write the signed PSBT with the same view that produced the signatures. """ counter = 0 for i in range(self.num_inputs): @@ -978,15 +1290,47 @@ 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 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) + res = len(self.MAGIC) + 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) + 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: + # 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 +1341,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..deccd9cb 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": @@ -240,7 +243,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 +254,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( @@ -332,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_liquid.py b/tests/tests/test_liquid.py index cc8a290b..fbd010c2 100644 --- a/tests/tests/test_liquid.py +++ b/tests/tests/test_liquid.py @@ -2,7 +2,10 @@ 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.psbt import PSBTError +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 +31,13 @@ ) +SIGN_TPRV = "tprv8ZgxMBicQKsPf27gmh4DbQqN2K6xnXA7m7AeceqQVGkRYny3X49sgcufzbJcq4k5eaGZDMijccdDzvQga2Saqd78dKqN52QwLyqgY8apX3j" +# unsigned PSETv0 with one input +PSET_V0_B64 = "cHNldP8BAOUCAAAAAAGuRyPZXPN6wSbiWqD1H2SPAc71iny/ypyV8WCEVan99wAAAAAA/f///wMBbVIcOOweoVc0riK3xGBkQSgpwNBXnwpxPRwE7el5Am8BAAAAAAAAFX4AFgAU0f9GbzioopmlUxwIEtw2A7bBoqcBbVIcOOweoVc0riK3xGBkQSgpwNBXnwpxPRwE7el5Am8BAAAAAAAAB9AAF6kUhbF7AcbaCecOBk/Gxt9QgPuv5WmHAW1SHDjsHqFXNK4it8RgZEEoKcDQV58KcT0cBO3peQJvAQAAAAAAAAD5AAAAAAAAAAEBegoqZjByOhLSsE8bxsVLenapedhf/F6WMSVZzaoBDjblOAlS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55yQE1auU5B8yILP/T4mQBIwf8BHBzZXQIBAAAAAAA" + + @skipUnless( _LIQUID_AVAILABLE, "Liquid/ZKP backend unavailable; pure-Python fallback in use", @@ -92,18 +102,125 @@ 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+JkASMBAAAAAACYloAAFgAUisfI8pzbq6cwbzmkzqfy3Ks4y2QBGGls8j4yCaboRHnQHxP+eckBNWrlOQfMiCz/0+JkASMBAAd18FluqWoAFgAUA2V92wLjWqu3hH/ptYAJmo6/MUQBGGls8j4yCaboRHnQHxP+eckBNWrlOQfMiCz/0+JkASMBAAAAAAAAABYAAAoAAAABAUIBGGls8j4yCaboRHnQHxP+eckBNWrlOQfMiCz/0+JkASMBAAAAAACYloAAFgAUisfI8pzbq6cwbzmkzqfy3Ks4y2QiBgOhnuyHYV5cZ5bX/jWEgTl4SyWP3oVJy8Pp95KqiRmsChjRKMetVAAAgAEAAIAAAACAAAAAAAIAAAABDiB7ALW7kD6wSBz+JHoahpFXiVehkjlkCgG9IUZQP80H4wEPBAAAAAABEAT9////ACICA4u6NSSv6aHUr4gObKCSK+Ej17xHjEeyVcoEcIN11l0tGNEox61UAACAAQAAgAAAAIABAAAAAwAAAAEEFgAUlI/NhKVhaPasthkSL1rf+Mu50z0BAwgniZUAAAAAAAf8BHBzZXQCIBhpbPI+Mgmm6ER50B8T/nnJATVq5TkHzIgs/9PiZAEjB/wEcHNldAgEAAAAAAABBBYAFASJSHYjLEnVdj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E9huhpIMMUv9nMpOGXxgdGOJaAM3jrCkrmutyNeJ/7i6d7qomlY1rt7nZTZ9AsLQqn19sOqgxm/OrCVwJRD9dBxvlpI0cGo0n7kPSQ74gD24AawSukdh6CYtxIKjo19BRIz4R9aJBktUgxsDPrvb3LxCd0s0prRvoqs/KhPZh+RmmlEoFbXXtrQZNt3CbKFa91shs89zqEeoOoUTw2udiekZhf47tORf5NfLiim31ssglpFShlHch4RCYRvvB0s9bfnZZzpMRyXwivR07CSTSkZEQYr3ZaQrv+8Ttv6iJWosJEAEQDLkEeR9JxuTNB/wEcHNldAVDAQABpo4NtvKS8tTm+XBSMxVgJ9n0/PyoNzYupvwN/jTxowK7wpU9wq3zG8ZjXuNP9iBDA16miq7iyqE/agp1CQmujwf8BHBzZXQGIQO76oHHphQNAVUtYNp3LMyLbHDhacNdtHz6sq+P0SLD6Qf8BHBzZXQHIQPZdPeFpD4dsI+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_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 + 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_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() + # 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_parsing.py b/tests/tests/test_parsing.py index b09d26c1..572b61d6 100644 --- a/tests/tests/test_parsing.py +++ b/tests/tests/test_parsing.py @@ -5,15 +5,18 @@ 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 +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 ( @@ -109,8 +112,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 +256,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 +301,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): @@ -376,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): @@ -486,6 +537,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): 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 new file mode 100644 index 00000000..d41922dc --- /dev/null +++ b/tests/tests/test_psbtV2.py @@ -0,0 +1,1912 @@ +""" +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, a2b_base64 +from io import BytesIO +from embit.psbt import ( + PSBT, + PSBTError, + TxModifiable, + InputScope, + OutputScope, + CompressMode, +) +from embit.psbtview import PSBTView +from embit.script import Script, Witness +from embit import compact, ec, script +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 +SIGNABLE_V2_B64 = VIEW_PSBTS[1] +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""" + + def test_invalid_psbtv0_with_version_field(self): + """PSBTv0 but with PSBT_GLOBAL_VERSION set to 2 should be invalid""" + hex_data = "70736274ff01007102000000010b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc80000000000feffffff020008af2f00000000160014c430f64c4756da310dbd1a085572ef299926272c8bbdeb0b00000000160014a07dac8ab6ca942d379ed795f835ba71c9cc68850000000001fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e01086b02473044022005275a485734e0ae1f3b971237586f0e72dc85833d278c0e474cd23112c0fa5e02206b048c83cebc3c41d0b93cc7da76185cedbd030d005b08018be2b98bbacbdf7b012103760dcca05f3997dc65b293060f7f29f1514c8c527048e12802b041d4fc340a2700220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a000000002202036efe2c255621986553ba9d65c3ddc64165ca1436e05aa35a4c6eb02451cf796d18f69d873e540000800100008000000080010000006200000000" + + 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" + + 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" + + 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" + + 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" + + 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" + + 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" + + 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" + + assert_invalid(hex_data) + + def test_invalid_psbtv0_with_sequence(self): + """PSBTv0 but with PSBT_IN_SEQUENCE should be invalid""" + hex_data = "70736274ff01007102000000010b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc80000000000feffffff020008af2f00000000160014c430f64c4756da310dbd1a085572ef299926272c8bbdeb0b00000000160014a07dac8ab6ca942d379ed795f835ba71c9cc688500000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e01086b02473044022005275a485734e0ae1f3b971237586f0e72dc85833d278c0e474cd23112c0fa5e02206b048c83cebc3c41d0b93cc7da76185cedbd030d005b08018be2b98bbacbdf7b012103760dcca05f3997dc65b293060f7f29f1514c8c527048e12802b041d4fc340a27011004ffffffff00220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a000000002202036efe2c255621986553ba9d65c3ddc64165ca1436e05aa35a4c6eb02451cf796d18f69d873e540000800100008000000080010000006200000000" + + 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" + + 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" + + assert_invalid(hex_data) + + def test_invalid_psbtv0_with_output_amount(self): + """PSBTv0 but with PSBT_OUT_AMOUNT should be invalid""" + hex_data = "70736274ff01007102000000010b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc80000000000feffffff020008af2f00000000160014c430f64c4756da310dbd1a085572ef299926272c8bbdeb0b00000000160014a07dac8ab6ca942d379ed795f835ba71c9cc688500000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e01086b02473044022005275a485734e0ae1f3b971237586f0e72dc85833d278c0e474cd23112c0fa5e02206b048c83cebc3c41d0b93cc7da76185cedbd030d005b08018be2b98bbacbdf7b012103760dcca05f3997dc65b293060f7f29f1514c8c527048e12802b041d4fc340a2700220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f00000000002202036efe2c255621986553ba9d65c3ddc64165ca1436e05aa35a4c6eb02451cf796d18f69d873e540000800100008000000080010000006200000000" + + assert_invalid(hex_data) + + def test_invalid_psbtv0_with_output_script(self): + """PSBTv0 but with PSBT_OUT_SCRIPT should be invalid""" + hex_data = "70736274ff01007102000000010b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc80000000000feffffff020008af2f00000000160014c430f64c4756da310dbd1a085572ef299926272c8bbdeb0b00000000160014a07dac8ab6ca942d379ed795f835ba71c9cc688500000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e01086b02473044022005275a485734e0ae1f3b971237586f0e72dc85833d278c0e474cd23112c0fa5e02206b048c83cebc3c41d0b93cc7da76185cedbd030d005b08018be2b98bbacbdf7b012103760dcca05f3997dc65b293060f7f29f1514c8c527048e12802b041d4fc340a2700220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000104160014a07dac8ab6ca942d379ed795f835ba71c9cc6885002202036efe2c255621986553ba9d65c3ddc64165ca1436e05aa35a4c6eb02451cf796d18f69d873e540000800100008000000080010000006200000000" + + assert_invalid(hex_data) + + def test_invalid_psbtv2_global_unsigned_tx(self): + """PSBTv2 with PSBT_GLOBAL_UNSIGNED_TX should be invalid""" + hex_data = "70736274ff0100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e00000000010204020000000103040000000001040101010501020106010701fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f0400000000011004feffffff0111048c8dc4620112041027000000220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c00220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" + + assert_invalid(hex_data) + + def test_invalid_psbtv2_missing_input_count(self): + """PSBTv2 missing PSBT_GLOBAL_INPUT_COUNT should be invalid""" + hex_data = "70736274ff01020402000000010304000000000105010201fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f0400000000011004feffffff00220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c00220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" + + assert_invalid(hex_data) + + def test_invalid_psbtv2_missing_output_count(self): + """PSBTv2 missing PSBT_GLOBAL_OUTPUT_COUNT should be invalid""" + hex_data = "70736274ff01020402000000010304000000000104010101fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f0400000000011004feffffff00220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c00220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" + + assert_invalid(hex_data) + + def test_invalid_psbtv2_missing_tx_version(self): + """PSBTv2 missing PSBT_GLOBAL_TX_VERSION should be invalid""" + hex_data = "70736274ff010401010105010201fb040200000000010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f0400000000000103080008af2f000000000104160014c430f64c4756da310dbd1a085572ef299926272c000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" + + 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" + + 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" + + assert_invalid(hex_data) + + def test_invalid_psbtv2_missing_output_amount(self): + """PSBTv2 output missing PSBT_OUT_AMOUNT should be invalid""" + hex_data = "70736274ff0102040200000001030400000000010401010105010201fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f0400000000011004feffffff00220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000104160014c430f64c4756da310dbd1a085572ef299926272c00220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" + + assert_invalid(hex_data) + + def test_invalid_psbtv2_missing_output_script(self): + """PSBTv2 output missing PSBT_OUT_SCRIPT should be invalid""" + hex_data = "70736274ff0102040200000001030400000000010401010105010201fb0402000000000100520200000001c1aa256e214b96a1822f93de42bff3b5f3ff8d0519306e3515d7515a5e805b120000000000ffffffff0118c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e0000000001011f18c69a3b00000000160014b0a3af144208412693ca7d166852b52db0aef06e010e200b0ad921419c1c8719735d72dc739f9ea9e0638d1fe4c1eef0f9944084815fc8010f0400000000011004feffffff00220202d601f84846a6755f776be00e3d9de8fb10acc935fb83c45fb0162d4cad5ab79218f69d873e540000800100008000000080000000002a0000000103080008af2f0000000000220202e36fbff53dd534070cf8fd396614680f357a9b85db7340bf1cfa745d2ad7b34018f69d873e54000080010000800000008001000000640000000103088bbdeb0b0000000001041600144dd193ac964a56ac1b9e1cca8454fe2f474f851300" + + 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" + + 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" + + 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" + + 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 = parse_valid(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 = parse_valid(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 = parse_valid(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 = parse_valid(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 = parse_valid(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 = parse_valid(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 = parse_valid(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 = parse_valid(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 = parse_valid(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 = parse_valid(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 = parse_valid(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 = parse_valid(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 = parse_valid(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 = parse_valid(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 = 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 = 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 = 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 = parse_valid(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 = parse_valid(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 = parse_valid(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 = parse_valid(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 = parse_valid(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 = parse_valid(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 = parse_valid(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 len(PSBT.parse(psbt.serialize()).inputs) == 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) + + @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 len(PSBT.parse(original).inputs) == 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() + out = OutputScope() + out.value = 100000 + out.script_pubkey = Script(b"\x00\x14" + bytes(20)) + psbt.add_output(out) + assert len(psbt.outputs) == 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""" + 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_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) + 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 = 0xFFFFFFFF + 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() + + 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 + + +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"" + + +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) + 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): + for raw in self.SAMPLES: + # 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) + 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) + 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]) + # 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) + + @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 + 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_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]) + 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() + + +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() + + @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( + {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_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) + 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() + + 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""" + + @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\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) + 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 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""" + + @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", + [ + (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 + + +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"" + + @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" +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 + ) + + +# 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""" + + 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 + + +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)) diff --git a/tests/tests/test_psbt_verification.py b/tests/tests/test_psbt_verification.py new file mode 100644 index 00000000..2a2376a3 --- /dev/null +++ b/tests/tests/test_psbt_verification.py @@ -0,0 +1,295 @@ +from io import BytesIO + +import pytest + +from embit import ec, script +from embit.psbt import PSBT, PSBTError, CompressMode, InputScope +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 + + +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 + + +@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() 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)), 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) 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)