{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE StandaloneDeriving #-}

module Cardano.CLI.Read.Committee.HotKey
  ( AnyCommitteeHotVerificationKey (..)

    -- * Read bech32 or hex encoded Committee Hot verification key
  , readCommitteeHotBech32VerificationKeyText
  , readCommitteeHotHexVerificationKeyText

    -- * Read TextEnvelope Committee Hot verification key file
  , readCommitteeHotVerificationKeyFile
  )
where

import Cardano.Api

import Cardano.CLI.Read
import Cardano.Prelude qualified as Text

import Prelude

import Data.Text (Text)
import Data.Validation

data AnyCommitteeHotVerificationKey where
  AnyCommitteeHotVerificationKey :: VerificationKey CommitteeHotKey -> AnyCommitteeHotVerificationKey
  AnyCommitteeHotExtendedVerificationKey
    :: VerificationKey CommitteeHotExtendedKey -> AnyCommitteeHotVerificationKey

deriving instance Show AnyCommitteeHotVerificationKey

readCommitteeHotBech32VerificationKeyText
  :: Text -> Validation [Bech32DecodeError] AnyCommitteeHotVerificationKey
readCommitteeHotBech32VerificationKeyText :: Text
-> Validation [Bech32DecodeError] AnyCommitteeHotVerificationKey
readCommitteeHotBech32VerificationKeyText Text
committeeHot =
  let vkey :: Validation [Bech32DecodeError] AnyCommitteeHotVerificationKey
vkey =
        (Bech32DecodeError -> [Bech32DecodeError])
-> Either Bech32DecodeError AnyCommitteeHotVerificationKey
-> Validation [Bech32DecodeError] AnyCommitteeHotVerificationKey
forall b e a. (b -> e) -> Either b a -> Validation e a
liftError Bech32DecodeError -> [Bech32DecodeError]
forall a. a -> [a]
forall (m :: * -> *) a. Monad m => a -> m a
return (Either Bech32DecodeError AnyCommitteeHotVerificationKey
 -> Validation [Bech32DecodeError] AnyCommitteeHotVerificationKey)
-> Either Bech32DecodeError AnyCommitteeHotVerificationKey
-> Validation [Bech32DecodeError] AnyCommitteeHotVerificationKey
forall a b. (a -> b) -> a -> b
$
          VerificationKey CommitteeHotKey -> AnyCommitteeHotVerificationKey
AnyCommitteeHotVerificationKey
            (VerificationKey CommitteeHotKey -> AnyCommitteeHotVerificationKey)
-> Either Bech32DecodeError (VerificationKey CommitteeHotKey)
-> Either Bech32DecodeError AnyCommitteeHotVerificationKey
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Either Bech32DecodeError (VerificationKey CommitteeHotKey)
forall a. SerialiseAsBech32 a => Text -> Either Bech32DecodeError a
deserialiseFromBech32 Text
committeeHot
      extendedVkey :: Validation [Bech32DecodeError] AnyCommitteeHotVerificationKey
extendedVkey =
        (Bech32DecodeError -> [Bech32DecodeError])
-> Either Bech32DecodeError AnyCommitteeHotVerificationKey
-> Validation [Bech32DecodeError] AnyCommitteeHotVerificationKey
forall b e a. (b -> e) -> Either b a -> Validation e a
liftError Bech32DecodeError -> [Bech32DecodeError]
forall a. a -> [a]
forall (m :: * -> *) a. Monad m => a -> m a
return (Either Bech32DecodeError AnyCommitteeHotVerificationKey
 -> Validation [Bech32DecodeError] AnyCommitteeHotVerificationKey)
-> Either Bech32DecodeError AnyCommitteeHotVerificationKey
-> Validation [Bech32DecodeError] AnyCommitteeHotVerificationKey
forall a b. (a -> b) -> a -> b
$
          VerificationKey CommitteeHotExtendedKey
-> AnyCommitteeHotVerificationKey
AnyCommitteeHotExtendedVerificationKey
            (VerificationKey CommitteeHotExtendedKey
 -> AnyCommitteeHotVerificationKey)
-> Either
     Bech32DecodeError (VerificationKey CommitteeHotExtendedKey)
-> Either Bech32DecodeError AnyCommitteeHotVerificationKey
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text
-> Either
     Bech32DecodeError (VerificationKey CommitteeHotExtendedKey)
forall a. SerialiseAsBech32 a => Text -> Either Bech32DecodeError a
deserialiseFromBech32 Text
committeeHot
   in Validation [Bech32DecodeError] AnyCommitteeHotVerificationKey
vkey Validation [Bech32DecodeError] AnyCommitteeHotVerificationKey
-> Validation [Bech32DecodeError] AnyCommitteeHotVerificationKey
-> Validation [Bech32DecodeError] AnyCommitteeHotVerificationKey
forall a. Semigroup a => a -> a -> a
<> Validation [Bech32DecodeError] AnyCommitteeHotVerificationKey
extendedVkey

readCommitteeHotHexVerificationKeyText
  :: Text -> Validation [RawBytesHexError] AnyCommitteeHotVerificationKey
readCommitteeHotHexVerificationKeyText :: Text
-> Validation [RawBytesHexError] AnyCommitteeHotVerificationKey
readCommitteeHotHexVerificationKeyText Text
committeeHotText =
  let committeeHotBs :: ByteString
committeeHotBs = Text -> ByteString
Text.encodeUtf8 Text
committeeHotText
      vkey :: Validation [RawBytesHexError] AnyCommitteeHotVerificationKey
vkey =
        (RawBytesHexError -> [RawBytesHexError])
-> Either RawBytesHexError AnyCommitteeHotVerificationKey
-> Validation [RawBytesHexError] AnyCommitteeHotVerificationKey
forall b e a. (b -> e) -> Either b a -> Validation e a
liftError RawBytesHexError -> [RawBytesHexError]
forall a. a -> [a]
forall (m :: * -> *) a. Monad m => a -> m a
return (Either RawBytesHexError AnyCommitteeHotVerificationKey
 -> Validation [RawBytesHexError] AnyCommitteeHotVerificationKey)
-> Either RawBytesHexError AnyCommitteeHotVerificationKey
-> Validation [RawBytesHexError] AnyCommitteeHotVerificationKey
forall a b. (a -> b) -> a -> b
$
          VerificationKey CommitteeHotKey -> AnyCommitteeHotVerificationKey
AnyCommitteeHotVerificationKey
            (VerificationKey CommitteeHotKey -> AnyCommitteeHotVerificationKey)
-> Either RawBytesHexError (VerificationKey CommitteeHotKey)
-> Either RawBytesHexError AnyCommitteeHotVerificationKey
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString
-> Either RawBytesHexError (VerificationKey CommitteeHotKey)
forall a.
SerialiseAsRawBytes a =>
ByteString -> Either RawBytesHexError a
deserialiseFromRawBytesHex ByteString
committeeHotBs
      extendedVkey :: Validation [RawBytesHexError] AnyCommitteeHotVerificationKey
extendedVkey =
        (RawBytesHexError -> [RawBytesHexError])
-> Either RawBytesHexError AnyCommitteeHotVerificationKey
-> Validation [RawBytesHexError] AnyCommitteeHotVerificationKey
forall b e a. (b -> e) -> Either b a -> Validation e a
liftError RawBytesHexError -> [RawBytesHexError]
forall a. a -> [a]
forall (m :: * -> *) a. Monad m => a -> m a
return (Either RawBytesHexError AnyCommitteeHotVerificationKey
 -> Validation [RawBytesHexError] AnyCommitteeHotVerificationKey)
-> Either RawBytesHexError AnyCommitteeHotVerificationKey
-> Validation [RawBytesHexError] AnyCommitteeHotVerificationKey
forall a b. (a -> b) -> a -> b
$
          VerificationKey CommitteeHotExtendedKey
-> AnyCommitteeHotVerificationKey
AnyCommitteeHotExtendedVerificationKey
            (VerificationKey CommitteeHotExtendedKey
 -> AnyCommitteeHotVerificationKey)
-> Either
     RawBytesHexError (VerificationKey CommitteeHotExtendedKey)
-> Either RawBytesHexError AnyCommitteeHotVerificationKey
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString
-> Either
     RawBytesHexError (VerificationKey CommitteeHotExtendedKey)
forall a.
SerialiseAsRawBytes a =>
ByteString -> Either RawBytesHexError a
deserialiseFromRawBytesHex ByteString
committeeHotBs
   in Validation [RawBytesHexError] AnyCommitteeHotVerificationKey
vkey Validation [RawBytesHexError] AnyCommitteeHotVerificationKey
-> Validation [RawBytesHexError] AnyCommitteeHotVerificationKey
-> Validation [RawBytesHexError] AnyCommitteeHotVerificationKey
forall a. Semigroup a => a -> a -> a
<> Validation [RawBytesHexError] AnyCommitteeHotVerificationKey
extendedVkey

readCommitteeHotVerificationKeyFile
  :: FileOrPipe -> IO (Either (FileError TextEnvelopeError) AnyCommitteeHotVerificationKey)
readCommitteeHotVerificationKeyFile :: FileOrPipe
-> IO
     (Either
        (FileError TextEnvelopeError) AnyCommitteeHotVerificationKey)
readCommitteeHotVerificationKeyFile = [FromSomeType HasTextEnvelope AnyCommitteeHotVerificationKey]
-> FileOrPipe
-> IO
     (Either
        (FileError TextEnvelopeError) AnyCommitteeHotVerificationKey)
forall b.
[FromSomeType HasTextEnvelope b]
-> FileOrPipe -> IO (Either (FileError TextEnvelopeError) b)
readFileOrPipeTextEnvelopeAnyOf [FromSomeType HasTextEnvelope AnyCommitteeHotVerificationKey]
types
 where
  types :: [FromSomeType HasTextEnvelope AnyCommitteeHotVerificationKey]
types =
    [ AsType (VerificationKey CommitteeHotKey)
-> (VerificationKey CommitteeHotKey
    -> AnyCommitteeHotVerificationKey)
-> FromSomeType HasTextEnvelope AnyCommitteeHotVerificationKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType CommitteeHotKey -> AsType (VerificationKey CommitteeHotKey)
forall a. AsType a -> AsType (VerificationKey a)
AsVerificationKey AsType CommitteeHotKey
AsCommitteeHotKey) VerificationKey CommitteeHotKey -> AnyCommitteeHotVerificationKey
AnyCommitteeHotVerificationKey
    , AsType (VerificationKey CommitteeHotExtendedKey)
-> (VerificationKey CommitteeHotExtendedKey
    -> AnyCommitteeHotVerificationKey)
-> FromSomeType HasTextEnvelope AnyCommitteeHotVerificationKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType CommitteeHotExtendedKey
-> AsType (VerificationKey CommitteeHotExtendedKey)
forall a. AsType a -> AsType (VerificationKey a)
AsVerificationKey AsType CommitteeHotExtendedKey
AsCommitteeHotExtendedKey) VerificationKey CommitteeHotExtendedKey
-> AnyCommitteeHotVerificationKey
AnyCommitteeHotExtendedVerificationKey
    ]