Skip to content

Commit

Permalink
Add governance action commands.
Browse files Browse the repository at this point in the history
  • Loading branch information
newhoggy committed May 26, 2023
1 parent b2b49c6 commit 193e250
Show file tree
Hide file tree
Showing 28 changed files with 1,414 additions and 61 deletions.
42 changes: 39 additions & 3 deletions cardano-api/internal/Cardano/Api/Certificate.hs
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
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
6 changes: 5 additions & 1 deletion cardano-api/src/Cardano/Api.hs
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 @@ -874,8 +878,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
1 change: 1 addition & 0 deletions cardano-cli/cardano-cli.cabal
Expand Up @@ -84,6 +84,7 @@ library
Cardano.CLI.Shelley.Run.Address.Info
Cardano.CLI.Shelley.Run.Genesis
Cardano.CLI.Shelley.Run.Governance
Cardano.CLI.Shelley.Run.Governance.Committee
Cardano.CLI.Shelley.Run.Key
Cardano.CLI.Shelley.Run.Node
Cardano.CLI.Shelley.Run.Pool
Expand Down
29 changes: 21 additions & 8 deletions cardano-cli/src/Cardano/CLI/Run/Friendly.hs
Expand Up @@ -10,6 +10,15 @@
-- | User-friendly pretty-printing for textual user interfaces (TUI)
module Cardano.CLI.Run.Friendly (friendlyTxBS, friendlyTxBodyBS) where

import Cardano.Api as Api
import Cardano.Api.Byron (KeyWitness (ByronKeyWitness))
import Cardano.Api.Shelley (Address (ShelleyAddress),
KeyWitness (ShelleyBootstrapWitness, ShelleyKeyWitness), StakeAddress (..),
StakeCredential (..), StakePoolParameters (..), fromShelleyPaymentCredential,
fromShelleyStakeCredential, fromShelleyStakeReference)

import qualified Cardano.Ledger.Shelley.API as Shelley

import Data.Aeson (Value (..), object, toJSON, (.=))
import qualified Data.Aeson as Aeson
import qualified Data.Aeson.Key as Aeson
Expand All @@ -29,14 +38,6 @@ import qualified Data.Yaml.Pretty as Yaml
import GHC.Real (denominator)
import GHC.Unicode (isAlphaNum)

import Cardano.Api as Api
import Cardano.Api.Byron (KeyWitness (ByronKeyWitness))
import Cardano.Api.Shelley (Address (ShelleyAddress),
KeyWitness (ShelleyBootstrapWitness, ShelleyKeyWitness), StakeAddress (..),
StakeCredential (..), StakePoolParameters (..), fromShelleyPaymentCredential,
fromShelleyStakeCredential, fromShelleyStakeReference)
import qualified Cardano.Ledger.Shelley.API as Shelley

yamlConfig :: Yaml.Config
yamlConfig = Yaml.defConfig & setConfCompare compare

Expand Down Expand Up @@ -338,6 +339,18 @@ friendlyCertificate =
.= serialiseToRawBytesHexText delegateKeyHash,
"VRF key hash" .= serialiseToRawBytesHexText vrfKeyHash
]

CommitteeDelegationCertificate coldKeyHash hotKeyHash ->
"committee hot key registration" .= object
[ "cold key hash" .= serialiseToRawBytesHexText coldKeyHash
, "hot key hash" .= serialiseToRawBytesHexText hotKeyHash
]

CommitteeHotKeyUnregistrationCertificate coldKeyHash ->
"committee hot key unregistration" .= object
[ "cold key hash" .= serialiseToRawBytesHexText coldKeyHash
]

MIRCertificate pot target ->
"MIR" .= object ["pot" .= friendlyMirPot pot, friendlyMirTarget target]

Expand Down

0 comments on commit 193e250

Please sign in to comment.