karask / python-bitcoin-utils

Library to interact with the Bitcoin network. Ideal for low-level learning and experimenting.
MIT License
262 stars 99 forks source link

fix: make Transaction.from_raw() able to parse unsigned segwit tx #100

Open hcleonis opened 1 week ago

hcleonis commented 1 week ago

AlthoughTransaction.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 payloads do not have any witness field.

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 tx payload and by skipping the witnesses parsing if that number is not greater than 4 (i.e. last tx field i.e. locktime length).

karask commented 4 days ago

Hi @hcleonis ! Looks good. Can you add a test (in tests) with the example you used to test it? I will then review and merge.

Thank you