Skip to content

Commit 091fb01

Browse files
committed
refactor(scripts): fix imports and linter issues
1 parent 4e09b47 commit 091fb01

17 files changed

+88
-75
lines changed

hathor/transaction/base_transaction.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1269,8 +1269,8 @@ def can_melt_token(self) -> bool:
12691269
def to_human_readable(self) -> dict[str, Any]:
12701270
"""Checks what kind of script this is and returns it in human readable form
12711271
"""
1272-
from hathor.transaction.script.scripts import parse_address_script
12731272
from hathor.transaction.script.nano_contract_match_values import NanoContractMatchValues
1273+
from hathor.transaction.script.scripts import parse_address_script
12741274

12751275
script_type = parse_address_script(self.script)
12761276
if script_type:

hathor/transaction/script/__init__.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +0,0 @@
1-
# Copyright 2023 Hathor Labs
2-
#
3-
# Licensed under the Apache License, Version 2.0 (the "License");
4-
# you may not use this file except in compliance with the License.
5-
# You may obtain a copy of the License at
6-
#
7-
# http://www.apache.org/licenses/LICENSE-2.0
8-
#
9-
# Unless required by applicable law or agreed to in writing, software
10-
# distributed under the License is distributed on an "AS IS" BASIS,
11-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
# See the License for the specific language governing permissions and
13-
# limitations under the License.

hathor/transaction/script/multi_sig.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
# limitations under the License.
1414

1515
import struct
16-
from typing import Optional, Any
16+
from typing import Any, Optional
1717

1818
from hathor.crypto.util import decode_address, get_address_b58_from_redeem_script_hash
1919
from hathor.transaction.script.base_script import BaseScript
2020
from hathor.transaction.script.hathor_script import HathorScript
21-
from hathor.transaction.script.scripts import re_compile, get_script_op, get_pushdata, Stack
2221
from hathor.transaction.script.opcode import Opcode, op_pushdata, op_pushdata1
22+
from hathor.transaction.script.scripts import Stack, get_pushdata, get_script_op, re_compile
2323

2424

2525
class MultiSig(BaseScript):

hathor/transaction/script/nano_contract_match_values.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
from hathor.crypto.util import get_address_b58_from_bytes
2020
from hathor.transaction.script.hathor_script import HathorScript
21-
from hathor.transaction.script.scripts import re_compile, get_pushdata, binary_to_int
2221
from hathor.transaction.script.opcode import Opcode
22+
from hathor.transaction.script.scripts import binary_to_int, get_pushdata, re_compile
2323

2424

2525
# XXX: does it make sense to make this BaseScript too?

hathor/transaction/script/opcode.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,30 @@
2222
from cryptography.hazmat.primitives.asymmetric import ec
2323

2424
from hathor.conf.get_settings import get_settings
25-
from hathor.crypto.util import is_pubkey_compressed, get_public_key_from_bytes_compressed, get_hash160, \
26-
get_address_b58_from_bytes
27-
from hathor.transaction.exceptions import MissingStackItems, TimeLocked, EqualVerifyFailed, ScriptError, \
28-
OracleChecksigFailed, VerifyFailed, InvalidStackData, InvalidScriptError
29-
from hathor.transaction.script.p2pkh import P2PKH
30-
from hathor.transaction.script.scripts import Stack, get_script_op, ScriptExtras, get_data_value, binary_to_int, \
31-
decode_opn
25+
from hathor.crypto.util import (
26+
get_address_b58_from_bytes,
27+
get_hash160,
28+
get_public_key_from_bytes_compressed,
29+
is_pubkey_compressed,
30+
)
31+
from hathor.transaction.exceptions import (
32+
EqualVerifyFailed,
33+
InvalidScriptError,
34+
InvalidStackData,
35+
MissingStackItems,
36+
OracleChecksigFailed,
37+
ScriptError,
38+
TimeLocked,
39+
VerifyFailed,
40+
)
41+
from hathor.transaction.script.scripts import (
42+
ScriptExtras,
43+
Stack,
44+
binary_to_int,
45+
decode_opn,
46+
get_data_value,
47+
get_script_op,
48+
)
3249

