Skip to content

Add support for asset fingerprint (CIP14) #294

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions pycardano/cip/cip14.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from typing import Union

from nacl.encoding import RawEncoder
from nacl.hash import blake2b
from pycardano.crypto.bech32 import encode


def encode_asset(policy_id: Union[bytes, str], asset_name: Union[bytes, str]) -> str:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use the ScriptHash and AssetName classes of pycardano here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... because... Hahaha

I actually had considered that. I decided to just go with raw types. I can switch that over.

Ill add in as an additional input type?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I think adding it to the Union would be great :)

"""Implementation of CIP14 asset fingerprinting

This function encodes the asset policy and name into an asset fingerprint, which is
bech32 compliant.

For more information:
https://developers.cardano.org/docs/governance/cardano-improvement-proposals/cip-0014/

Args:
policy_id: The asset policy as `bytes` or a hex `str`
asset_name: The asset name as `bytes` or a hex `str`
"""
if isinstance(policy_id, str):
policy_id = bytes.fromhex(policy_id)

if isinstance(asset_name, str):
asset_name = bytes.fromhex(asset_name)

asset_hash = blake2b(
policy_id + asset_name,
digest_size=20,
encoder=RawEncoder,
)

return encode("asset", asset_hash)
56 changes: 56 additions & 0 deletions test/pycardano/test_cip14.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import pytest

from pycardano.cip.cip14 import encode_asset


@pytest.mark.parametrize(
"asset",
[
{
"policy_id": "7eae28af2208be856f7a119668ae52a49b73725e326dc16579dcc373",
"asset_name": "",
"asset_fingerprint": "asset1rjklcrnsdzqp65wjgrg55sy9723kw09mlgvlc3",
},
{
"policy_id": "7eae28af2208be856f7a119668ae52a49b73725e326dc16579dcc37e",
"asset_name": "",
"asset_fingerprint": "asset1nl0puwxmhas8fawxp8nx4e2q3wekg969n2auw3",
},
{
"policy_id": "1e349c9bdea19fd6c147626a5260bc44b71635f398b67c59881df209",
"asset_name": "",
"asset_fingerprint": "asset1uyuxku60yqe57nusqzjx38aan3f2wq6s93f6ea",
},
{
"policy_id": "7eae28af2208be856f7a119668ae52a49b73725e326dc16579dcc373",
"asset_name": "504154415445",
"asset_fingerprint": "asset13n25uv0yaf5kus35fm2k86cqy60z58d9xmde92",
},
{
"policy_id": "1e349c9bdea19fd6c147626a5260bc44b71635f398b67c59881df209",
"asset_name": "504154415445",
"asset_fingerprint": "asset1hv4p5tv2a837mzqrst04d0dcptdjmluqvdx9k3",
},
{
"policy_id": "1e349c9bdea19fd6c147626a5260bc44b71635f398b67c59881df209",
"asset_name": "7eae28af2208be856f7a119668ae52a49b73725e326dc16579dcc373",
"asset_fingerprint": "asset1aqrdypg669jgazruv5ah07nuyqe0wxjhe2el6f",
},
{
"policy_id": "7eae28af2208be856f7a119668ae52a49b73725e326dc16579dcc373",
"asset_name": "1e349c9bdea19fd6c147626a5260bc44b71635f398b67c59881df209",
"asset_fingerprint": "asset17jd78wukhtrnmjh3fngzasxm8rck0l2r4hhyyt",
},
{
"policy_id": "7eae28af2208be856f7a119668ae52a49b73725e326dc16579dcc373",
"asset_name": "0000000000000000000000000000000000000000000000000000000000000000",
"asset_fingerprint": "asset1pkpwyknlvul7az0xx8czhl60pyel45rpje4z8w",
},
],
)
def test_encode_asset(asset):
fingerprint = encode_asset(
policy_id=asset["policy_id"], asset_name=asset["asset_name"]
)

assert fingerprint == asset["asset_fingerprint"]