module Bcc.CLI.Sophie.Run.StakeAddress
  ( SophieStakeAddressCmdError(SophieStakeAddressCmdReadKeyFileError)
  , renderSophieStakeAddressCmdError
  , runStakeAddressCmd
  , runStakeAddressKeyGen
  ) where

import           Bcc.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           Bcc.Api
import           Bcc.Api.Sophie

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

data SophieStakeAddressCmdError
  = SophieStakeAddressCmdReadKeyFileError !(FileError InputDecodeError)
  | SophieStakeAddressCmdReadScriptFileError !(FileError ScriptDecodeError)
  | SophieStakeAddressCmdWriteFileError !(FileError ())
  deriving Int -> SophieStakeAddressCmdError -> ShowS
[SophieStakeAddressCmdError] -> ShowS
SophieStakeAddressCmdError -> String
(Int -> SophieStakeAddressCmdError -> ShowS)
-> (SophieStakeAddressCmdError -> String)
-> ([SophieStakeAddressCmdError] -> ShowS)
-> Show SophieStakeAddressCmdError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SophieStakeAddressCmdError] -> ShowS
$cshowList :: [SophieStakeAddressCmdError] -> ShowS
show :: SophieStakeAddressCmdError -> String
$cshow :: SophieStakeAddressCmdError -> String
showsPrec :: Int -> SophieStakeAddressCmdError -> ShowS
$cshowsPrec :: Int -> SophieStakeAddressCmdError -> ShowS
Show

renderSophieStakeAddressCmdError :: SophieStakeAddressCmdError -> Text
renderSophieStakeAddressCmdError :: SophieStakeAddressCmdError -> Text
renderSophieStakeAddressCmdError SophieStakeAddressCmdError
err =
  case SophieStakeAddressCmdError
err of
    SophieStakeAddressCmdReadKeyFileError FileError InputDecodeError
fileErr -> String -> Text
Text.pack (FileError InputDecodeError -> String
forall e. Error e => e -> String
displayError FileError InputDecodeError
fileErr)
    SophieStakeAddressCmdWriteFileError FileError ()
fileErr -> String -> Text
Text.pack (FileError () -> String
forall e. Error e => e -> String
displayError FileError ()
fileErr)
    SophieStakeAddressCmdReadScriptFileError FileError ScriptDecodeError
fileErr -> String -> Text
Text.pack (FileError ScriptDecodeError -> String
forall e. Error e => e -> String
displayError FileError ScriptDecodeError
fileErr)

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


--
-- Stake address command implementations
--

runStakeAddressKeyGen :: VerificationKeyFile -> SigningKeyFile -> ExceptT SophieStakeAddressCmdError IO ()
runStakeAddressKeyGen :: VerificationKeyFile
-> SigningKeyFile -> ExceptT SophieStakeAddressCmdError IO ()
runStakeAddressKeyGen (VerificationKeyFile String
vkFp) (SigningKeyFile String
skFp) = do
    SigningKey StakeKey
skey <- IO (SigningKey StakeKey)
-> ExceptT SophieStakeAddressCmdError IO (SigningKey StakeKey)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (SigningKey StakeKey)
 -> ExceptT SophieStakeAddressCmdError IO (SigningKey StakeKey))
-> IO (SigningKey StakeKey)
-> ExceptT SophieStakeAddressCmdError 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 () -> SophieStakeAddressCmdError)
-> ExceptT (FileError ()) IO ()
-> ExceptT SophieStakeAddressCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError () -> SophieStakeAddressCmdError
SophieStakeAddressCmdWriteFileError
      (ExceptT (FileError ()) IO ()
 -> ExceptT SophieStakeAddressCmdError IO ())
-> (IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ())
-> IO (Either (FileError ()) ())
-> ExceptT SophieStakeAddressCmdError 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 SophieStakeAddressCmdError IO ())
-> IO (Either (FileError ()) ())
-> ExceptT SophieStakeAddressCmdError 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 () -> SophieStakeAddressCmdError)
-> ExceptT (FileError ()) IO ()
-> ExceptT SophieStakeAddressCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError () -> SophieStakeAddressCmdError
SophieStakeAddressCmdWriteFileError
      (ExceptT (FileError ()) IO ()
 -> ExceptT SophieStakeAddressCmdError IO ())
