{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE UndecidableInstances #-}

-- | Shelley CLI option data types and functions for cryptographic keys.
module Cardano.CLI.Types.Key
  ( VerificationKeyOrFile (..)
  , readVerificationKeyOrFile
  , readVerificationKeyOrTextEnvFile
  , VerificationKeyTextOrFile (..)
  , VerificationKeyTextOrFileError (..)
  , readVerificationKeyTextOrFileAnyOf
  , renderVerificationKeyTextOrFileError
  , VerificationKeyOrHashOrFile (..)
  , readVerificationKeyOrHashOrFile
  , readVerificationKeyOrHashOrTextEnvFile
  , VerificationKeyOrHashOrFileOrScript (..)
  , VerificationKeyOrHashOrFileOrScriptHash (..)
  , VerificationKeySource (..)
  , readVerificationKeyOrHashOrFileOrScriptHash
  , PaymentVerifier (..)
  , StakeIdentifier (..)
  , StakeVerifier (..)
  , generateKeyPair
  --- Legacy
  , StakePoolRegistrationParserRequirements (..)
  -- NewEraBased
  , AnyDelegationTarget (..)
  , StakeTarget (..)
  , ColdVerificationKeyOrFile (..)
  , DRepHashSource (..)
  , readDRepCredential
  , SPOHashSource (..)
  , readSPOCredential
  , SomeSigningKey (..)
  , withSomeSigningKey
  , readSigningKeyFile
  )
where

import           Cardano.Api
import qualified Cardano.Api.Ledger as L
import           Cardano.Api.Shelley

import           Cardano.CLI.Types.Common

import           Data.Bifunctor (Bifunctor (..))
import qualified Data.ByteString as BS
import           Data.Text (Text)
import qualified Data.Text.Encoding as Text
import           GHC.Exts (IsList (..))

------------------------------------------------------------------------------
-- Verification key deserialisation
------------------------------------------------------------------------------

-- | Either a verification key or path to a verification key file.
data VerificationKeyOrFile keyrole
  = -- | A verification key.
    VerificationKeyValue !(VerificationKey keyrole)
  | -- | A path to a verification key file.
    -- Note that this file hasn't been validated at all (whether it exists,
    -- contains a key of the correct type, etc.)
    VerificationKeyFilePath !(VerificationKeyFile In)

deriving instance
  Show (VerificationKey keyrole)
  => Show (VerificationKeyOrFile keyrole)

deriving instance
  Eq (VerificationKey keyrole)
  => Eq (VerificationKeyOrFile keyrole)

-- | Read a verification key or verification key file and return a
-- verification key.
--
-- If a filepath is provided, the file can either be formatted as Bech32, hex,
-- or text envelope.
readVerificationKeyOrFile
  :: MonadIOTransError (FileError InputDecodeError) t m
  => HasTextEnvelope (VerificationKey keyrole)
  => SerialiseAsBech32 (VerificationKey keyrole)
  => AsType keyrole
  -> VerificationKeyOrFile keyrole
  -> t m (VerificationKey keyrole)
readVerificationKeyOrFile :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) keyrole.
(MonadIOTransError (FileError InputDecodeError) t m,
 HasTextEnvelope (VerificationKey keyrole),
 SerialiseAsBech32 (VerificationKey keyrole)) =>
AsType keyrole
-> VerificationKeyOrFile keyrole -> t m (VerificationKey keyrole)
readVerificationKeyOrFile AsType keyrole
asType VerificationKeyOrFile keyrole
verKeyOrFile =
  case VerificationKeyOrFile keyrole
verKeyOrFile of
    VerificationKeyValue VerificationKey keyrole
vk -> VerificationKey keyrole -> t m (VerificationKey keyrole)
forall a. a -> t m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure VerificationKey keyrole
vk
    VerificationKeyFilePath (File String
fp) ->
      IO (Either (FileError InputDecodeError) (VerificationKey keyrole))
-> t m (VerificationKey keyrole)
forall e (t :: (* -> *) -> * -> *) (m :: * -> *) a.
MonadIOTransError e t m =>
IO (Either e a) -> t m a
hoistIOEither (IO (Either (FileError InputDecodeError) (VerificationKey keyrole))
 -> t m (VerificationKey keyrole))
-> IO
     (Either (FileError InputDecodeError) (VerificationKey keyrole))
-> t m (VerificationKey keyrole)
forall a b. (a -> b) -> a -> b
$
        AsType (VerificationKey keyrole)
-> NonEmpty (InputFormat (VerificationKey keyrole))
-> String
-> IO
     (Either (FileError InputDecodeError) (VerificationKey keyrole))
forall a.
AsType a
-> NonEmpty (InputFormat a)
-> String
-> IO (Either (FileError InputDecodeError) a)
readKeyFile
          (AsType keyrole -> AsType (VerificationKey keyrole)
forall a. AsType a -> AsType (VerificationKey a)
AsVerificationKey AsType keyrole
asType)
          ([Item (NonEmpty (InputFormat (VerificationKey keyrole)))]
-> NonEmpty (InputFormat (VerificationKey keyrole))
forall l. IsList l => [Item l] -> l
fromList [Item (NonEmpty (InputFormat (VerificationKey keyrole)))
InputFormat (VerificationKey keyrole)
forall a. SerialiseAsBech32 a => InputFormat a
InputFormatBech32, Item (NonEmpty (InputFormat (VerificationKey keyrole)))
InputFormat (VerificationKey keyrole)
forall a. SerialiseAsRawBytes a => InputFormat a
InputFormatHex, Item (NonEmpty (InputFormat (VerificationKey keyrole)))
InputFormat (VerificationKey keyrole)
forall a. HasTextEnvelope a => InputFormat a
InputFormatTextEnvelope])
          String
fp

-- | Read a verification key or verification key file and return a
-- verification key.
--
-- If a filepath is provided, it will be interpreted as a text envelope
-- formatted file.
readVerificationKeyOrTextEnvFile
  :: MonadIOTransError (FileError InputDecodeError) t m
  => HasTextEnvelope (VerificationKey keyrole)
  => AsType keyrole
  -> VerificationKeyOrFile keyrole
  -> t m (VerificationKey keyrole)
readVerificationKeyOrTextEnvFile :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) keyrole.
(MonadIOTransError (FileError InputDecodeError) t m,
 HasTextEnvelope (VerificationKey keyrole)) =>
AsType keyrole
-> VerificationKeyOrFile keyrole -> t m (VerificationKey keyrole)
readVerificationKeyOrTextEnvFile AsType keyrole
asType VerificationKeyOrFile keyrole
verKeyOrFile =
  case VerificationKeyOrFile keyrole
verKeyOrFile of
    VerificationKeyValue VerificationKey keyrole
vk -> VerificationKey keyrole -> t m (VerificationKey keyrole)
forall a. a -> t m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure VerificationKey keyrole
vk
    VerificationKeyFilePath VerificationKeyFile 'In
fp -> IO (Either (FileError InputDecodeError) (VerificationKey keyrole))
-> t m (VerificationKey keyrole)
forall e (t :: (* -> *) -> * -> *) (m :: * -> *) a.
MonadIOTransError e t m =>
IO (Either e a) -> t m a
hoistIOEither (IO (Either (FileError InputDecodeError) (VerificationKey keyrole))
 -> t m (VerificationKey keyrole))
-> IO
     (Either (FileError InputDecodeError) (VerificationKey keyrole))
-> t m (VerificationKey keyrole)
forall a b. (a -> b) -> a -> b
$ AsType (VerificationKey keyrole)
-> VerificationKeyFile 'In
-> IO
     (Either (FileError InputDecodeError) (VerificationKey keyrole))
