Skip to content

Commit 8f80fbc

Browse files
Added support for ScriptHash and AssetName
1 parent f9c8754 commit 8f80fbc

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

pycardano/cip/cip14.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
from nacl.encoding import RawEncoder
44
from nacl.hash import blake2b
55
from pycardano.crypto.bech32 import encode
6+
from pycardano.hash import ScriptHash
7+
from pycardano.transaction import AssetName
68

79

8-
def encode_asset(policy_id: Union[bytes, str], asset_name: Union[bytes, str]) -> str:
10+
def encode_asset(
11+
policy_id: Union[ScriptHash, bytes, str], asset_name: Union[AssetName, bytes, str]
12+
) -> str:
913
"""Implementation of CIP14 asset fingerprinting
1014
1115
This function encodes the asset policy and name into an asset fingerprint, which is
@@ -15,14 +19,18 @@ def encode_asset(policy_id: Union[bytes, str], asset_name: Union[bytes, str]) ->
1519
https://developers.cardano.org/docs/governance/cardano-improvement-proposals/cip-0014/
1620
1721
Args:
18-
policy_id: The asset policy as `bytes` or a hex `str`
19-
asset_name: The asset name as `bytes` or a hex `str`
22+
policy_id: The asset policy as `ScriptHash`, `bytes`, or a hex `str`
23+
asset_name: The asset name as `AssetName`, `bytes`, or a hex `str`
2024
"""
2125
if isinstance(policy_id, str):
2226
policy_id = bytes.fromhex(policy_id)
27+
elif isinstance(policy_id, ScriptHash):
28+
policy_id = policy_id.payload
2329

2430
if isinstance(asset_name, str):
2531
asset_name = bytes.fromhex(asset_name)
32+
elif isinstance(asset_name, AssetName):
33+
asset_name = asset_name.payload
2634

2735
asset_hash = blake2b(
2836
policy_id + asset_name,

test/pycardano/test_cip14.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import pytest
22

33
from pycardano.cip.cip14 import encode_asset
4+
from pycardano.hash import ScriptHash
5+
from pycardano.transaction import AssetName
46

57

8+
@pytest.mark.parametrize(
9+
"input_types", [(str, str), (bytes, bytes), (ScriptHash, AssetName)]
10+
)
611
@pytest.mark.parametrize(
712
"asset",
813
[
@@ -48,7 +53,18 @@
4853
},
4954
],
5055
)
51-
def test_encode_asset(asset):
56+
def test_encode_asset(asset, input_types):
57+
if isinstance(input_types[0], bytes):
58+
policy_id = bytes.fromhex(asset["policy_id"])
59+
asset_name = bytes.fromhex(asset["asset_name"])
60+
elif isinstance(input_types[0], str):
61+
policy_id = asset["policy_id"]
62+
asset_name = asset["asset_name"]
63+
64+
if isinstance(input_types[0], ScriptHash):
65+
policy_id = ScriptHash(policy_id)
66+
asset_name = AssetName(asset_name)
67+
5268
fingerprint = encode_asset(
5369
policy_id=asset["policy_id"], asset_name=asset["asset_name"]
5470
)

0 commit comments

Comments
 (0)