Skip to content

WIP: CIP67 and CIP68 Support #297

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

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
10 changes: 10 additions & 0 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions pycardano/cip/cip67.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from typing import Union

Check warning on line 1 in pycardano/cip/cip67.py

View check run for this annotation

Codecov / codecov/patch

pycardano/cip/cip67.py#L1

Added line #L1 was not covered by tests

from crc8 import crc8

Check warning on line 3 in pycardano/cip/cip67.py

View check run for this annotation

Codecov / codecov/patch

pycardano/cip/cip67.py#L3

Added line #L3 was not covered by tests

from pycardano.transaction import AssetName

Check warning on line 5 in pycardano/cip/cip67.py

View check run for this annotation

Codecov / codecov/patch

pycardano/cip/cip67.py#L5

Added line #L5 was not covered by tests


class InvalidCIP67Token(Exception):
pass

Check warning on line 9 in pycardano/cip/cip67.py

View check run for this annotation

Codecov / codecov/patch

pycardano/cip/cip67.py#L8-L9

Added lines #L8 - L9 were not covered by tests


class CIP67TokenName(AssetName):

Check warning on line 12 in pycardano/cip/cip67.py

View check run for this annotation

Codecov / codecov/patch

pycardano/cip/cip67.py#L12

Added line #L12 was not covered by tests
def __repr__(self):
return f"{self.__class__.__name__}({self.payload})"

def __init__(self, data: Union[bytes, str, AssetName]):

Check warning on line 16 in pycardano/cip/cip67.py

View check run for this annotation

Codecov / codecov/patch

pycardano/cip/cip67.py#L16

Added line #L16 was not covered by tests
if isinstance(data, AssetName):
data = data.payload

Check warning on line 18 in pycardano/cip/cip67.py

View check run for this annotation

Codecov / codecov/patch

pycardano/cip/cip67.py#L18

Added line #L18 was not covered by tests

if isinstance(data, bytes):
data = data.hex()

Check warning on line 21 in pycardano/cip/cip67.py

View check run for this annotation

Codecov / codecov/patch

pycardano/cip/cip67.py#L21

Added line #L21 was not covered by tests

if data[0] != "0" or data[7] != "0":
raise InvalidCIP67Token(

Check warning on line 24 in pycardano/cip/cip67.py

View check run for this annotation

Codecov / codecov/patch

pycardano/cip/cip67.py#L24

Added line #L24 was not covered by tests
"The first and eighth hex values must be 0. Instead found:\n"
+ f"first={data[0]}\n"
+ f"eigth={data[7]}"
)

checksum = crc8(bytes.fromhex(data[1:5])).hexdigest()

Check warning on line 30 in pycardano/cip/cip67.py

View check run for this annotation

Codecov / codecov/patch

pycardano/cip/cip67.py#L30

Added line #L30 was not covered by tests
if data[5:7] != checksum:
raise InvalidCIP67Token(

Check warning on line 32 in pycardano/cip/cip67.py

View check run for this annotation

Codecov / codecov/patch

pycardano/cip/cip67.py#L32

Added line #L32 was not covered by tests
f"Token label {data[1:5]} does not match token checksum.\n"
+ f"expected={checksum}\n"
+ f"received={data[5:7]}"
)

super().__init__(bytes.fromhex(data))

Check warning on line 38 in pycardano/cip/cip67.py

View check run for this annotation

Codecov / codecov/patch

pycardano/cip/cip67.py#L38

Added line #L38 was not covered by tests

@property
def label(self) -> int:
return int.from_bytes(self.payload[:3], "big") >> 4

Check warning on line 42 in pycardano/cip/cip67.py

View check run for this annotation

Codecov / codecov/patch

pycardano/cip/cip67.py#L40-L42

Added lines #L40 - L42 were not covered by tests
93 changes: 93 additions & 0 deletions pycardano/cip/cip68.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
from typing import Union

Check warning on line 1 in pycardano/cip/cip68.py

View check run for this annotation

Codecov / codecov/patch

pycardano/cip/cip68.py#L1

Added line #L1 was not covered by tests

from pycardano.cip.cip67 import CIP67TokenName
from pycardano.plutus import PlutusData
from pycardano.serialization import ArrayCBORSerializable
from pycardano.serialization import MapCBORSerializable
from pycardano.transaction import AssetName

Check warning on line 7 in pycardano/cip/cip68.py

View check run for this annotation

Codecov / codecov/patch

pycardano/cip/cip68.py#L3-L7

Added lines #L3 - L7 were not covered by tests


class InvalidCIP68ReferenceNFT(Exception):
pass

Check warning on line 11 in pycardano/cip/cip68.py

View check run for this annotation

Codecov / codecov/patch

pycardano/cip/cip68.py#L10-L11

Added lines #L10 - L11 were not covered by tests


class CIP68TokenName(CIP67TokenName):
@property
def reference_token(self) -> "CIP68ReferenceNFTName":
ref_token = self.payload.hex()[0] + "00643b" + self.payload.hex()[7:]

Check warning on line 17 in pycardano/cip/cip68.py

View check run for this annotation

Codecov / codecov/patch

pycardano/cip/cip68.py#L14-L17

Added lines #L14 - L17 were not covered by tests