forall a content.
HasTextEnvelope a =>
AsType a
-> File content 'In -> IO (Either (FileError InputDecodeError) a)
readKeyFileTextEnvelope (AsType keyrole -> AsType (VerificationKey keyrole)
forall a. AsType a -> AsType (VerificationKey a)
AsVerificationKey AsType keyrole
asType) VerificationKeyFile 'In
fp

data PaymentVerifier
  = PaymentVerifierKey VerificationKeyTextOrFile
  | PaymentVerifierScriptFile ScriptFile
  deriving (PaymentVerifier -> PaymentVerifier -> Bool
(PaymentVerifier -> PaymentVerifier -> Bool)
-> (PaymentVerifier -> PaymentVerifier -> Bool)
-> Eq PaymentVerifier
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PaymentVerifier -> PaymentVerifier -> Bool
== :: PaymentVerifier -> PaymentVerifier -> Bool
$c/= :: PaymentVerifier -> PaymentVerifier -> Bool
/= :: PaymentVerifier -> PaymentVerifier -> Bool
Eq, Int -> PaymentVerifier -> ShowS
[PaymentVerifier] -> ShowS
PaymentVerifier -> String
(Int -> PaymentVerifier -> ShowS)
-> (PaymentVerifier -> String)
-> ([PaymentVerifier] -> ShowS)
-> Show PaymentVerifier
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PaymentVerifier -> ShowS
showsPrec :: Int -> PaymentVerifier -> ShowS
$cshow :: PaymentVerifier -> String
show :: PaymentVerifier -> String
$cshowList :: [PaymentVerifier] -> ShowS
showList :: [PaymentVerifier] -> ShowS
Show)

data StakeVerifier
  = StakeVerifierKey (VerificationKeyOrHashOrFile StakeKey)
  | StakeVerifierScriptFile ScriptFile
  deriving (StakeVerifier -> StakeVerifier -> Bool
(StakeVerifier -> StakeVerifier -> Bool)
-> (StakeVerifier -> StakeVerifier -> Bool) -> Eq StakeVerifier
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: StakeVerifier -> StakeVerifier -> Bool
== :: StakeVerifier -> StakeVerifier -> Bool
$c/= :: StakeVerifier -> StakeVerifier -> Bool
/= :: StakeVerifier -> StakeVerifier -> Bool
Eq, Int -> StakeVerifier -> ShowS
[StakeVerifier] -> ShowS
StakeVerifier -> String
(Int -> StakeVerifier -> ShowS)
-> (StakeVerifier -> String)
-> ([StakeVerifier] -> ShowS)
-> Show StakeVerifier
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> StakeVerifier -> ShowS
showsPrec :: Int -> StakeVerifier -> ShowS
$cshow :: StakeVerifier -> String
show :: StakeVerifier -> String
$cshowList :: [StakeVerifier] -> ShowS
showList :: [StakeVerifier] -> ShowS
Show)

data StakeIdentifier
  = StakeIdentifierVerifier StakeVerifier
  | StakeIdentifierAddress StakeAddress
  deriving (StakeIdentifier -> StakeIdentifier -> Bool
(StakeIdentifier -> StakeIdentifier -> Bool)
-> (StakeIdentifier -> StakeIdentifier -> Bool)
-> Eq StakeIdentifier
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: StakeIdentifier -> StakeIdentifier -> Bool
== :: StakeIdentifier -> StakeIdentifier -> Bool
$c/= :: StakeIdentifier -> StakeIdentifier -> Bool
/= :: StakeIdentifier -> StakeIdentifier -> Bool
Eq, Int -> StakeIdentifier -> ShowS
[StakeIdentifier] -> ShowS
StakeIdentifier -> String
(Int -> StakeIdentifier -> ShowS)
-> (StakeIdentifier -> String)
-> ([StakeIdentifier] -> ShowS)
-> Show StakeIdentifier
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> StakeIdentifier -> ShowS
showsPrec :: Int -> StakeIdentifier -> ShowS
$cshow :: StakeIdentifier -> String
show :: StakeIdentifier -> String
$cshowList :: [StakeIdentifier] -> ShowS
showList :: [StakeIdentifier] -> ShowS
Show)

data StakePoolRegistrationParserRequirements
  = StakePoolRegistrationParserRequirements
  { StakePoolRegistrationParserRequirements
-> VerificationKeyOrFile StakePoolKey
sprStakePoolKey :: VerificationKeyOrFile StakePoolKey
  -- ^ Stake pool verification key.
  , StakePoolRegistrationParserRequirements
-> VerificationKeyOrFile VrfKey
sprVrfKey :: VerificationKeyOrFile VrfKey
  -- ^ VRF Verification key.
  , StakePoolRegistrationParserRequirements -> Lovelace
sprPoolPledge :: Lovelace
  -- ^ Pool pledge.
  , StakePoolRegistrationParserRequirements -> Lovelace
sprPoolCost :: Lovelace
  -- ^ Pool cost.
  , StakePoolRegistrationParserRequirements -> Rational
sprPoolMargin :: Rational
  -- ^ Pool margin.
  , StakePoolRegistrationParserRequirements
-> VerificationKeyOrFile StakeKey
sprRewardAccountKey :: VerificationKeyOrFile StakeKey
  -- ^ Reward account verification staking key.
  , StakePoolRegistrationParserRequirements
-> [VerificationKeyOrFile StakeKey]
spoPoolOwnerKeys :: [VerificationKeyOrFile StakeKey]
  -- ^ Pool owner verification staking key(s).
  , StakePoolRegistrationParserRequirements -> [StakePoolRelay]
sprRelays :: [StakePoolRelay]
  -- ^ Stake pool relays.
  , StakePoolRegistrationParserRequirements
-> Maybe
     (PotentiallyCheckedAnchor
        StakePoolMetadataReference StakePoolMetadataReference)
sprMetadata
      :: Maybe (PotentiallyCheckedAnchor StakePoolMetadataReference StakePoolMetadataReference)
  -- ^ Stake pool metadata.
  , StakePoolRegistrationParserRequirements -> NetworkId
sprNetworkId :: NetworkId
  }

-- | A resource that identifies the delegation target. We can delegate
-- our stake for two reasons:
-- 1. To gain rewards. This is limited to choosing a stake pool
-- 2. To delegate voting power. We can delegate this to a DRep, always
-- abstain our vote or vote no confidence
data AnyDelegationTarget where
  ShelleyToBabbageDelegTarget
    :: ShelleyToBabbageEra era
    -> VerificationKeyOrHashOrFile StakePoolKey
    -- ^ Stake pool target
    -> AnyDelegationTarget
  ConwayOnwardDelegTarget
    :: ConwayEraOnwards era
    -> StakeTarget era
    -> AnyDelegationTarget

deriving instance Show AnyDelegationTarget

data StakeTarget era where
  -- This delegates stake to earn rewards
  TargetStakePool
    :: ConwayEraOnwards era
    -> VerificationKeyOrHashOrFile StakePoolKey
    -> StakeTarget era
  -- This delegates stake for voting
  TargetVotingDrep
    :: ConwayEraOnwards era
    -> VerificationKeyOrHashOrFile DRepKey
    -> StakeTarget era
  -- This delegates stake for voting and rewards
  TargetVotingDrepAndStakePool
    :: ConwayEraOnwards era
    -> VerificationKeyOrHashOrFile DRepKey
    -> VerificationKeyOrHashOrFile StakePoolKey
    -> StakeTarget era
  TargetAlwaysAbstain
    :: ConwayEraOnwards era
    -> StakeTarget era
  TargetAlwaysNoConfidence
    :: ConwayEraOnwards era
    -> StakeTarget era
  TargetVotingDRepScriptHash
    :: ConwayEraOnwards era
    -> ScriptHash
    -> StakeTarget era

deriving instance Show (StakeTarget era)

