Skip to content

Commit 6277364

Browse files
author
defiant1708
committed
Script class enhancement and core classes type safety - Add from_bytes and to_bytes methods to Script class - Fix circular import issues between transaction and script modules - Update transaction classes with proper type annotations - Resolve circular dependencies using lazy imports
1 parent 35668c4 commit 6277364

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

bsv/script/script.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,28 @@ def from_chunks(cls, chunks: List[ScriptChunk]) -> 'Script':
114114
s.chunks = chunks
115115
return s
116116

117+
@classmethod
118+
def from_bytes(cls, data: bytes) -> 'Script':
119+
"""
120+
Create a Script object from bytes data.
121+
122+
Args:
123+
data: Raw script bytes
124+
125+
Returns:
126+
Script: A new Script object
127+
"""
128+
return cls(data)
129+
130+
def to_bytes(self) -> bytes:
131+
"""
132+
Convert the Script object to bytes.
133+
134+
Returns:
135+
bytes: The serialized script bytes
136+
"""
137+
return self.serialize()
138+
117139
@classmethod
118140
def from_asm(cls, asm: str) -> 'Script':
119141
chunks: [ScriptChunk] = []

bsv/transaction.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@
1414
)
1515
from .hash import hash256
1616
from .merkle_path import MerklePath
17-
from .script.spend import Spend
1817
from .script.type import P2PKH
1918
from .transaction_input import TransactionInput
2019
from .transaction_output import TransactionOutput
2120
from .transaction_preimage import tx_preimage
2221
from .utils import unsigned_to_varint, Reader, Writer, reverse_hex_byte_order
2322

23+
# Lazy import to avoid circular dependency
24+
def Spend(params):
25+
from .script.spend import Spend as SpendClass
26+
return SpendClass(params)
27+
2428

2529
class InsufficientFunds(ValueError):
2630
pass

0 commit comments

Comments
 (0)