Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
31ac982
feat(BIP-370): PSBTv2 support
notTanveer Jul 6, 2026
0b81ab3
fix(psbt): dedupe the BIP-370 TX_MODIFIABLE update between PSBT and P…
notTanveer Sep 2, 2026
9577f0c
fix(psbt): close PSBTv2 global-field validation gaps left by upstream…
notTanveer Aug 19, 2026
24d4b59
fix(psbt): correctness, RAM fixes in PSBTv2 parsing paths and revert …
notTanveer Aug 22, 2026
d9828a2
ref(psbt): extract taproot key-path signature storage into _sign_tapr…
notTanveer Sep 2, 2026
8a2f626
changelog: add PSBTv2 support entry
odudex Sep 2, 2026
8af2113
fix(psbtview): match PSBTv2 scope fields exactly and validate scopes …
odudex Sep 2, 2026
7cc5ce2
fix(pset): sign PSETv2 with the BIP-370 locktime and keep a zero sequ…
odudex Sep 2, 2026
e92934c
fix(psbt): report every malformed PSBT as PSBTError
odudex Sep 2, 2026
ddadb23
fix(psbtview): reject trailing data and count/map mismatches when vie…
odudex Sep 2, 2026
c2b83c2
fix(psbt): align PSBT and PSBTView on locktime, key order, key-path s…
odudex Sep 2, 2026
99b69fc
test(psbt): hold PSBT_IN_NON_WITNESS_UTXO to its declared length
odudex Sep 2, 2026
12ce2e0
perf(psbtview): hash prevouts, sequences and outputs in one pass per …
odudex Sep 2, 2026
4a36c62
test(psbt): run the BIP-370 vectors through PSBTView and compare sign…
odudex Sep 2, 2026
1d7166d
changelog: PSBT hardening follow-ups
odudex Sep 2, 2026
76bbf90
chore(psbtview): keep the global-tx bound check and scope walk mypy-c…
odudex Sep 2, 2026
d972fc5
fix(psbt): close gaps found reviewing the hardening series
odudex Sep 2, 2026
3cd810c
fix(transaction): compute BIP-341 sighashes correctly for ANYONECANPA…
odudex Sep 2, 2026
e5e6996
fix(psbt): parse PSBTv0 unsigned transactions with no inputs
odudex Sep 2, 2026
52883cc
changelog: taproot sighash, zero-input PSBT and PSET asset fixes
odudex Sep 2, 2026
bdcb788
fix(psbt): pass the PSBT version through PSBTScope.read_from
odudex Sep 2, 2026
3937ff0
fix(psbt): bound the taproot leaf hash count before reading leaves
odudex Sep 3, 2026
0e0e914
fix(psbtview): stream deferred PSBTv2 global values
odudex Sep 8, 2026
6bc88c2
fix(psbt): verify witness UTXOs against authenticated outputs
odudex Sep 18, 2026
bc09352
fix(psbt): preserve signed locktime when adding v2 inputs
odudex Sep 18, 2026
628154d
fix(psbt): update v2 flags when signing directly with taproot keys
odudex Sep 18, 2026
6ea495a
fix(psbt): re-verify inputs after update() merges UTXO data
odudex Sep 18, 2026
baf8e8d
ref(psbt): drop flag updates made redundant by the taproot signers
odudex Sep 18, 2026
527e34a
fix: blank legacy SIGHASH_SINGLE outputs the way core does
notTanveer Sep 29, 2026
ee3807d
fix: fail compressed PSBTv2 parsing on streams that can't seek
notTanveer Sep 29, 2026
19d7ffb
fix: reject an output index past the end of non_witness_utxo
notTanveer Sep 29, 2026
3c485cd
fix: reject duplicate keys in input and output maps in every mode
notTanveer Sep 29, 2026
6905eb9
fix: validate each PSBTv2 map right after it is parsed
notTanveer Sep 29, 2026
7f9686f
fix: check PSBT_GLOBAL_XPUB in PSBTView like PSBT.parse does
notTanveer Sep 29, 2026
91e3c10
fix: match PSET rangeproof keys exactly in PSETView
notTanveer Sep 29, 2026
07a431e
refactor: reuse the shared signing helpers in PSBTView.sign_input
notTanveer Sep 29, 2026
bb1dba8
refactor: stop updating the parsed global counts in add_input and add…
notTanveer Sep 29, 2026
611cf51
changelog: legacy SIGHASH_SINGLE, PSBTv2 parsing and PSETView proof k…
odudex Oct 15, 2026
9b33a67
fix: use exact reads in the Liquid transaction parser and honor segwi…
odudex Oct 21, 2026
159e862
fix: bound nested reads of in-memory PSBT values by the value length
odudex Nov 11, 2026
a21459a
fix(pset): accept PSETv2 fee outputs that omit PSBT_OUT_SCRIPT
odudex Nov 11, 2026
6dfa18c
fix(psbt): drop a stale non_witness_utxo when update() adopts another…
odudex Nov 11, 2026
829fa0a
test: keep the non-witness UTXO key in the length mismatch fixture
odudex Nov 11, 2026
6b7b281
fix(psbtview): write only new signatures to the signature stream
odudex Nov 11, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 11 additions & 5 deletions src/embit/compact.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
""" Compact Int parsing / serialization """
"""Compact Int parsing / serialization"""

import io


Expand Down Expand Up @@ -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
80 changes: 59 additions & 21 deletions src/embit/liquid/pset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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,
)

Expand All @@ -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.
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
)

Expand All @@ -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)
)
),
)

Expand Down Expand Up @@ -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"]:
Expand Down Expand Up @@ -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)
Expand All @@ -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):
Expand Down Expand Up @@ -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],
)
Expand Down
Loading
Loading