-- | Either an unvalidated text representation of a verification key or a path
-- to a verification key file.
data VerificationKeyTextOrFile
  = VktofVerificationKeyText !Text
  | VktofVerificationKeyFile !(VerificationKeyFile In)
  deriving (VerificationKeyTextOrFile -> VerificationKeyTextOrFile -> Bool
(VerificationKeyTextOrFile -> VerificationKeyTextOrFile -> Bool)
-> (VerificationKeyTextOrFile -> VerificationKeyTextOrFile -> Bool)
-> Eq VerificationKeyTextOrFile
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: VerificationKeyTextOrFile -> VerificationKeyTextOrFile -> Bool
== :: VerificationKeyTextOrFile -> VerificationKeyTextOrFile -> Bool
$c/= :: VerificationKeyTextOrFile -> VerificationKeyTextOrFile -> Bool
/= :: VerificationKeyTextOrFile -> VerificationKeyTextOrFile -> Bool
Eq, Int -> VerificationKeyTextOrFile -> ShowS
[VerificationKeyTextOrFile] -> ShowS
VerificationKeyTextOrFile -> String
(Int -> VerificationKeyTextOrFile -> ShowS)
-> (VerificationKeyTextOrFile -> String)
-> ([VerificationKeyTextOrFile] -> ShowS)
-> Show VerificationKeyTextOrFile
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> VerificationKeyTextOrFile -> ShowS
showsPrec :: Int -> VerificationKeyTextOrFile -> ShowS
$cshow :: VerificationKeyTextOrFile -> String
show :: VerificationKeyTextOrFile -> String
$cshowList :: [VerificationKeyTextOrFile] -> ShowS
showList :: [VerificationKeyTextOrFile] -> ShowS
Show)

-- | An error in deserialising a 'VerificationKeyTextOrFile' to a
-- 'VerificationKey'.
data VerificationKeyTextOrFileError
  = VerificationKeyTextError !InputDecodeError
  | VerificationKeyFileError !(FileError InputDecodeError)
  deriving Int -> VerificationKeyTextOrFileError -> ShowS
[VerificationKeyTextOrFileError] -> ShowS
VerificationKeyTextOrFileError -> String
(Int -> VerificationKeyTextOrFileError -> ShowS)
-> (VerificationKeyTextOrFileError -> String)
-> ([VerificationKeyTextOrFileError] -> ShowS)
-> Show VerificationKeyTextOrFileError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> VerificationKeyTextOrFileError -> ShowS
showsPrec :: Int -> VerificationKeyTextOrFileError -> ShowS
$cshow :: VerificationKeyTextOrFileError -> String
show :: VerificationKeyTextOrFileError -> String
$cshowList :: [VerificationKeyTextOrFileError] -> ShowS
showList :: [VerificationKeyTextOrFileError] -> ShowS
Show

-- | Render an error message for a 'VerificationKeyTextOrFileError'.
renderVerificationKeyTextOrFileError :: VerificationKeyTextOrFileError -> Doc ann
renderVerificationKeyTextOrFileError :: forall ann. VerificationKeyTextOrFileError -> Doc ann
renderVerificationKeyTextOrFileError VerificationKeyTextOrFileError
vkTextOrFileErr =
  case VerificationKeyTextOrFileError
vkTextOrFileErr of
    VerificationKeyTextError InputDecodeError
err -> InputDecodeError -> Doc ann
forall ann. InputDecodeError -> Doc ann
renderInputDecodeError InputDecodeError
err
    VerificationKeyFileError FileError InputDecodeError
err -> FileError InputDecodeError -> Doc ann
forall e ann. Error e => e -> Doc ann
forall ann. FileError InputDecodeError -> Doc ann
prettyError FileError InputDecodeError
err

-- | Deserialise a verification key from text or a verification key file.
-- If a filepath is provided, the file can either be formatted as Bech32, hex,
-- or text envelope.
readVerificationKeyTextOrFileAnyOf
  :: VerificationKeyTextOrFile
  -> IO (Either VerificationKeyTextOrFileError SomeAddressVerificationKey)
readVerificationKeyTextOrFileAnyOf :: VerificationKeyTextOrFile
-> IO
     (Either VerificationKeyTextOrFileError SomeAddressVerificationKey)
readVerificationKeyTextOrFileAnyOf VerificationKeyTextOrFile
verKeyTextOrFile =
  case VerificationKeyTextOrFile
verKeyTextOrFile of
    VktofVerificationKeyText Text
vkText ->
      Either VerificationKeyTextOrFileError SomeAddressVerificationKey
-> IO
     (Either VerificationKeyTextOrFileError SomeAddressVerificationKey)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either VerificationKeyTextOrFileError SomeAddressVerificationKey
 -> IO
      (Either VerificationKeyTextOrFileError SomeAddressVerificationKey))
-> Either VerificationKeyTextOrFileError SomeAddressVerificationKey
-> IO
     (Either VerificationKeyTextOrFileError SomeAddressVerificationKey)
forall a b. (a -> b) -> a -> b
$
        (InputDecodeError -> VerificationKeyTextOrFileError)
-> Either InputDecodeError SomeAddressVerificationKey
-> Either VerificationKeyTextOrFileError SomeAddressVerificationKey
forall a b c. (a -> b) -> Either a c -> Either b c
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first InputDecodeError -> VerificationKeyTextOrFileError
VerificationKeyTextError (Either InputDecodeError SomeAddressVerificationKey
 -> Either
      VerificationKeyTextOrFileError SomeAddressVerificationKey)
-> Either InputDecodeError SomeAddressVerificationKey
-> Either VerificationKeyTextOrFileError SomeAddressVerificationKey
forall a b. (a -> b) -> a -> b
$
          ByteString -> Either InputDecodeError SomeAddressVerificationKey
deserialiseAnyVerificationKey (Text -> ByteString
Text.encodeUtf8 Text
vkText)
    VktofVerificationKeyFile (File String
fp) -> do
      ByteString
vkBs <- IO ByteString -> IO ByteString
forall a. IO a -> IO a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO ByteString -> IO ByteString) -> IO ByteString -> IO ByteString
forall a b. (a -> b) -> a -> b
$ String -> IO ByteString
BS.readFile String
fp
      Either VerificationKeyTextOrFileError SomeAddressVerificationKey
-> IO
     (Either VerificationKeyTextOrFileError SomeAddressVerificationKey)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either VerificationKeyTextOrFileError SomeAddressVerificationKey
 -> IO
      (Either VerificationKeyTextOrFileError SomeAddressVerificationKey))
-> Either VerificationKeyTextOrFileError SomeAddressVerificationKey
-> IO
     (Either VerificationKeyTextOrFileError SomeAddressVerificationKey)
forall a b. (a -> b) -> a -> b
$
        (InputDecodeError -> VerificationKeyTextOrFileError)
-> Either InputDecodeError SomeAddressVerificationKey
-> Either VerificationKeyTextOrFileError SomeAddressVerificationKey
forall a b c. (a -> b) -> Either a c -> Either b c
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first InputDecodeError -> VerificationKeyTextOrFileError
VerificationKeyTextError (Either InputDecodeError SomeAddressVerificationKey
 -> Either
      VerificationKeyTextOrFileError SomeAddressVerificationKey)
-> Either InputDecodeError SomeAddressVerificationKey
-> Either VerificationKeyTextOrFileError SomeAddressVerificationKey
forall a b. (a -> b) -> a -> b
$
          ByteString -> Either InputDecodeError SomeAddressVerificationKey
deserialiseAnyVerificationKey ByteString
vkBs

-- | Verification key, verification key hash, or path to a verification key
-- file.
data VerificationKeyOrHashOrFile keyrole
  = -- | Either a verification key or path to a verification key file.
    VerificationKeyOrFile !(VerificationKeyOrFile keyrole)
  | -- | A verification key hash.
    VerificationKeyHash !(Hash keyrole)