-> (IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ())
-> IO (Either (FileError ()) ())
-> ExceptT SophieStakeAddressCmdError 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 SophieStakeAddressCmdError IO ())
-> IO (Either (FileError ()) ())
-> ExceptT SophieStakeAddressCmdError 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 SophieStakeAddressCmdError IO ()
runStakeAddressKeyHash :: VerificationKeyOrFile StakeKey
-> Maybe OutputFile -> ExceptT SophieStakeAddressCmdError IO ()
runStakeAddressKeyHash VerificationKeyOrFile StakeKey
stakeVerKeyOrFile Maybe OutputFile
mOutputFp = do
  VerificationKey StakeKey
vkey <- (FileError InputDecodeError -> SophieStakeAddressCmdError)
-> ExceptT
     (FileError InputDecodeError) IO (VerificationKey StakeKey)
-> ExceptT SophieStakeAddressCmdError IO (VerificationKey StakeKey)
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError InputDecodeError -> SophieStakeAddressCmdError
SophieStakeAddressCmdReadKeyFileError
    (ExceptT (FileError InputDecodeError) IO (VerificationKey StakeKey)
 -> ExceptT
      SophieStakeAddressCmdError IO (VerificationKey StakeKey))
-> (IO
      (Either (FileError InputDecodeError) (VerificationKey StakeKey))
    -> ExceptT
         (FileError InputDecodeError) IO (VerificationKey StakeKey))
-> IO
     (Either (FileError InputDecodeError) (VerificationKey StakeKey))
-> ExceptT SophieStakeAddressCmdError 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
      SophieStakeAddressCmdError IO (VerificationKey StakeKey))
-> IO
     (Either (FileError InputDecodeError) (VerificationKey StakeKey))
-> ExceptT SophieStakeAddressCmdError 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 SophieStakeAddressCmdError IO ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ExceptT SophieStakeAddressCmdError IO ())
-> IO () -> ExceptT SophieStakeAddressCmdError IO ()
forall a b. (a -> b) -> a -> b
$ String -> ByteString -> IO ()
BS.writeFile String
fpath ByteString
hexKeyHash
    Maybe OutputFile
Nothing -> IO () -> ExceptT SophieStakeAddressCmdError IO ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ExceptT SophieStakeAddressCmdError IO ())
-> IO () -> ExceptT SophieStakeAddressCmdError IO ()
forall a b. (a -> b) -> a -> b
$ ByteString -> IO ()
BS.putStrLn ByteString
hexKeyHash

runStakeAddressBuild :: VerificationKeyOrFile StakeKey -> NetworkId -> Maybe OutputFile
                     -> ExceptT SophieStakeAddressCmdError IO ()
runStakeAddressBuild :: VerificationKeyOrFile StakeKey
-> NetworkId
-> Maybe OutputFile
-> ExceptT SophieStakeAddressCmdError IO ()
runStakeAddressBuild VerificationKeyOrFile StakeKey
stakeVerKeyOrFile NetworkId
network Maybe OutputFile
mOutputFp = do
    VerificationKey StakeKey
stakeVerKey <- (FileError InputDecodeError -> SophieStakeAddressCmdError)
-> ExceptT
     (FileError InputDecodeError) IO (VerificationKey StakeKey)
-> ExceptT SophieStakeAddressCmdError IO (VerificationKey StakeKey)
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError InputDecodeError -> SophieStakeAddressCmdError
SophieStakeAddressCmdReadKeyFileError
      (ExceptT (FileError InputDecodeError) IO (VerificationKey StakeKey)
 -> ExceptT
      SophieStakeAddressCmdError IO (VerificationKey StakeKey))
-> (IO
      (Either (FileError InputDecodeError) (VerificationKey StakeKey))
    -> ExceptT
         (FileError InputDecodeError) IO (VerificationKey StakeKey))
-> IO
     (Either (FileError InputDecodeError) (VerificationKey StakeKey))
-> ExceptT SophieStakeAddressCmdError 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
      SophieStakeAddressCmdError IO (VerificationKey StakeKey))
-> IO
     (Either (FileError InputDecodeError) (VerificationKey StakeKey))
