Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 4 additions & 1 deletion src/embit/psbt.py
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,10 @@ def read_from(cls, stream, compress=CompressMode.KEEP_ALL):
# tx
if key == b"\x00":
if tx is None:
tx = cls.TX_CLS.parse(value)
# BIP-174: the global unsigned tx is non-witness serialized,
# so a leading 0x00 is a real (possibly empty) input count,
# not a segwit marker.
tx = cls.TX_CLS.parse(value, segwit=False)
else:
raise PSBTError(
"Failed to parse PSBT - duplicated transaction field"
Expand Down
15 changes: 11 additions & 4 deletions src/embit/transaction.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from io import BytesIO
import hashlib
from . import compact
from . import hashes
Expand Down Expand Up @@ -143,15 +144,21 @@ 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` controls whether a leading 0x00 input-count is interpreted as
# the BIP-144 segwit marker. A standalone network tx may be segwit
# (segwit=True, the default). The BIP-174 unsigned tx inside a PSBT is
# mandated non-witness, so a 0x00 there is a genuine "no inputs yet"
# count and must be parsed with segwit=False — otherwise a legacy
# 0-input / 1-output tx is indistinguishable from a segwit prefix.
ver = int.from_bytes(stream.read(4), "little")
num_vin = compact.read_from(stream)
# if num_vin is zero it is a segwit transaction
is_segwit = num_vin == 0
if is_segwit:
is_segwit = False
if segwit and num_vin == 0:
marker = stream.read(1)
if marker != b"\x01":
raise TransactionError("Invalid segwit marker")
is_segwit = True
num_vin = compact.read_from(stream)
vin = []
for i in range(num_vin):
Expand Down