{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}

module Cardano.Ledger.Keys.Internal (
  KeyRole (..),
  HasKeyRole (..),
  asWitness,

  -- * DSIGN
  DSignable,
  VKey (..),
  signedDSIGN,
  verifySignedDSIGN,
  hashSignature,

  -- * Key hashes
  KeyHash (..),
  hashKey,

  -- * VRF Key Hashes
  KeyRoleVRF (..),
  VRFVerKeyHash (..),
  hashVerKeyVRF,

  -- * Genesis delegations
  GenDelegPair (..),
  GenDelegs (..),

  -- * KES
  KESignable,

  -- * VRF
  VRFSignable,

  -- * Re-exports from cardano-crypto-class
  decodeSignedDSIGN,
  encodeSignedDSIGN,
  Hash.hashWithSerialiser,
  decodeSignedKES,
  decodeVerKeyKES,
  encodeSignedKES,
  encodeVerKeyKES,
  KES.signedKES,
  KES.updateKES,
  KES.verifyKES,
  KES.verifySignedKES,
  decodeVerKeyVRF,
  encodeVerKeyVRF,
  VRF.verifyVRF,

  -- * Re-parametrised types over `crypto`
  CertifiedVRF,
  Hash,
  SignedDSIGN,
  SignKeyDSIGN,
  SignedKES,
  SignKeyKES,
  SignKeyVRF,
  VerKeyKES,
  VerKeyVRF,
)
where

import qualified Cardano.Crypto.DSIGN as DSIGN
import qualified Cardano.Crypto.Hash as Hash
import qualified Cardano.Crypto.KES as KES
import qualified Cardano.Crypto.VRF as VRF
import Cardano.Ledger.Binary (
  DecCBOR (..),
  EncCBOR (..),
  FromCBOR (..),
  ToCBOR (..),
  decodeRecordNamed,
  encodeListLen,
 )
import Cardano.Ledger.Binary.Crypto
import Cardano.Ledger.Crypto (ADDRHASH, Crypto, DSIGN, HASH, KES, VRF)
import Cardano.Ledger.Orphans ()
import Control.DeepSeq (NFData)
import Data.Aeson (FromJSON (..), FromJSONKey, ToJSON (..), ToJSONKey, (.:), (.=))
import qualified Data.Aeson as Aeson
import Data.Coerce (Coercible, coerce)
import Data.Default (Default (..))
import Data.Kind (Type)
import Data.Map.Strict (Map)
import Data.Typeable (Typeable)
import GHC.Generics (Generic)
import NoThunks.Class (NoThunks (..))
import Quiet

-- | The role of a key.
--
--   Note that a role is not _fixed_, nor is it unique. In particular, keys may
--   variously be used as witnesses, and so in many case we will change the role
--   of a key to the 'Witness' role.
--
--   It is also perfectly allowable for a key to be used in many roles; there is
--   nothing prohibiting somebody using the same underlying key as their payment
--   and staking key, as well as the key for their stake pool. So these roles
--   are more intended for two purposes:
--
--   - To make explicit how we are using a key in the specifications
--   - To provide a guide to downstream implementors, for whom the profusion of
--     keys may be confusing.
data KeyRole
  = Genesis
  | GenesisDelegate
  | Payment
  | Staking
  | StakePool
  | BlockIssuer
  | Witness
  | DRepRole
  | HotCommitteeRole
  | ColdCommitteeRole
  deriving (Int -> KeyRole -> ShowS
[KeyRole] -> ShowS
KeyRole -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [KeyRole] -> ShowS
$cshowList :: [KeyRole] -> ShowS
show :: KeyRole -> String
$cshow :: KeyRole -> String
showsPrec :: Int -> KeyRole -> ShowS
$cshowsPrec :: Int -> KeyRole -> ShowS
Show)

