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

module Cardano.CLI.Read.DRep
  ( AnyDrepVerificationKey (..)

    -- * Read bech32 or hex encoded DRep verification key
  , readDRepBech32VerificationKeyText
  , readDRepHexVerificationKeyText

    -- * Read TextEnvelope DRep verification key file
  , readDrepVerificationKeyFile
  )
where

import Cardano.Api

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

import Prelude

import Data.Text (Text)
import Data.Validation

data AnyDrepVerificationKey where
  AnyDrepVerificationKey :: VerificationKey DRepKey -> AnyDrepVerificationKey
  AnyDrepExtendedVerificationKey :: VerificationKey DRepExtendedKey -> AnyDrepVerificationKey

deriving instance Show AnyDrepVerificationKey

readDRepBech32VerificationKeyText :: Text -> Validation [Bech32DecodeError] AnyDrepVerificationKey
readDRepBech32VerificationKeyText :: Text -> Validation [Bech32DecodeError] AnyDrepVerificationKey
readDRepBech32VerificationKeyText Text
drep =
  let vkey :: Validation [Bech32DecodeError] AnyDrepVerificationKey
vkey =
        (Bech32DecodeError -> [Bech32DecodeError])
-> Either Bech32DecodeError AnyDrepVerificationKey
-> Validation [Bech32DecodeError] AnyDrepVerificationKey
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 AnyDrepVerificationKey
 -> Validation [Bech32DecodeError] AnyDrepVerificationKey)
-> Either Bech32DecodeError AnyDrepVerificationKey
-> Validation [Bech32DecodeError] AnyDrepVerificationKey
forall a b. (a -> b) -> a -> b
$
          VerificationKey DRepKey -> AnyDrepVerificationKey
AnyDrepVerificationKey (VerificationKey DRepKey -> AnyDrepVerificationKey)
-> Either Bech32DecodeError (VerificationKey DRepKey)
-> Either Bech32DecodeError AnyDrepVerificationKey
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Either Bech32DecodeError (VerificationKey DRepKey)
forall a. SerialiseAsBech32 a => Text -> Either Bech32DecodeError a
deserialiseFromBech32 Text
drep
      extendedVkey :: Validation [Bech32DecodeError] AnyDrepVerificationKey
extendedVkey =
        (Bech32DecodeError -> [Bech32DecodeError])
-> Either Bech32DecodeError AnyDrepVerificationKey
-> Validation [Bech32DecodeError] AnyDrepVerificationKey
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 AnyDrepVerificationKey
 -> Validation [Bech32DecodeError] AnyDrepVerificationKey)
-> Either Bech32DecodeError AnyDrepVerificationKey
-> Validation [Bech32DecodeError] AnyDrepVerificationKey
forall a b. (a -> b) -> a -> b
$
          VerificationKey DRepExtendedKey -> AnyDrepVerificationKey
AnyDrepExtendedVerificationKey (VerificationKey DRepExtendedKey -> AnyDrepVerificationKey)
-> Either Bech32DecodeError (VerificationKey DRepExtendedKey)
-> Either Bech32DecodeError AnyDrepVerificationKey
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Either Bech32DecodeError (VerificationKey DRepExtendedKey)
forall a. SerialiseAsBech32 a => Text -> Either Bech32DecodeError a
deserialiseFromBech32 Text
drep
   in Validation [Bech32DecodeError] AnyDrepVerificationKey
vkey Validation [Bech32DecodeError] AnyDrepVerificationKey
-> Validation [Bech32DecodeError] AnyDrepVerificationKey
-> Validation [Bech32DecodeError] AnyDrepVerificationKey
forall a. Semigroup a => a -> a -> a
<> Validation [Bech32DecodeError] AnyDrepVerificationKey
extendedVkey

readDRepHexVerificationKeyText :: Text -> Validation [RawBytesHexError] AnyDrepVerificationKey
readDRepHexVerificationKeyText :: Text -> Validation [RawBytesHexError] AnyDrepVerificationKey
readDRepHexVerificationKeyText Text
drepText =
  let drepBs :: ByteString
drepBs = Text -> ByteString
Text.encodeUtf8 Text
drepText
      vkey :: Validation [RawBytesHexError] AnyDrepVerificationKey
vkey =
        (RawBytesHexError -> [RawBytesHexError])