-> ExceptT SophieStakeAddressCmdError 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 SophieStakeAddressCmdError IO ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ExceptT SophieStakeAddressCmdError IO ())
-> IO () -> ExceptT SophieStakeAddressCmdError IO ()
forall a b. (a -> b) -> a -> b
$ String -> Text -> IO ()
Text.writeFile String
fpath Text
stakeAddrText
      Maybe OutputFile
Nothing -> IO () -> ExceptT SophieStakeAddressCmdError IO ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ExceptT SophieStakeAddressCmdError IO ())
-> IO () -> ExceptT SophieStakeAddressCmdError IO ()
forall a b. (a -> b) -> a -> b
$ Text -> IO ()
Text.putStrLn Text
stakeAddrText


runStakeCredentialRegistrationCert
  :: StakeVerifier
  -> OutputFile
  -> ExceptT SophieStakeAddressCmdError IO ()
runStakeCredentialRegistrationCert :: StakeVerifier
-> OutputFile -> ExceptT SophieStakeAddressCmdError IO ()
runStakeCredentialRegistrationCert StakeVerifier
stakeVerifier (OutputFile String
oFp) =
  case StakeVerifier
stakeVerifier of
    StakeVerifierScriptFile (ScriptFile String
sFile) -> do
      ScriptInAnyLang ScriptLanguage lang
_ Script lang
script <- (FileError ScriptDecodeError -> SophieStakeAddressCmdError)
-> ExceptT (FileError ScriptDecodeError) IO ScriptInAnyLang
-> ExceptT SophieStakeAddressCmdError IO ScriptInAnyLang
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError ScriptDecodeError -> SophieStakeAddressCmdError
SophieStakeAddressCmdReadScriptFileError
                                    (ExceptT (FileError ScriptDecodeError) IO ScriptInAnyLang
 -> ExceptT SophieStakeAddressCmdError IO ScriptInAnyLang)
-> ExceptT (FileError ScriptDecodeError) IO ScriptInAnyLang
-> ExceptT SophieStakeAddressCmdError 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 SophieStakeAddressCmdError IO ()
writeRegistrationCert StakeCredential
stakeCred
    StakeVerifierKey VerificationKeyOrFile StakeKey
stakeVerKeyOrFile -> do
      VerificationKey StakeKey
stakeVerKey <- (FileError InputDecodeError -> SophieStakeAddressCmdError)
-> ExceptT
     (FileError InputDecodeError) IO (VerificationKey StakeKey)
-> ExceptT SophieStakeAddressCmdError IO (VerificationKey StakeKey)
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError InputDecodeError -> SophieStakeAddressCmdError
SophieStakeAddressCmdReadKeyFileError
        (ExceptT (FileError InputDecodeError) IO (VerificationKey StakeKey)
 -> ExceptT
      SophieStakeAddressCmdError IO (VerificationKey StakeKey))
-> (IO
      (Either (FileError InputDecodeError) (VerificationKey StakeKey))
    -> ExceptT
         (FileError InputDecodeError) IO (VerificationKey StakeKey))
-> IO
     (Either (FileError InputDecodeError) (VerificationKey StakeKey))
-> ExceptT SophieStakeAddressCmdError 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
      SophieStakeAddressCmdError IO (VerificationKey StakeKey))
-> IO
     (Either (FileError InputDecodeError) (VerificationKey StakeKey))
-> ExceptT SophieStakeAddressCmdError 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 SophieStakeAddressCmdError IO ()
writeRegistrationCert StakeCredential
stakeCred
 where
  writeRegistrationCert
    :: StakeCredential
    -> ExceptT SophieStakeAddressCmdError IO ()
  writeRegistrationCert :: StakeCredential -> ExceptT SophieStakeAddressCmdError IO ()
writeRegistrationCert StakeCredential
sCred = do
    let deRegCert :: Certificate
deRegCert = StakeCredential -> Certificate
makeStakeAddressRegistrationCertificate StakeCredential
sCred
    (FileError () -> SophieStakeAddressCmdError)
-> ExceptT (FileError ()) IO ()
-> ExceptT SophieStakeAddressCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError () -> SophieStakeAddressCmdError
SophieStakeAddressCmdWriteFileError
      (ExceptT (FileError ()) IO ()
 -> ExceptT SophieStakeAddressCmdError IO ())