deriving instance
  (Show (VerificationKeyOrFile keyrole), Show (Hash keyrole))
  => Show (VerificationKeyOrHashOrFile keyrole)

deriving instance
  (Eq (VerificationKeyOrFile keyrole), Eq (Hash keyrole))
  => Eq (VerificationKeyOrHashOrFile keyrole)

-- | Read a verification key or verification key hash or verification key file
-- and return a verification key hash.
--
-- If a filepath is provided, the file can either be formatted as Bech32, hex,
-- or text envelope.
readVerificationKeyOrHashOrFile
  :: MonadIOTransError (FileError InputDecodeError) t m
  => Key keyrole
  => SerialiseAsBech32 (VerificationKey keyrole)
  => AsType keyrole
  -> VerificationKeyOrHashOrFile keyrole
  -> t m (Hash keyrole)
readVerificationKeyOrHashOrFile :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) keyrole.
(MonadIOTransError (FileError InputDecodeError) t m, Key keyrole,
 SerialiseAsBech32 (VerificationKey keyrole)) =>
AsType keyrole
-> VerificationKeyOrHashOrFile keyrole -> t m (Hash keyrole)
readVerificationKeyOrHashOrFile AsType keyrole
asType =
  \case
    VerificationKeyOrFile VerificationKeyOrFile keyrole
vkOrFile ->
      VerificationKey keyrole -> Hash keyrole
forall keyrole.
Key keyrole =>
VerificationKey keyrole -> Hash keyrole
verificationKeyHash (VerificationKey keyrole -> Hash keyrole)
-> t m (VerificationKey keyrole) -> t m (Hash keyrole)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> AsType keyrole
-> VerificationKeyOrFile keyrole -> t m (VerificationKey keyrole)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) keyrole.
(MonadIOTransError (FileError InputDecodeError) t m,
 HasTextEnvelope (VerificationKey keyrole),
 SerialiseAsBech32 (VerificationKey keyrole)) =>
AsType keyrole
-> VerificationKeyOrFile keyrole -> t m (VerificationKey keyrole)
readVerificationKeyOrFile AsType keyrole
asType VerificationKeyOrFile keyrole
vkOrFile
    VerificationKeyHash Hash keyrole
vkHash -> Hash keyrole -> t m (Hash keyrole)
forall a. a -> t m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Hash keyrole
vkHash

-- | Read a verification key or verification key hash or verification key file
-- and return a verification key hash.
--
-- If a filepath is provided, it will be interpreted as a text envelope
-- formatted file.
readVerificationKeyOrHashOrTextEnvFile
  :: MonadIOTransError (FileError InputDecodeError) t m
  => Key keyrole
  => AsType keyrole
  -> VerificationKeyOrHashOrFile keyrole
  -> t m (Hash keyrole)
readVerificationKeyOrHashOrTextEnvFile :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) keyrole.
(MonadIOTransError (FileError InputDecodeError) t m,
 Key keyrole) =>
AsType keyrole
-> VerificationKeyOrHashOrFile keyrole -> t m (Hash keyrole)
readVerificationKeyOrHashOrTextEnvFile AsType keyrole
asType =
  \case
    VerificationKeyOrFile VerificationKeyOrFile keyrole
vkOrFile ->
      VerificationKey keyrole -> Hash keyrole
forall keyrole.
Key keyrole =>
VerificationKey keyrole -> Hash keyrole
verificationKeyHash (VerificationKey keyrole -> Hash keyrole)
-> t m (VerificationKey keyrole) -> t m (Hash keyrole)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> AsType keyrole
-> VerificationKeyOrFile keyrole -> t m (VerificationKey keyrole)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) keyrole.
(MonadIOTransError (FileError InputDecodeError) t m,
 HasTextEnvelope (VerificationKey keyrole)) =>
AsType keyrole
-> VerificationKeyOrFile keyrole -> t m (VerificationKey keyrole)
readVerificationKeyOrTextEnvFile AsType keyrole
asType VerificationKeyOrFile keyrole
vkOrFile
    VerificationKeyHash Hash keyrole
vkHash -> Hash keyrole -> t m (Hash keyrole)
forall a. a -> t m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Hash keyrole
vkHash

generateKeyPair
  :: MonadIO m
  => Key keyrole
  => HasTypeProxy keyrole
  => AsType keyrole
  -> m (VerificationKey keyrole, SigningKey keyrole)
generateKeyPair :: forall (m :: * -> *) keyrole.
(MonadIO m, Key keyrole, HasTypeProxy keyrole) =>
AsType keyrole -> m (VerificationKey keyrole, SigningKey keyrole)
generateKeyPair AsType keyrole
asType = do
  SigningKey keyrole
skey <- AsType keyrole -> m (SigningKey keyrole)
forall (m :: * -> *) keyrole.
(MonadIO m, Key keyrole) =>
AsType keyrole -> m (SigningKey keyrole)
generateSigningKey AsType keyrole
asType
  (VerificationKey keyrole, SigningKey keyrole)
-> m (VerificationKey keyrole, SigningKey keyrole)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (SigningKey keyrole -> VerificationKey keyrole
forall keyrole.
(Key keyrole, HasTypeProxy keyrole) =>
SigningKey keyrole -> VerificationKey keyrole
getVerificationKey SigningKey keyrole
skey, SigningKey keyrole
skey)

-- | Either a stake pool verification key, genesis delegate verification key,
-- or a path to a cold verification key file.
--
-- Note that a "cold verification key" refers to either a stake pool or
-- genesis delegate verification key.
--
-- TODO: A genesis delegate extended key should also be valid here.
data ColdVerificationKeyOrFile
  = ColdStakePoolVerificationKey !(VerificationKey StakePoolKey)
  | ColdGenesisDelegateVerificationKey !(VerificationKey GenesisDelegateKey)
  | ColdVerificationKeyFile !(VerificationKeyFile In)
  deriving Int -> ColdVerificationKeyOrFile -> ShowS
[ColdVerificationKeyOrFile] -> ShowS
ColdVerificationKeyOrFile -> String
(Int -> ColdVerificationKeyOrFile -> ShowS)
-> (ColdVerificationKeyOrFile -> String)
-> ([ColdVerificationKeyOrFile] -> ShowS)
-> Show ColdVerificationKeyOrFile
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ColdVerificationKeyOrFile -> ShowS
showsPrec :: Int -> ColdVerificationKeyOrFile -> ShowS
$cshow :: ColdVerificationKeyOrFile -> String
show :: ColdVerificationKeyOrFile -> String
$cshowList :: [ColdVerificationKeyOrFile] -> ShowS
showList :: [ColdVerificationKeyOrFile] -> ShowS
Show

data DRepHashSource
  = DRepHashSourceScript
      ScriptHash
  | DRepHashSourceVerificationKey
      (VerificationKeyOrHashOrFile DRepKey)
  deriving (DRepHashSource -> DRepHashSource -> Bool
(DRepHashSource -> DRepHashSource -> Bool)
-> (DRepHashSource -> DRepHashSource -> Bool) -> Eq DRepHashSource
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: DRepHashSource -> DRepHashSource -> Bool
== :: DRepHashSource -> DRepHashSource -> Bool
$c/= :: DRepHashSource -> DRepHashSource -> Bool
/= :: DRepHashSource -> DRepHashSource -> Bool
Eq, Int -> DRepHashSource -> ShowS
[DRepHashSource] -> ShowS
DRepHashSource -> String
(Int -> DRepHashSource -> ShowS)
-> (DRepHashSource -> String)
-> ([DRepHashSource] -> ShowS)
-> Show DRepHashSource
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DRepHashSource -> ShowS
showsPrec :: Int -> DRepHashSource -> ShowS
$cshow :: DRepHashSource -> String
show :: DRepHashSource -> String
$cshowList :: [DRepHashSource] -> ShowS
showList :: [DRepHashSource] -> ShowS
Show)

