Skip to content
This repository has been archived by the owner on Dec 1, 2023. It is now read-only.

Commit

Permalink
make get_hash return hex (#257)
Browse files Browse the repository at this point in the history
* make get_hash return hex

* fix linter

* apply review suggestions

* update class_hash len

* fix tests

* fix lenght

* fix lint
  • Loading branch information
martriay committed Nov 7, 2022
1 parent c2152a9 commit 1a3da15
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 22 deletions.
6 changes: 4 additions & 2 deletions src/nile/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
)
11 changes: 5 additions & 6 deletions src/nile/core/declare.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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):
Expand Down
24 changes: 18 additions & 6 deletions src/nile/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
TRUE = 1
FALSE = 0

ADDRESS_LEN = 64
CLASS_HASH_LEN = 63


_root = Path(__file__).parent.parent

Expand Down Expand Up @@ -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
17 changes: 9 additions & 8 deletions tests/commands/test_declare.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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


Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
),
],
)
Expand All @@ -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)
Expand All @@ -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

Expand Down

0 comments on commit 1a3da15

Please sign in to comment.