Skip to content

Commit

Permalink
feat(cardano): conway staking reg certificate
Browse files Browse the repository at this point in the history
  • Loading branch information
jaskp committed Nov 6, 2023
1 parent 72c52f2 commit 2f2adc9
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 70 deletions.
2 changes: 2 additions & 0 deletions common/protob/messages-cardano.proto
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ enum CardanoCertificateType {
STAKE_DEREGISTRATION = 1;
STAKE_DELEGATION = 2;
STAKE_POOL_REGISTRATION = 3;
STAKE_REGISTRATION_CONWAY = 7;
}

enum CardanoPoolRelayType {
Expand Down Expand Up @@ -346,6 +347,7 @@ message CardanoTxCertificate {
optional CardanoPoolParametersType pool_parameters = 4; // used for stake pool registration certificate
optional bytes script_hash = 5; // stake credential script hash
optional bytes key_hash = 6; // stake credential key hash
optional uint64 coins = 7; // used for stake key registration certificate
}

/**
Expand Down
13 changes: 13 additions & 0 deletions core/src/apps/cardano/certificates.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def validate(
if certificate.type in (
CCT.STAKE_DELEGATION,
CCT.STAKE_REGISTRATION,
CCT.STAKE_REGISTRATION_CONWAY,
CCT.STAKE_DEREGISTRATION,
):
validate_stake_credential(
Expand Down Expand Up @@ -72,6 +73,7 @@ def _validate_structure(certificate: messages.CardanoTxCertificate) -> None:

fields_to_be_empty: dict[CCT, tuple[Any, ...]] = {
CCT.STAKE_REGISTRATION: (pool, pool_parameters),
CCT.STAKE_REGISTRATION_CONWAY: (pool, pool_parameters),
CCT.STAKE_DELEGATION: (pool_parameters,),
CCT.STAKE_DEREGISTRATION: (pool, pool_parameters),
CCT.STAKE_POOL_REGISTRATION: (
Expand Down Expand Up @@ -106,6 +108,17 @@ def cborize(
certificate.key_hash,
),
)
elif cert_type == CardanoCertificateType.STAKE_REGISTRATION_CONWAY:
return (
cert_type,
cborize_stake_credential(
keychain,
certificate.path,
certificate.script_hash,
certificate.key_hash,
),
certificate.coins,
)
elif cert_type == CardanoCertificateType.STAKE_DELEGATION:
return (
cert_type,
Expand Down
8 changes: 7 additions & 1 deletion core/src/apps/cardano/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@

CERTIFICATE_TYPE_NAMES = {
CardanoCertificateType.STAKE_REGISTRATION: "Stake key registration",
CardanoCertificateType.STAKE_REGISTRATION_CONWAY: "Stake key registration with deposit",
CardanoCertificateType.STAKE_DEREGISTRATION: "Stake key deregistration",
CardanoCertificateType.STAKE_DELEGATION: "Stake delegation",
CardanoCertificateType.STAKE_POOL_REGISTRATION: "Stakepool registration",
Expand Down Expand Up @@ -532,7 +533,9 @@ async def confirm_tx(
)


async def confirm_certificate(certificate: messages.CardanoTxCertificate) -> None:
async def confirm_certificate(
certificate: messages.CardanoTxCertificate, network_id: int
) -> None:
# stake pool registration requires custom confirmation logic not covered
# in this call
assert certificate.type != CardanoCertificateType.STAKE_POOL_REGISTRATION
Expand All @@ -547,6 +550,9 @@ async def confirm_certificate(certificate: messages.CardanoTxCertificate) -> Non
if certificate.type == CardanoCertificateType.STAKE_DELEGATION:
assert certificate.pool is not None # validate_certificate
props.append(("to pool:", format_stake_pool_id(certificate.pool)))
elif certificate.type == CardanoCertificateType.STAKE_REGISTRATION_CONWAY:
assert certificate.coins is not None
props.append(("deposit:", format_coin_amount(certificate.coins, network_id)))

await confirm_properties(
"confirm_certificate",
Expand Down
2 changes: 1 addition & 1 deletion core/src/apps/cardano/sign_tx/signer.py
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ async def _show_certificate(
certificate.pool_parameters.metadata
)
else:
await layout.confirm_certificate(certificate)
await layout.confirm_certificate(certificate, self.msg.network_id)

# pool owners

Expand Down
1 change: 1 addition & 0 deletions core/src/trezor/enums/CardanoCertificateType.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
STAKE_DEREGISTRATION = 1
STAKE_DELEGATION = 2
STAKE_POOL_REGISTRATION = 3
STAKE_REGISTRATION_CONWAY = 7
1 change: 1 addition & 0 deletions core/src/trezor/enums/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ class CardanoCertificateType(IntEnum):
STAKE_DEREGISTRATION = 1
STAKE_DELEGATION = 2
STAKE_POOL_REGISTRATION = 3
STAKE_REGISTRATION_CONWAY = 7

class CardanoPoolRelayType(IntEnum):
SINGLE_HOST_IP = 0
Expand Down
2 changes: 2 additions & 0 deletions core/src/trezor/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -1617,6 +1617,7 @@ class CardanoTxCertificate(protobuf.MessageType):
pool_parameters: "CardanoPoolParametersType | None"
script_hash: "bytes | None"
key_hash: "bytes | None"
coins: "int | None"

def __init__(
self,
Expand All @@ -1627,6 +1628,7 @@ def __init__(
pool_parameters: "CardanoPoolParametersType | None" = None,
script_hash: "bytes | None" = None,
key_hash: "bytes | None" = None,
coins: "int | None" = None,
) -> None:
pass

Expand Down
18 changes: 18 additions & 0 deletions python/src/trezorlib/cardano.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,24 @@ def parse_certificate(certificate: dict) -> CertificateWithPoolOwnersAndRelays:
),
None,
)
elif certificate_type == messages.CardanoCertificateType.STAKE_REGISTRATION_CONWAY:
if "coins" not in certificate:
raise CERTIFICATE_MISSING_FIELDS_ERROR

path, script_hash, key_hash = _parse_credential(
certificate, CERTIFICATE_MISSING_FIELDS_ERROR
)

return (
messages.CardanoTxCertificate(
type=certificate_type,
path=path,
script_hash=script_hash,
key_hash=key_hash,
coins=int(certificate["coins"]),
),
None,
)
elif certificate_type == messages.CardanoCertificateType.STAKE_POOL_REGISTRATION:
pool_parameters = certificate["pool_parameters"]

Expand Down
4 changes: 4 additions & 0 deletions python/src/trezorlib/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ class CardanoCertificateType(IntEnum):
STAKE_DEREGISTRATION = 1
STAKE_DELEGATION = 2
STAKE_POOL_REGISTRATION = 3
STAKE_REGISTRATION_CONWAY = 7


class CardanoPoolRelayType(IntEnum):
Expand Down Expand Up @@ -2631,6 +2632,7 @@ class CardanoTxCertificate(protobuf.MessageType):
4: protobuf.Field("pool_parameters", "CardanoPoolParametersType", repeated=False, required=False, default=None),
5: protobuf.Field("script_hash", "bytes", repeated=False, required=False, default=None),
6: protobuf.Field("key_hash", "bytes", repeated=False, required=False, default=None),
7: protobuf.Field("coins", "uint64", repeated=False, required=False, default=None),
}

def __init__(
Expand All @@ -2642,13 +2644,15 @@ def __init__(
pool_parameters: Optional["CardanoPoolParametersType"] = None,
script_hash: Optional["bytes"] = None,
key_hash: Optional["bytes"] = None,
coins: Optional["int"] = None,
) -> None:
self.path: Sequence["int"] = path if path is not None else []
self.type = type
self.pool = pool
self.pool_parameters = pool_parameters
self.script_hash = script_hash
self.key_hash = key_hash
self.coins = coins


class CardanoTxWithdrawal(protobuf.MessageType):
Expand Down
Loading

0 comments on commit 2f2adc9

Please sign in to comment.