readDRepCredential
  :: MonadIOTransError (FileError InputDecodeError) t m
  => DRepHashSource
  -> t m (L.Credential L.DRepRole L.StandardCrypto)
readDRepCredential :: forall (t :: (* -> *) -> * -> *) (m :: * -> *).
MonadIOTransError (FileError InputDecodeError) t m =>
DRepHashSource -> t m (Credential 'DRepRole StandardCrypto)
readDRepCredential = \case
  DRepHashSourceScript (ScriptHash ScriptHash StandardCrypto
scriptHash) ->
    Credential 'DRepRole StandardCrypto
-> t m (Credential 'DRepRole StandardCrypto)
forall a. a -> t m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ScriptHash StandardCrypto -> Credential 'DRepRole StandardCrypto
forall (kr :: KeyRole) c. ScriptHash c -> Credential kr c
L.ScriptHashObj ScriptHash StandardCrypto
scriptHash)
  DRepHashSourceVerificationKey VerificationKeyOrHashOrFile DRepKey
drepVKeyOrHashOrFile ->
    KeyHash 'DRepRole StandardCrypto
-> Credential 'DRepRole StandardCrypto
forall (kr :: KeyRole) c. KeyHash kr c -> Credential kr c
L.KeyHashObj (KeyHash 'DRepRole StandardCrypto
 -> Credential 'DRepRole StandardCrypto)
-> (Hash DRepKey -> KeyHash 'DRepRole StandardCrypto)
-> Hash DRepKey
-> Credential 'DRepRole StandardCrypto
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Hash DRepKey -> KeyHash 'DRepRole StandardCrypto
unDRepKeyHash
      (Hash DRepKey -> Credential 'DRepRole StandardCrypto)
-> t m (Hash DRepKey) -> t m (Credential 'DRepRole StandardCrypto)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> AsType DRepKey
-> VerificationKeyOrHashOrFile DRepKey -> t m (Hash DRepKey)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) keyrole.
(MonadIOTransError (FileError InputDecodeError) t m,
 Key keyrole) =>
AsType keyrole
-> VerificationKeyOrHashOrFile keyrole -> t m (Hash keyrole)
readVerificationKeyOrHashOrTextEnvFile AsType DRepKey
AsDRepKey VerificationKeyOrHashOrFile DRepKey
drepVKeyOrHashOrFile

newtype SPOHashSource
  = SPOHashSourceVerificationKey
      (VerificationKeyOrHashOrFile StakePoolKey)
  deriving (SPOHashSource -> SPOHashSource -> Bool
(SPOHashSource -> SPOHashSource -> Bool)
-> (SPOHashSource -> SPOHashSource -> Bool) -> Eq SPOHashSource
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: SPOHashSource -> SPOHashSource -> Bool
== :: SPOHashSource -> SPOHashSource -> Bool
$c/= :: SPOHashSource -> SPOHashSource -> Bool
/= :: SPOHashSource -> SPOHashSource -> Bool
Eq, Int -> SPOHashSource -> ShowS
[SPOHashSource] -> ShowS
SPOHashSource -> String
(Int -> SPOHashSource -> ShowS)
-> (SPOHashSource -> String)
-> ([SPOHashSource] -> ShowS)
-> Show SPOHashSource
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SPOHashSource -> ShowS
showsPrec :: Int -> SPOHashSource -> ShowS
$cshow :: SPOHashSource -> String
show :: SPOHashSource -> String
$cshowList :: [SPOHashSource] -> ShowS
showList :: [SPOHashSource] -> ShowS
Show)

readSPOCredential
  :: MonadIOTransError (FileError InputDecodeError) t m
  => SPOHashSource
  -> t m (L.KeyHash L.StakePool L.StandardCrypto)
readSPOCredential :: forall (t :: (* -> *) -> * -> *) (m :: * -> *).
MonadIOTransError (FileError InputDecodeError) t m =>
SPOHashSource -> t m (KeyHash 'StakePool StandardCrypto)
readSPOCredential = \case
  SPOHashSourceVerificationKey VerificationKeyOrHashOrFile StakePoolKey
spoVKeyOrHashOrFile ->
    Hash StakePoolKey -> KeyHash 'StakePool StandardCrypto
unStakePoolKeyHash (Hash StakePoolKey -> KeyHash 'StakePool StandardCrypto)
-> t m (Hash StakePoolKey)
-> t m (KeyHash 'StakePool StandardCrypto)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> AsType StakePoolKey
-> VerificationKeyOrHashOrFile StakePoolKey
-> t m (Hash StakePoolKey)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) keyrole.
(MonadIOTransError (FileError InputDecodeError) t m,
 Key keyrole) =>
AsType keyrole
-> VerificationKeyOrHashOrFile keyrole -> t m (Hash keyrole)
readVerificationKeyOrHashOrTextEnvFile AsType StakePoolKey
AsStakePoolKey VerificationKeyOrHashOrFile StakePoolKey
spoVKeyOrHashOrFile

data VerificationKeyOrHashOrFileOrScript keyrole
  = VkhfsKeyHashFile !(VerificationKeyOrHashOrFile keyrole)
  | VkhfsScript !(File ScriptInAnyLang In)

deriving instance
  Eq (VerificationKeyOrHashOrFile keyrole)
  => Eq (VerificationKeyOrHashOrFileOrScript keyrole)

deriving instance
  Show (VerificationKeyOrHashOrFile keyrole)
  => Show (VerificationKeyOrHashOrFileOrScript keyrole)

data VerificationKeyOrHashOrFileOrScriptHash keyrole
  = VkhfshKeyHashFile !(VerificationKeyOrHashOrFile keyrole)
  | VkhfshScriptHash !ScriptHash

deriving instance
  Eq (VerificationKeyOrHashOrFile keyrole)
  => Eq (VerificationKeyOrHashOrFileOrScriptHash keyrole)

deriving instance
  Show (VerificationKeyOrHashOrFile keyrole)
  => Show (VerificationKeyOrHashOrFileOrScriptHash keyrole)

data VerificationKeySource keyrole
  = VksKeyHashFile !(VerificationKeyOrHashOrFile keyrole)
  | VksScript !(File ScriptInAnyLang In)
  | VksScriptHash !ScriptHash

deriving instance
  Eq (VerificationKeyOrHashOrFile keyrole)
  => Eq (VerificationKeySource keyrole)

deriving instance
  Show (VerificationKeyOrHashOrFile keyrole)
  => Show (VerificationKeySource keyrole)

readVerificationKeyOrHashOrFileOrScriptHash
  :: MonadIOTransError (FileError InputDecodeError) t m
  => Key keyrole
  => AsType keyrole
  -> (Hash keyrole -> L.KeyHash kr L.StandardCrypto)
  -> VerificationKeyOrHashOrFileOrScriptHash keyrole
  -> t m (L.Credential kr L.StandardCrypto)
readVerificationKeyOrHashOrFileOrScriptHash :: forall (t :: (* -> *) -> * -> *) (m :: * -> *) keyrole
       (kr :: KeyRole).
(MonadIOTransError (FileError InputDecodeError) t m,
 Key keyrole) =>
AsType keyrole
-> (Hash keyrole -> KeyHash kr StandardCrypto)
-> VerificationKeyOrHashOrFileOrScriptHash keyrole
-> t m (Credential kr StandardCrypto)
readVerificationKeyOrHashOrFileOrScriptHash AsType keyrole
asType Hash keyrole -> KeyHash kr StandardCrypto
extractHash = \case
  VkhfshScriptHash (ScriptHash ScriptHash StandardCrypto
scriptHash) ->
    Credential kr StandardCrypto -> t m (Credential kr StandardCrypto)
forall a. a -> t m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ScriptHash StandardCrypto -> Credential kr StandardCrypto
forall (kr :: KeyRole) c. ScriptHash c -> Credential kr c
L.ScriptHashObj ScriptHash StandardCrypto
scriptHash)
  VkhfshKeyHashFile VerificationKeyOrHashOrFile keyrole
vKeyOrHashOrFile ->
    KeyHash kr StandardCrypto -> Credential kr StandardCrypto
forall (kr :: KeyRole) c. KeyHash kr c -> Credential kr c
L.KeyHashObj (KeyHash kr StandardCrypto -> Credential kr StandardCrypto)
-> (Hash keyrole -> KeyHash kr StandardCrypto)
-> Hash keyrole
-> Credential kr StandardCrypto
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Hash keyrole -> KeyHash kr StandardCrypto
extractHash
      (Hash keyrole -> Credential kr StandardCrypto)
-> t m (Hash keyrole) -> t m (Credential kr StandardCrypto)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> AsType keyrole
-> VerificationKeyOrHashOrFile keyrole -> t m (Hash keyrole)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) keyrole.
(MonadIOTransError (FileError InputDecodeError) t m,
 Key keyrole) =>
AsType keyrole
-> VerificationKeyOrHashOrFile keyrole -> t m (Hash keyrole)
readVerificationKeyOrHashOrTextEnvFile AsType keyrole
asType VerificationKeyOrHashOrFile keyrole
vKeyOrHashOrFile

data SomeSigningKey
  = AByronSigningKey (SigningKey ByronKey)
  | APaymentSigningKey (SigningKey PaymentKey)
  | APaymentExtendedSigningKey (SigningKey PaymentExtendedKey)
  | AStakeSigningKey (SigningKey StakeKey)
  | AStakeExtendedSigningKey (SigningKey StakeExtendedKey)
  | AStakePoolSigningKey (SigningKey StakePoolKey)
  | AGenesisSigningKey (SigningKey GenesisKey)
  | AGenesisExtendedSigningKey (SigningKey GenesisExtendedKey)
  | AGenesisDelegateSigningKey (SigningKey GenesisDelegateKey)
  | AGenesisDelegateExtendedSigningKey (SigningKey GenesisDelegateExtendedKey)
  | AGenesisUTxOSigningKey (SigningKey GenesisUTxOKey)
  | ADRepSigningKey (SigningKey DRepKey)
  | ADRepExtendedSigningKey (SigningKey DRepExtendedKey)
  | ACommitteeColdSigningKey (SigningKey CommitteeColdKey)
  | ACommitteeColdExtendedSigningKey (SigningKey CommitteeColdExtendedKey)
  | ACommitteeHotSigningKey (SigningKey CommitteeHotKey)
  | ACommitteeHotExtendedSigningKey (SigningKey CommitteeHotExtendedKey)
  | AVrfSigningKey (SigningKey VrfKey)
  | AKesSigningKey (SigningKey KesKey)

withSomeSigningKey
  :: ()
  => SomeSigningKey
  -> (forall keyrole. (Key keyrole, HasTypeProxy keyrole) => SigningKey keyrole -> a)
  -> a
withSomeSigningKey :: forall a.
SomeSigningKey
-> (forall keyrole.
    (Key keyrole, HasTypeProxy keyrole) =>
    SigningKey keyrole -> a)
-> a
withSomeSigningKey SomeSigningKey
ssk forall keyrole.
(Key keyrole, HasTypeProxy keyrole) =>
SigningKey keyrole -> a
f =
  case SomeSigningKey
ssk of
    AByronSigningKey SigningKey ByronKey
sk -> SigningKey ByronKey -> a
forall keyrole.
(Key keyrole, HasTypeProxy keyrole) =>
SigningKey keyrole -> a
f SigningKey ByronKey
sk
    APaymentSigningKey SigningKey PaymentKey
sk -> SigningKey PaymentKey -> a
forall keyrole.
(Key keyrole, HasTypeProxy keyrole) =>
SigningKey keyrole -> a
f SigningKey PaymentKey
sk
    APaymentExtendedSigningKey SigningKey PaymentExtendedKey
sk -> SigningKey PaymentExtendedKey -> a
forall keyrole.
(Key keyrole, HasTypeProxy keyrole) =>
SigningKey keyrole -> a
f SigningKey PaymentExtendedKey
sk
    AStakeSigningKey SigningKey StakeKey
sk -> SigningKey StakeKey -> a
forall keyrole.
(Key keyrole, HasTypeProxy keyrole) =>
SigningKey keyrole -> a
f SigningKey StakeKey
sk
    AStakeExtendedSigningKey SigningKey StakeExtendedKey
sk -> SigningKey StakeExtendedKey -> a
forall keyrole.
(Key keyrole, HasTypeProxy keyrole) =>
SigningKey keyrole -> a
f SigningKey StakeExtendedKey
sk
    AStakePoolSigningKey SigningKey StakePoolKey
sk -> SigningKey StakePoolKey -> a
forall keyrole.
(Key keyrole, HasTypeProxy keyrole) =>
SigningKey keyrole -> a
f SigningKey StakePoolKey
sk
    AGenesisSigningKey SigningKey GenesisKey
sk -> SigningKey GenesisKey -> a
forall keyrole.
(Key keyrole, HasTypeProxy keyrole) =>
SigningKey keyrole -> a
f SigningKey GenesisKey
sk
    AGenesisExtendedSigningKey SigningKey GenesisExtendedKey
sk -> SigningKey GenesisExtendedKey -> a
forall keyrole.
(Key keyrole, HasTypeProxy keyrole) =>
SigningKey keyrole -> a
f SigningKey GenesisExtendedKey
sk
    AGenesisDelegateSigningKey SigningKey GenesisDelegateKey
sk -> SigningKey GenesisDelegateKey -> a
forall keyrole.
(Key keyrole, HasTypeProxy keyrole) =>
SigningKey keyrole -> a
f SigningKey GenesisDelegateKey
sk
    AGenesisDelegateExtendedSigningKey SigningKey GenesisDelegateExtendedKey
sk -> SigningKey GenesisDelegateExtendedKey -> a
forall keyrole.
(Key keyrole, HasTypeProxy keyrole) =>
SigningKey keyrole -> a
f SigningKey GenesisDelegateExtendedKey
sk
    AGenesisUTxOSigningKey SigningKey GenesisUTxOKey
sk -> SigningKey GenesisUTxOKey -> a
forall keyrole.
(Key keyrole, HasTypeProxy keyrole) =>
SigningKey keyrole -> a
f SigningKey GenesisUTxOKey
sk
    ADRepSigningKey SigningKey DRepKey
sk -> SigningKey DRepKey -> a
forall keyrole.
(Key keyrole, HasTypeProxy keyrole) =>
SigningKey keyrole -> a
f SigningKey DRepKey
sk
    ADRepExtendedSigningKey SigningKey DRepExtendedKey
sk -> SigningKey DRepExtendedKey -> a
forall keyrole.
(Key keyrole, HasTypeProxy keyrole) =>
SigningKey keyrole -> a
f SigningKey DRepExtendedKey
sk
    ACommitteeColdSigningKey SigningKey CommitteeColdKey
sk -> SigningKey CommitteeColdKey -> a
forall keyrole.
(Key keyrole, HasTypeProxy keyrole) =>
SigningKey keyrole -> a
f SigningKey CommitteeColdKey
sk
    ACommitteeColdExtendedSigningKey SigningKey CommitteeColdExtendedKey
sk -> SigningKey CommitteeColdExtendedKey -> a
forall keyrole.
(Key keyrole, HasTypeProxy keyrole) =>
SigningKey keyrole -> a
f SigningKey CommitteeColdExtendedKey
sk
    ACommitteeHotSigningKey SigningKey CommitteeHotKey
sk -> SigningKey CommitteeHotKey -> a
forall keyrole.
(Key keyrole, HasTypeProxy keyrole) =>
SigningKey keyrole -> a
f SigningKey CommitteeHotKey
sk
    ACommitteeHotExtendedSigningKey SigningKey CommitteeHotExtendedKey
sk -> SigningKey CommitteeHotExtendedKey -> a
forall keyrole.
(Key keyrole, HasTypeProxy keyrole) =>
SigningKey keyrole -> a
f SigningKey CommitteeHotExtendedKey
sk
    AVrfSigningKey SigningKey VrfKey
sk -> SigningKey VrfKey -> a
forall keyrole.
(Key keyrole, HasTypeProxy keyrole) =>
SigningKey keyrole -> a
f SigningKey VrfKey
sk
    AKesSigningKey SigningKey KesKey
sk -> SigningKey KesKey -> a
forall keyrole.
(Key keyrole, HasTypeProxy keyrole) =>
SigningKey keyrole -> a
f SigningKey KesKey
sk

readSigningKeyFile
  :: ()
  => SigningKeyFile In
  -> ExceptT (FileError InputDecodeError) IO SomeSigningKey
readSigningKeyFile :: SigningKeyFile 'In
-> ExceptT (FileError InputDecodeError) IO SomeSigningKey
readSigningKeyFile SigningKeyFile 'In
skFile =
  IO (Either (FileError InputDecodeError) SomeSigningKey)
-> ExceptT (FileError InputDecodeError) IO SomeSigningKey
forall (m :: * -> *) x a. m (Either x a) -> ExceptT x m a
newExceptT (IO (Either (FileError InputDecodeError) SomeSigningKey)
 -> ExceptT (FileError InputDecodeError) IO SomeSigningKey)
-> IO (Either (FileError InputDecodeError) SomeSigningKey)
-> ExceptT (FileError InputDecodeError) IO SomeSigningKey
forall a b. (a -> b) -> a -> b
$
    [FromSomeType SerialiseAsBech32 SomeSigningKey]
-> [FromSomeType HasTextEnvelope SomeSigningKey]
-> SigningKeyFile 'In
-> IO (Either (FileError InputDecodeError) SomeSigningKey)
forall content b.
[FromSomeType SerialiseAsBech32 b]
-> [FromSomeType HasTextEnvelope b]
-> File content 'In
-> IO (Either (FileError InputDecodeError) b)
readKeyFileAnyOf [FromSomeType SerialiseAsBech32 SomeSigningKey]
bech32FileTypes [FromSomeType HasTextEnvelope SomeSigningKey]
textEnvFileTypes SigningKeyFile 'In
skFile
 where
  -- If you update these variables, consider updating the ones with the same
  -- names in Cardano.CLI.Read
  textEnvFileTypes :: [FromSomeType HasTextEnvelope SomeSigningKey]
textEnvFileTypes =
    [ AsType (SigningKey ByronKey)
-> (SigningKey ByronKey -> SomeSigningKey)
-> FromSomeType HasTextEnvelope SomeSigningKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType ByronKey -> AsType (SigningKey ByronKey)
forall a. AsType a -> AsType (SigningKey a)
AsSigningKey AsType ByronKey
AsByronKey) SigningKey ByronKey -> SomeSigningKey
AByronSigningKey
    , AsType (SigningKey PaymentKey)
-> (SigningKey PaymentKey -> SomeSigningKey)
-> FromSomeType HasTextEnvelope SomeSigningKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType PaymentKey -> AsType (SigningKey PaymentKey)
forall a. AsType a -> AsType (SigningKey a)
AsSigningKey AsType PaymentKey
AsPaymentKey) SigningKey PaymentKey -> SomeSigningKey
APaymentSigningKey
    , AsType (SigningKey PaymentExtendedKey)
-> (SigningKey PaymentExtendedKey -> SomeSigningKey)
-> FromSomeType HasTextEnvelope SomeSigningKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType PaymentExtendedKey -> AsType (SigningKey PaymentExtendedKey)
forall a. AsType a -> AsType (SigningKey a)
AsSigningKey AsType PaymentExtendedKey
AsPaymentExtendedKey) SigningKey PaymentExtendedKey -> SomeSigningKey
APaymentExtendedSigningKey
    , AsType (SigningKey StakeKey)
-> (SigningKey StakeKey -> SomeSigningKey)
-> FromSomeType HasTextEnvelope SomeSigningKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType StakeKey -> AsType (SigningKey StakeKey)
forall a. AsType a -> AsType (SigningKey a)
AsSigningKey AsType StakeKey
AsStakeKey) SigningKey StakeKey -> SomeSigningKey
AStakeSigningKey
    , AsType (SigningKey StakeExtendedKey)
-> (SigningKey StakeExtendedKey -> SomeSigningKey)
-> FromSomeType HasTextEnvelope SomeSigningKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType StakeExtendedKey -> AsType (SigningKey StakeExtendedKey)
forall a. AsType a -> AsType (SigningKey a)
AsSigningKey AsType StakeExtendedKey
AsStakeExtendedKey) SigningKey StakeExtendedKey -> SomeSigningKey
AStakeExtendedSigningKey
    , AsType (SigningKey StakePoolKey)
