Skip to content

Commit

Permalink
New types and functions for committee keys and certificates:
Browse files Browse the repository at this point in the history
* CommitteeColdKey
* CommitteeDelegationCertificate
* CommitteeHotKey
* CommitteeHotKeyUnregistrationCertificate
* GenesisKeyDelegationCertificate
* makeCommitteeDelegationCertificate
* makeCommitteeHotKeyUnregistrationCertificate
  • Loading branch information
newhoggy committed Jun 6, 2023
1 parent 8487100 commit c898fc6
Show file tree
Hide file tree
Showing 3 changed files with 250 additions and 4 deletions.
42 changes: 39 additions & 3 deletions cardano-api/internal/Cardano/Api/Certificate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ module Cardano.Api.Certificate (
StakePoolRelay(..),
StakePoolMetadataReference(..),

makeCommitteeDelegationCertificate,
makeCommitteeHotKeyUnregistrationCertificate,

-- * Special certificates
makeMIRCertificate,
makeGenesisKeyDelegationCertificate,
Expand Down Expand Up @@ -87,9 +90,18 @@ data Certificate =
| StakePoolRetirementCertificate PoolId EpochNo

-- Special certificates
| GenesisKeyDelegationCertificate (Hash GenesisKey)
(Hash GenesisDelegateKey)
(Hash VrfKey)
| GenesisKeyDelegationCertificate
(Hash GenesisKey)
(Hash GenesisDelegateKey)
(Hash VrfKey)

| CommitteeDelegationCertificate
(Hash CommitteeColdKey)
(Hash CommitteeHotKey)

| CommitteeHotKeyUnregistrationCertificate
(Hash CommitteeColdKey)

| MIRCertificate MIRPot MIRTarget

deriving stock (Eq, Show)
Expand All @@ -114,6 +126,8 @@ instance HasTextEnvelope Certificate where
StakePoolRegistrationCertificate{} -> "Pool registration"
StakePoolRetirementCertificate{} -> "Pool retirement"
GenesisKeyDelegationCertificate{} -> "Genesis key delegation"
CommitteeDelegationCertificate{} -> "Constitution Committee key delegation"
CommitteeHotKeyUnregistrationCertificate{} -> "Constitution Committee hot key unregistration"
MIRCertificate{} -> "MIR"

-- | The 'MIRTarget' determines the target of a 'MIRCertificate'.
Expand Down Expand Up @@ -203,6 +217,17 @@ makeGenesisKeyDelegationCertificate :: Hash GenesisKey
-> Certificate
makeGenesisKeyDelegationCertificate = GenesisKeyDelegationCertificate

makeCommitteeDelegationCertificate :: ()
=> Hash CommitteeColdKey
-> Hash CommitteeHotKey
-> Certificate
makeCommitteeDelegationCertificate = CommitteeDelegationCertificate

makeCommitteeHotKeyUnregistrationCertificate :: ()
=> Hash CommitteeColdKey
-> Certificate
makeCommitteeHotKeyUnregistrationCertificate = CommitteeHotKeyUnregistrationCertificate

makeMIRCertificate :: MIRPot -> MIRTarget -> Certificate
makeMIRCertificate = MIRCertificate

Expand Down Expand Up @@ -252,6 +277,17 @@ toShelleyCertificate (GenesisKeyDelegationCertificate
delegatekh
vrfkh

toShelleyCertificate
( CommitteeDelegationCertificate
(CommitteeColdKeyHash _ckh)
(CommitteeHotKeyHash _hkh)
) = error "TODO CIP-1694 Need ledger types for CommitteeDelegationCertificate"

toShelleyCertificate
( CommitteeHotKeyUnregistrationCertificate
(CommitteeColdKeyHash _ckh)
) = error "TODO CIP-1694 Need ledger types for CommitteeHotKeyUnregistrationCertificate"

toShelleyCertificate (MIRCertificate mirpot (StakeAddressesMIR amounts)) =
Shelley.DCertMir $
Shelley.MIRCert
Expand Down
199 changes: 199 additions & 0 deletions cardano-api/internal/Cardano/Api/Keys/Shelley.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
module Cardano.Api.Keys.Shelley (

-- * Key types
CommitteeColdKey,
CommitteeHotKey,
PaymentKey,
PaymentExtendedKey,
StakeKey,
Expand Down Expand Up @@ -667,6 +669,203 @@ instance CastVerificationKeyRole GenesisKey PaymentKey where
PaymentVerificationKey (Shelley.VKey vk)


--
-- Constitutional Committee Hot Keys
--

type KeyRoleCommitteeHotKey = Shelley.Genesis -- TODO CIP-1694 this should be a newtype Shelley.CommitteeHotKey

data CommitteeHotKey

instance HasTypeProxy CommitteeHotKey where
data AsType CommitteeHotKey = AsCommitteeHotKey
proxyToAsType _ = AsCommitteeHotKey

instance Key CommitteeHotKey where

newtype VerificationKey CommitteeHotKey =
CommitteeHotVerificationKey (Shelley.VKey KeyRoleCommitteeHotKey StandardCrypto)
deriving stock (Eq)
deriving (Show, IsString) via UsingRawBytesHex (VerificationKey CommitteeHotKey)
deriving newtype (ToCBOR, FromCBOR)
deriving anyclass SerialiseAsCBOR

newtype SigningKey CommitteeHotKey =
CommitteeHotSigningKey (Shelley.SignKeyDSIGN StandardCrypto)
deriving (Show, IsString) via UsingRawBytesHex (SigningKey CommitteeHotKey)
deriving newtype (ToCBOR, FromCBOR)
deriving anyclass SerialiseAsCBOR

deterministicSigningKey :: AsType CommitteeHotKey -> Crypto.Seed -> SigningKey CommitteeHotKey
deterministicSigningKey AsCommitteeHotKey seed =
CommitteeHotSigningKey (Crypto.genKeyDSIGN seed)

deterministicSigningKeySeedSize :: AsType CommitteeHotKey -> Word
deterministicSigningKeySeedSize AsCommitteeHotKey =
Crypto.seedSizeDSIGN proxy
where
proxy :: Proxy (Shelley.DSIGN StandardCrypto)
proxy = Proxy

getVerificationKey :: SigningKey CommitteeHotKey -> VerificationKey CommitteeHotKey
getVerificationKey (CommitteeHotSigningKey sk) =
CommitteeHotVerificationKey (Shelley.VKey (Crypto.deriveVerKeyDSIGN sk))

verificationKeyHash :: VerificationKey CommitteeHotKey -> Hash CommitteeHotKey
verificationKeyHash (CommitteeHotVerificationKey vkey) =
CommitteeHotKeyHash (Shelley.hashKey vkey)


instance SerialiseAsRawBytes (VerificationKey CommitteeHotKey) where
serialiseToRawBytes (CommitteeHotVerificationKey (Shelley.VKey vk)) =
Crypto.rawSerialiseVerKeyDSIGN vk

deserialiseFromRawBytes (AsVerificationKey AsCommitteeHotKey) bs =
maybeToRight (SerialiseAsRawBytesError "Unable to deserialise VerificationKey Constitutional Committee Hot Key") $
CommitteeHotVerificationKey . Shelley.VKey <$>
Crypto.rawDeserialiseVerKeyDSIGN bs

instance SerialiseAsRawBytes (SigningKey CommitteeHotKey) where
serialiseToRawBytes (CommitteeHotSigningKey sk) =
Crypto.rawSerialiseSignKeyDSIGN sk

deserialiseFromRawBytes (AsSigningKey AsCommitteeHotKey) bs =
maybeToRight (SerialiseAsRawBytesError "Unable to deserialise SigningKey Constitional Committee Hot Key") $
CommitteeHotSigningKey <$> Crypto.rawDeserialiseSignKeyDSIGN bs


newtype instance Hash CommitteeHotKey =
CommitteeHotKeyHash (Shelley.KeyHash KeyRoleCommitteeHotKey StandardCrypto)
deriving stock (Eq, Ord)
deriving (Show, IsString) via UsingRawBytesHex (Hash CommitteeHotKey)
deriving (ToCBOR, FromCBOR) via UsingRawBytes (Hash CommitteeHotKey)
deriving anyclass SerialiseAsCBOR

instance SerialiseAsRawBytes (Hash CommitteeHotKey) where
serialiseToRawBytes (CommitteeHotKeyHash (Shelley.KeyHash vkh)) =
Crypto.hashToBytes vkh

deserialiseFromRawBytes (AsHash AsCommitteeHotKey) bs =
maybeToRight (SerialiseAsRawBytesError "Unable to deserialise Hash Constitional Committee Hot Key") $
CommitteeHotKeyHash . Shelley.KeyHash <$> Crypto.hashFromBytes bs

instance HasTextEnvelope (VerificationKey CommitteeHotKey) where
textEnvelopeType _ = "ConstitionalCommitteeHotVerificationKey_"
<> fromString (Crypto.algorithmNameDSIGN proxy)
where
proxy :: Proxy (Shelley.DSIGN StandardCrypto)
proxy = Proxy

instance HasTextEnvelope (SigningKey CommitteeHotKey) where
textEnvelopeType _ = "ConstitionalCommitteeHotSigningKey_"
<> fromString (Crypto.algorithmNameDSIGN proxy)
where
proxy :: Proxy (Shelley.DSIGN StandardCrypto)
proxy = Proxy

instance CastVerificationKeyRole CommitteeHotKey PaymentKey where
castVerificationKey (CommitteeHotVerificationKey (Shelley.VKey vk)) =
PaymentVerificationKey (Shelley.VKey vk)


--
-- Constitutional Committee Cold Keys
--

type KeyRoleCommitteeColdKey = Shelley.Genesis -- TODO CIP-1694 this should be a newtype Shelley.CommitteeColdKey

data CommitteeColdKey

instance HasTypeProxy CommitteeColdKey where
data AsType CommitteeColdKey = AsCommitteeColdKey
proxyToAsType _ = AsCommitteeColdKey

instance Key CommitteeColdKey where

newtype VerificationKey CommitteeColdKey =
CommitteeColdVerificationKey (Shelley.VKey KeyRoleCommitteeColdKey StandardCrypto)
deriving stock (Eq)
deriving (Show, IsString) via UsingRawBytesHex (VerificationKey CommitteeColdKey)
deriving newtype (ToCBOR, FromCBOR)
deriving anyclass SerialiseAsCBOR

newtype SigningKey CommitteeColdKey =
CommitteeColdSigningKey (Shelley.SignKeyDSIGN StandardCrypto)
deriving (Show, IsString) via UsingRawBytesHex (SigningKey CommitteeColdKey)
deriving newtype (ToCBOR, FromCBOR)
deriving anyclass SerialiseAsCBOR

deterministicSigningKey :: AsType CommitteeColdKey -> Crypto.Seed -> SigningKey CommitteeColdKey
deterministicSigningKey AsCommitteeColdKey seed =
CommitteeColdSigningKey (Crypto.genKeyDSIGN seed)

deterministicSigningKeySeedSize :: AsType CommitteeColdKey -> Word
deterministicSigningKeySeedSize AsCommitteeColdKey =
Crypto.seedSizeDSIGN proxy
where
proxy :: Proxy (Shelley.DSIGN StandardCrypto)
proxy = Proxy

getVerificationKey :: SigningKey CommitteeColdKey -> VerificationKey CommitteeColdKey
getVerificationKey (CommitteeColdSigningKey sk) =
CommitteeColdVerificationKey (Shelley.VKey (Crypto.deriveVerKeyDSIGN sk))

verificationKeyHash :: VerificationKey CommitteeColdKey -> Hash CommitteeColdKey
verificationKeyHash (CommitteeColdVerificationKey vkey) =
CommitteeColdKeyHash (Shelley.hashKey vkey)


instance SerialiseAsRawBytes (VerificationKey CommitteeColdKey) where
serialiseToRawBytes (CommitteeColdVerificationKey (Shelley.VKey vk)) =
Crypto.rawSerialiseVerKeyDSIGN vk

deserialiseFromRawBytes (AsVerificationKey AsCommitteeColdKey) bs =
maybeToRight (SerialiseAsRawBytesError "Unable to deserialise VerificationKey Constitutional Committee Cold Key") $
CommitteeColdVerificationKey . Shelley.VKey <$>
Crypto.rawDeserialiseVerKeyDSIGN bs

instance SerialiseAsRawBytes (SigningKey CommitteeColdKey) where
serialiseToRawBytes (CommitteeColdSigningKey sk) =
Crypto.rawSerialiseSignKeyDSIGN sk

deserialiseFromRawBytes (AsSigningKey AsCommitteeColdKey) bs =
maybeToRight (SerialiseAsRawBytesError "Unable to deserialise SigningKey Constitional Committee Cold Key") $
CommitteeColdSigningKey <$> Crypto.rawDeserialiseSignKeyDSIGN bs


newtype instance Hash CommitteeColdKey =
CommitteeColdKeyHash (Shelley.KeyHash KeyRoleCommitteeColdKey StandardCrypto)
deriving stock (Eq, Ord)
deriving (Show, IsString) via UsingRawBytesHex (Hash CommitteeColdKey)
deriving (ToCBOR, FromCBOR) via UsingRawBytes (Hash CommitteeColdKey)
deriving anyclass SerialiseAsCBOR

instance SerialiseAsRawBytes (Hash CommitteeColdKey) where
serialiseToRawBytes (CommitteeColdKeyHash (Shelley.KeyHash vkh)) =
Crypto.hashToBytes vkh

deserialiseFromRawBytes (AsHash AsCommitteeColdKey) bs =
maybeToRight (SerialiseAsRawBytesError "Unable to deserialise Hash Constitional Committee Cold Key") $
CommitteeColdKeyHash . Shelley.KeyHash <$> Crypto.hashFromBytes bs

instance HasTextEnvelope (VerificationKey CommitteeColdKey) where
textEnvelopeType _ = "ConstitionalCommitteeColdVerificationKey_"
<> fromString (Crypto.algorithmNameDSIGN proxy)
where
proxy :: Proxy (Shelley.DSIGN StandardCrypto)
proxy = Proxy

instance HasTextEnvelope (SigningKey CommitteeColdKey) where
textEnvelopeType _ = "ConstitionalCommitteeColdSigningKey_"
<> fromString (Crypto.algorithmNameDSIGN proxy)
where
proxy :: Proxy (Shelley.DSIGN StandardCrypto)
proxy = Proxy

instance CastVerificationKeyRole CommitteeColdKey PaymentKey where
castVerificationKey (CommitteeColdVerificationKey (Shelley.VKey vk)) =
PaymentVerificationKey (Shelley.VKey vk)

--
-- Shelley genesis extended ed25519 keys
--
Expand Down
13 changes: 12 additions & 1 deletion cardano-api/src/Cardano/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,10 @@ module Cardano.Api (
getOpCertCount,
issueOperationalCertificate,

-- * Constitutional Committee keys
CommitteeColdKey,
CommitteeHotKey,

-- * Genesis file
-- | Types and functions needed to inspect or create a genesis file.
GenesisKey,
Expand Down Expand Up @@ -845,6 +849,13 @@ module Cardano.Api (

-- ** CLI option parsing
bounded,

-- * Governance

-- ** Governance Committee
makeCommitteeDelegationCertificate,
makeCommitteeHotKeyUnregistrationCertificate,

) where

import Cardano.Api.Address
Expand Down Expand Up @@ -874,8 +885,8 @@ import Cardano.Api.LedgerEvent
import Cardano.Api.LedgerState
import Cardano.Api.Modes
import Cardano.Api.NetworkId
import Cardano.Api.Orphans ()
import Cardano.Api.OperationalCertificate
import Cardano.Api.Orphans ()
import Cardano.Api.Protocol
import Cardano.Api.ProtocolParameters
import Cardano.Api.Query hiding (LedgerState (..))
Expand Down

0 comments on commit c898fc6

Please sign in to comment.