Skip to content

Commit f9c8754

Browse files
Added support for CIP 14
1 parent 1edb549 commit f9c8754

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

pycardano/cip/cip14.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from typing import Union
2+
3+
from nacl.encoding import RawEncoder
4+
from nacl.hash import blake2b
5+
from pycardano.crypto.bech32 import encode
6+
7+
8+
def encode_asset(policy_id: Union[bytes, str], asset_name: Union[bytes, str]) -> str:
9+
"""Implementation of CIP14 asset fingerprinting
10+
11+
This function encodes the asset policy and name into an asset fingerprint, which is
12+
bech32 compliant.
13+
14+
For more information:
15+
https://developers.cardano.org/docs/governance/cardano-improvement-proposals/cip-0014/
16+
17+
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`
20+
"""
21+
if isinstance(policy_id, str):
22+
policy_id = bytes.fromhex(policy_id)
23+
24+
if isinstance(asset_name, str):
25+
asset_name = bytes.fromhex(asset_name)
26+
27+
asset_hash = blake2b(
28+
policy_id + asset_name,
29+
digest_size=20,
30+
encoder=RawEncoder,
31+
)
32+
33+
return encode("asset", asset_hash)

test/pycardano/test_cip14.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import pytest
2+
3+
from pycardano.cip.cip14 import encode_asset
4+
5+
6+
@pytest.mark.parametrize(
7+
"asset",
8+
[
9+
{
10+
"policy_id": "7eae28af2208be856f7a119668ae52a49b73725e326dc16579dcc373",
11+
"asset_name": "",
12+
"asset_fingerprint": "asset1rjklcrnsdzqp65wjgrg55sy9723kw09mlgvlc3",
13+
},
14+
{
15+
"policy_id": "7eae28af2208be856f7a119668ae52a49b73725e326dc16579dcc37e",
16+
"asset_name": "",
17+
"asset_fingerprint": "asset1nl0puwxmhas8fawxp8nx4e2q3wekg969n2auw3",
18+
},
19+
{
20+
"policy_id": "1e349c9bdea19fd6c147626a5260bc44b71635f398b67c59881df209",
21+
"asset_name": "",
22+
"asset_fingerprint": "asset1uyuxku60yqe57nusqzjx38aan3f2wq6s93f6ea",
23+
},
24+
{
25+
"policy_id": "7eae28af2208be856f7a119668ae52a49b73725e326dc16579dcc373",
26+
"asset_name": "504154415445",
27+
"asset_fingerprint": "asset13n25uv0yaf5kus35fm2k86cqy60z58d9xmde92",
28+
},
29+
{
30+
"policy_id": "1e349c9bdea19fd6c147626a5260bc44b71635f398b67c59881df209",
31+
"asset_name": "504154415445",
32+
"asset_fingerprint": "asset1hv4p5tv2a837mzqrst04d0dcptdjmluqvdx9k3",
33+
},
34+
{
35+
"policy_id": "1e349c9bdea19fd6c147626a5260bc44b71635f398b67c59881df209",
36+
"asset_name": "7eae28af2208be856f7a119668ae52a49b73725e326dc16579dcc373",
37+
"asset_fingerprint": "asset1aqrdypg669jgazruv5ah07nuyqe0wxjhe2el6f",
38+
},
39+
{
40+
"policy_id": "7eae28af2208be856f7a119668ae52a49b73725e326dc16579dcc373",
41+
"asset_name": "1e349c9bdea19fd6c147626a5260bc44b71635f398b67c59881df209",
42+
"asset_fingerprint": "asset17jd78wukhtrnmjh3fngzasxm8rck0l2r4hhyyt",
43+
},
44+
{
45+
"policy_id": "7eae28af2208be856f7a119668ae52a49b73725e326dc16579dcc373",
46+
"asset_name": "0000000000000000000000000000000000000000000000000000000000000000",
47+
"asset_fingerprint": "asset1pkpwyknlvul7az0xx8czhl60pyel45rpje4z8w",
48+
},
49+
],
50+
)
51+
def test_encode_asset(asset):
52+
fingerprint = encode_asset(
53+
policy_id=asset["policy_id"], asset_name=asset["asset_name"]
54+
)
55+
56+
assert fingerprint == asset["asset_fingerprint"]

0 commit comments

Comments
 (0)