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

module Cardano.CLI.Read.Committee.ColdKey
  ( AnyCommitteeColdVerificationKey (..)

    -- * Read bech32 or hex encoded Committee Hot verification key
  , readCommitteeColdBech32VerificationKeyText
  , readCommitteeColdHexVerificationKeyText

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

import Cardano.Api

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

import Prelude

import Data.Text (Text)
import Data.Validation

data AnyCommitteeColdVerificationKey where
  AnyCommitteeColdVerificationKey
    :: VerificationKey CommitteeColdKey -> AnyCommitteeColdVerificationKey
  AnyCommitteeColdExtendedVerificationKey
    :: VerificationKey CommitteeColdExtendedKey -> AnyCommitteeColdVerificationKey

deriving instance Show AnyCommitteeColdVerificationKey

readCommitteeColdBech32VerificationKeyText
  :: Text -> Validation [Bech32DecodeError] AnyCommitteeColdVerificationKey
readCommitteeColdBech32VerificationKeyText :: Text
-> Validation [Bech32DecodeError] AnyCommitteeColdVerificationKey
readCommitteeColdBech32VerificationKeyText Text
committeeColdText =
  let vkey :: Validation [Bech32DecodeError] AnyCommitteeColdVerificationKey
vkey =
        (Bech32DecodeError -> [Bech32DecodeError])
-> Either Bech32DecodeError AnyCommitteeColdVerificationKey
-> Validation [Bech32DecodeError] AnyCommitteeColdVerificationKey
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 AnyCommitteeColdVerificationKey
 -> Validation [Bech32DecodeError] AnyCommitteeColdVerificationKey)
-> Either Bech32DecodeError AnyCommitteeColdVerificationKey
-> Validation [Bech32DecodeError] AnyCommitteeColdVerificationKey
forall a b. (a -> b) -> a -> b
$
          VerificationKey CommitteeColdKey -> AnyCommitteeColdVerificationKey
AnyCommitteeColdVerificationKey
            (VerificationKey CommitteeColdKey
 -> AnyCommitteeColdVerificationKey)
-> Either Bech32DecodeError (VerificationKey CommitteeColdKey)
-> Either Bech32DecodeError AnyCommitteeColdVerificationKey
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Either Bech32DecodeError (VerificationKey CommitteeColdKey)
forall a. SerialiseAsBech32 a => Text -> Either Bech32DecodeError a
deserialiseFromBech32 Text
committeeColdText
      extendedVkey :: Validation [Bech32DecodeError] AnyCommitteeColdVerificationKey
extendedVkey =
        (Bech32DecodeError -> [Bech32DecodeError])
-> Either Bech32DecodeError AnyCommitteeColdVerificationKey
-> Validation [Bech32DecodeError] AnyCommitteeColdVerificationKey
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 AnyCommitteeColdVerificationKey
 -> Validation [Bech32DecodeError] AnyCommitteeColdVerificationKey)
-> Either Bech32DecodeError AnyCommitteeColdVerificationKey
-> Validation [Bech32DecodeError] AnyCommitteeColdVerificationKey
forall a b. (a -> b) -> a -> b
$
          VerificationKey CommitteeColdExtendedKey
-> AnyCommitteeColdVerificationKey
AnyCommitteeColdExtendedVerificationKey
            (VerificationKey CommitteeColdExtendedKey
 -> AnyCommitteeColdVerificationKey)
-> Either
     Bech32DecodeError (VerificationKey CommitteeColdExtendedKey)
-> Either Bech32DecodeError AnyCommitteeColdVerificationKey
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text
-> Either
     Bech32DecodeError (VerificationKey CommitteeColdExtendedKey)
forall a. SerialiseAsBech32 a => Text -> Either Bech32DecodeError a
deserialiseFromBech32 Text
committeeColdText
   in Validation [Bech32DecodeError] AnyCommitteeColdVerificationKey
vkey Validation [Bech32DecodeError] AnyCommitteeColdVerificationKey
-> Validation [Bech32DecodeError] AnyCommitteeColdVerificationKey
-> Validation [Bech32DecodeError] AnyCommitteeColdVerificationKey
forall a. Semigroup a => a -> a -> a
<> Validation [Bech32DecodeError] AnyCommitteeColdVerificationKey
extendedVkey

readCommitteeColdHexVerificationKeyText
  :: Text -> Validation [RawBytesHexError] AnyCommitteeColdVerificationKey
readCommitteeColdHexVerificationKeyText :: Text
-> Validation [RawBytesHexError] AnyCommitteeColdVerificationKey
readCommitteeColdHexVerificationKeyText Text
committeeColdText =
  let committeeColdBs :: ByteString
committeeColdBs = Text -> ByteString
Text.encodeUtf8 Text
committeeColdText
      vkey :: Validation [RawBytesHexError] AnyCommitteeColdVerificationKey
vkey =
        (RawBytesHexError -> [RawBytesHexError])
-> Either RawBytesHexError AnyCommitteeColdVerificationKey
-> Validation [RawBytesHexError] AnyCommitteeColdVerificationKey
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 AnyCommitteeColdVerificationKey
 -> Validation [RawBytesHexError] AnyCommitteeColdVerificationKey)