3350

3451
class Opcode(IntEnum):
@@ -486,6 +503,7 @@ def op_find_p2pkh(stack: Stack, log: list[str], extras: ScriptExtras) -> None:
486503
if not len(stack):
487504
raise MissingStackItems('OP_FIND_P2PKH: empty stack')
488505

506+
from hathor.transaction.script.p2pkh import P2PKH
489507
spent_tx = extras.spent_tx
490508
txin = extras.txin
491509
tx = extras.tx

hathor/transaction/script/p2pkh.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
# limitations under the License.
1414

1515
import struct
16-
from typing import Optional, Any
16+
from typing import Any, Optional
1717

1818
from hathor.crypto.util import decode_address, get_address_b58_from_public_key_hash
1919
from hathor.transaction.script.base_script import BaseScript
2020
from hathor.transaction.script.hathor_script import HathorScript
21-
from hathor.transaction.script.scripts import re_compile, get_pushdata
2221
from hathor.transaction.script.opcode import Opcode
22+
from hathor.transaction.script.scripts import get_pushdata, re_compile
2323

2424

2525
class P2PKH(BaseScript):

hathor/transaction/script/scripts.py

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,3 @@
1-
# Copyright 2023 Hathor Labs
2-
#
3-
# Licensed under the Apache License, Version 2.0 (the "License");
4-
# you may not use this file except in compliance with the License.
5-
# You may obtain a copy of the License at
6-
#
7-
# http://www.apache.org/licenses/LICENSE-2.0
8-
#
9-
# Unless required by applicable law or agreed to in writing, software
10-
# distributed under the License is distributed on an "AS IS" BASIS,
11-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
# See the License for the specific language governing permissions and
13-
# limitations under the License.
14-
151
# Copyright 2021 Hathor Labs
162
#
173
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -28,24 +14,18 @@
2814

2915
import re
3016
import struct
31-
from typing import Any, Generator, NamedTuple, Optional, Pattern, Union
17+
from typing import TYPE_CHECKING, Any, Generator, NamedTuple, Optional, Pattern, Union
3218

3319
from hathor.conf.get_settings import get_settings
34-
from hathor.crypto.util import (
35-
decode_address,
36-
)
20+
from hathor.crypto.util import decode_address
3721
from hathor.transaction import BaseTransaction, Transaction, TxInput
38-
from hathor.transaction.exceptions import (
39-
DataIndexError,
40-
FinalStackInvalid,
41-
InvalidScriptError,
42-
OutOfData,
43-
ScriptError,
44-
)
22+
from hathor.transaction.exceptions import DataIndexError, FinalStackInvalid, InvalidScriptError, OutOfData, ScriptError
4523
from hathor.transaction.script.base_script import BaseScript
46-
from hathor.transaction.script.multi_sig import MultiSig
47-
from hathor.transaction.script.opcode import Opcode, MAP_OPCODE_TO_FN
48-
from hathor.transaction.script.p2pkh import P2PKH
24+
25+
if TYPE_CHECKING:
26+
from hathor.transaction.script.multi_sig import MultiSig
27+
from hathor.transaction.script.opcode import Opcode
28+
from hathor.transaction.script.p2pkh import P2PKH
4929

