From 1a3da1500aa0e5c641b9eb9319775ff4118dbb26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Triay?= Date: Mon, 7 Nov 2022 19:11:54 +0100 Subject: [PATCH] make get_hash return hex (#257) * make get_hash return hex * fix linter * apply review suggestions * update class_hash len * fix tests * fix lenght * fix lint --- src/nile/common.py | 6 ++++-- src/nile/core/declare.py | 11 +++++------ src/nile/utils/__init__.py | 24 ++++++++++++++++++------ tests/commands/test_declare.py | 17 +++++++++-------- 4 files changed, 36 insertions(+), 22 deletions(-) diff --git a/src/nile/common.py b/src/nile/common.py index 0355454e..33d9ad7c 100644 --- a/src/nile/common.py +++ b/src/nile/common.py @@ -9,7 +9,7 @@ from starkware.starknet.core.os.class_hash import compute_class_hash from starkware.starknet.services.api.contract_class import ContractClass -from nile.utils import normalize_number, str_to_felt +from nile.utils import hex_class_hash, normalize_number, str_to_felt CONTRACTS_DIRECTORY = "contracts" BUILD_DIRECTORY = "artifacts" @@ -209,4 +209,6 @@ def get_contract_class(contract_name, overriding_path=None): def get_hash(contract_name, overriding_path=None): """Return the class_hash for a given contract name.""" contract_class = get_contract_class(contract_name, overriding_path) - return compute_class_hash(contract_class=contract_class, hash_func=pedersen_hash) + return hex_class_hash( + compute_class_hash(contract_class=contract_class, hash_func=pedersen_hash) + ) diff --git a/src/nile/core/declare.py b/src/nile/core/declare.py index f51e65a4..973064e3 100644 --- a/src/nile/core/declare.py +++ b/src/nile/core/declare.py @@ -3,7 +3,7 @@ from nile import deployments from nile.common import DECLARATIONS_FILENAME, parse_information, run_command -from nile.utils import hex_address +from nile.utils import hex_address, hex_class_hash def declare( @@ -38,13 +38,12 @@ def declare( ) class_hash, tx_hash = parse_information(output) - logging.info( - f"⏳ Successfully sent declaration of {contract_name} as {hex(class_hash)}" - ) + padded_hash = hex_class_hash(class_hash) + logging.info(f"⏳ Successfully sent declaration of {contract_name} as {padded_hash}") logging.info(f"🧾 Transaction hash: {hex(tx_hash)}") - deployments.register_class_hash(class_hash, network, alias) - return class_hash + deployments.register_class_hash(padded_hash, network, alias) + return padded_hash def alias_exists(alias, network): diff --git a/src/nile/utils/__init__.py b/src/nile/utils/__init__.py index 66451fe2..b4d545d8 100644 --- a/src/nile/utils/__init__.py +++ b/src/nile/utils/__init__.py @@ -16,6 +16,9 @@ TRUE = 1 FALSE = 0 +ADDRESS_LEN = 64 +CLASS_HASH_LEN = 63 + _root = Path(__file__).parent.parent @@ -125,14 +128,23 @@ def normalize_number(number): def hex_address(number): - """Return the 64 hexadecimal characters length address.""" + """Return the 66 hexadecimal characters length address.""" + if type(number) == str and number.startswith("0x"): + return _pad_hex_to(number, ADDRESS_LEN) + else: + return _pad_hex_to(hex(int(number)), ADDRESS_LEN) + + +def hex_class_hash(number): + """Return the 65 hexadecimal characters length class hash.""" if type(number) == str and number.startswith("0x"): - return _pad_hex_to_64(number) + return _pad_hex_to(number, CLASS_HASH_LEN) else: - return _pad_hex_to_64(hex(int(number))) + return _pad_hex_to(hex(int(number)), CLASS_HASH_LEN) -def _pad_hex_to_64(hexadecimal): - if len(hexadecimal) < 66: - missing_zeros = 66 - len(hexadecimal) +def _pad_hex_to(hexadecimal, n): + if len(hexadecimal) < n + 2: + missing_zeros = n + 2 - len(hexadecimal) return hexadecimal[:2] + missing_zeros * "0" + hexadecimal[2:] + return hexadecimal diff --git a/tests/commands/test_declare.py b/tests/commands/test_declare.py index b3448725..19c535da 100644 --- a/tests/commands/test_declare.py +++ b/tests/commands/test_declare.py @@ -6,7 +6,7 @@ from nile.common import DECLARATIONS_FILENAME from nile.core.declare import alias_exists, declare -from nile.utils import hex_address +from nile.utils import hex_address, hex_class_hash @pytest.fixture(autouse=True) @@ -33,7 +33,7 @@ def test_alias_exists(): # when alias exists with patch("nile.core.declare.deployments.load_class") as mock_load: - mock_load.__iter__.side_effect = HASH + mock_load.__iter__.side_effect = hex_class_hash(HASH) assert alias_exists(ALIAS, NETWORK) is True @@ -51,7 +51,7 @@ def test_alias_exists(): "max_fee": "0", "mainnet_token": None, }, - [HASH, NETWORK, None], # expected register + [hex_class_hash(HASH), NETWORK, None], # expected register ), ( [SENDER, CONTRACT, SIGNATURE, NETWORK, ALIAS], # args @@ -64,7 +64,7 @@ def test_alias_exists(): "max_fee": "0", "mainnet_token": None, }, - [HASH, NETWORK, ALIAS], # expected register + [hex_class_hash(HASH), NETWORK, ALIAS], # expected register ), ( [SENDER, CONTRACT, SIGNATURE, NETWORK, ALIAS, PATH], # args @@ -77,7 +77,7 @@ def test_alias_exists(): "max_fee": "0", "mainnet_token": None, }, - [HASH, NETWORK, ALIAS], # expected register + [hex_class_hash(HASH), NETWORK, ALIAS], # expected register ), ( [SENDER, CONTRACT, SIGNATURE, NETWORK, ALIAS, PATH, MAX_FEE], # args @@ -90,7 +90,7 @@ def test_alias_exists(): "max_fee": str(MAX_FEE), "mainnet_token": None, }, - [HASH, NETWORK, ALIAS], # expected register + [hex_class_hash(HASH), NETWORK, ALIAS], # expected register ), ], ) @@ -104,7 +104,7 @@ def test_declare( # check return value res = declare(*args) - assert res == HASH + assert res == hex_class_hash(HASH) # check internals mock_run_cmd.assert_called_once_with(operation="declare", **exp_command) @@ -114,7 +114,8 @@ def test_declare( # check logs assert f"🚀 Declaring {CONTRACT}" in caplog.text assert ( - f"⏳ Successfully sent declaration of {CONTRACT} as {hex(HASH)}" in caplog.text + f"⏳ Successfully sent declaration of {CONTRACT} as {hex_class_hash(HASH)}" + in caplog.text ) assert f"🧾 Transaction hash: {hex(TX_HASH)}" in caplog.text