module Cardano.CLI.Shelley.Run.StakeAddress
  ( ShelleyStakeAddressCmdError(ShelleyStakeAddressCmdReadKeyFileError)
  , renderShelleyStakeAddressCmdError
  , runStakeAddressCmd
  , runStakeAddressKeyGen
  ) where

import           Cardano.Prelude

import qualified Data.ByteString.Char8 as BS
import qualified Data.Text as Text
import qualified Data.Text.IO as Text

import           Control.Monad.Trans.Except.Extra (firstExceptT, newExceptT)

import           Cardano.Api
import           Cardano.Api.Shelley

import           Cardano.CLI.Shelley.Key (InputDecodeError, StakeVerifier (..),
                   VerificationKeyOrFile, VerificationKeyOrHashOrFile, readVerificationKeyOrFile,
                   readVerificationKeyOrHashOrFile)
import           Cardano.CLI.Shelley.Parsers
import           Cardano.CLI.Shelley.Script (ScriptDecodeError, readFileScriptInAnyLang)
import           Cardano.CLI.Types

data ShelleyStakeAddressCmdError
  = ShelleyStakeAddressCmdReadKeyFileError !(FileError InputDecodeError)
  | ShelleyStakeAddressCmdReadScriptFileError !(FileError ScriptDecodeError)
  | ShelleyStakeAddressCmdWriteFileError !(FileError ())
  deriving Int -> ShelleyStakeAddressCmdError -> ShowS
[ShelleyStakeAddressCmdError] -> ShowS
ShelleyStakeAddressCmdError -> String
(Int -> ShelleyStakeAddressCmdError -> ShowS)
-> (ShelleyStakeAddressCmdError -> String)
-> ([ShelleyStakeAddressCmdError] -> ShowS)
-> Show ShelleyStakeAddressCmdError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ShelleyStakeAddressCmdError] -> ShowS
$cshowList :: [ShelleyStakeAddressCmdError] -> ShowS
show :: ShelleyStakeAddressCmdError -> String
$cshow :: ShelleyStakeAddressCmdError -> String
showsPrec :: Int -> ShelleyStakeAddressCmdError -> ShowS
$cshowsPrec :: Int -> ShelleyStakeAddressCmdError -> ShowS
Show

renderShelleyStakeAddressCmdError :: ShelleyStakeAddressCmdError -> Text
renderShelleyStakeAddressCmdError :: ShelleyStakeAddressCmdError -> Text
renderShelleyStakeAddressCmdError ShelleyStakeAddressCmdError
err =
  case ShelleyStakeAddressCmdError
err of
    ShelleyStakeAddressCmdReadKeyFileError FileError InputDecodeError
fileErr -> String -> Text
Text.pack (FileError InputDecodeError -> String
forall e. Error e => e -> String
displayError FileError InputDecodeError
fileErr)
    ShelleyStakeAddressCmdWriteFileError FileError ()
fileErr -> String -> Text
Text.pack (FileError () -> String
forall e. Error e => e -> String
displayError FileError ()
fileErr)
    ShelleyStakeAddressCmdReadScriptFileError FileError ScriptDecodeError
fileErr -> String -> Text
Text.pack (FileError ScriptDecodeError -> String
forall e. Error e => e -> String
displayError FileError ScriptDecodeError
fileErr)

runStakeAddressCmd :: StakeAddressCmd -> ExceptT ShelleyStakeAddressCmdError IO ()
runStakeAddressCmd :: StakeAddressCmd -> ExceptT ShelleyStakeAddressCmdError IO ()
runStakeAddressCmd (StakeAddressKeyGen VerificationKeyFile
vk SigningKeyFile
sk) = VerificationKeyFile
-> SigningKeyFile -> ExceptT ShelleyStakeAddressCmdError IO ()
runStakeAddressKeyGen VerificationKeyFile
vk SigningKeyFile
sk
runStakeAddressCmd (StakeAddressKeyHash VerificationKeyOrFile StakeKey
vk Maybe OutputFile
mOutputFp) = VerificationKeyOrFile StakeKey
-> Maybe OutputFile -> ExceptT ShelleyStakeAddressCmdError IO ()
runStakeAddressKeyHash VerificationKeyOrFile StakeKey
vk Maybe OutputFile
mOutputFp
runStakeAddressCmd (StakeAddressBuild VerificationKeyOrFile StakeKey
vk NetworkId
nw Maybe OutputFile
mOutputFp) = VerificationKeyOrFile StakeKey
-> NetworkId
-> Maybe OutputFile
-> ExceptT ShelleyStakeAddressCmdError IO ()
runStakeAddressBuild VerificationKeyOrFile StakeKey
vk NetworkId
nw Maybe OutputFile
mOutputFp
runStakeAddressCmd (StakeRegistrationCert StakeVerifier
stakeVerifier OutputFile
outputFp) =
  StakeVerifier
-> OutputFile -> ExceptT ShelleyStakeAddressCmdError IO ()
runStakeCredentialRegistrationCert StakeVerifier
stakeVerifier OutputFile
outputFp
runStakeAddressCmd (StakeCredentialDelegationCert StakeVerifier
stakeVerifier VerificationKeyOrHashOrFile StakePoolKey
stkPoolVerKeyHashOrFp OutputFile
outputFp) =
  StakeVerifier