5030
# XXX: Because the Stack is a heterogeneous list of bytes and int, and some OPs only work for when the stack has some
5131
# or the other type, there are many places that require an assert to prevent the wrong type from being used,
@@ -90,6 +70,7 @@ def re_compile(pattern: str) -> Pattern[bytes]:
9070
def _to_byte_pattern(m):
9171
x = m.group().decode('ascii').strip()
9272
if x.startswith('OP_'):
73+
from hathor.transaction.script.opcode import Opcode
9374
return bytes([Opcode[x]])
9475
elif x.startswith('DATA_'):
9576
length = int(m.group()[5:])
@@ -112,6 +93,7 @@ def _re_pushdata(length: int) -> bytes:
11293
:return: A non-compiled regular expression
11394
:rtype: bytes
11495
"""
96+
from hathor.transaction.script.opcode import Opcode
11597
ret = [bytes([Opcode.OP_PUSHDATA1]), bytes([length]), b'.{', str(length).encode('ascii'), b'}']
11698

11799
if length <= 75:
@@ -127,8 +109,10 @@ def create_base_script(address: str, timelock: Optional[Any] = None) -> BaseScri
127109
settings = get_settings()
128110
baddress = decode_address(address)
129111
if baddress[0] == binary_to_int(settings.P2PKH_VERSION_BYTE):
112+
from hathor.transaction.script.p2pkh import P2PKH
130113
return P2PKH(address, timelock)
131114
elif baddress[0] == binary_to_int(settings.MULTISIG_VERSION_BYTE):
115+
from hathor.transaction.script.multi_sig import MultiSig
132116
return MultiSig(address, timelock)
133117
else:
134118
raise ScriptError('The address is not valid')
@@ -150,14 +134,16 @@ def create_output_script(address: bytes, timelock: Optional[Any] = None) -> byte
150134
settings = get_settings()
151135
# XXX: if the address class can somehow be simplified create_base_script could be used here
152136
if address[0] == binary_to_int(settings.P2PKH_VERSION_BYTE):
137+
from hathor.transaction.script.p2pkh import P2PKH
153138
return P2PKH.create_output_script(address, timelock)
154139
elif address[0] == binary_to_int(settings.MULTISIG_VERSION_BYTE):
140+
from hathor.transaction.script.multi_sig import MultiSig
155141
return MultiSig.create_output_script(address, timelock)
156142
else:
157143
raise ScriptError('The address is not valid')
158144

159145

160-
def parse_address_script(script: bytes) -> Optional[Union[P2PKH, MultiSig]]:
146+
def parse_address_script(script: bytes) -> Optional[Union['P2PKH', 'MultiSig']]:
161147
""" Verifies if address is P2PKH or Multisig and calls correct parse_script method
162148
163149
:param script: script to decode
@@ -166,6 +152,8 @@ def parse_address_script(script: bytes) -> Optional[Union[P2PKH, MultiSig]]:
166152
:return: P2PKH or MultiSig class or None
167153
:rtype: class or None
168154
"""
155+
from hathor.transaction.script.multi_sig import MultiSig
156+
from hathor.transaction.script.p2pkh import P2PKH
169157
script_classes: list[type[Union[P2PKH, MultiSig]]] = [P2PKH, MultiSig]
170158
# Each class verifies its script
171159
for script_class in script_classes:
@@ -185,6 +173,7 @@ def decode_opn(opcode: int) -> int:
185173
:return: int value for opcode param
186174
:rtype: int
187175
"""
176+
from hathor.transaction.script.opcode import Opcode
188177
int_val = opcode - Opcode.OP_0
189178
if not (0 <= int_val <= 16):
190179
raise InvalidScriptError('unknown opcode {}'.format(opcode))
@@ -258,6 +247,7 @@ def get_script_op(pos: int, data: bytes, stack: Optional[Stack] = None) -> Opcod
258247
opcode = get_data_single_byte(pos, data)
259248

260249
# validate opcode
250+
from hathor.transaction.script.opcode import Opcode
261251
if not Opcode.is_valid_opcode(opcode):
262252
raise InvalidScriptError('Invalid Opcode ({}) at position {} in {!r}'.format(opcode, pos, data))
263253

@@ -293,7 +283,7 @@ def get_script_op(pos: int, data: bytes, stack: Optional[Stack] = None) -> Opcod
293283

294284

295285
class _ScriptOperation(NamedTuple):
296-
opcode: Union[Opcode, int]
286+
opcode: Union['Opcode', int]
297287
position: int
298288
data: Union[None, bytes, int, str]
299289

