-
-
Notifications
You must be signed in to change notification settings - Fork 77
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
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
fdfd3e2
Added extended signing key support for cip8
theeldermillenial 01230ac
Fixed unused imports, flake8 checks pass.
theeldermillenial 143e843
Fixed mypy error for overloaded variable
theeldermillenial 049c6bf
Remove extraneous parameter for verify
theeldermillenial 434a163
Merge branch 'main' of https://github.com/Python-Cardano/pycardano in…
theeldermillenial 1bf3f81
Added ByteString to _restored_typed_primitive
theeldermillenial 67add7a
Added type checking
theeldermillenial f0644e7
Merge pull request #1 from theeldermillenial/bugfix/bytestring
theeldermillenial 1edb549
Merge branch 'main' of https://github.com/Python-Cardano/pycardano
theeldermillenial f9c8754
Added support for CIP 14
theeldermillenial 8f80fbc
Added support for ScriptHash and AssetName
theeldermillenial b9e6120
Add import to cips and docs
nielstron File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | ||
"""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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 :)