-> VerificationKeyOrHashOrFile StakePoolKey
-> OutputFile
-> ExceptT ShelleyStakeAddressCmdError IO ()
runStakeCredentialDelegationCert StakeVerifier
stakeVerifier VerificationKeyOrHashOrFile StakePoolKey
stkPoolVerKeyHashOrFp OutputFile
outputFp
runStakeAddressCmd (StakeCredentialDeRegistrationCert StakeVerifier
stakeVerifier OutputFile
outputFp) =
  StakeVerifier
-> OutputFile -> ExceptT ShelleyStakeAddressCmdError IO ()
runStakeCredentialDeRegistrationCert StakeVerifier
stakeVerifier OutputFile
outputFp


--
-- Stake address command implementations
--

runStakeAddressKeyGen :: VerificationKeyFile -> SigningKeyFile -> ExceptT ShelleyStakeAddressCmdError IO ()
runStakeAddressKeyGen :: VerificationKeyFile
-> SigningKeyFile -> ExceptT ShelleyStakeAddressCmdError IO ()
runStakeAddressKeyGen (VerificationKeyFile String
vkFp) (SigningKeyFile String
skFp) = do
    SigningKey StakeKey
skey <- IO (SigningKey StakeKey)
-> ExceptT ShelleyStakeAddressCmdError IO (SigningKey StakeKey)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (SigningKey StakeKey)
 -> ExceptT ShelleyStakeAddressCmdError IO (SigningKey StakeKey))
-> IO (SigningKey StakeKey)
-> ExceptT ShelleyStakeAddressCmdError IO (SigningKey StakeKey)
forall a b. (a -> b) -> a -> b
$ AsType StakeKey -> IO (SigningKey StakeKey)
forall keyrole.
Key keyrole =>
AsType keyrole -> IO (SigningKey keyrole)
generateSigningKey AsType StakeKey
AsStakeKey
    let vkey :: VerificationKey StakeKey
vkey = SigningKey StakeKey -> VerificationKey StakeKey
forall keyrole.
Key keyrole =>
SigningKey keyrole -> VerificationKey keyrole
getVerificationKey SigningKey StakeKey
skey
    (FileError () -> ShelleyStakeAddressCmdError)
-> ExceptT (FileError ()) IO ()
-> ExceptT ShelleyStakeAddressCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError () -> ShelleyStakeAddressCmdError
ShelleyStakeAddressCmdWriteFileError
      (ExceptT (FileError ()) IO ()
 -> ExceptT ShelleyStakeAddressCmdError IO ())
-> (IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ())
-> IO (Either (FileError ()) ())
-> ExceptT ShelleyStakeAddressCmdError IO ()
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ()
forall (m :: * -> *) x a. m (Either x a) -> ExceptT x m a
newExceptT
      (IO (Either (FileError ()) ())
 -> ExceptT ShelleyStakeAddressCmdError IO ())
-> IO (Either (FileError ()) ())
-> ExceptT ShelleyStakeAddressCmdError IO ()
forall a b. (a -> b) -> a -> b
$ String
-> Maybe TextEnvelopeDescr
-> SigningKey StakeKey
-> IO (Either (FileError ()) ())
forall a.
HasTextEnvelope a =>
String
-> Maybe TextEnvelopeDescr -> a -> IO (Either (FileError ()) ())
writeFileTextEnvelope String
skFp (TextEnvelopeDescr -> Maybe TextEnvelopeDescr
forall a. a -> Maybe a
Just TextEnvelopeDescr
skeyDesc) SigningKey StakeKey
skey
    (FileError () -> ShelleyStakeAddressCmdError)
-> ExceptT (FileError ()) IO ()
-> ExceptT ShelleyStakeAddressCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError () -> ShelleyStakeAddressCmdError
ShelleyStakeAddressCmdWriteFileError
      (ExceptT (FileError ()) IO ()
 -> ExceptT ShelleyStakeAddressCmdError IO ())
-> (IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ())
-> IO (Either (FileError ()) ())
-> ExceptT ShelleyStakeAddressCmdError IO ()
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ()
forall (m :: * -> *) x a. m (Either x a) -> ExceptT x m a
newExceptT
      (IO (Either (FileError ()) ())
 -> ExceptT ShelleyStakeAddressCmdError IO ())
-> IO (Either (FileError ()) ())
-> ExceptT ShelleyStakeAddressCmdError IO ()
forall a b. (a -> b) -> a -> b
$ String
-> Maybe TextEnvelopeDescr
-> VerificationKey StakeKey
-> IO (Either (FileError ()) ())
forall a.
HasTextEnvelope a =>
String
-> Maybe TextEnvelopeDescr -> a -> IO (Either (FileError ()) ())
writeFileTextEnvelope String
vkFp (TextEnvelopeDescr -> Maybe TextEnvelopeDescr
forall a. a -> Maybe a
Just TextEnvelopeDescr
vkeyDesc) VerificationKey StakeKey
vkey
  where
    skeyDesc, vkeyDesc :: TextEnvelopeDescr
    skeyDesc :: TextEnvelopeDescr
skeyDesc = TextEnvelopeDescr
"Stake Signing Key"
    vkeyDesc :: TextEnvelopeDescr
vkeyDesc = TextEnvelopeDescr
"Stake Verification Key"