-> (SigningKey StakePoolKey -> SomeSigningKey)
-> FromSomeType HasTextEnvelope SomeSigningKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType StakePoolKey -> AsType (SigningKey StakePoolKey)
forall a. AsType a -> AsType (SigningKey a)
AsSigningKey AsType StakePoolKey
AsStakePoolKey) SigningKey StakePoolKey -> SomeSigningKey
AStakePoolSigningKey
    , AsType (SigningKey GenesisKey)
-> (SigningKey GenesisKey -> SomeSigningKey)
-> FromSomeType HasTextEnvelope SomeSigningKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType GenesisKey -> AsType (SigningKey GenesisKey)
forall a. AsType a -> AsType (SigningKey a)
AsSigningKey AsType GenesisKey
AsGenesisKey) SigningKey GenesisKey -> SomeSigningKey
AGenesisSigningKey
    , AsType (SigningKey GenesisExtendedKey)
-> (SigningKey GenesisExtendedKey -> SomeSigningKey)
-> FromSomeType HasTextEnvelope SomeSigningKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType GenesisExtendedKey -> AsType (SigningKey GenesisExtendedKey)
forall a. AsType a -> AsType (SigningKey a)
AsSigningKey AsType GenesisExtendedKey
AsGenesisExtendedKey) SigningKey GenesisExtendedKey -> SomeSigningKey
AGenesisExtendedSigningKey
    , AsType (SigningKey GenesisDelegateKey)
