Skip to content

Commit 430c192

Browse files
committed
Merge branch 'fix/witness-parsing' of https://github.com/LedgerHQ/python-bitcoin-utils-ledger into LedgerHQ-fix/witness-parsing
2 parents 1c6f062 + 558f9e8 commit 430c192

4 files changed

Lines changed: 42 additions & 4 deletions

File tree

bitcoinutils/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.7.2"
1+
__version__ = "0.7.3"

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

tests/test_from_raw_segwit.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Copyright (C) 2018-2024 The python-bitcoin-utils developers
2+
#
3+
# This file is part of python-bitcoin-utils
4+
#
5+
# It is subject to the license terms in the LICENSE file found in the top-level
6+
# directory of this distribution.
7+
#
8+
# No part of python-bitcoin-utils, including this file, may be copied,
9+
# modified, propagated, or distributed except according to the terms contained
10+
# in the LICENSE file.
11+
12+
13+
import unittest
14+
15+
from bitcoinutils.setup import setup
16+
from bitcoinutils.transactions import Transaction
17+
18+
19+
class TestFromRawUnsignedSegwit(unittest.TestCase):
20+
def setUp(self):
21+
setup("mainnet")
22+
self.raw_segwit_tx = [
23+
# Unsigned
24+
"02000000000102daf4d7b97a62dd9933bd6977b5da9a3edb7c2d853678c9932108f1eb4d27b7a90000000000fdffffffdaf4d7b97a62dd9933bd6977b5da9a3edb7c2d853678c9932108f1eb4d27b7a90100000000fdffffff0101410f0000000000160014e4d3a1ec51102902f6bbede1318047880c9c7680a7011900",
25+
# Signed
26+
"02000000000102daf4d7b97a62dd9933bd6977b5da9a3edb7c2d853678c9932108f1eb4d27b7a90000000000fdffffffdaf4d7b97a62dd9933bd6977b5da9a3edb7c2d853678c9932108f1eb4d27b7a90100000000fdffffff0101410f0000000000160014e4d3a1ec51102902f6bbede1318047880c9c7680024730440220495838c36533616d8cbd6474842459596f4f312dce5483fe650791c82e17221c02200660520a2584144915efa8519a72819091e5ed78c52689b24235182f17d96302012102ddf4af49ff0eae1d507cc50c86f903cd6aa0395f3239759c440ea67556a3b91b0247304402200090c2507517abc7a9cb32452aabc8d1c8a0aee75ce63618ccd901542415f2db02205bb1d22cb6e8173e91dc82780481ea55867b8e753c35424da664f1d2662ecb1301210254c54648226a45dd2ad79f736ebf7d5f0fc03b6f8f0e6d4a61df4e531aaca431a7011900"
27+
]
28+
29+
def test_segwit_tx_from_raw(self):
30+
for tx in self.raw_segwit_tx:
31+
tx_from_raw = Transaction.from_raw(tx)
32+
self.assertEqual(tx_from_raw.to_hex(), tx)
33+
34+
35+
if __name__ == "__main__":
36+
unittest.main()

update_version.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
pip uninstall -y bitcoin-utils
22
python setup.py sdist bdist_wheel
3-
pip install dist/bitcoin-utils-0.7.2.tar.gz
3+
pip install dist/bitcoin-utils-0.7.3.tar.gz

0 commit comments

Comments
 (0)