Skip to content

Commit 2ae224b

Browse files
committed
fix: make Transaction.from_raw() able to parse unsigned segwit tx
Although Transaction.from_raw() is able to parse a signed or unsigned legacy tx, it is only able to correctly parse signed segwit txs because it assumes the presence of witnesses in the tx payload. But unsigned segwit tx payload do not have any witness. This PR proposes to fix that by checking the number of remaining bytes between the end of the last output and the end of the payload and by skipping the witnesses parsing if that number is not greater than 4 (locktime field length).
1 parent a93da8b commit 2ae224b

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

bitcoinutils/transactions.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -567,9 +567,11 @@ def from_raw(rawtxhex: str):
567567
output, cursor = TxOutput.from_raw(rawtx.hex(), cursor, has_segwit)
568568
outputs.append(output)
569569

570-
# Handle witnesses if SegWit is enabled
570+
# Handle witnesses if SegWit is enabled and if they are present i.e. if
571+
# remaining payload length is greater than last tx field length (locktime)
572+
has_witness_field = True if len(rawtx) - cursor > 4 else False
571573
witnesses = []
572-
if has_segwit:
574+
if has_segwit and has_witness_field:
573575
for _ in range(n_inputs):
574576
n_items, size = parse_compact_size(rawtx[cursor:])
575577
cursor += size

0 commit comments

Comments
 (0)