class HasKeyRole (a :: KeyRole -> Type -> Type) where
  -- | General coercion of key roles.
  --
  --   The presence of this function is mostly to help the user realise where they
  --   are converting key roles.
  coerceKeyRole ::
    a r c ->
    a r' c
  default coerceKeyRole ::
    Coercible (a r c) (a r' c) =>
    a r c ->
    a r' c
  coerceKeyRole = coerce :: forall a b. Coercible a b => a -> b
coerce

-- | Use a key as a witness.
--
--   This is the most common coercion between key roles, because most keys can
--   be used as witnesses to some types of transaction. As such, we provide an
--   explicit coercion for it.
asWitness ::
  HasKeyRole a =>
  a r c ->
  a 'Witness c
asWitness :: forall (a :: KeyRole -> * -> *) (r :: KeyRole) c.
HasKeyRole a =>
a r c -> a 'Witness c
asWitness = forall (a :: KeyRole -> * -> *) (r :: KeyRole) c (r' :: KeyRole).
HasKeyRole a =>
a r c -> a r' c
coerceKeyRole

--------------------------------------------------------------------------------
-- Verification keys
--------------------------------------------------------------------------------

type DSignable c = DSIGN.Signable (DSIGN c)

-- | Discriminated verification key
--
--   We wrap the basic `VerKeyDSIGN` in order to add the key role.
newtype VKey (kd :: KeyRole) c = VKey {forall (kd :: KeyRole) c. VKey kd c -> VerKeyDSIGN (DSIGN c)
unVKey :: DSIGN.VerKeyDSIGN (DSIGN c)}
  deriving (forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (kd :: KeyRole) c x. Rep (VKey kd c) x -> VKey kd c
forall (kd :: KeyRole) c x. VKey kd c -> Rep (VKey kd c) x
$cto :: forall (kd :: KeyRole) c x. Rep (VKey kd c) x -> VKey kd c
$cfrom :: forall (kd :: KeyRole) c x. VKey kd c -> Rep (VKey kd c) x
Generic)

deriving via Quiet (VKey kd c) instance Crypto c => Show (VKey kd c)

deriving instance Crypto c => Eq (VKey kd c)

deriving instance
  (Crypto c, NFData (DSIGN.VerKeyDSIGN (DSIGN c))) =>
  NFData (VKey kd c)

deriving instance Crypto c => NoThunks (VKey kd c)

instance HasKeyRole VKey

instance (Crypto c, Typeable kd) => FromCBOR (VKey kd c) where
  fromCBOR :: forall s. Decoder s (VKey kd c)
fromCBOR = forall (kd :: KeyRole) c. VerKeyDSIGN (DSIGN c) -> VKey kd c
VKey forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall v s. DSIGNAlgorithm v => Decoder s (VerKeyDSIGN v)
DSIGN.decodeVerKeyDSIGN
  {-# INLINE fromCBOR #-}

instance (Crypto c, Typeable kd) => ToCBOR (VKey kd c) where
  toCBOR :: VKey kd c -> Encoding
toCBOR = forall v. DSIGNAlgorithm v => VerKeyDSIGN v -> Encoding
DSIGN.encodeVerKeyDSIGN forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (kd :: KeyRole) c. VKey kd c -> VerKeyDSIGN (DSIGN c)
unVKey

deriving instance (Crypto c, Typeable kd) => DecCBOR (VKey kd c)

deriving instance (Crypto c, Typeable kd) => EncCBOR (VKey kd c)

-- | Produce a digital signature
signedDSIGN ::
  (Crypto c, DSIGN.Signable (DSIGN c) a) =>
  DSIGN.SignKeyDSIGN (DSIGN c) ->
  a ->
  SignedDSIGN c a
signedDSIGN :: forall c a.
(Crypto c, Signable (DSIGN c) a) =>
SignKeyDSIGN (DSIGN c) -> a -> SignedDSIGN c a
signedDSIGN SignKeyDSIGN (DSIGN c)
key a
a = forall v a.
(DSIGNAlgorithm v, Signable v a) =>
ContextDSIGN v -> a -> SignKeyDSIGN v -> SignedDSIGN v a
DSIGN.signedDSIGN () a
a SignKeyDSIGN (DSIGN c)
key

-- | Verify a digital signature
verifySignedDSIGN ::
  (Crypto c, DSIGN.Signable (DSIGN c) a) =>
  VKey kd c ->
  a ->
  SignedDSIGN c a ->
  Bool
verifySignedDSIGN :: forall c a (kd :: KeyRole).
(Crypto c, Signable (DSIGN c) a) =>
VKey kd c -> a -> SignedDSIGN c a -> Bool
verifySignedDSIGN (VKey VerKeyDSIGN (DSIGN c)
vk) a
vd SignedDSIGN c a
sigDSIGN =
  forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (forall a b. a -> b -> a
const Bool
False) (forall a b. a -> b -> a
const Bool
True) forall a b. (a -> b) -> a -> b
$ forall v a.
(DSIGNAlgorithm v, Signable v a, HasCallStack) =>
ContextDSIGN v
-> VerKeyDSIGN v -> a -> SignedDSIGN v a -> Either String ()
DSIGN.verifySignedDSIGN () VerKeyDSIGN (DSIGN c)
vk a
vd SignedDSIGN c a
sigDSIGN
{-# INLINE verifySignedDSIGN #-}

-- | Hash a given signature
hashSignature ::
  Crypto c =>
  SignedDSIGN c (Hash c h) ->
  Hash c (SignedDSIGN c (Hash c h))
hashSignature :: forall c h.
Crypto c =>
SignedDSIGN c (Hash c h) -> Hash c (SignedDSIGN c (Hash c h))
hashSignature = forall h a. HashAlgorithm h => (a -> ByteString) -> a -> Hash h a
Hash.hashWith (forall v. DSIGNAlgorithm v => SigDSIGN v -> ByteString
DSIGN.rawSerialiseSigDSIGN forall b c a. (b -> c) -> (a -> b) -> a -> c
. coerce :: forall a b. Coercible a b => a -> b
coerce)
{-# INLINE hashSignature #-}

--------------------------------------------------------------------------------
-- Key Hashes
--------------------------------------------------------------------------------

-- | Discriminated hash of public Key
newtype KeyHash (r :: KeyRole) c = KeyHash
  {forall (r :: KeyRole) c.
KeyHash r c -> Hash (ADDRHASH c) (VerKeyDSIGN (DSIGN c))
unKeyHash :: Hash.Hash (ADDRHASH c) (DSIGN.VerKeyDSIGN (DSIGN c))}
  deriving (Int -> KeyHash r c -> ShowS
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall (r :: KeyRole) c. Int -> KeyHash r c -> ShowS
forall (r :: KeyRole) c. [KeyHash r c] -> ShowS
forall (r :: KeyRole) c. KeyHash r c -> String
showList :: [KeyHash r c] -> ShowS
$cshowList :: forall (r :: KeyRole) c. [KeyHash r c] -> ShowS
show :: KeyHash r c -> String
$cshow :: forall (r :: KeyRole) c. KeyHash r c -> String
showsPrec :: Int -> KeyHash r c -> ShowS
$cshowsPrec :: forall (r :: KeyRole) c. Int -> KeyHash r c -> ShowS
Show, KeyHash r c -> KeyHash r c -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall (r :: KeyRole) c. KeyHash r c -> KeyHash r c -> Bool
/= :: KeyHash r c -> KeyHash r c -> Bool
$c/= :: forall (r :: KeyRole) c. KeyHash r c -> KeyHash r c -> Bool
== :: KeyHash r c -> KeyHash r c -> Bool
$c== :: forall (r :: KeyRole) c. KeyHash r c -> KeyHash r c -> Bool
Eq, KeyHash r c -> KeyHash r c -> Bool
KeyHash r c -> KeyHash r c -> Ordering
KeyHash r c -> KeyHash r c -> KeyHash r c
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall (r :: KeyRole) c. Eq (KeyHash r c)
forall (r :: KeyRole) c. KeyHash r c -> KeyHash r c -> Bool
forall (r :: KeyRole) c. KeyHash r c -> KeyHash r c -> Ordering
forall (r :: KeyRole) c. KeyHash r c -> KeyHash r c -> KeyHash r c
min :: KeyHash r c -> KeyHash r c -> KeyHash r c
$cmin :: forall (r :: KeyRole) c. KeyHash r c -> KeyHash r c -> KeyHash r c
max :: KeyHash r c -> KeyHash r c -> KeyHash r c
$cmax :: forall (r :: KeyRole) c. KeyHash r c -> KeyHash r c -> KeyHash r c
>= :: KeyHash r c -> KeyHash r c -> Bool
$c>= :: forall (r :: KeyRole) c. KeyHash r c -> KeyHash r c -> Bool
> :: KeyHash r c -> KeyHash r c -> Bool
$c> :: forall (r :: KeyRole) c. KeyHash r c -> KeyHash r c -> Bool
<= :: KeyHash r c -> KeyHash r c -> Bool
$c<= :: forall (r :: KeyRole) c. KeyHash r c -> KeyHash r c -> Bool
< :: KeyHash r c -> KeyHash r c -> Bool
$c< :: forall (r :: KeyRole) c. KeyHash r c -> KeyHash r c -> Bool
compare :: KeyHash r c -> KeyHash r c -> Ordering
$ccompare :: forall (r :: KeyRole) c. KeyHash r c -> KeyHash r c -> Ordering
Ord)
  deriving newtype (KeyHash r c -> ()
forall a. (a -> ()) -> NFData a
forall (r :: KeyRole) c. KeyHash r c -> ()
rnf :: KeyHash r c -> ()
$crnf :: forall (r :: KeyRole) c. KeyHash r c -> ()
NFData, Context -> KeyHash r c -> IO (Maybe ThunkInfo)
Proxy (KeyHash r c) -> String
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> String)
-> NoThunks a
forall (r :: KeyRole) c.
Context -> KeyHash r c -> IO (Maybe ThunkInfo)
forall (r :: KeyRole) c. Proxy (KeyHash r c) -> String
showTypeOf :: Proxy (KeyHash r c) -> String
$cshowTypeOf :: forall (r :: KeyRole) c. Proxy (KeyHash r c) -> String
wNoThunks :: Context -> KeyHash r c -> IO (Maybe ThunkInfo)
$cwNoThunks :: forall (r :: KeyRole) c.
Context -> KeyHash r c -> IO (Maybe ThunkInfo)
noThunks :: Context -> KeyHash r c -> IO (Maybe ThunkInfo)
$cnoThunks :: forall (r :: KeyRole) c.
Context -> KeyHash r c -> IO (Maybe ThunkInfo)
NoThunks, forall x. Rep (KeyHash r c) x -> KeyHash r c
forall x. KeyHash r c -> Rep (KeyHash r c) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (r :: KeyRole) c x. Rep (KeyHash r c) x -> KeyHash r c
forall (r :: KeyRole) c x. KeyHash r c -> Rep (KeyHash r c) x
to :: forall x. Rep (KeyHash r c) x -> KeyHash r c
$cto :: forall (r :: KeyRole) c x. Rep (KeyHash r c) x -> KeyHash r c
from :: forall x. KeyHash r c -> Rep (KeyHash r c) x
$cfrom :: forall (r :: KeyRole) c x. KeyHash r c -> Rep (KeyHash r c) x
Generic)

deriving newtype instance (Crypto c, Typeable r) => ToCBOR (KeyHash r c)

deriving newtype instance (Crypto c, Typeable r) => FromCBOR (KeyHash r c)

deriving newtype instance (Crypto c, Typeable r) => EncCBOR (KeyHash r c)

deriving newtype instance (Crypto c, Typeable r) => DecCBOR (KeyHash r c)

deriving newtype instance Crypto c => ToJSONKey (KeyHash r c)

deriving newtype instance Crypto c => FromJSONKey (KeyHash r c)

deriving newtype instance Crypto c => ToJSON (KeyHash r c)

deriving newtype instance Crypto c => FromJSON (KeyHash r c)

deriving newtype instance Crypto c => Default (KeyHash r c)

instance HasKeyRole KeyHash

-- | Hash a given public key
hashKey ::
  Crypto c =>
  VKey kd c ->
  KeyHash kd c
hashKey :: forall c (kd :: KeyRole). Crypto c => VKey kd c -> KeyHash kd c
hashKey (VKey VerKeyDSIGN (DSIGN c)
vk) = forall (r :: KeyRole) c.
Hash (ADDRHASH c) (VerKeyDSIGN (DSIGN c)) -> KeyHash r c
KeyHash forall a b. (a -> b) -> a -> b
$ forall v h.
(DSIGNAlgorithm v, HashAlgorithm h) =>
VerKeyDSIGN v -> Hash h (VerKeyDSIGN v)
DSIGN.hashVerKeyDSIGN VerKeyDSIGN (DSIGN c)
vk

--------------------------------------------------------------------------------
-- KES
--------------------------------------------------------------------------------

type KESignable c = KES.Signable (KES c)

--------------------------------------------------------------------------------
-- VRF
--------------------------------------------------------------------------------

type VRFSignable c = VRF.Signable (VRF c)

--------------------------------------------------------------------------------
-- Genesis delegation
--
-- TODO should this really live in here?
--------------------------------------------------------------------------------

data GenDelegPair c = GenDelegPair
  { forall c. GenDelegPair c -> KeyHash 'GenesisDelegate c
genDelegKeyHash :: !(KeyHash 'GenesisDelegate c)
  , forall c. GenDelegPair c -> VRFVerKeyHash 'GenDelegVRF c
genDelegVrfHash :: !(VRFVerKeyHash 'GenDelegVRF c)
  }
  deriving (Int -> GenDelegPair c -> ShowS
forall c. Int -> GenDelegPair c -> ShowS
forall c. [GenDelegPair c] -> ShowS
forall c. GenDelegPair c -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [GenDelegPair c] -> ShowS
$cshowList :: forall c. [GenDelegPair c] -> ShowS
show :: GenDelegPair c -> String
$cshow :: forall c. GenDelegPair c -> String
showsPrec :: Int -> GenDelegPair c -> ShowS
$cshowsPrec :: forall c. Int -> GenDelegPair c -> ShowS
Show, GenDelegPair c -> GenDelegPair c -> Bool
forall c. GenDelegPair c -> GenDelegPair c -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: GenDelegPair c -> GenDelegPair c -> Bool
$c/= :: forall c. GenDelegPair c -> GenDelegPair c -> Bool
== :: GenDelegPair c -> GenDelegPair c -> Bool
$c== :: forall c. GenDelegPair c -> GenDelegPair c -> Bool
Eq, GenDelegPair c -> GenDelegPair c -> Bool
GenDelegPair c -> GenDelegPair c -> Ordering
forall c. Eq (GenDelegPair c)
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall c. GenDelegPair c -> GenDelegPair c -> Bool
forall c. GenDelegPair c -> GenDelegPair c -> Ordering
forall c. GenDelegPair c -> GenDelegPair c -> GenDelegPair c
min :: GenDelegPair c -> GenDelegPair c -> GenDelegPair c
$cmin :: forall c. GenDelegPair c -> GenDelegPair c -> GenDelegPair c
max :: GenDelegPair c -> GenDelegPair c -> GenDelegPair c
$cmax :: forall c. GenDelegPair c -> GenDelegPair c -> GenDelegPair c
>= :: GenDelegPair c -> GenDelegPair c -> Bool
$c>= :: forall c. GenDelegPair c -> GenDelegPair c -> Bool
> :: GenDelegPair c -> GenDelegPair c -> Bool
$c> :: forall c. GenDelegPair c -> GenDelegPair c -> Bool
<= :: GenDelegPair c -> GenDelegPair c -> Bool
$c<= :: forall c. GenDelegPair c -> GenDelegPair c -> Bool
< :: GenDelegPair c -> GenDelegPair c -> Bool
$c< :: forall c. GenDelegPair c -> GenDelegPair c -> Bool
compare :: GenDelegPair c -> GenDelegPair c -> Ordering
$ccompare :: forall c. GenDelegPair c -> GenDelegPair c -> Ordering
Ord, forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall c x. Rep (GenDelegPair c) x -> GenDelegPair c
forall c x. GenDelegPair c -> Rep (GenDelegPair c) x
$cto :: forall c x. Rep (GenDelegPair c) x -> GenDelegPair c
$cfrom :: forall c x. GenDelegPair c -> Rep (GenDelegPair c) x
Generic)

instance NoThunks (GenDelegPair c)

instance NFData (GenDelegPair c)

instance Crypto c => EncCBOR (GenDelegPair c) where
  encCBOR :: GenDelegPair c -> Encoding
encCBOR (GenDelegPair KeyHash 'GenesisDelegate c
hk VRFVerKeyHash 'GenDelegVRF c
vrf) =
    Word -> Encoding
encodeListLen Word
2 forall a. Semigroup a => a -> a -> a
<> forall a. EncCBOR a => a -> Encoding
encCBOR KeyHash 'GenesisDelegate c
hk forall a. Semigroup a => a -> a -> a
<> forall a. EncCBOR a => a -> Encoding
encCBOR VRFVerKeyHash 'GenDelegVRF c
vrf

instance Crypto c => DecCBOR (GenDelegPair c) where
  decCBOR :: forall s. Decoder s (GenDelegPair c)
decCBOR = do
    forall a s. Text -> (a -> Int) -> Decoder s a -> Decoder s a
decodeRecordNamed
      Text
"GenDelegPair"
      (forall a b. a -> b -> a
const Int
2)
      (forall c.
KeyHash 'GenesisDelegate c
-> VRFVerKeyHash 'GenDelegVRF c -> GenDelegPair c
GenDelegPair forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. DecCBOR a => Decoder s a
decCBOR forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a s. DecCBOR a => Decoder s a
decCBOR)
  {-# INLINE decCBOR #-}

instance Crypto c => ToJSON (GenDelegPair c) where
  toJSON :: GenDelegPair c -> Value
toJSON (GenDelegPair KeyHash 'GenesisDelegate c
d VRFVerKeyHash 'GenDelegVRF c
v) =
    [Pair] -> Value
Aeson.object
      [ Key
"delegate" forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= KeyHash 'GenesisDelegate c
d
      , Key
"vrf" forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= VRFVerKeyHash 'GenDelegVRF c
v
      ]

instance Crypto c => FromJSON (GenDelegPair c) where
  parseJSON :: Value -> Parser (GenDelegPair c)
parseJSON =
    forall a. String -> (Object -> Parser a) -> Value -> Parser a
Aeson.withObject String
"GenDelegPair" forall a b. (a -> b) -> a -> b
$ \Object
obj ->
      forall c.
KeyHash 'GenesisDelegate c
-> VRFVerKeyHash 'GenDelegVRF c -> GenDelegPair c
GenDelegPair
        forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
obj forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"delegate"
        forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
obj forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"vrf"

newtype GenDelegs c = GenDelegs
  { forall c. GenDelegs c -> Map (KeyHash 'Genesis c) (GenDelegPair c)
unGenDelegs :: Map (KeyHash 'Genesis c) (GenDelegPair c)
  }
  deriving (GenDelegs c -> GenDelegs c -> Bool
forall c. GenDelegs c -> GenDelegs c -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: GenDelegs c -> GenDelegs c -> Bool
$c/= :: forall c. GenDelegs c -> GenDelegs c -> Bool
== :: GenDelegs c -> GenDelegs c -> Bool
$c== :: forall c. GenDelegs c -> GenDelegs c -> Bool
Eq, GenDelegs c -> Encoding
(forall t. EncCBOR t => Proxy t -> Size)
-> Proxy [GenDelegs c] -> Size
(forall t. EncCBOR t => Proxy t -> Size)
-> Proxy (GenDelegs c) -> Size
forall a.
Typeable a
-> (a -> Encoding)
-> ((forall t. EncCBOR t => Proxy t -> Size) -> Proxy a -> Size)
-> ((forall t. EncCBOR t => Proxy t -> Size) -> Proxy [a] -> Size)
-> EncCBOR a
forall {c}. Crypto c => Typeable (GenDelegs c)
forall c. Crypto c => GenDelegs c -> Encoding
forall c.
Crypto c =>
(forall t. EncCBOR t => Proxy t -> Size)
-> Proxy [GenDelegs c] -> Size
forall c.
Crypto c =>
(forall t. EncCBOR t => Proxy t -> Size)
-> Proxy (GenDelegs c) -> Size
encodedListSizeExpr :: (forall t. EncCBOR t => Proxy t -> Size)
-> Proxy [GenDelegs c] -> Size
$cencodedListSizeExpr :: forall c.
Crypto c =>
(forall t. EncCBOR t => Proxy t -> Size)
-> Proxy [GenDelegs c] -> Size
encodedSizeExpr :: (forall t. EncCBOR t => Proxy t -> Size)
-> Proxy (GenDelegs c) -> Size
$cencodedSizeExpr :: forall c.
Crypto c =>
(forall t. EncCBOR t => Proxy t -> Size)
-> Proxy (GenDelegs c) -> Size
encCBOR :: GenDelegs c -> Encoding
$cencCBOR :: forall c. Crypto c => GenDelegs c -> Encoding
EncCBOR, Proxy (GenDelegs c) -> Text
forall s. Decoder s (GenDelegs c)
forall a.
Typeable a
-> (forall s. Decoder s a)
-> (forall s. Proxy a -> Decoder s ())
-> (Proxy a -> Text)
-> DecCBOR a
forall s. Proxy (GenDelegs c) -> Decoder s ()
forall {c}. Crypto c => Typeable (GenDelegs c)
forall c. Crypto c => Proxy (GenDelegs c) -> Text
forall c s. Crypto c => Decoder s (GenDelegs c)
forall c s. Crypto c => Proxy (GenDelegs c) -> Decoder s ()
label :: Proxy (GenDelegs c) -> Text
$clabel :: forall c. Crypto c => Proxy (GenDelegs c) -> Text
dropCBOR :: forall s. Proxy (GenDelegs c) -> Decoder s ()
$cdropCBOR :: forall c s. Crypto c => Proxy (GenDelegs c) -> Decoder s ()
decCBOR :: forall s. Decoder s (GenDelegs c)
$cdecCBOR :: forall c s. Crypto c => Decoder s (GenDelegs c)
DecCBOR, Context -> GenDelegs c -> IO (Maybe ThunkInfo)
Proxy (GenDelegs c) -> String
forall c. Context -> GenDelegs c -> IO (Maybe ThunkInfo)
forall c. Proxy (GenDelegs c) -> String
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> String)
-> NoThunks a
showTypeOf :: Proxy (GenDelegs c) -> String
$cshowTypeOf :: forall c. Proxy (GenDelegs c) -> String
wNoThunks :: Context -> GenDelegs c -> IO (Maybe ThunkInfo)
$cwNoThunks :: forall c. Context -> GenDelegs c -> IO (Maybe ThunkInfo)
noThunks :: Context -> GenDelegs c -> IO (Maybe ThunkInfo)
$cnoThunks :: forall c. Context -> GenDelegs c -> IO (Maybe ThunkInfo)
NoThunks, GenDelegs c -> ()
forall c. GenDelegs c -> ()
forall a. (a -> ()) -> NFData a
rnf :: GenDelegs c -> ()
$crnf :: forall c. GenDelegs c -> ()
NFData, forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall c x. Rep (GenDelegs c) x -> GenDelegs c
forall c x. GenDelegs c -> Rep (GenDelegs c) x
$cto :: forall c x. Rep (GenDelegs c) x -> GenDelegs c
$cfrom :: forall c x. GenDelegs c -> Rep (GenDelegs c) x
Generic, Maybe (GenDelegs c)
Value -> Parser [GenDelegs c]
Value -> Parser (GenDelegs c)
forall c. Crypto c => Maybe (GenDelegs c)
forall c. Crypto c => Value -> Parser [GenDelegs c]
forall c. Crypto c => Value -> Parser (GenDelegs c)
forall a.
(Value -> Parser a)
-> (Value -> Parser [a]) -> Maybe a -> FromJSON a
omittedField :: Maybe (GenDelegs c)
$comittedField :: forall c. Crypto c => Maybe (GenDelegs c)
parseJSONList :: Value -> Parser [GenDelegs c]
$cparseJSONList :: forall c. Crypto c => Value -> Parser [GenDelegs c]
parseJSON :: Value -> Parser (GenDelegs c)
$cparseJSON :: forall c. Crypto c => Value -> Parser (GenDelegs c)
FromJSON)
  deriving (Int -> GenDelegs c -> ShowS
[GenDelegs c] -> ShowS
GenDelegs c -> String
forall c. Int -> GenDelegs c -> ShowS
forall c. [GenDelegs c] -> ShowS
forall c. GenDelegs c -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [GenDelegs c] -> ShowS
$cshowList :: forall c. [GenDelegs c] -> ShowS
show :: GenDelegs c -> String
$cshow :: forall c. GenDelegs c -> String
showsPrec :: Int -> GenDelegs c -> ShowS
$cshowsPrec :: forall c. Int -> GenDelegs c -> ShowS
Show) via Quiet (GenDelegs c)

deriving instance Crypto c => ToJSON (GenDelegs c)

--------------------------------------------------------------------------------
-- crypto-parametrised types
--
-- Within `cardano-ledger`, we parametrise everything on our `crypto` type
-- "package". However, in `cardano-crypto-class`, things are parametrised on the
-- original algorithm. In order to make using types from that module easier, we
-- provide some type aliases which unwrap the crypto parameters.
--------------------------------------------------------------------------------

type Hash c = Hash.Hash (HASH c)

type SignedDSIGN c = DSIGN.SignedDSIGN (DSIGN c)

type SignKeyDSIGN c = DSIGN.SignKeyDSIGN (DSIGN c)

type SignedKES c = KES.SignedKES (KES c)

type SignKeyKES c = KES.SignKeyKES (KES c)

type VerKeyKES c = KES.VerKeyKES (KES c)

type CertifiedVRF c = VRF.CertifiedVRF (VRF c)

type SignKeyVRF c = VRF.SignKeyVRF (VRF c)

type VerKeyVRF c = VRF.VerKeyVRF (VRF c)

data KeyRoleVRF
  = StakePoolVRF
  | GenDelegVRF
  | BlockIssuerVRF

-- | Discriminated hash of VRF Verification Key
newtype VRFVerKeyHash (r :: KeyRoleVRF) c = VRFVerKeyHash
  {forall (r :: KeyRoleVRF) c.
VRFVerKeyHash r c -> Hash (HASH c) KeyRoleVRF
unVRFVerKeyHash :: Hash.Hash (HASH c) KeyRoleVRF}
  deriving (Int -> VRFVerKeyHash r c -> ShowS
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall (r :: KeyRoleVRF) c. Int -> VRFVerKeyHash r c -> ShowS
forall (r :: KeyRoleVRF) c. [VRFVerKeyHash r c] -> ShowS
forall (r :: KeyRoleVRF) c. VRFVerKeyHash r c -> String
showList :: [VRFVerKeyHash r c] -> ShowS
$cshowList :: forall (r :: KeyRoleVRF) c. [VRFVerKeyHash r c] -> ShowS
show :: VRFVerKeyHash r c -> String
$cshow :: forall (r :: KeyRoleVRF) c. VRFVerKeyHash r c -> String
showsPrec :: Int -> VRFVerKeyHash r c -> ShowS
$cshowsPrec :: forall (r :: KeyRoleVRF) c. Int -> VRFVerKeyHash r c -> ShowS
Show, VRFVerKeyHash r c -> VRFVerKeyHash r c -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall (r :: KeyRoleVRF) c.
VRFVerKeyHash r c -> VRFVerKeyHash r c -> Bool
/= :: VRFVerKeyHash r c -> VRFVerKeyHash r c -> Bool
$c/= :: forall (r :: KeyRoleVRF) c.
VRFVerKeyHash r c -> VRFVerKeyHash r c -> Bool
== :: VRFVerKeyHash r c -> VRFVerKeyHash r c -> Bool
$c== :: forall (r :: KeyRoleVRF) c.
VRFVerKeyHash r c -> VRFVerKeyHash r c -> Bool
Eq, VRFVerKeyHash r c -> VRFVerKeyHash r c -> Bool
VRFVerKeyHash r c -> VRFVerKeyHash r c -> Ordering
VRFVerKeyHash r c -> VRFVerKeyHash r c -> VRFVerKeyHash r c
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall (r :: KeyRoleVRF) c. Eq (VRFVerKeyHash r c)
forall (r :: KeyRoleVRF) c.
VRFVerKeyHash r c -> VRFVerKeyHash r c -> Bool
forall (r :: KeyRoleVRF) c.
VRFVerKeyHash r c -> VRFVerKeyHash r c -> Ordering
forall (r :: KeyRoleVRF) c.
VRFVerKeyHash r c -> VRFVerKeyHash r c -> VRFVerKeyHash r c
min :: VRFVerKeyHash r c -> VRFVerKeyHash r c -> VRFVerKeyHash r c
$cmin :: forall (r :: KeyRoleVRF) c.
VRFVerKeyHash r c -> VRFVerKeyHash r c -> VRFVerKeyHash r c
max :: VRFVerKeyHash r c -> VRFVerKeyHash r c -> VRFVerKeyHash r c
$cmax :: forall (r :: KeyRoleVRF) c.
VRFVerKeyHash r c -> VRFVerKeyHash r c -> VRFVerKeyHash r c
>= :: VRFVerKeyHash r c -> VRFVerKeyHash r c -> Bool
$c>= :: forall (r :: KeyRoleVRF) c.
VRFVerKeyHash r c -> VRFVerKeyHash r c -> Bool
> :: VRFVerKeyHash r c -> VRFVerKeyHash r c -> Bool
$c> :: forall (r :: KeyRoleVRF) c.
VRFVerKeyHash r c -> VRFVerKeyHash r c -> Bool
<= :: VRFVerKeyHash r c -> VRFVerKeyHash r c -> Bool
$c<= :: forall (r :: KeyRoleVRF) c.
VRFVerKeyHash r c -> VRFVerKeyHash r c -> Bool
< :: VRFVerKeyHash r c -> VRFVerKeyHash r c -> Bool
$c< :: forall (r :: KeyRoleVRF) c.
VRFVerKeyHash r c -> VRFVerKeyHash r c -> Bool
compare :: VRFVerKeyHash r c -> VRFVerKeyHash r c -> Ordering
$ccompare :: forall (r :: KeyRoleVRF) c.
VRFVerKeyHash r c -> VRFVerKeyHash r c -> Ordering
Ord)
  deriving newtype (VRFVerKeyHash r c -> ()
forall a. (a -> ()) -> NFData a
forall (r :: KeyRoleVRF) c. VRFVerKeyHash r c -> ()
rnf :: VRFVerKeyHash r c -> ()
$crnf :: forall (r :: KeyRoleVRF) c. VRFVerKeyHash r c -> ()
NFData, Context -> VRFVerKeyHash r c -> IO (Maybe ThunkInfo)
Proxy (VRFVerKeyHash r c) -> String
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> String)
-> NoThunks a
forall (r :: KeyRoleVRF) c.
Context -> VRFVerKeyHash r c -> IO (Maybe ThunkInfo)
forall (r :: KeyRoleVRF) c. Proxy (VRFVerKeyHash r c) -> String
showTypeOf :: Proxy (VRFVerKeyHash r c) -> String
$cshowTypeOf :: forall (r :: KeyRoleVRF) c. Proxy (VRFVerKeyHash r c) -> String
wNoThunks :: Context -> VRFVerKeyHash r c -> IO (Maybe ThunkInfo)
$cwNoThunks :: forall (r :: KeyRoleVRF) c.
Context -> VRFVerKeyHash r c -> IO (Maybe ThunkInfo)
noThunks :: Context -> VRFVerKeyHash r c -> IO (Maybe ThunkInfo)
$cnoThunks :: forall (r :: KeyRoleVRF) c.
Context -> VRFVerKeyHash r c -> IO (Maybe ThunkInfo)
NoThunks, forall x. Rep (VRFVerKeyHash r c) x -> VRFVerKeyHash r c
forall x. VRFVerKeyHash r c -> Rep (VRFVerKeyHash r c) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (r :: KeyRoleVRF) c x.
Rep (VRFVerKeyHash r c) x -> VRFVerKeyHash r c
forall (r :: KeyRoleVRF) c x.
VRFVerKeyHash r c -> Rep (VRFVerKeyHash r c) x
to :: forall x. Rep (VRFVerKeyHash r c) x -> VRFVerKeyHash r c
$cto :: forall (r :: KeyRoleVRF) c x.
Rep (VRFVerKeyHash r c) x -> VRFVerKeyHash r c
from :: forall x. VRFVerKeyHash r c -> Rep (VRFVerKeyHash r c) x
$cfrom :: forall (r :: KeyRoleVRF) c x.
VRFVerKeyHash r c -> Rep (VRFVerKeyHash r c) x
Generic)

deriving newtype instance (Crypto c, Typeable r) => ToCBOR (VRFVerKeyHash r c)

deriving newtype instance (Crypto c, Typeable r) => FromCBOR (VRFVerKeyHash r c)

deriving newtype instance (Crypto c, Typeable r) => EncCBOR (VRFVerKeyHash r c)

deriving newtype instance (Crypto c, Typeable r) => DecCBOR (VRFVerKeyHash r c)

deriving newtype instance Crypto c => ToJSONKey (VRFVerKeyHash r c)

deriving newtype instance Crypto c => FromJSONKey (VRFVerKeyHash r c)

deriving newtype instance Crypto c => ToJSON (VRFVerKeyHash r c)

deriving newtype instance Crypto c => FromJSON (VRFVerKeyHash r c)

deriving newtype instance Crypto c => Default (VRFVerKeyHash r c)

hashVerKeyVRF ::
  Crypto c => VerKeyVRF c -> VRFVerKeyHash (r :: KeyRoleVRF) c
hashVerKeyVRF :: forall c (r :: KeyRoleVRF).
Crypto c =>
VerKeyVRF c -> VRFVerKeyHash r c
hashVerKeyVRF = forall (r :: KeyRoleVRF) c.
Hash (HASH c) KeyRoleVRF -> VRFVerKeyHash r c
VRFVerKeyHash forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall h a b. Hash h a -> Hash h b
Hash.castHash forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall v h.
(VRFAlgorithm v, HashAlgorithm h) =>
VerKeyVRF v -> Hash h (VerKeyVRF v)
VRF.hashVerKeyVRF