Skip to content

Commit

Permalink
Merge pull request #1721 from saratomaz/test_address_key_hash
Browse files Browse the repository at this point in the history
check 'test address key-hash'
  • Loading branch information
mkoura committed Mar 29, 2023
2 parents 9478f3f + eb5e1a4 commit e774928
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 21 deletions.
5 changes: 5 additions & 0 deletions cardano_node_tests/tests/data/golden_payment.vkey
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "PaymentVerificationKeyShelley_ed25519",
"description": "Payment Verification Key",
"cborHex": "58201f78453da13a79e1226e2f61bcdd14a4e27f96750cbc66171f31d8de109f7217"
}
61 changes: 61 additions & 0 deletions cardano_node_tests/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,67 @@ def test_address_info_with_invalid_address(self, cluster: clusterlib.ClusterLib,
assert "Invalid address" in err_str, err_str


@pytest.mark.smoke
class TestAddressKeyHash:
"""Tests for cardano-cli address key-hash."""

@allure.link(helpers.get_vcs_link())
@pytest.mark.parametrize("option", ("vkey", "vkey_file"))
def test_valid_verification_key(self, cluster: clusterlib.ClusterLib, option: str):
"""Check `address key-hash` with valid verification key."""
common.get_test_id(cluster)

vkey_file = DATA_DIR / "golden_payment.vkey"

expected_hash = "0dc8e171258165131d45451bf9e2a1be44a97d684ce2f775b7734263"

if option == "vkey":
with open(vkey_file, encoding="utf-8") as infile:
# Ignore the first 4 chars, just an informative keyword
vkey = helpers.encode_bech32(
prefix="addr_vk", data=json.loads(infile.read().strip()).get("cborHex", "")[4:]
)

vkey_hash = cluster.g_address.get_payment_vkey_hash(
payment_vkey=vkey if option == "vkey" else None,
payment_vkey_file=vkey_file if option == "vkey_file" else None,
)

assert vkey_hash == expected_hash, f"Unexpected vkey hash: {vkey_hash} != {expected_hash}"

@allure.link(helpers.get_vcs_link())
@pytest.mark.parametrize("option", ("vkey", "vkey_file"))
@hypothesis.given(vkey=st.text(alphabet=ADDR_ALPHABET, min_size=1))
@common.hypothesis_settings(max_examples=300)
def test_invalid_verification_key(self, cluster: clusterlib.ClusterLib, option: str, vkey: str):
"""Try to use `address key-hash` with invalid verification key (property-based test).
Expect failure.
"""
temp_template = f"{common.get_test_id(cluster)}_{option}_{common.unique_time_str()}"

if option == "vkey_file":
vkey_file = f"{temp_template}.redeemer"
vkey_file_content = {
"type": "PaymentVerificationKeyShelley_ed25519",
"description": "Payment Verification Key",
"cborHex": vkey,
}

with open(vkey_file, "w", encoding="utf-8") as outfile:
json.dump(vkey_file_content, outfile)

with pytest.raises(clusterlib.CLIError) as excinfo:
cluster.g_address.get_payment_vkey_hash(
payment_vkey=vkey if option == "vkey" else None,
payment_vkey_file=vkey_file if option == "vkey_file" else None,
)

err_str = str(excinfo.value)

assert "Invalid key" in err_str, err_str


@common.SKIPIF_WRONG_ERA
@pytest.mark.smoke
class TestKey:
Expand Down
20 changes: 12 additions & 8 deletions cardano_node_tests/tests/test_native_tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def simple_script_policyid(
issuer_addr = issuers_addrs[1]

# create simple script
keyhash = cluster.g_address.get_payment_vkey_hash(issuer_addr.vkey_file)
keyhash = cluster.g_address.get_payment_vkey_hash(payment_vkey_file=issuer_addr.vkey_file)
script_content = {"keyHash": keyhash, "type": "sig"}
script = Path(f"{temp_template}.script")
with open(script, "w", encoding="utf-8") as out_json:
Expand Down Expand Up @@ -251,7 +251,7 @@ def test_minting_and_burning_sign(
)[0]

# create simple script
keyhash = cluster.g_address.get_payment_vkey_hash(issuer_addr.vkey_file)
keyhash = cluster.g_address.get_payment_vkey_hash(payment_vkey_file=issuer_addr.vkey_file)
script_content = {"keyHash": keyhash, "type": "sig"}
script = Path(f"{temp_template}.script")
with open(script, "w", encoding="utf-8") as out_json:
Expand Down Expand Up @@ -334,7 +334,9 @@ def test_minting_multiple_scripts(
tokens_mint = []
for i in range(num_of_scripts):
# create simple script
keyhash = cluster.g_address.get_payment_vkey_hash(i_addrs[i].vkey_file)
keyhash = cluster.g_address.get_payment_vkey_hash(
payment_vkey_file=i_addrs[i].vkey_file
)
script_content = {"keyHash": keyhash, "type": "sig"}
script = Path(f"{temp_template}_{i}.script")
with open(script, "w", encoding="utf-8") as out_json:
Expand Down Expand Up @@ -433,7 +435,7 @@ def test_minting_burning_diff_tokens_single_tx(
issuer_addr = issuers_addrs[1]

# create simple script
keyhash = cluster.g_address.get_payment_vkey_hash(issuer_addr.vkey_file)
keyhash = cluster.g_address.get_payment_vkey_hash(payment_vkey_file=issuer_addr.vkey_file)
script_content = {"keyHash": keyhash, "type": "sig"}
script = Path(f"{temp_template}.script")
with open(script, "w", encoding="utf-8") as out_json:
Expand Down Expand Up @@ -536,7 +538,7 @@ def test_minting_burning_same_token_single_tx(
issuer_addr = issuers_addrs[1]

# create simple script
keyhash = cluster.g_address.get_payment_vkey_hash(issuer_addr.vkey_file)
keyhash = cluster.g_address.get_payment_vkey_hash(payment_vkey_file=issuer_addr.vkey_file)
script_content = {"keyHash": keyhash, "type": "sig"}
script = Path(f"{temp_template}.script")
with open(script, "w", encoding="utf-8") as out_json:
Expand Down Expand Up @@ -954,7 +956,7 @@ def test_minting_unicode_asset_name(
issuer_addr = issuers_addrs[1]

# create simple script
keyhash = cluster.g_address.get_payment_vkey_hash(issuer_addr.vkey_file)
keyhash = cluster.g_address.get_payment_vkey_hash(payment_vkey_file=issuer_addr.vkey_file)
script_content = {"keyHash": keyhash, "type": "sig"}
script = Path(f"{temp_template}.script")
with open(script, "w", encoding="utf-8") as out_json:
Expand Down Expand Up @@ -2148,7 +2150,7 @@ def test_multiasset_txouts_syntax(
issuer_addr = issuers_addrs[1]

# create simple script
keyhash = cluster.g_address.get_payment_vkey_hash(issuer_addr.vkey_file)
keyhash = cluster.g_address.get_payment_vkey_hash(payment_vkey_file=issuer_addr.vkey_file)
script_content = {"keyHash": keyhash, "type": "sig"}
script = Path(f"{temp_template}.script")
with open(script, "w", encoding="utf-8") as out_json:
Expand Down Expand Up @@ -2308,7 +2310,9 @@ def test_script_reference_utxo(

reference_type = clusterlib.ScriptTypes.SIMPLE_V1

keyhash = cluster.g_address.get_payment_vkey_hash(issuer_addr.vkey_file)
keyhash = cluster.g_address.get_payment_vkey_hash(
payment_vkey_file=issuer_addr.vkey_file
)
script_content = {"keyHash": keyhash, "type": "sig"}
script = Path(f"{temp_template}.script")
with open(script, "w", encoding="utf-8") as out_json:
Expand Down
44 changes: 33 additions & 11 deletions cardano_node_tests/tests/test_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -2052,7 +2052,9 @@ def test_script_utxo_datum(
if script_version == "simple_v1":
multisig_script = Path(f"{temp_template}_multisig.script")
script_content = {
"keyHash": cluster.g_address.get_payment_vkey_hash(dst_addr.vkey_file),
"keyHash": cluster.g_address.get_payment_vkey_hash(
payment_vkey_file=dst_addr.vkey_file
),
"type": "sig",
}
with open(multisig_script, "w", encoding="utf-8") as fp_out:
Expand Down Expand Up @@ -2175,7 +2177,9 @@ def test_script_reference_utxo(

multisig_script = Path(f"{temp_template}_multisig.script")
script_content = {
"keyHash": cluster.g_address.get_payment_vkey_hash(dst_addr.vkey_file),
"keyHash": cluster.g_address.get_payment_vkey_hash(
payment_vkey_file=dst_addr.vkey_file
),
"type": "sig",
}
with open(multisig_script, "w", encoding="utf-8") as fp_out:
Expand Down Expand Up @@ -2333,7 +2337,9 @@ def test_spend_reference_script(
if script_version == "simple_v1":
multisig_script = Path(f"{temp_template}_multisig.script")
script_content = {
"keyHash": cluster.g_address.get_payment_vkey_hash(payment_addr.vkey_file),
"keyHash": cluster.g_address.get_payment_vkey_hash(
payment_vkey_file=payment_addr.vkey_file
),
"type": "sig",
}
with open(multisig_script, "w", encoding="utf-8") as fp_out:
Expand Down Expand Up @@ -2463,19 +2469,25 @@ def test_nested_script(
"scripts": [
{
"type": "sig",
"keyHash": cluster.g_address.get_payment_vkey_hash(dst_addr1.vkey_file),
"keyHash": cluster.g_address.get_payment_vkey_hash(
payment_vkey_file=dst_addr1.vkey_file
),
},
{
"type": type_nested,
"scripts": [
{"type": "after", "slot": 100},
{
"type": "sig",
"keyHash": cluster.g_address.get_payment_vkey_hash(dst_addr2.vkey_file),
"keyHash": cluster.g_address.get_payment_vkey_hash(
payment_vkey_file=dst_addr2.vkey_file
),
},
{
"type": "sig",
"keyHash": cluster.g_address.get_payment_vkey_hash(dst_addr3.vkey_file),
"keyHash": cluster.g_address.get_payment_vkey_hash(
payment_vkey_file=dst_addr3.vkey_file
),
},
],
},
Expand Down Expand Up @@ -2551,7 +2563,9 @@ def test_nested_optional_all(
"scripts": [
{
"type": "sig",
"keyHash": cluster.g_address.get_payment_vkey_hash(dst_addr1.vkey_file),
"keyHash": cluster.g_address.get_payment_vkey_hash(
payment_vkey_file=dst_addr1.vkey_file
),
},
{
"type": "all",
Expand All @@ -2560,7 +2574,9 @@ def test_nested_optional_all(
*[
{
"type": "sig",
"keyHash": cluster.g_address.get_payment_vkey_hash(r.vkey_file),
"keyHash": cluster.g_address.get_payment_vkey_hash(
payment_vkey_file=r.vkey_file
),
}
for r in payment_addrs[2:]
],
Expand Down Expand Up @@ -2742,7 +2758,9 @@ def test_invalid( # noqa: C901
"scripts": [
{
"type": "sig",
"keyHash": cluster.g_address.get_payment_vkey_hash(dst_addr1.vkey_file),
"keyHash": cluster.g_address.get_payment_vkey_hash(
payment_vkey_file=dst_addr1.vkey_file
),
},
*script_top,
{
Expand All @@ -2751,11 +2769,15 @@ def test_invalid( # noqa: C901
*script_nested,
{
"type": "sig",
"keyHash": cluster.g_address.get_payment_vkey_hash(dst_addr2.vkey_file),
"keyHash": cluster.g_address.get_payment_vkey_hash(
payment_vkey_file=dst_addr2.vkey_file
),
},
{
"type": "sig",
"keyHash": cluster.g_address.get_payment_vkey_hash(dst_addr3.vkey_file),
"keyHash": cluster.g_address.get_payment_vkey_hash(
payment_vkey_file=dst_addr3.vkey_file
),
},
],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def test_redeemer_with_simple_minting_script(
)

# Create simple script
keyhash = cluster.g_address.get_payment_vkey_hash(issuer_addr.vkey_file)
keyhash = cluster.g_address.get_payment_vkey_hash(payment_vkey_file=issuer_addr.vkey_file)
script_content = {"keyHash": keyhash, "type": "sig"}
script = Path(f"{temp_template}.script")

Expand Down
2 changes: 1 addition & 1 deletion cardano_node_tests/utils/clusterlib_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,7 @@ def new_tokens(
) -> List[TokenRecord]:
"""Mint new token, sign using skeys."""
# create simple script
keyhash = cluster_obj.g_address.get_payment_vkey_hash(issuer_addr.vkey_file)
keyhash = cluster_obj.g_address.get_payment_vkey_hash(payment_vkey_file=issuer_addr.vkey_file)
script_content = {"keyHash": keyhash, "type": "sig"}
script = Path(f"{temp_template}.script")
with open(f"{temp_template}.script", "w", encoding="utf-8") as out_json:
Expand Down

0 comments on commit e774928

Please sign in to comment.