Skip to content

Commit ac759d9

Browse files
committed
Merge branch 'abhigit-saha-from-raw-extended-support'
2 parents 5a973b0 + e1d2da5 commit ac759d9

2 files changed

Lines changed: 33 additions & 10 deletions

File tree

bitcoinutils/script.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import copy
1313
import hashlib
1414
import struct
15-
from typing import Any
15+
from typing import Any, Union
1616

1717
from bitcoinutils.ripemd160 import ripemd160
1818
from bitcoinutils.utils import b_to_h, h_to_b, vi_to_int
@@ -359,7 +359,7 @@ def to_hex(self) -> str:
359359
return b_to_h(self.to_bytes())
360360

361361
@staticmethod
362-
def from_raw(scriptrawhex: str, has_segwit: bool = False):
362+
def from_raw(scriptrawhex: Union[str, bytes], has_segwit: bool = False):
363363
"""
364364
Imports a Script commands list from raw hexadecimal data
365365
Attributes
@@ -369,7 +369,13 @@ def from_raw(scriptrawhex: str, has_segwit: bool = False):
369369
has_segwit : boolean
370370
Is the Tx Input segwit or not
371371
"""
372-
scriptraw = h_to_b(scriptrawhex)
372+
if isinstance(scriptrawhex, str):
373+
scriptraw = h_to_b(scriptrawhex)
374+
elif isinstance(scriptrawhex, bytes):
375+
scriptraw = scriptrawhex
376+
else:
377+
raise TypeError("Input must be a hexadecimal string or bytes")
378+
373379
commands = []
374380
index = 0
375381

bitcoinutils/transactions.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import math
1313
import hashlib
1414
import struct
15-
from typing import Optional
15+
from typing import Optional, Union
1616

1717
from 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

Comments
 (0)