return CIP68ReferenceNFTName(ref_token)

Check warning on line 19 in pycardano/cip/cip68.py

View check run for this annotation

Codecov / codecov/patch

pycardano/cip/cip68.py#L19

Added line #L19 was not covered by tests


class CIP68ReferenceNFTName(CIP68TokenName):
def __init__(self, data: Union[bytes, str, AssetName]):
super().__init__(data)

Check warning on line 24 in pycardano/cip/cip68.py

View check run for this annotation

Codecov / codecov/patch

pycardano/cip/cip68.py#L22-L24

Added lines #L22 - L24 were not covered by tests

if self.label != 100:
raise InvalidCIP68ReferenceNFT("Reference NFT must have label 100.")

Check warning on line 27 in pycardano/cip/cip68.py

View check run for this annotation

Codecov / codecov/patch

pycardano/cip/cip68.py#L27

Added line #L27 was not covered by tests


class CIP68UserNFTName(CIP68TokenName):
def __init__(self, data: Union[bytes, str, AssetName]):
super().__init__(data)

Check warning on line 32 in pycardano/cip/cip68.py

View check run for this annotation

Codecov / codecov/patch

pycardano/cip/cip68.py#L30-L32

Added lines #L30 - L32 were not covered by tests

if self.label != 222:
raise InvalidCIP68ReferenceNFT("User NFT must have label 222.")

Check warning on line 35 in pycardano/cip/cip68.py

View check run for this annotation

Codecov / codecov/patch

pycardano/cip/cip68.py#L35

Added line #L35 was not covered by tests


class CIP68UserNFTFiles(MapCBORSerializable):
name: Union[bytes, None] = None
mediaType: bytes
src: bytes

Check warning on line 41 in pycardano/cip/cip68.py

View check run for this annotation

Codecov / codecov/patch

pycardano/cip/cip68.py#L38-L41

Added lines #L38 - L41 were not covered by tests


class CIP68UserNFTMetadata(MapCBORSerializable):
name: bytes
image: bytes
description: Union[bytes, None] = None
files: Union[CIP68UserNFTFiles, None] = None

Check warning on line 48 in pycardano/cip/cip68.py

View check run for this annotation

Codecov / codecov/patch

pycardano/cip/cip68.py#L44-L48

Added lines #L44 - L48 were not covered by tests


class CIP68UserFTName(CIP68TokenName):
def __init__(self, data: Union[bytes, str, AssetName]):
super().__init__(data)

Check warning on line 53 in pycardano/cip/cip68.py

View check run for this annotation

Codecov / codecov/patch

pycardano/cip/cip68.py#L51-L53

Added lines #L51 - L53 were not covered by tests

if self.label != 333:
raise InvalidCIP68ReferenceNFT("User NFT must have label 333.")

Check warning on line 56 in pycardano/cip/cip68.py

View check run for this annotation

Codecov / codecov/patch

pycardano/cip/cip68.py#L56

Added line #L56 was not covered by tests


class CIP68UserFTMetadata(MapCBORSerializable):
name: bytes
description: bytes
ticker: Union[bytes, None] = None
url: Union[bytes, None] = None
decimals: Union[int, None] = None
logo: Union[bytes, None] = None

Check warning on line 65 in pycardano/cip/cip68.py

View check run for this annotation

Codecov / codecov/patch

pycardano/cip/cip68.py#L59-L65

Added lines #L59 - L65 were not covered by tests


class CIP68UserRFTName(CIP68TokenName):
def __init__(self, data: Union[bytes, str, AssetName]):
super().__init__(data)

Check warning on line 70 in pycardano/cip/cip68.py

View check run for this annotation

Codecov / codecov/patch

pycardano/cip/cip68.py#L68-L70

Added lines #L68 - L70 were not covered by tests

if self.label != 444:
raise InvalidCIP68ReferenceNFT("User NFT must have label 444.")

Check warning on line 73 in pycardano/cip/cip68.py

View check run for this annotation

Codecov / codecov/patch

pycardano/cip/cip68.py#L73

Added line #L73 was not covered by tests


class CIP68UserRFTMetadata(MapCBORSerializable):
name: bytes
image: bytes
description: Union[bytes, None] = None
decimals: Union[int, None] = None
files: Union[CIP68UserNFTFiles, None] = None

Check warning on line 81 in pycardano/cip/cip68.py

View check run for this annotation

Codecov / codecov/patch

pycardano/cip/cip68.py#L76-L81

Added lines #L76 - L81 were not covered by tests


class CIP68Metadata(ArrayCBORSerializable):
metadata: Union[

Check warning on line 85 in pycardano/cip/cip68.py

View check run for this annotation

Codecov / codecov/patch

pycardano/cip/cip68.py#L84-L85

Added lines #L84 - L85 were not covered by tests
CIP68UserNFTMetadata,
CIP68UserFTMetadata,
CIP68UserRFTMetadata,
MapCBORSerializable,
ArrayCBORSerializable,
]
version: int
extra: Union[PlutusData, None] = None

Check warning on line 93 in pycardano/cip/cip68.py

View check run for this annotation

Codecov / codecov/patch

pycardano/cip/cip68.py#L92-L93

Added lines #L92 - L93 were not covered by tests
Loading