|
| 1 | +import pytest |
| 2 | + |
| 3 | +from pycardano.cip.cip14 import encode_asset |
| 4 | +from pycardano.hash import ScriptHash |
| 5 | +from pycardano.transaction import AssetName |
| 6 | + |
| 7 | + |
| 8 | +@pytest.mark.parametrize( |
| 9 | + "input_types", [(str, str), (bytes, bytes), (ScriptHash, AssetName)] |
| 10 | +) |
| 11 | +@pytest.mark.parametrize( |
| 12 | + "asset", |
| 13 | + [ |
| 14 | + { |
| 15 | + "policy_id": "7eae28af2208be856f7a119668ae52a49b73725e326dc16579dcc373", |
| 16 | + "asset_name": "", |
| 17 | + "asset_fingerprint": "asset1rjklcrnsdzqp65wjgrg55sy9723kw09mlgvlc3", |
| 18 | + }, |
| 19 | + { |
| 20 | + "policy_id": "7eae28af2208be856f7a119668ae52a49b73725e326dc16579dcc37e", |
| 21 | + "asset_name": "", |
| 22 | + "asset_fingerprint": "asset1nl0puwxmhas8fawxp8nx4e2q3wekg969n2auw3", |
| 23 | + }, |
| 24 | + { |
| 25 | + "policy_id": "1e349c9bdea19fd6c147626a5260bc44b71635f398b67c59881df209", |
| 26 | + "asset_name": "", |
| 27 | + "asset_fingerprint": "asset1uyuxku60yqe57nusqzjx38aan3f2wq6s93f6ea", |
| 28 | + }, |
| 29 | + { |
| 30 | + "policy_id": "7eae28af2208be856f7a119668ae52a49b73725e326dc16579dcc373", |
| 31 | + "asset_name": "504154415445", |
| 32 | + "asset_fingerprint": "asset13n25uv0yaf5kus35fm2k86cqy60z58d9xmde92", |
| 33 | + }, |
| 34 | + { |
| 35 | + "policy_id": "1e349c9bdea19fd6c147626a5260bc44b71635f398b67c59881df209", |
| 36 | + "asset_name": "504154415445", |
| 37 | + "asset_fingerprint": "asset1hv4p5tv2a837mzqrst04d0dcptdjmluqvdx9k3", |
| 38 | + }, |
| 39 | + { |
| 40 | + "policy_id": "1e349c9bdea19fd6c147626a5260bc44b71635f398b67c59881df209", |
| 41 | + "asset_name": "7eae28af2208be856f7a119668ae52a49b73725e326dc16579dcc373", |
| 42 | + "asset_fingerprint": "asset1aqrdypg669jgazruv5ah07nuyqe0wxjhe2el6f", |
| 43 | + }, |
| 44 | + { |
| 45 | + "policy_id": "7eae28af2208be856f7a119668ae52a49b73725e326dc16579dcc373", |
| 46 | + "asset_name": "1e349c9bdea19fd6c147626a5260bc44b71635f398b67c59881df209", |
| 47 | + "asset_fingerprint": "asset17jd78wukhtrnmjh3fngzasxm8rck0l2r4hhyyt", |
| 48 | + }, |
| 49 | + { |
| 50 | + "policy_id": "7eae28af2208be856f7a119668ae52a49b73725e326dc16579dcc373", |
| 51 | + "asset_name": "0000000000000000000000000000000000000000000000000000000000000000", |
| 52 | + "asset_fingerprint": "asset1pkpwyknlvul7az0xx8czhl60pyel45rpje4z8w", |
| 53 | + }, |
| 54 | + ], |
| 55 | +) |
| 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 | + |
| 68 | + fingerprint = encode_asset( |
| 69 | + policy_id=asset["policy_id"], asset_name=asset["asset_name"] |
| 70 | + ) |
| 71 | + |
| 72 | + assert fingerprint == asset["asset_fingerprint"] |
0 commit comments