1212import math
1313import hashlib
1414import struct
15- from typing import Optional
15+ from typing import Optional , Union
1616
1717from bitcoinutils .constants import (
1818 DEFAULT_TX_SEQUENCE ,
@@ -141,7 +141,7 @@ def __repr__(self):
141141 return self .__str__ ()
142142
143143 @staticmethod
144- def from_raw (txinputrawhex : str , cursor : int = 0 , has_segwit : bool = False ):
144+ def from_raw (txinputrawhex : Union [ str , bytes ] , cursor : int = 0 , has_segwit : bool = False ):
145145 """
146146 Imports a TxInput from a Transaction's hexadecimal data
147147
@@ -154,7 +154,12 @@ def from_raw(txinputrawhex: str, cursor: int = 0, has_segwit: bool = False):
154154 has_segwit : boolean
155155 Is the Tx Input segwit or not
156156 """
157- txinputraw = h_to_b (txinputrawhex )
157+ if isinstance (txinputrawhex , str ):
158+ txinputraw = h_to_b (txinputrawhex )
159+ elif isinstance (txinputrawhex , bytes ):
160+ txinputraw = txinputrawhex
161+ else :
162+ raise TypeError ("Input must be a hexadecimal string or bytes" )
158163
159164 # Unpack transaction ID (hash) in bytes and output index
160165 txid , vout = struct .unpack_from ('<32sI' , txinputraw , cursor )
@@ -286,7 +291,7 @@ def to_bytes(self) -> bytes:
286291 return data
287292
288293 @staticmethod
289- def from_raw (txoutputrawhex : str , cursor : int = 0 , has_segwit : bool = False ):
294+ def from_raw (txoutputrawhex : Union [ str , bytes ] , cursor : int = 0 , has_segwit : bool = False ):
290295 """
291296 Imports a TxOutput from a Transaction's hexadecimal data
292297
@@ -299,7 +304,13 @@ def from_raw(txoutputrawhex: str, cursor: int = 0, has_segwit: bool = False):
299304 has_segwit : boolean
300305 Is the Tx Output segwit or not
301306 """
302- txoutputraw = h_to_b (txoutputrawhex )
307+ if isinstance (txoutputrawhex , str ):
308+ txoutputraw = h_to_b (txoutputrawhex )
309+ elif isinstance (txoutputrawhex , bytes ):
310+ txoutputraw = txoutputrawhex
311+ else :
312+ raise TypeError ("Input must be a hexadecimal string or bytes" )
313+
303314
304315 # Unpack the amount of the TxOutput directly in bytes
305316 amount_format = "<Q" # Little-endian unsigned long long (8 bytes)
@@ -526,7 +537,7 @@ def __init__(
526537 self .version = version
527538
528539 @staticmethod
529- def from_raw (rawtxhex : str ):
540+ def from_raw (rawtxhex : Union [ str , bytes ] ):
530541 """
531542 Imports a Transaction from hexadecimal data.
532543
@@ -535,7 +546,13 @@ def from_raw(rawtxhex: str):
535546 rawtxhex : string (hex)
536547 The hexadecimal raw string of the Transaction.
537548 """
538- rawtx = h_to_b (rawtxhex )
549+ if isinstance (rawtxhex , str ):
550+ rawtx = h_to_b (rawtxhex )
551+ elif isinstance (rawtxhex , bytes ):
552+ rawtx = rawtxhex
553+ else :
554+ raise TypeError ("Input must be a hexadecimal string or bytes" )
555+
539556
540557 # Read version (4 bytes)
541558 version = rawtx [0 :4 ]
0 commit comments