runStakeAddressKeyHash
  :: VerificationKeyOrFile StakeKey
  -> Maybe OutputFile
  -> ExceptT ShelleyStakeAddressCmdError IO ()
runStakeAddressKeyHash :: VerificationKeyOrFile StakeKey
-> Maybe OutputFile -> ExceptT ShelleyStakeAddressCmdError IO ()
runStakeAddressKeyHash VerificationKeyOrFile StakeKey
stakeVerKeyOrFile Maybe OutputFile
mOutputFp = do
  VerificationKey StakeKey
vkey <- (FileError InputDecodeError -> ShelleyStakeAddressCmdError)
-> ExceptT
     (FileError InputDecodeError) IO (VerificationKey StakeKey)
-> ExceptT
     ShelleyStakeAddressCmdError IO (VerificationKey StakeKey)
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError InputDecodeError -> ShelleyStakeAddressCmdError
ShelleyStakeAddressCmdReadKeyFileError
    (ExceptT (FileError InputDecodeError) IO (VerificationKey StakeKey)
 -> ExceptT
      ShelleyStakeAddressCmdError IO (VerificationKey StakeKey))
-> (IO
      (Either (FileError InputDecodeError) (VerificationKey StakeKey))
    -> ExceptT
         (FileError InputDecodeError) IO (VerificationKey StakeKey))
-> IO
     (Either (FileError InputDecodeError) (VerificationKey StakeKey))
-> ExceptT
     ShelleyStakeAddressCmdError IO (VerificationKey StakeKey)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. IO (Either (FileError InputDecodeError) (VerificationKey StakeKey))
-> ExceptT
     (FileError InputDecodeError) IO (VerificationKey StakeKey)
forall (m :: * -> *) x a. m (Either x a) -> ExceptT x m a
newExceptT
    (IO
   (Either (FileError InputDecodeError) (VerificationKey StakeKey))
 -> ExceptT
      ShelleyStakeAddressCmdError IO (VerificationKey StakeKey))
-> IO
     (Either (FileError InputDecodeError) (VerificationKey StakeKey))
-> ExceptT
     ShelleyStakeAddressCmdError IO (VerificationKey StakeKey)
forall a b. (a -> b) -> a -> b
$ AsType StakeKey
-> VerificationKeyOrFile StakeKey
-> IO
     (Either (FileError InputDecodeError) (VerificationKey StakeKey))
forall keyrole.
(HasTextEnvelope (VerificationKey keyrole),
 SerialiseAsBech32 (VerificationKey keyrole)) =>
AsType keyrole
-> VerificationKeyOrFile keyrole
-> IO
     (Either (FileError InputDecodeError) (VerificationKey keyrole))
readVerificationKeyOrFile AsType StakeKey
AsStakeKey VerificationKeyOrFile StakeKey
stakeVerKeyOrFile

  let hexKeyHash :: ByteString
hexKeyHash = Hash StakeKey -> ByteString
forall a. SerialiseAsRawBytes a => a -> ByteString
serialiseToRawBytesHex (VerificationKey StakeKey -> Hash StakeKey
forall keyrole.
Key keyrole =>
VerificationKey keyrole -> Hash keyrole
verificationKeyHash VerificationKey StakeKey
vkey)

  case Maybe OutputFile
mOutputFp of
    Just (OutputFile String
fpath) -> IO () -> ExceptT ShelleyStakeAddressCmdError IO ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ExceptT ShelleyStakeAddressCmdError IO ())
-> IO () -> ExceptT ShelleyStakeAddressCmdError IO ()
forall a b. (a -> b) -> a -> b
$ String -> ByteString -> IO ()
BS.writeFile String
fpath ByteString
hexKeyHash
    Maybe OutputFile
Nothing -> IO () -> ExceptT ShelleyStakeAddressCmdError IO ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ExceptT ShelleyStakeAddressCmdError IO ())
-> IO () -> ExceptT ShelleyStakeAddressCmdError IO ()
forall a b. (a -> b) -> a -> b
$ ByteString -> IO ()
BS.putStrLn ByteString
hexKeyHash

runStakeAddressBuild :: VerificationKeyOrFile StakeKey -> NetworkId -> Maybe OutputFile
                     -> ExceptT ShelleyStakeAddressCmdError IO ()
runStakeAddressBuild :: VerificationKeyOrFile StakeKey
-> NetworkId
-> Maybe OutputFile
-> ExceptT ShelleyStakeAddressCmdError IO ()
runStakeAddressBuild VerificationKeyOrFile StakeKey
stakeVerKeyOrFile NetworkId
network Maybe OutputFile
mOutputFp = do
    VerificationKey StakeKey
stakeVerKey <- (FileError InputDecodeError -> ShelleyStakeAddressCmdError)
-> ExceptT
     (FileError InputDecodeError) IO (VerificationKey StakeKey)
-> ExceptT
     ShelleyStakeAddressCmdError IO (VerificationKey StakeKey)
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError InputDecodeError -> ShelleyStakeAddressCmdError
ShelleyStakeAddressCmdReadKeyFileError
      (ExceptT (FileError InputDecodeError) IO (VerificationKey StakeKey)
 -> ExceptT
      ShelleyStakeAddressCmdError IO (VerificationKey StakeKey))