-> Either RawBytesHexError AnyDrepVerificationKey
-> Validation [RawBytesHexError] AnyDrepVerificationKey
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 AnyDrepVerificationKey
 -> Validation [RawBytesHexError] AnyDrepVerificationKey)
-> Either RawBytesHexError AnyDrepVerificationKey
-> Validation [RawBytesHexError] AnyDrepVerificationKey
forall a b. (a -> b) -> a -> b
$
          VerificationKey DRepKey -> AnyDrepVerificationKey
AnyDrepVerificationKey (VerificationKey DRepKey -> AnyDrepVerificationKey)
-> Either RawBytesHexError (VerificationKey DRepKey)
-> Either RawBytesHexError AnyDrepVerificationKey
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Either RawBytesHexError (VerificationKey DRepKey)
forall a.
SerialiseAsRawBytes a =>
ByteString -> Either RawBytesHexError a
deserialiseFromRawBytesHex ByteString
drepBs
      extendedVkey :: Validation [RawBytesHexError] AnyDrepVerificationKey
extendedVkey =
        (RawBytesHexError -> [RawBytesHexError])
-> Either RawBytesHexError AnyDrepVerificationKey
-> Validation [RawBytesHexError] AnyDrepVerificationKey
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 AnyDrepVerificationKey
 -> Validation [RawBytesHexError] AnyDrepVerificationKey)
-> Either RawBytesHexError AnyDrepVerificationKey
-> Validation [RawBytesHexError] AnyDrepVerificationKey
forall a b. (a -> b) -> a -> b
$
          VerificationKey DRepExtendedKey -> AnyDrepVerificationKey
AnyDrepExtendedVerificationKey
            (VerificationKey DRepExtendedKey -> AnyDrepVerificationKey)
-> Either RawBytesHexError (VerificationKey DRepExtendedKey)
-> Either RawBytesHexError AnyDrepVerificationKey
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString
-> Either RawBytesHexError (VerificationKey DRepExtendedKey)
forall a.
SerialiseAsRawBytes a =>
ByteString -> Either RawBytesHexError a
deserialiseFromRawBytesHex ByteString
drepBs
   in Validation [RawBytesHexError] AnyDrepVerificationKey
vkey Validation [RawBytesHexError] AnyDrepVerificationKey
-> Validation [RawBytesHexError] AnyDrepVerificationKey
-> Validation [RawBytesHexError] AnyDrepVerificationKey
forall a. Semigroup a => a -> a -> a
<> Validation [RawBytesHexError] AnyDrepVerificationKey
extendedVkey

readDrepVerificationKeyFile
  :: FileOrPipe -> IO (Either (FileError TextEnvelopeError) AnyDrepVerificationKey)
readDrepVerificationKeyFile :: FileOrPipe
-> IO (Either (FileError TextEnvelopeError) AnyDrepVerificationKey)
readDrepVerificationKeyFile = [FromSomeType HasTextEnvelope AnyDrepVerificationKey]
-> FileOrPipe
-> IO (Either (FileError TextEnvelopeError) AnyDrepVerificationKey)
forall b.
[FromSomeType HasTextEnvelope b]
-> FileOrPipe -> IO (Either (FileError TextEnvelopeError) b)
readFileOrPipeTextEnvelopeAnyOf [FromSomeType HasTextEnvelope AnyDrepVerificationKey]
types
 where
  types :: [FromSomeType HasTextEnvelope AnyDrepVerificationKey]
types =
    [ AsType (VerificationKey DRepKey)
-> (VerificationKey DRepKey -> AnyDrepVerificationKey)
-> FromSomeType HasTextEnvelope AnyDrepVerificationKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType DRepKey -> AsType (VerificationKey DRepKey)
forall a. AsType a -> AsType (VerificationKey a)
AsVerificationKey AsType DRepKey
AsDRepKey) VerificationKey DRepKey -> AnyDrepVerificationKey
AnyDrepVerificationKey
    , AsType (VerificationKey DRepExtendedKey)
-> (VerificationKey DRepExtendedKey -> AnyDrepVerificationKey)
-> FromSomeType HasTextEnvelope AnyDrepVerificationKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType DRepExtendedKey -> AsType (VerificationKey DRepExtendedKey)
forall a. AsType a -> AsType (VerificationKey a)
AsVerificationKey AsType DRepExtendedKey
AsDRepExtendedKey) VerificationKey DRepExtendedKey -> AnyDrepVerificationKey
AnyDrepExtendedVerificationKey
    ]