-> (SigningKey GenesisDelegateKey -> SomeSigningKey)
-> FromSomeType HasTextEnvelope SomeSigningKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType GenesisDelegateKey -> AsType (SigningKey GenesisDelegateKey)
forall a. AsType a -> AsType (SigningKey a)
AsSigningKey AsType GenesisDelegateKey
AsGenesisDelegateKey) SigningKey GenesisDelegateKey -> SomeSigningKey
AGenesisDelegateSigningKey
    , AsType (SigningKey GenesisDelegateExtendedKey)
-> (SigningKey GenesisDelegateExtendedKey -> SomeSigningKey)
-> FromSomeType HasTextEnvelope SomeSigningKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType GenesisDelegateExtendedKey
-> AsType (SigningKey GenesisDelegateExtendedKey)
forall a. AsType a -> AsType (SigningKey a)
AsSigningKey AsType GenesisDelegateExtendedKey
AsGenesisDelegateExtendedKey) SigningKey GenesisDelegateExtendedKey -> SomeSigningKey
AGenesisDelegateExtendedSigningKey
    , AsType (SigningKey GenesisUTxOKey)
-> (SigningKey GenesisUTxOKey -> SomeSigningKey)
-> FromSomeType HasTextEnvelope SomeSigningKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType GenesisUTxOKey -> AsType (SigningKey GenesisUTxOKey)
forall a. AsType a -> AsType (SigningKey a)
AsSigningKey AsType GenesisUTxOKey
AsGenesisUTxOKey) SigningKey GenesisUTxOKey -> SomeSigningKey
AGenesisUTxOSigningKey
    , AsType (SigningKey DRepKey)