@@ -308,6 +298,7 @@ def parse_script_ops(data: bytes) -> Generator[_ScriptOperation, None, None]:
308298
:return: generator for operations on script
309299
:rtype: Generator[_ScriptOperation, None, None]
310300
"""
301+
from hathor.transaction.script.opcode import Opcode
311302
op: Union[Opcode, int]
312303

313304
pos = 0
@@ -341,6 +332,7 @@ def count_sigops(data: bytes) -> int:
341332
:return: number of signature operations the script would do if it was executed
342333
:rtype: int
343334
"""
335+
from hathor.transaction.script.opcode import Opcode
344336
settings = get_settings()
345337
n_ops: int = 0
346338
data_len: int = len(data)
@@ -388,6 +380,7 @@ def get_sigops_count(data: bytes, output_script: Optional[bytes] = None) -> int:
388380
# If validating an input, should check the spent_tx for MultiSig
389381
if output_script is not None:
390382
# If it's multisig we have to validate the redeem_script sigop count
383+
from hathor.transaction.script.multi_sig import MultiSig
391384
if MultiSig.re_match.search(output_script):
392385
multisig_data = MultiSig.get_multisig_data(data)
393386
# input_script + redeem_script
@@ -411,6 +404,7 @@ def execute_eval(data: bytes, log: list[str], extras: ScriptExtras) -> None:
411404
:raises ScriptError: case opcode is not found
412405
:raises FinalStackInvalid: case the evaluation fails
413406
"""
407+
from hathor.transaction.script.opcode import MAP_OPCODE_TO_FN, Opcode
414408
stack: Stack = []
415409
data_len = len(data)
416410
pos = 0
@@ -466,6 +460,7 @@ def script_eval(tx: Transaction, txin: TxInput, spent_tx: BaseTransaction) -> No
466460
log: list[str] = []
467461
extras = ScriptExtras(tx=tx, txin=txin, spent_tx=spent_tx)
468462

463+
from hathor.transaction.script.multi_sig import MultiSig
469464
if MultiSig.re_match.search(output_script):
470465
# For MultiSig there are 2 executions:
471466
# First we need to evaluate that redeem_script matches redeem_script_hash
@@ -551,5 +546,3 @@ def binary_to_int(binary: bytes) -> int:
551546

552547
(value,) = struct.unpack(_format, binary)
553548
return value
554-
555-

hathor/wallet/base_wallet.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
from hathor.pubsub import EventArguments, HathorEvents, PubSubManager
3030
from hathor.transaction import BaseTransaction, Block, TxInput, TxOutput
3131
from hathor.transaction.base_transaction import int_to_bytes
32-
from hathor.transaction.script.scripts import create_output_script, parse_address_script
3332
from hathor.transaction.script.p2pkh import P2PKH
33+
from hathor.transaction.script.scripts import create_output_script, parse_address_script
3434
from hathor.transaction.storage import TransactionStorage
3535
from hathor.transaction.transaction import Transaction
3636
from hathor.types import AddressB58, Amount, TokenUid

hathor/wallet/util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121

2222
from hathor.conf.get_settings import get_settings
2323
from hathor.crypto.util import get_hash160, get_private_key_from_bytes
24-
from hathor.transaction.script.opcode import Opcode
2524
from hathor.transaction.script.hathor_script import HathorScript
25+
from hathor.transaction.script.opcode import Opcode
2626
from hathor.transaction.transaction import Transaction
2727

2828

tests/resources/transaction/test_create_tx.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
from hathor.daa import TestMode, _set_test_mode
66
from hathor.transaction import Transaction
77
from hathor.transaction.resources import CreateTxResource
8-
from hathor.transaction.script.scripts import create_base_script
98
from hathor.transaction.script.p2pkh import P2PKH
9+
from hathor.transaction.script.scripts import create_base_script
1010
from tests import unittest
1111
from tests.resources.base_resource import StubSite, _BaseResourceTest
1212
from tests.utils import add_blocks_unlock_reward, add_new_blocks, add_new_tx

0 commit comments

Comments
 (0)