-> (IO
      (Either (FileError InputDecodeError) (VerificationKey StakeKey))
    -> ExceptT
         (FileError InputDecodeError) IO (VerificationKey StakeKey))
-> IO
     (Either (FileError InputDecodeError) (VerificationKey StakeKey))
-> ExceptT
     ShelleyStakeAddressCmdError IO (VerificationKey StakeKey)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. IO (Either (FileError InputDecodeError) (VerificationKey StakeKey))
-> ExceptT
     (FileError InputDecodeError) IO (VerificationKey StakeKey)
forall (m :: * -> *) x a. m (Either x a) -> ExceptT x m a
newExceptT
      (IO
   (Either (FileError InputDecodeError) (VerificationKey StakeKey))
 -> ExceptT
      ShelleyStakeAddressCmdError IO (VerificationKey StakeKey))
-> IO
     (Either (FileError InputDecodeError) (VerificationKey StakeKey))
-> ExceptT
     ShelleyStakeAddressCmdError IO (VerificationKey StakeKey)
forall a b. (a -> b) -> a -> b
$ AsType StakeKey
-> VerificationKeyOrFile StakeKey
-> IO
     (Either (FileError InputDecodeError) (VerificationKey StakeKey))
forall keyrole.
(HasTextEnvelope (VerificationKey keyrole),
 SerialiseAsBech32 (VerificationKey keyrole)) =>
AsType keyrole
-> VerificationKeyOrFile keyrole
-> IO
     (Either (FileError InputDecodeError) (VerificationKey keyrole))
readVerificationKeyOrFile AsType StakeKey
AsStakeKey VerificationKeyOrFile StakeKey
stakeVerKeyOrFile

    let stakeCred :: StakeCredential
stakeCred = Hash StakeKey -> StakeCredential
StakeCredentialByKey (VerificationKey StakeKey -> Hash StakeKey
forall keyrole.
Key keyrole =>
VerificationKey keyrole -> Hash keyrole
verificationKeyHash VerificationKey StakeKey
stakeVerKey)
        stakeAddr :: StakeAddress
stakeAddr = NetworkId -> StakeCredential -> StakeAddress
makeStakeAddress NetworkId
network StakeCredential
stakeCred
        stakeAddrText :: Text
stakeAddrText = StakeAddress -> Text
forall addr. SerialiseAddress addr => addr -> Text
serialiseAddress StakeAddress
stakeAddr

    case Maybe OutputFile
mOutputFp of
      Just (OutputFile String
fpath) -> IO () -> ExceptT ShelleyStakeAddressCmdError IO ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ExceptT ShelleyStakeAddressCmdError IO ())
-> IO () -> ExceptT ShelleyStakeAddressCmdError IO ()
forall a b. (a -> b) -> a -> b
$ String -> Text -> IO ()
Text.writeFile String
fpath Text
stakeAddrText
      Maybe OutputFile
Nothing -> IO () -> ExceptT ShelleyStakeAddressCmdError IO ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ExceptT ShelleyStakeAddressCmdError IO ())
-> IO () -> ExceptT ShelleyStakeAddressCmdError IO ()
forall a b. (a -> b) -> a -> b
$ Text -> IO ()
Text.putStrLn Text
stakeAddrText


runStakeCredentialRegistrationCert
  :: StakeVerifier
  -> OutputFile
  -> ExceptT ShelleyStakeAddressCmdError IO ()
runStakeCredentialRegistrationCert :: StakeVerifier
-> OutputFile -> ExceptT ShelleyStakeAddressCmdError IO ()
runStakeCredentialRegistrationCert StakeVerifier
stakeVerifier (OutputFile String
oFp) =
  case StakeVerifier
stakeVerifier of
    StakeVerifierScriptFile (ScriptFile String
sFile) -> do
      ScriptInAnyLang ScriptLanguage lang
_ Script lang
script <- (FileError ScriptDecodeError -> ShelleyStakeAddressCmdError)
-> ExceptT (FileError ScriptDecodeError) IO ScriptInAnyLang
-> ExceptT ShelleyStakeAddressCmdError IO ScriptInAnyLang
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError ScriptDecodeError -> ShelleyStakeAddressCmdError
ShelleyStakeAddressCmdReadScriptFileError
                                    (ExceptT (FileError ScriptDecodeError) IO ScriptInAnyLang
 -> ExceptT ShelleyStakeAddressCmdError IO ScriptInAnyLang)
-> ExceptT (FileError ScriptDecodeError) IO ScriptInAnyLang
-> ExceptT ShelleyStakeAddressCmdError IO ScriptInAnyLang
forall a b. (a -> b) -> a -> b
$ String -> ExceptT (FileError ScriptDecodeError) IO ScriptInAnyLang
readFileScriptInAnyLang String
sFile
      let stakeCred :: StakeCredential
stakeCred = ScriptHash -> StakeCredential
StakeCredentialByScript (ScriptHash -> StakeCredential) -> ScriptHash -> StakeCredential
forall a b. (a -> b) -> a -> b
$ Script lang -> ScriptHash
forall lang. Script lang -> ScriptHash
hashScript Script lang
script
      StakeCredential -> ExceptT ShelleyStakeAddressCmdError IO ()
writeRegistrationCert StakeCredential
stakeCred
    StakeVerifierKey VerificationKeyOrFile StakeKey
stakeVerKeyOrFile -> do
      VerificationKey StakeKey