-> (SigningKey DRepKey -> SomeSigningKey)
-> FromSomeType HasTextEnvelope SomeSigningKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType DRepKey -> AsType (SigningKey DRepKey)
forall a. AsType a -> AsType (SigningKey a)
AsSigningKey AsType DRepKey
AsDRepKey) SigningKey DRepKey -> SomeSigningKey
ADRepSigningKey
    , AsType (SigningKey DRepExtendedKey)
-> (SigningKey DRepExtendedKey -> SomeSigningKey)
-> FromSomeType HasTextEnvelope SomeSigningKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType DRepExtendedKey -> AsType (SigningKey DRepExtendedKey)
forall a. AsType a -> AsType (SigningKey a)
AsSigningKey AsType DRepExtendedKey
AsDRepExtendedKey) SigningKey DRepExtendedKey -> SomeSigningKey
ADRepExtendedSigningKey
    , AsType (SigningKey CommitteeColdKey)
-> (SigningKey CommitteeColdKey -> SomeSigningKey)
-> FromSomeType HasTextEnvelope SomeSigningKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType CommitteeColdKey -> AsType (SigningKey CommitteeColdKey)
forall a. AsType a -> AsType (SigningKey a)
AsSigningKey AsType CommitteeColdKey
AsCommitteeColdKey) SigningKey CommitteeColdKey -> SomeSigningKey
ACommitteeColdSigningKey
    , AsType (SigningKey CommitteeColdExtendedKey)
-> (SigningKey CommitteeColdExtendedKey -> SomeSigningKey)
-> FromSomeType HasTextEnvelope SomeSigningKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType CommitteeColdExtendedKey
-> AsType (SigningKey CommitteeColdExtendedKey)
forall a. AsType a -> AsType (SigningKey a)
AsSigningKey AsType CommitteeColdExtendedKey
AsCommitteeColdExtendedKey) SigningKey CommitteeColdExtendedKey -> SomeSigningKey
ACommitteeColdExtendedSigningKey
    , AsType (SigningKey CommitteeHotKey)
-> (SigningKey CommitteeHotKey -> SomeSigningKey)
-> FromSomeType HasTextEnvelope SomeSigningKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType CommitteeHotKey -> AsType (SigningKey CommitteeHotKey)
forall a. AsType a -> AsType (SigningKey a)
AsSigningKey AsType CommitteeHotKey
AsCommitteeHotKey) SigningKey CommitteeHotKey -> SomeSigningKey
ACommitteeHotSigningKey
    , AsType (SigningKey CommitteeHotExtendedKey)
-> (SigningKey CommitteeHotExtendedKey -> SomeSigningKey)
-> FromSomeType HasTextEnvelope SomeSigningKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType CommitteeHotExtendedKey
-> AsType (SigningKey CommitteeHotExtendedKey)
forall a. AsType a -> AsType (SigningKey a)
AsSigningKey AsType CommitteeHotExtendedKey
AsCommitteeHotExtendedKey) SigningKey CommitteeHotExtendedKey -> SomeSigningKey
ACommitteeHotExtendedSigningKey
    , AsType (SigningKey VrfKey)
-> (SigningKey VrfKey -> SomeSigningKey)
-> FromSomeType HasTextEnvelope SomeSigningKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType VrfKey -> AsType (SigningKey VrfKey)
forall a. AsType a -> AsType (SigningKey a)
AsSigningKey AsType VrfKey
AsVrfKey) SigningKey VrfKey -> SomeSigningKey
AVrfSigningKey
    , AsType (SigningKey KesKey)
-> (SigningKey KesKey -> SomeSigningKey)
-> FromSomeType HasTextEnvelope SomeSigningKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType KesKey -> AsType (SigningKey KesKey)
forall a. AsType a -> AsType (SigningKey a)
AsSigningKey AsType KesKey
AsKesKey) SigningKey KesKey -> SomeSigningKey
AKesSigningKey
    ]

  bech32FileTypes :: [FromSomeType SerialiseAsBech32 SomeSigningKey]
bech32FileTypes =
    [ AsType (SigningKey PaymentKey)
-> (SigningKey PaymentKey -> SomeSigningKey)
-> FromSomeType SerialiseAsBech32 SomeSigningKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType PaymentKey -> AsType (SigningKey PaymentKey)
forall a. AsType a -> AsType (SigningKey a)
AsSigningKey AsType PaymentKey
AsPaymentKey) SigningKey PaymentKey -> SomeSigningKey
APaymentSigningKey
    , AsType (SigningKey PaymentExtendedKey)
-> (SigningKey PaymentExtendedKey -> SomeSigningKey)
-> FromSomeType SerialiseAsBech32 SomeSigningKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType PaymentExtendedKey -> AsType (SigningKey PaymentExtendedKey)
forall a. AsType a -> AsType (SigningKey a)
AsSigningKey AsType PaymentExtendedKey
AsPaymentExtendedKey) SigningKey PaymentExtendedKey -> SomeSigningKey
APaymentExtendedSigningKey
    , AsType (SigningKey StakeKey)
-> (SigningKey StakeKey -> SomeSigningKey)
-> FromSomeType SerialiseAsBech32 SomeSigningKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType StakeKey -> AsType (SigningKey StakeKey)
forall a. AsType a -> AsType (SigningKey a)
AsSigningKey AsType StakeKey
AsStakeKey) SigningKey StakeKey -> SomeSigningKey
AStakeSigningKey
    , AsType (SigningKey StakeExtendedKey)
-> (SigningKey StakeExtendedKey -> SomeSigningKey)
-> FromSomeType SerialiseAsBech32 SomeSigningKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType StakeExtendedKey -> AsType (SigningKey StakeExtendedKey)
forall a. AsType a -> AsType (SigningKey a)
AsSigningKey AsType StakeExtendedKey
AsStakeExtendedKey) SigningKey StakeExtendedKey -> SomeSigningKey
AStakeExtendedSigningKey
    , AsType (SigningKey StakePoolKey)
-> (SigningKey StakePoolKey -> SomeSigningKey)
-> FromSomeType SerialiseAsBech32 SomeSigningKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType StakePoolKey -> AsType (SigningKey StakePoolKey)
forall a. AsType a -> AsType (SigningKey a)
AsSigningKey AsType StakePoolKey
AsStakePoolKey) SigningKey StakePoolKey -> SomeSigningKey
AStakePoolSigningKey
    , AsType (SigningKey VrfKey)
-> (SigningKey VrfKey -> SomeSigningKey)
-> FromSomeType SerialiseAsBech32 SomeSigningKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType VrfKey -> AsType (SigningKey VrfKey)
forall a. AsType a -> AsType (SigningKey a)
AsSigningKey AsType VrfKey
AsVrfKey) SigningKey VrfKey -> SomeSigningKey
AVrfSigningKey
    , AsType (SigningKey KesKey)
-> (SigningKey KesKey -> SomeSigningKey)
-> FromSomeType SerialiseAsBech32 SomeSigningKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType KesKey -> AsType (SigningKey KesKey)
forall a. AsType a -> AsType (SigningKey a)
AsSigningKey AsType KesKey
AsKesKey) SigningKey KesKey -> SomeSigningKey
AKesSigningKey
    ]