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 25, 2023
1 parent 75cbfe4 commit 30e5696
Show file tree
Hide file tree
Showing 30 changed files with 1,510 additions and 61 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(..),

makeCcDelegationCertificate,
makeCcHotKeyUnregistrationCertificate,

-- * 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)

| CcDelegationCertificate
(Hash CcColdKey)
(Hash CcHotKey)

| CcHotKeyUnregistrationCertificate
(Hash CcColdKey)

| 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"
CcDelegationCertificate{} -> "Constitution Committee key delegation"
CcHotKeyUnregistrationCertificate{} -> "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

makeCcDelegationCertificate :: ()
=> Hash CcColdKey
-> Hash CcHotKey
-> Certificate
makeCcDelegationCertificate = CcDelegationCertificate

makeCcHotKeyUnregistrationCertificate :: ()
=> Hash CcColdKey
-> Certificate
makeCcHotKeyUnregistrationCertificate = CcHotKeyUnregistrationCertificate

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

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

toShelleyCertificate
( CcDelegationCertificate
(CcColdKeyHash _ckh)
(CcHotKeyHash _hkh)
) = error "TODO CIP-1694 Need ledger types for CcDelegationCertificate"

toShelleyCertificate
( CcHotKeyUnregistrationCertificate
(CcColdKeyHash _ckh)
) = error "TODO CIP-1694 Need ledger types for CcHotKeyUnregistrationCertificate"

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
CcColdKey,
CcHotKey,
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 KeyRoleCcHotKey = Shelley.Genesis -- TODO CIP-1694 this should be a newtype Shelley.CommitteeHotKey

data CcHotKey

instance HasTypeProxy CcHotKey where
data AsType CcHotKey = AsCcHotKey
proxyToAsType _ = AsCcHotKey

instance Key CcHotKey where

newtype VerificationKey CcHotKey =
CcHotVerificationKey (Shelley.VKey KeyRoleCcHotKey StandardCrypto)
deriving stock (Eq)
deriving (Show, IsString) via UsingRawBytesHex (VerificationKey CcHotKey)
deriving newtype (ToCBOR, FromCBOR)
deriving anyclass SerialiseAsCBOR

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

deterministicSigningKey :: AsType CcHotKey -> Crypto.Seed -> SigningKey CcHotKey
deterministicSigningKey AsCcHotKey seed =
CcHotSigningKey (Crypto.genKeyDSIGN seed)

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

getVerificationKey :: SigningKey CcHotKey -> VerificationKey CcHotKey
getVerificationKey (CcHotSigningKey sk) =
CcHotVerificationKey (Shelley.VKey (Crypto.deriveVerKeyDSIGN sk))

verificationKeyHash :: VerificationKey CcHotKey -> Hash CcHotKey
verificationKeyHash (CcHotVerificationKey vkey) =
CcHotKeyHash (Shelley.hashKey vkey)


instance SerialiseAsRawBytes (VerificationKey CcHotKey) where
serialiseToRawBytes (CcHotVerificationKey (Shelley.VKey vk)) =
Crypto.rawSerialiseVerKeyDSIGN vk

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

instance SerialiseAsRawBytes (SigningKey CcHotKey) where
serialiseToRawBytes (CcHotSigningKey sk) =
Crypto.rawSerialiseSignKeyDSIGN sk

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


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

instance SerialiseAsRawBytes (Hash CcHotKey) where
serialiseToRawBytes (CcHotKeyHash (Shelley.KeyHash vkh)) =
Crypto.hashToBytes vkh

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

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

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

instance CastVerificationKeyRole CcHotKey PaymentKey where
castVerificationKey (CcHotVerificationKey (Shelley.VKey vk)) =
PaymentVerificationKey (Shelley.VKey vk)


--
-- Constitutional Committee Cold Keys
--

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

data CcColdKey

instance HasTypeProxy CcColdKey where
data AsType CcColdKey = AsCcColdKey
proxyToAsType _ = AsCcColdKey

instance Key CcColdKey where

newtype VerificationKey CcColdKey =
CcColdVerificationKey (Shelley.VKey KeyRoleCcColdKey StandardCrypto)
deriving stock (Eq)
deriving (Show, IsString) via UsingRawBytesHex (VerificationKey CcColdKey)
deriving newtype (ToCBOR, FromCBOR)
deriving anyclass SerialiseAsCBOR

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

deterministicSigningKey :: AsType CcColdKey -> Crypto.Seed -> SigningKey CcColdKey
deterministicSigningKey AsCcColdKey seed =
CcColdSigningKey (Crypto.genKeyDSIGN seed)

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

getVerificationKey :: SigningKey CcColdKey -> VerificationKey CcColdKey
getVerificationKey (CcColdSigningKey sk) =
CcColdVerificationKey (Shelley.VKey (Crypto.deriveVerKeyDSIGN sk))

verificationKeyHash :: VerificationKey CcColdKey -> Hash CcColdKey
verificationKeyHash (CcColdVerificationKey vkey) =
CcColdKeyHash (Shelley.hashKey vkey)


instance SerialiseAsRawBytes (VerificationKey CcColdKey) where
serialiseToRawBytes (CcColdVerificationKey (Shelley.VKey vk)) =
Crypto.rawSerialiseVerKeyDSIGN vk

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

instance SerialiseAsRawBytes (SigningKey CcColdKey) where
serialiseToRawBytes (CcColdSigningKey sk) =
Crypto.rawSerialiseSignKeyDSIGN sk

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


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

instance SerialiseAsRawBytes (Hash CcColdKey) where
serialiseToRawBytes (CcColdKeyHash (Shelley.KeyHash vkh)) =
Crypto.hashToBytes vkh

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

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

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

instance CastVerificationKeyRole CcColdKey PaymentKey where
castVerificationKey (CcColdVerificationKey (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
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,10 @@ module Cardano.Api (
getOpCertCount,
issueOperationalCertificate,

-- * Constitutional Committee keys
CcColdKey,
CcHotKey,

-- * 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
Original file line number Diff line number Diff line change
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.Cc
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
Original file line number Diff line number Diff line change
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
]

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

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

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

Expand Down
Loading

0 comments on commit 30e5696

Please sign in to comment.