-> Either RawBytesHexError AnyCommitteeColdVerificationKey
-> Validation [RawBytesHexError] AnyCommitteeColdVerificationKey
forall a b. (a -> b) -> a -> b
$
          VerificationKey CommitteeColdKey -> AnyCommitteeColdVerificationKey
AnyCommitteeColdVerificationKey
            (VerificationKey CommitteeColdKey
 -> AnyCommitteeColdVerificationKey)
-> Either RawBytesHexError (VerificationKey CommitteeColdKey)
-> Either RawBytesHexError AnyCommitteeColdVerificationKey
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString
-> Either RawBytesHexError (VerificationKey CommitteeColdKey)
forall a.
SerialiseAsRawBytes a =>
ByteString -> Either RawBytesHexError a
deserialiseFromRawBytesHex ByteString
committeeColdBs
      extendedVkey :: Validation [RawBytesHexError] AnyCommitteeColdVerificationKey
extendedVkey =
        (RawBytesHexError -> [RawBytesHexError])
-> Either RawBytesHexError AnyCommitteeColdVerificationKey
-> Validation [RawBytesHexError] AnyCommitteeColdVerificationKey
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 AnyCommitteeColdVerificationKey
 -> Validation [RawBytesHexError] AnyCommitteeColdVerificationKey)
-> Either RawBytesHexError AnyCommitteeColdVerificationKey
-> Validation [RawBytesHexError] AnyCommitteeColdVerificationKey
forall a b. (a -> b) -> a -> b
$
          VerificationKey CommitteeColdExtendedKey
-> AnyCommitteeColdVerificationKey
AnyCommitteeColdExtendedVerificationKey
            (VerificationKey CommitteeColdExtendedKey
 -> AnyCommitteeColdVerificationKey)
-> Either
     RawBytesHexError (VerificationKey CommitteeColdExtendedKey)
-> Either RawBytesHexError AnyCommitteeColdVerificationKey
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString
-> Either
     RawBytesHexError (VerificationKey CommitteeColdExtendedKey)
forall a.
SerialiseAsRawBytes a =>
ByteString -> Either RawBytesHexError a
deserialiseFromRawBytesHex ByteString
committeeColdBs
   in Validation [RawBytesHexError] AnyCommitteeColdVerificationKey
vkey Validation [RawBytesHexError] AnyCommitteeColdVerificationKey
-> Validation [RawBytesHexError] AnyCommitteeColdVerificationKey
-> Validation [RawBytesHexError] AnyCommitteeColdVerificationKey
forall a. Semigroup a => a -> a -> a
<> Validation [RawBytesHexError] AnyCommitteeColdVerificationKey
extendedVkey

readCommitteeColdVerificationKeyFile
  :: FileOrPipe -> IO (Either (FileError TextEnvelopeError) AnyCommitteeColdVerificationKey)
readCommitteeColdVerificationKeyFile :: FileOrPipe
-> IO
     (Either
        (FileError TextEnvelopeError) AnyCommitteeColdVerificationKey)
readCommitteeColdVerificationKeyFile = [FromSomeType HasTextEnvelope AnyCommitteeColdVerificationKey]
-> FileOrPipe
-> IO
     (Either
        (FileError TextEnvelopeError) AnyCommitteeColdVerificationKey)
forall b.
[FromSomeType HasTextEnvelope b]
-> FileOrPipe -> IO (Either (FileError TextEnvelopeError) b)
readFileOrPipeTextEnvelopeAnyOf [FromSomeType HasTextEnvelope AnyCommitteeColdVerificationKey]
types
 where
  types :: [FromSomeType HasTextEnvelope AnyCommitteeColdVerificationKey]
types =
    [ AsType (VerificationKey CommitteeColdKey)
-> (VerificationKey CommitteeColdKey
    -> AnyCommitteeColdVerificationKey)
-> FromSomeType HasTextEnvelope AnyCommitteeColdVerificationKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType CommitteeColdKey
-> AsType (VerificationKey CommitteeColdKey)
forall a. AsType a -> AsType (VerificationKey a)
AsVerificationKey AsType CommitteeColdKey
AsCommitteeColdKey) VerificationKey CommitteeColdKey -> AnyCommitteeColdVerificationKey
AnyCommitteeColdVerificationKey
    , AsType (VerificationKey CommitteeColdExtendedKey)
-> (VerificationKey CommitteeColdExtendedKey
    -> AnyCommitteeColdVerificationKey)
-> FromSomeType HasTextEnvelope AnyCommitteeColdVerificationKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType CommitteeColdExtendedKey
-> AsType (VerificationKey CommitteeColdExtendedKey)
forall a. AsType a -> AsType (VerificationKey a)
AsVerificationKey AsType CommitteeColdExtendedKey
AsCommitteeColdExtendedKey) VerificationKey CommitteeColdExtendedKey
-> AnyCommitteeColdVerificationKey
AnyCommitteeColdExtendedVerificationKey
    ]