-> (IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ())
-> IO (Either (FileError ()) ())
-> ExceptT SophieStakeAddressCmdError 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 SophieStakeAddressCmdError IO ())
-> IO (Either (FileError ()) ())
-> ExceptT SophieStakeAddressCmdError 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 SophieStakeAddressCmdError IO ()
runStakeCredentialDelegationCert :: StakeVerifier
-> VerificationKeyOrHashOrFile StakePoolKey
-> OutputFile
-> ExceptT SophieStakeAddressCmdError IO ()
runStakeCredentialDelegationCert StakeVerifier
stakeVerifier VerificationKeyOrHashOrFile StakePoolKey
poolVKeyOrHashOrFile (OutputFile String
outFp) = do
  Hash StakePoolKey
poolStakeVKeyHash <-
    (FileError InputDecodeError -> SophieStakeAddressCmdError)
-> ExceptT (FileError InputDecodeError) IO (Hash StakePoolKey)
-> ExceptT SophieStakeAddressCmdError IO (Hash StakePoolKey)
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT
      FileError InputDecodeError -> SophieStakeAddressCmdError
SophieStakeAddressCmdReadKeyFileError
      (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 -> SophieStakeAddressCmdError)
-> ExceptT (FileError ScriptDecodeError) IO ScriptInAnyLang
-> ExceptT SophieStakeAddressCmdError IO ScriptInAnyLang
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError ScriptDecodeError -> SophieStakeAddressCmdError
SophieStakeAddressCmdReadScriptFileError
                                    (ExceptT (FileError ScriptDecodeError) IO ScriptInAnyLang
 -> ExceptT SophieStakeAddressCmdError IO ScriptInAnyLang)
-> ExceptT (FileError ScriptDecodeError) IO ScriptInAnyLang
-> ExceptT SophieStakeAddressCmdError 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 SophieStakeAddressCmdError IO ()
writeDelegationCert StakeCredential
stakeCred Hash StakePoolKey
poolStakeVKeyHash
    StakeVerifierKey VerificationKeyOrFile StakeKey
stakeVerKeyOrFile -> do
      VerificationKey StakeKey
stakeVkey <- (FileError InputDecodeError -> SophieStakeAddressCmdError)
-> ExceptT
     (FileError InputDecodeError) IO (VerificationKey StakeKey)
-> ExceptT SophieStakeAddressCmdError IO (VerificationKey StakeKey)
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError InputDecodeError -> SophieStakeAddressCmdError
SophieStakeAddressCmdReadKeyFileError
        (ExceptT (FileError InputDecodeError) IO (VerificationKey StakeKey)
 -> ExceptT
      SophieStakeAddressCmdError IO (VerificationKey StakeKey))
-> (IO
      (Either (FileError InputDecodeError) (VerificationKey StakeKey))
    -> ExceptT
         (FileError InputDecodeError) IO (VerificationKey StakeKey))
-> IO
     (Either (FileError InputDecodeError) (VerificationKey StakeKey))
-> ExceptT SophieStakeAddressCmdError 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
      SophieStakeAddressCmdError IO (VerificationKey StakeKey))
-> IO
     (Either (FileError InputDecodeError) (VerificationKey StakeKey))
-> ExceptT SophieStakeAddressCmdError 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 SophieStakeAddressCmdError IO ()
writeDelegationCert StakeCredential
stakeCred Hash StakePoolKey
poolStakeVKeyHash
  where
    writeDelegationCert
      :: StakeCredential
      -> Hash StakePoolKey
      -> ExceptT SophieStakeAddressCmdError IO ()
    writeDelegationCert :: StakeCredential
-> Hash StakePoolKey -> ExceptT SophieStakeAddressCmdError IO ()
writeDelegationCert StakeCredential
sCred Hash StakePoolKey
poolStakeVKeyHash = do
      let delegCert :: Certificate
delegCert = StakeCredential -> Hash StakePoolKey -> Certificate
makeStakeAddressDelegationCertificate StakeCredential
sCred Hash StakePoolKey
poolStakeVKeyHash
      (FileError () -> SophieStakeAddressCmdError)