stakeVerKey <- (FileError InputDecodeError -> ShelleyStakeAddressCmdError)
-> ExceptT
     (FileError InputDecodeError) IO (VerificationKey StakeKey)
-> ExceptT
     ShelleyStakeAddressCmdError IO (VerificationKey StakeKey)
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError InputDecodeError -> ShelleyStakeAddressCmdError
ShelleyStakeAddressCmdReadKeyFileError
        (ExceptT (FileError InputDecodeError) IO (VerificationKey StakeKey)
 -> ExceptT
      ShelleyStakeAddressCmdError IO (VerificationKey StakeKey))
-> (IO
      (Either (FileError InputDecodeError) (VerificationKey StakeKey))
    -> ExceptT
         (FileError InputDecodeError) IO (VerificationKey StakeKey))
-> IO
     (Either (FileError InputDecodeError) (VerificationKey StakeKey))
-> ExceptT
     ShelleyStakeAddressCmdError IO (VerificationKey StakeKey)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. IO (Either (FileError InputDecodeError) (VerificationKey StakeKey))
-> ExceptT
     (FileError InputDecodeError) IO (VerificationKey StakeKey)
forall (m :: * -> *) x a. m (Either x a) -> ExceptT x m a
newExceptT
        (IO
   (Either (FileError InputDecodeError) (VerificationKey StakeKey))
 -> ExceptT
      ShelleyStakeAddressCmdError IO (VerificationKey StakeKey))
-> IO
     (Either (FileError InputDecodeError) (VerificationKey StakeKey))
-> ExceptT
     ShelleyStakeAddressCmdError IO (VerificationKey StakeKey)
forall a b. (a -> b) -> a -> b
$ AsType StakeKey
-> VerificationKeyOrFile StakeKey
-> IO
     (Either (FileError InputDecodeError) (VerificationKey StakeKey))
forall keyrole.
(HasTextEnvelope (VerificationKey keyrole),
 SerialiseAsBech32 (VerificationKey keyrole)) =>
AsType keyrole
-> VerificationKeyOrFile keyrole
-> IO
     (Either (FileError InputDecodeError) (VerificationKey keyrole))
readVerificationKeyOrFile AsType StakeKey
AsStakeKey VerificationKeyOrFile StakeKey
stakeVerKeyOrFile
      let stakeCred :: StakeCredential
stakeCred = Hash StakeKey -> StakeCredential
StakeCredentialByKey (VerificationKey StakeKey -> Hash StakeKey
forall keyrole.
Key keyrole =>
VerificationKey keyrole -> Hash keyrole
verificationKeyHash VerificationKey StakeKey
stakeVerKey)
      StakeCredential -> ExceptT ShelleyStakeAddressCmdError IO ()
writeRegistrationCert StakeCredential
stakeCred
 where
  writeRegistrationCert
    :: StakeCredential
    -> ExceptT ShelleyStakeAddressCmdError IO ()
  writeRegistrationCert :: StakeCredential -> ExceptT ShelleyStakeAddressCmdError IO ()
writeRegistrationCert StakeCredential
sCred = do
    let deRegCert :: Certificate
deRegCert = StakeCredential -> Certificate
makeStakeAddressRegistrationCertificate StakeCredential
sCred
    (FileError () -> ShelleyStakeAddressCmdError)
-> ExceptT (FileError ()) IO ()
-> ExceptT ShelleyStakeAddressCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError () -> ShelleyStakeAddressCmdError
ShelleyStakeAddressCmdWriteFileError
      (ExceptT (FileError ()) IO ()
 -> ExceptT ShelleyStakeAddressCmdError IO ())
-> (IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ())
-> IO (Either (FileError ()) ())
-> ExceptT ShelleyStakeAddressCmdError IO ()
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ()
forall (m :: * -> *) x a. m (Either x a) -> ExceptT x m a
newExceptT
      (IO (Either (FileError ()) ())
 -> ExceptT ShelleyStakeAddressCmdError IO ())
-> IO (Either (FileError ()) ())
-> ExceptT ShelleyStakeAddressCmdError IO ()
forall a b. (a -> b) -> a -> b
$ String
-> Maybe TextEnvelopeDescr
-> Certificate
-> IO (Either (FileError ()) ())
forall a.
HasTextEnvelope a =>
String
-> Maybe TextEnvelopeDescr -> a -> IO (Either (FileError ()) ())
writeFileTextEnvelope String
oFp (TextEnvelopeDescr -> Maybe TextEnvelopeDescr
forall a. a -> Maybe a
Just TextEnvelopeDescr
regCertDesc) Certificate
deRegCert

  regCertDesc :: TextEnvelopeDescr
  regCertDesc :: TextEnvelopeDescr
regCertDesc = TextEnvelopeDescr
"Stake Address Registration Certificate"


runStakeCredentialDelegationCert
  :: StakeVerifier
  -- ^ Delegator stake verification key, verification key file or script file.
  -> VerificationKeyOrHashOrFile StakePoolKey
  -- ^ Delegatee stake pool verification key or verification key file or
  -- verification key hash.
  -> OutputFile
  -> ExceptT ShelleyStakeAddressCmdError IO ()
