Skip to content
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

Add Conway Certificates #417

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
157 changes: 157 additions & 0 deletions pycardano/certificate.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,30 @@
"StakeRegistrationAndDelegationAndVoteDelegation",
"DRep",
"DRepKind",
"AuthCommitteeHotCertificate",
"ResignCommitteeColdCertificate",
"Anchor",
"DrepCredential",
"UnregDrepCertificate",
"UpdateDrepCertificate",
]

from pycardano.pool_params import PoolParams

unit_interval = Tuple[int, int]


@dataclass(repr=False)
class Anchor(ArrayCBORSerializable):
url: str
data_hash: bytes

@classmethod
@limit_primitive_type(list)
def from_primitive(cls: Type[Anchor], values: Union[list, tuple]) -> Anchor:
return cls(url=values[0], data_hash=values[1])


@dataclass(repr=False)
class StakeCredential(ArrayCBORSerializable):
_CODE: Optional[int] = field(init=False, default=None)
Expand Down Expand Up @@ -403,6 +420,142 @@
return [self.kind.value]


@dataclass(repr=False)
class AuthCommitteeHotCertificate(ArrayCBORSerializable):
_CODE: int = field(init=False, default=14)

committee_cold_credential: StakeCredential
committee_hot_credential: StakeCredential

def __post_init__(self):
self._CODE = 14

@classmethod
@limit_primitive_type(list)
def from_primitive(
cls: Type[AuthCommitteeHotCertificate], values: Union[list, tuple]
) -> AuthCommitteeHotCertificate:
if values[0] == 14:
return cls(
committee_cold_credential=StakeCredential.from_primitive(values[1]),
committee_hot_credential=StakeCredential.from_primitive(values[2]),
)
else:
raise DeserializeException(
f"Invalid AuthCommitteeHotCertificate type {values[0]}"
)


@dataclass(repr=False)
class ResignCommitteeColdCertificate(ArrayCBORSerializable):
_CODE: int = field(init=False, default=15)

committee_cold_credential: StakeCredential
anchor: Optional[Anchor]

def __post_init__(self):
self._CODE = 15

@classmethod
@limit_primitive_type(list)
def from_primitive(
cls: Type[ResignCommitteeColdCertificate], values: Union[list, tuple]
) -> ResignCommitteeColdCertificate:
if values[0] == 15:
return cls(
committee_cold_credential=StakeCredential.from_primitive(values[1]),
anchor=(
Anchor.from_primitive(values[2]) if values[2] is not None else None
),
)
else:
raise DeserializeException(
f"Invalid ResignCommitteeColdCertificate type {values[0]}"
)


@dataclass(repr=False)
class DrepCredential(ArrayCBORSerializable):
"""DRep credential is identical to StakeCredential in structure."""

_CODE: int = field(init=False, default=16)

drep_credential: StakeCredential
coin: int
anchor: Optional[Anchor] = field(default=None)

def __post_init__(self):
self._CODE = 16

@classmethod
@limit_primitive_type(list)
def from_primitive(
cls: Type[DrepCredential], values: Union[list, tuple]
) -> DrepCredential:
if values[0] == 16:
return cls(
drep_credential=StakeCredential.from_primitive(values[1]),
coin=values[2],
anchor=(
Anchor.from_primitive(values[3]) if values[3] is not None else None
),
)
else:
raise DeserializeException(f"Invalid DrepCredential type {values[0]}")

Check warning on line 504 in pycardano/certificate.py

View check run for this annotation

Codecov / codecov/patch

pycardano/certificate.py#L504

Added line #L504 was not covered by tests


@dataclass(repr=False)
class UnregDrepCertificate(ArrayCBORSerializable):
_CODE: int = field(init=False, default=17)

drep_credential: DrepCredential
coin: int

def __post_init__(self):
self._CODE = 17

@classmethod
@limit_primitive_type(list)
def from_primitive(
cls: Type[UnregDrepCertificate], values: Union[list, tuple]
) -> UnregDrepCertificate:
if values[0] == 17:
return cls(
drep_credential=DrepCredential.from_primitive(values[1]),
coin=values[2],
)
else:
raise DeserializeException(f"Invalid UnregDrepCertificate type {values[0]}")


@dataclass(repr=False)
class UpdateDrepCertificate(ArrayCBORSerializable):
_CODE: int = field(init=False, default=18)

drep_credential: DrepCredential
anchor: Optional[Anchor]

def __post_init__(self):
self._CODE = 18

@classmethod
@limit_primitive_type(list)
def from_primitive(
cls: Type[UpdateDrepCertificate], values: Union[list, tuple]
) -> UpdateDrepCertificate:
if values[0] == 18:
return cls(
drep_credential=DrepCredential.from_primitive(values[1]),
anchor=(
Anchor.from_primitive(values[2]) if values[2] is not None else None
),
)
else:
raise DeserializeException(
f"Invalid UpdateDrepCertificate type {values[0]}"
)


Certificate = Union[
StakeRegistration,
StakeDeregistration,
Expand All @@ -416,4 +569,8 @@
StakeRegistrationAndDelegation,
StakeRegistrationAndVoteDelegation,
StakeRegistrationAndDelegationAndVoteDelegation,
AuthCommitteeHotCertificate,
ResignCommitteeColdCertificate,
UnregDrepCertificate,
UpdateDrepCertificate,
]
Loading
Loading