-> ExceptT (FileError ()) IO ()
-> ExceptT SophieStakeAddressCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError () -> SophieStakeAddressCmdError
SophieStakeAddressCmdWriteFileError
        (ExceptT (FileError ()) IO ()
 -> ExceptT SophieStakeAddressCmdError IO ())
-> (IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ())
-> IO (Either (FileError ()) ())
-> ExceptT SophieStakeAddressCmdError 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 SophieStakeAddressCmdError IO ())
-> IO (Either (FileError ()) ())
-> ExceptT SophieStakeAddressCmdError 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 SophieStakeAddressCmdError IO ()
runStakeCredentialDeRegistrationCert :: StakeVerifier
-> OutputFile -> ExceptT SophieStakeAddressCmdError IO ()
runStakeCredentialDeRegistrationCert StakeVerifier
stakeVerifier (OutputFile String
oFp) =
  case StakeVerifier
stakeVerifier of
    StakeVerifierScriptFile (ScriptFile String
sFile) -> do
      ScriptInAnyLang ScriptLanguage lang
_ Script lang
script <- (FileError ScriptDecodeError -> SophieStakeAddressCmdError)
-> ExceptT (FileError ScriptDecodeError) IO ScriptInAnyLang
-> ExceptT SophieStakeAddressCmdError IO ScriptInAnyLang
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError ScriptDecodeError -> SophieStakeAddressCmdError
SophieStakeAddressCmdReadScriptFileError
                                    (ExceptT (FileError ScriptDecodeError) IO ScriptInAnyLang
 -> ExceptT SophieStakeAddressCmdError IO ScriptInAnyLang)
-> ExceptT (FileError ScriptDecodeError) IO ScriptInAnyLang
-> ExceptT SophieStakeAddressCmdError 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 SophieStakeAddressCmdError IO ()
writeDeregistrationCert StakeCredential
stakeCred
    StakeVerifierKey VerificationKeyOrFile StakeKey
stakeVerKeyOrFile -> do
      VerificationKey StakeKey
stakeVkey <- (FileError InputDecodeError -> SophieStakeAddressCmdError)
-> ExceptT
     (FileError InputDecodeError) IO (VerificationKey StakeKey)
-> ExceptT SophieStakeAddressCmdError IO (VerificationKey StakeKey)
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError InputDecodeError -> SophieStakeAddressCmdError
SophieStakeAddressCmdReadKeyFileError
        (ExceptT (FileError InputDecodeError) IO (VerificationKey StakeKey)
 -> ExceptT
      SophieStakeAddressCmdError IO (VerificationKey StakeKey))
-> (IO
      (Either (FileError InputDecodeError) (VerificationKey StakeKey))
    -> ExceptT
         (FileError InputDecodeError) IO (VerificationKey StakeKey))
-> IO
     (Either (FileError InputDecodeError) (VerificationKey StakeKey))
-> ExceptT SophieStakeAddressCmdError 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
      SophieStakeAddressCmdError IO (VerificationKey StakeKey))
-> IO
     (Either (FileError InputDecodeError) (VerificationKey StakeKey))
-> ExceptT SophieStakeAddressCmdError 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 SophieStakeAddressCmdError IO ()
writeDeregistrationCert StakeCredential
stakeCred
  where
    writeDeregistrationCert
      :: StakeCredential
      -> ExceptT SophieStakeAddressCmdError IO ()
    writeDeregistrationCert :: StakeCredential -> ExceptT SophieStakeAddressCmdError IO ()
writeDeregistrationCert StakeCredential
sCred = do
      let deRegCert :: Certificate
deRegCert = StakeCredential -> Certificate
makeStakeAddressDeregistrationCertificate StakeCredential
sCred
      (FileError () -> SophieStakeAddressCmdError)
-> ExceptT (FileError ()) IO ()
-> ExceptT SophieStakeAddressCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError () -> SophieStakeAddressCmdError
SophieStakeAddressCmdWriteFileError
        (ExceptT (FileError ()) IO ()
 -> ExceptT SophieStakeAddressCmdError IO ())
-> (IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ())
-> IO (Either (FileError ()) ())
-> ExceptT SophieStakeAddressCmdError 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 SophieStakeAddressCmdError IO ())
-> IO (Either (FileError ()) ())
-> ExceptT SophieStakeAddressCmdError 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"