runStakeCredentialDelegationCert :: StakeVerifier
-> VerificationKeyOrHashOrFile StakePoolKey
-> OutputFile
-> ExceptT ShelleyStakeAddressCmdError IO ()
runStakeCredentialDelegationCert StakeVerifier
stakeVerifier VerificationKeyOrHashOrFile StakePoolKey
poolVKeyOrHashOrFile (OutputFile String
outFp) = do
  Hash StakePoolKey
poolStakeVKeyHash <-
    (FileError InputDecodeError -> ShelleyStakeAddressCmdError)
-> ExceptT (FileError InputDecodeError) IO (Hash StakePoolKey)
-> ExceptT ShelleyStakeAddressCmdError IO (Hash StakePoolKey)
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT
      FileError InputDecodeError -> ShelleyStakeAddressCmdError
ShelleyStakeAddressCmdReadKeyFileError
      (IO (Either (FileError InputDecodeError) (Hash StakePoolKey))
-> ExceptT (FileError InputDecodeError) IO (Hash StakePoolKey)
forall (m :: * -> *) x a. m (Either x a) -> ExceptT x m a
newExceptT (IO (Either (FileError InputDecodeError) (Hash StakePoolKey))
 -> ExceptT (FileError InputDecodeError) IO (Hash StakePoolKey))
-> IO (Either (FileError InputDecodeError) (Hash StakePoolKey))
-> ExceptT (FileError InputDecodeError) IO (Hash StakePoolKey)
forall a b. (a -> b) -> a -> b
$ AsType StakePoolKey
-> VerificationKeyOrHashOrFile StakePoolKey
-> IO (Either (FileError InputDecodeError) (Hash StakePoolKey))
forall keyrole.
(Key keyrole, SerialiseAsBech32 (VerificationKey keyrole)) =>
AsType keyrole
-> VerificationKeyOrHashOrFile keyrole
-> IO (Either (FileError InputDecodeError) (Hash keyrole))
readVerificationKeyOrHashOrFile AsType StakePoolKey
AsStakePoolKey VerificationKeyOrHashOrFile StakePoolKey
poolVKeyOrHashOrFile)

  case StakeVerifier
stakeVerifier of
    StakeVerifierScriptFile (ScriptFile String
sFile) -> do
      ScriptInAnyLang ScriptLanguage lang
_ Script lang
script <- (FileError ScriptDecodeError -> ShelleyStakeAddressCmdError)
-> ExceptT (FileError ScriptDecodeError) IO ScriptInAnyLang
-> ExceptT ShelleyStakeAddressCmdError IO ScriptInAnyLang
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError ScriptDecodeError -> ShelleyStakeAddressCmdError
ShelleyStakeAddressCmdReadScriptFileError
                                    (ExceptT (FileError ScriptDecodeError) IO ScriptInAnyLang
 -> ExceptT ShelleyStakeAddressCmdError IO ScriptInAnyLang)
-> ExceptT (FileError ScriptDecodeError) IO ScriptInAnyLang
-> ExceptT ShelleyStakeAddressCmdError IO ScriptInAnyLang
forall a b. (a -> b) -> a -> b
$ String -> ExceptT (FileError ScriptDecodeError) IO ScriptInAnyLang
readFileScriptInAnyLang String
sFile
      let stakeCred :: StakeCredential
stakeCred = ScriptHash -> StakeCredential
StakeCredentialByScript (ScriptHash -> StakeCredential) -> ScriptHash -> StakeCredential
forall a b. (a -> b) -> a -> b
$ Script lang -> ScriptHash
forall lang. Script lang -> ScriptHash
hashScript Script lang
script
      StakeCredential
-> Hash StakePoolKey -> ExceptT ShelleyStakeAddressCmdError IO ()
writeDelegationCert StakeCredential
stakeCred Hash StakePoolKey
poolStakeVKeyHash
    StakeVerifierKey VerificationKeyOrFile StakeKey
stakeVerKeyOrFile -> do
      VerificationKey StakeKey
stakeVkey <- (FileError InputDecodeError -> ShelleyStakeAddressCmdError)
-> ExceptT
     (FileError InputDecodeError) IO (VerificationKey StakeKey)
-> ExceptT
     ShelleyStakeAddressCmdError IO (VerificationKey StakeKey)
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError InputDecodeError -> ShelleyStakeAddressCmdError
ShelleyStakeAddressCmdReadKeyFileError
        (ExceptT (FileError InputDecodeError) IO (VerificationKey StakeKey)
 -> ExceptT
      ShelleyStakeAddressCmdError IO (VerificationKey StakeKey))
-> (IO
      (Either (FileError InputDecodeError) (VerificationKey StakeKey))
    -> ExceptT
         (FileError InputDecodeError) IO (VerificationKey StakeKey))
-> IO
     (Either (FileError InputDecodeError) (VerificationKey StakeKey))
-> ExceptT
     ShelleyStakeAddressCmdError IO (VerificationKey StakeKey)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. IO (Either (FileError InputDecodeError) (VerificationKey StakeKey))
-> ExceptT
     (FileError InputDecodeError) IO (VerificationKey StakeKey)
forall (m :: * -> *) x a. m (Either x a) -> ExceptT x m a
newExceptT
        (IO
   (Either (FileError InputDecodeError) (VerificationKey StakeKey))
 -> ExceptT
      ShelleyStakeAddressCmdError IO (VerificationKey StakeKey))
