3434)
3535from bitcoinutils .script import Script
3636from bitcoinutils .utils import (
37- vi_to_int ,
3837 encode_varint ,
3938 tagged_hash ,
4039 prepend_compact_size ,
@@ -141,7 +140,9 @@ def __repr__(self):
141140 return self .__str__ ()
142141
143142 @staticmethod
144- def from_raw (txinputrawhex : Union [str , bytes ], cursor : int = 0 , has_segwit : bool = False ):
143+ def from_raw (
144+ txinputrawhex : Union [str , bytes ], cursor : int = 0 , has_segwit : bool = False
145+ ):
145146 """
146147 Imports a TxInput from a Transaction's hexadecimal data
147148
@@ -162,7 +163,7 @@ def from_raw(txinputrawhex: Union[str, bytes], cursor: int = 0, has_segwit: bool
162163 raise TypeError ("Input must be a hexadecimal string or bytes" )
163164
164165 # Unpack transaction ID (hash) in bytes and output index
165- txid , vout = struct .unpack_from (' <32sI' , txinputraw , cursor )
166+ txid , vout = struct .unpack_from (" <32sI" , txinputraw , cursor )
166167 txid = txid [::- 1 ] # Reverse to match usual hexadecimal order
167168 cursor += 36 # 32 bytes for txid and 4 bytes for vout
168169
@@ -171,25 +172,26 @@ def from_raw(txinputrawhex: Union[str, bytes], cursor: int = 0, has_segwit: bool
171172 cursor += size
172173
173174 # Read the unlocking script in bytes
174- unlocking_script = struct .unpack_from (f'{ unlocking_script_size } s' , txinputraw , cursor )[0 ]
175+ unlocking_script = struct .unpack_from (
176+ f"{ unlocking_script_size } s" , txinputraw , cursor
177+ )[0 ]
175178 cursor += unlocking_script_size
176179
177180 # Read the sequence number in bytes
178- sequence , = struct .unpack_from (' <4s' , txinputraw , cursor )
181+ ( sequence ,) = struct .unpack_from (" <4s" , txinputraw , cursor )
179182 cursor += 4
180183
181184 # If coinbase input (utxo will be all zeros), handle script differently
182- if txid .hex () == '00' * 32 :
183- script_sig = Script ([unlocking_script .hex ()]) # Treat as single element for coinbase
185+ if txid .hex () == "00" * 32 :
186+ script_sig = Script (
187+ [unlocking_script .hex ()]
188+ ) # Treat as single element for coinbase
184189 else :
185190 script_sig = Script .from_raw (unlocking_script .hex (), has_segwit = has_segwit )
186191
187192 # Create the TxInput instance
188193 tx_input = TxInput (
189- txid = txid .hex (),
190- txout_index = vout ,
191- script_sig = script_sig ,
192- sequence = sequence
194+ txid = txid .hex (), txout_index = vout , script_sig = script_sig , sequence = sequence
193195 )
194196
195197 return tx_input , cursor
@@ -291,7 +293,9 @@ def to_bytes(self) -> bytes:
291293 return data
292294
293295 @staticmethod
294- def from_raw (txoutputrawhex : Union [str , bytes ], cursor : int = 0 , has_segwit : bool = False ):
296+ def from_raw (
297+ txoutputrawhex : Union [str , bytes ], cursor : int = 0 , has_segwit : bool = False
298+ ):
295299 """
296300 Imports a TxOutput from a Transaction's hexadecimal data
297301
@@ -310,11 +314,10 @@ def from_raw(txoutputrawhex: Union[str, bytes], cursor: int = 0, has_segwit: boo
310314 txoutputraw = txoutputrawhex
311315 else :
312316 raise TypeError ("Input must be a hexadecimal string or bytes" )
313-
314317
315318 # Unpack the amount of the TxOutput directly in bytes
316319 amount_format = "<Q" # Little-endian unsigned long long (8 bytes)
317- amount , = struct .unpack_from (amount_format , txoutputraw , cursor )
320+ ( amount ,) = struct .unpack_from (amount_format , txoutputraw , cursor )
318321 cursor += struct .calcsize (amount_format )
319322
320323 # Read the locking script size using parse_compact_size
@@ -323,18 +326,17 @@ def from_raw(txoutputrawhex: Union[str, bytes], cursor: int = 0, has_segwit: boo
323326
324327 # Read the locking script
325328 script_format = f"{ lock_script_size } s"
326- lock_script , = struct .unpack_from (script_format , txoutputraw , cursor )
329+ ( lock_script ,) = struct .unpack_from (script_format , txoutputraw , cursor )
327330 cursor += lock_script_size
328331
329332 # Create the TxOutput instance
330333 tx_output = TxOutput (
331334 amount = amount ,
332- script_pubkey = Script .from_raw (lock_script .hex (), has_segwit = has_segwit )
335+ script_pubkey = Script .from_raw (lock_script .hex (), has_segwit = has_segwit ),
333336 )
334337
335338 return tx_output , cursor
336339
337-
338340 def __str__ (self ) -> str :
339341 return str ({"amount" : self .amount , "script_pubkey" : self .script_pubkey })
340342
@@ -552,15 +554,14 @@ def from_raw(rawtxhex: Union[str, bytes]):
552554 rawtx = rawtxhex
553555 else :
554556 raise TypeError ("Input must be a hexadecimal string or bytes" )
555-
556557
557558 # Read version (4 bytes)
558559 version = rawtx [0 :4 ]
559560 cursor = 4
560561
561562 # Detect and handle SegWit
562563 has_segwit = False
563- if rawtx [cursor : cursor + 2 ] == b' \x00 \x01 ' :
564+ if rawtx [cursor : cursor + 2 ] == b" \x00 \x01 " :
564565 has_segwit = True
565566 cursor += 2 # Skipping past the marker and flag bytes
566567
@@ -596,16 +597,16 @@ def from_raw(rawtxhex: Union[str, bytes]):
596597 for _ in range (n_items ):
597598 item_size , size = parse_compact_size (rawtx [cursor :])
598599 cursor += size
599- witness_data = rawtx [cursor : cursor + item_size ]
600+ witness_data = rawtx [cursor : cursor + item_size ]
600601 cursor += item_size
601602 witnesses_tmp .append (witness_data .hex ())
602603 if witnesses_tmp :
603604 witnesses .append (TxWitnessInput (stack = witnesses_tmp ))
604605
605606 # Read locktime (4 bytes)
606- locktime = rawtx [cursor : cursor + 4 ]
607+ locktime = rawtx [cursor : cursor + 4 ]
607608
608- #Returning the Transaction object
609+ # Returning the Transaction object
609610 return Transaction (
610611 inputs = inputs ,
611612 outputs = outputs ,
0 commit comments