{-# 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 (..),
  KeyPair (..), -- deprecated
  signedDSIGN,
  verifySignedDSIGN,
  hashSignature,

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

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

  -- * 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.hashVerKeyVRF,
  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.Class (Default (..))
import Data.Kind (Type)
import Data.Map.Strict (Map)
import Data.Set (Set)
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)

data KeyPair (kd :: KeyRole) c = KeyPair
  { forall (kd :: KeyRole) c. KeyPair kd c -> VKey kd c
vKey :: !(VKey kd c)
  , forall (kd :: KeyRole) c. KeyPair kd c -> SignKeyDSIGN (DSIGN c)
sKey :: !(DSIGN.SignKeyDSIGN (DSIGN c))
  }
{-# DEPRECATED KeyPair "Use `Test.Cardano.Ledger.Core.KeyPair (KeyPair)` instead" #-}

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

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

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

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

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

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

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

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

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

instance Crypto b => Default (KeyHash a b) where
  def :: KeyHash a b
def = forall (discriminator :: KeyRole) c.
Hash (ADDRHASH c) (VerKeyDSIGN (DSIGN c))
-> KeyHash discriminator c
KeyHash forall a. Default a => a
def

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 (discriminator :: KeyRole) c.
Hash (ADDRHASH c) (VerKeyDSIGN (DSIGN c))
-> KeyHash discriminator 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 -> Hash c (VerKeyVRF c)
genDelegVrfHash :: !(Hash c (VerKeyVRF 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 Hash (HASH c) (VerKeyVRF (VRF 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 Hash (HASH c) (VerKeyVRF (VRF 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
-> Hash c (VerKeyVRF 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 Hash (HASH c) (VerKeyVRF (VRF 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
.= Hash (HASH c) (VerKeyVRF (VRF 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
-> Hash c (VerKeyVRF 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)

newtype GKeys c = GKeys {forall c. GKeys c -> Set (VKey 'Genesis c)
unGKeys :: Set (VKey 'Genesis c)}
  deriving (GKeys c -> GKeys c -> Bool
forall c. Crypto c => GKeys c -> GKeys c -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: GKeys c -> GKeys c -> Bool
$c/= :: forall c. Crypto c => GKeys c -> GKeys c -> Bool
== :: GKeys c -> GKeys c -> Bool
$c== :: forall c. Crypto c => GKeys c -> GKeys c -> Bool
Eq, Context -> GKeys c -> IO (Maybe ThunkInfo)
Proxy (GKeys c) -> String
forall c. Crypto c => Context -> GKeys c -> IO (Maybe ThunkInfo)
forall c. Crypto c => Proxy (GKeys c) -> String
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> String)
-> NoThunks a
showTypeOf :: Proxy (GKeys c) -> String
$cshowTypeOf :: forall c. Crypto c => Proxy (GKeys c) -> String
wNoThunks :: Context -> GKeys c -> IO (Maybe ThunkInfo)
$cwNoThunks :: forall c. Crypto c => Context -> GKeys c -> IO (Maybe ThunkInfo)
noThunks :: Context -> GKeys c -> IO (Maybe ThunkInfo)
$cnoThunks :: forall c. Crypto c => Context -> GKeys c -> IO (Maybe ThunkInfo)
NoThunks, forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall c x. Rep (GKeys c) x -> GKeys c
forall c x. GKeys c -> Rep (GKeys c) x
$cto :: forall c x. Rep (GKeys c) x -> GKeys c
$cfrom :: forall c x. GKeys c -> Rep (GKeys c) x
Generic)
  deriving (Int -> GKeys c -> ShowS
[GKeys c] -> ShowS
GKeys c -> String
forall c. Crypto c => Int -> GKeys c -> ShowS
forall c. Crypto c => [GKeys c] -> ShowS
forall c. Crypto c => GKeys c -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [GKeys c] -> ShowS
$cshowList :: forall c. Crypto c => [GKeys c] -> ShowS
show :: GKeys c -> String
$cshow :: forall c. Crypto c => GKeys c -> String
showsPrec :: Int -> GKeys c -> ShowS
$cshowsPrec :: forall c. Crypto c => Int -> GKeys c -> ShowS
Show) via Quiet (GKeys 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)