-> IO
     (Either (FileError InputDecodeError) (VerificationKey StakeKey))
-> ExceptT
     ShelleyStakeAddressCmdError IO (VerificationKey StakeKey)
forall a b. (a -> b) -> a -> b
$ AsType StakeKey
-> VerificationKeyOrFile StakeKey
-> IO
     (Either (FileError InputDecodeError) (VerificationKey StakeKey))
forall keyrole.
(HasTextEnvelope (VerificationKey keyrole),
 SerialiseAsBech32 (VerificationKey keyrole)) =>
AsType keyrole
-> VerificationKeyOrFile keyrole
-> IO
     (Either (FileError InputDecodeError) (VerificationKey keyrole))
readVerificationKeyOrFile AsType StakeKey
AsStakeKey VerificationKeyOrFile StakeKey
stakeVerKeyOrFile
      let stakeCred :: StakeCredential
stakeCred = Hash StakeKey -> StakeCredential
StakeCredentialByKey (VerificationKey StakeKey -> Hash StakeKey
forall keyrole.
Key keyrole =>
VerificationKey keyrole -> Hash keyrole
verificationKeyHash VerificationKey StakeKey
stakeVkey)
      StakeCredential
-> Hash StakePoolKey -> ExceptT ShelleyStakeAddressCmdError IO ()
writeDelegationCert StakeCredential
stakeCred Hash StakePoolKey
poolStakeVKeyHash
  where
    writeDelegationCert
      :: StakeCredential
      -> Hash StakePoolKey
      -> ExceptT ShelleyStakeAddressCmdError IO ()
    writeDelegationCert :: StakeCredential
-> Hash StakePoolKey -> ExceptT ShelleyStakeAddressCmdError IO ()
writeDelegationCert StakeCredential
sCred Hash StakePoolKey
poolStakeVKeyHash = do
      let delegCert :: Certificate
delegCert = StakeCredential -> Hash StakePoolKey -> Certificate
makeStakeAddressDelegationCertificate StakeCredential
sCred Hash StakePoolKey
poolStakeVKeyHash
      (FileError () -> ShelleyStakeAddressCmdError)
-> ExceptT (FileError ()) IO ()
-> ExceptT ShelleyStakeAddressCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError () -> ShelleyStakeAddressCmdError
ShelleyStakeAddressCmdWriteFileError
        (ExceptT (FileError ()) IO ()
 -> ExceptT ShelleyStakeAddressCmdError IO ())
-> (IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ())
-> IO (Either (FileError ()) ())
-> ExceptT ShelleyStakeAddressCmdError IO ()
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ()
forall (m :: * -> *) x a. m (Either x a) -> ExceptT x m a
newExceptT
        (IO (Either (FileError ()) ())
 -> ExceptT ShelleyStakeAddressCmdError IO ())
-> IO (Either (FileError ()) ())
-> ExceptT ShelleyStakeAddressCmdError IO ()
forall a b. (a -> b) -> a -> b
$ String
-> Maybe TextEnvelopeDescr
-> Certificate
-> IO (Either (FileError ()) ())
forall a.
HasTextEnvelope a =>
String
-> Maybe TextEnvelopeDescr -> a -> IO (Either (FileError ()) ())
writeFileTextEnvelope String
outFp (TextEnvelopeDescr -> Maybe TextEnvelopeDescr
forall a. a -> Maybe a
Just TextEnvelopeDescr
delegCertDesc) Certificate
delegCert

    delegCertDesc :: TextEnvelopeDescr
    delegCertDesc :: TextEnvelopeDescr
delegCertDesc = TextEnvelopeDescr
"Stake Address Delegation Certificate"


runStakeCredentialDeRegistrationCert
  :: StakeVerifier
  -> OutputFile
  -> ExceptT ShelleyStakeAddressCmdError IO ()
runStakeCredentialDeRegistrationCert :: StakeVerifier
-> OutputFile -> ExceptT ShelleyStakeAddressCmdError IO ()
runStakeCredentialDeRegistrationCert StakeVerifier
stakeVerifier (OutputFile String
oFp) =
  case StakeVerifier
stakeVerifier of
    StakeVerifierScriptFile (ScriptFile String
sFile) -> do
      ScriptInAnyLang ScriptLanguage lang
_ Script lang
script <- (FileError ScriptDecodeError -> ShelleyStakeAddressCmdError)
-> ExceptT (FileError ScriptDecodeError) IO ScriptInAnyLang
-> ExceptT ShelleyStakeAddressCmdError IO ScriptInAnyLang
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError ScriptDecodeError -> ShelleyStakeAddressCmdError
ShelleyStakeAddressCmdReadScriptFileError
                                    (ExceptT (FileError ScriptDecodeError) IO ScriptInAnyLang
 -> ExceptT ShelleyStakeAddressCmdError IO ScriptInAnyLang)
-> ExceptT (FileError ScriptDecodeError) IO ScriptInAnyLang
-> ExceptT ShelleyStakeAddressCmdError IO ScriptInAnyLang
forall a b. (a -> b) -> a -> b
$ String -> ExceptT (FileError ScriptDecodeError) IO ScriptInAnyLang
readFileScriptInAnyLang String
sFile
      let stakeCred :: StakeCredential
stakeCred = ScriptHash -> StakeCredential
StakeCredentialByScript (ScriptHash -> StakeCredential) -> ScriptHash -> StakeCredential
forall a b. (a -> b) -> a -> b
$ Script lang -> ScriptHash
forall lang. Script lang -> ScriptHash
hashScript Script lang
script
      StakeCredential -> ExceptT ShelleyStakeAddressCmdError IO ()
writeDeregistrationCert StakeCredential
stakeCred
    StakeVerifierKey VerificationKeyOrFile StakeKey
stakeVerKeyOrFile -> do
      VerificationKey StakeKey
stakeVkey <- (FileError InputDecodeError -> ShelleyStakeAddressCmdError)
-> ExceptT
     (FileError InputDecodeError) IO (VerificationKey StakeKey)
-> ExceptT
     ShelleyStakeAddressCmdError IO (VerificationKey StakeKey)
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError InputDecodeError -> ShelleyStakeAddressCmdError
ShelleyStakeAddressCmdReadKeyFileError
        (ExceptT (FileError InputDecodeError) IO (VerificationKey StakeKey)
 -> ExceptT
      ShelleyStakeAddressCmdError IO (VerificationKey StakeKey))
-> (IO
      (Either (FileError InputDecodeError) (VerificationKey StakeKey))
    -> ExceptT
         (FileError InputDecodeError) IO (VerificationKey StakeKey))
-> IO
     (Either (FileError InputDecodeError) (VerificationKey StakeKey))
-> ExceptT
     ShelleyStakeAddressCmdError IO (VerificationKey StakeKey)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. IO (Either (FileError InputDecodeError) (VerificationKey StakeKey))
-> ExceptT
     (FileError InputDecodeError) IO (VerificationKey StakeKey)
forall (m :: * -> *) x a. m (Either x a) -> ExceptT x m a
newExceptT
        (IO
   (Either (FileError InputDecodeError) (VerificationKey StakeKey))
 -> ExceptT
      ShelleyStakeAddressCmdError IO (VerificationKey StakeKey))
-> IO
     (Either (FileError InputDecodeError) (VerificationKey StakeKey))
-> ExceptT
     ShelleyStakeAddressCmdError IO (VerificationKey StakeKey)
forall a b. (a -> b) -> a -> b
$ AsType StakeKey
-> VerificationKeyOrFile StakeKey
-> IO
     (Either (FileError InputDecodeError) (VerificationKey StakeKey))
forall keyrole.
(HasTextEnvelope (VerificationKey keyrole),
 SerialiseAsBech32 (VerificationKey keyrole)) =>
AsType keyrole
-> VerificationKeyOrFile keyrole
-> IO
     (Either (FileError InputDecodeError) (VerificationKey keyrole))
readVerificationKeyOrFile AsType StakeKey
AsStakeKey VerificationKeyOrFile StakeKey
stakeVerKeyOrFile
      let stakeCred :: StakeCredential
stakeCred = Hash StakeKey -> StakeCredential
StakeCredentialByKey (VerificationKey StakeKey -> Hash StakeKey
forall keyrole.
Key keyrole =>
VerificationKey keyrole -> Hash keyrole
verificationKeyHash VerificationKey StakeKey
stakeVkey)
      StakeCredential -> ExceptT ShelleyStakeAddressCmdError IO ()
writeDeregistrationCert StakeCredential
stakeCred
  where
    writeDeregistrationCert
      :: StakeCredential
      -> ExceptT ShelleyStakeAddressCmdError IO ()
    writeDeregistrationCert :: StakeCredential -> ExceptT ShelleyStakeAddressCmdError IO ()
writeDeregistrationCert StakeCredential
sCred = do
      let deRegCert :: Certificate
deRegCert = StakeCredential -> Certificate
makeStakeAddressDeregistrationCertificate StakeCredential
sCred
      (FileError () -> ShelleyStakeAddressCmdError)
-> ExceptT (FileError ()) IO ()
-> ExceptT ShelleyStakeAddressCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError () -> ShelleyStakeAddressCmdError
ShelleyStakeAddressCmdWriteFileError
        (ExceptT (FileError ()) IO ()
 -> ExceptT ShelleyStakeAddressCmdError IO ())
-> (IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ())
-> IO (Either (FileError ()) ())
-> ExceptT ShelleyStakeAddressCmdError IO ()
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ()
forall (m :: * -> *) x a. m (Either x a) -> ExceptT x m a
newExceptT
        (IO (Either (FileError ()) ())
 -> ExceptT ShelleyStakeAddressCmdError IO ())
-> IO (Either (FileError ()) ())
-> ExceptT ShelleyStakeAddressCmdError IO ()
forall a b. (a -> b) -> a -> b
$ String
-> Maybe TextEnvelopeDescr
-> Certificate
-> IO (Either (FileError ()) ())
forall a.
HasTextEnvelope a =>
String
-> Maybe TextEnvelopeDescr -> a -> IO (Either (FileError ()) ())
writeFileTextEnvelope String
oFp (TextEnvelopeDescr -> Maybe TextEnvelopeDescr
forall a. a -> Maybe a
Just TextEnvelopeDescr
deregCertDesc) Certificate
deRegCert

    deregCertDesc :: TextEnvelopeDescr
    deregCertDesc :: TextEnvelopeDescr
deregCertDesc = TextEnvelopeDescr
"Stake Address Deregistration Certificate"