{-# LANGUAGE GADTs #-}

module Cardano.CLI.Byron.Run
  ( ByronClientCmdError
  , renderByronClientCmdError
  , runByronClientCommand
  ) where

import           Cardano.Prelude

import           Control.Monad.Trans.Except.Extra (firstExceptT, hoistEither, left)
import qualified Data.ByteString.Char8 as BS
import qualified Data.Text as Text
import qualified Data.Text.Lazy.Builder as Builder
import qualified Data.Text.Lazy.IO as TL
import qualified Formatting as F

import qualified Cardano.Chain.Genesis as Genesis

import qualified Cardano.Crypto.Hashing as Crypto
import qualified Cardano.Crypto.Signing as Crypto

import           Cardano.Api hiding (UpdateProposal, GenesisParameters)
import           Cardano.Api.Byron (SomeByronSigningKey (..), Tx (..), VerificationKey (..))

import           Ouroboros.Consensus.Byron.Ledger (ByronBlock)
import           Ouroboros.Consensus.Ledger.SupportsMempool (ApplyTxErr)

import           Cardano.CLI.Byron.Commands
import           Cardano.CLI.Byron.Delegation
import           Cardano.CLI.Byron.Genesis
import           Cardano.CLI.Byron.Key
import           Cardano.CLI.Byron.Query
import           Cardano.CLI.Byron.Tx
import           Cardano.CLI.Byron.UpdateProposal
import           Cardano.CLI.Byron.Vote
import           Cardano.CLI.Helpers
import           Cardano.CLI.Shelley.Commands (ByronKeyFormat (..))
import           Cardano.CLI.Types

-- | Data type that encompasses all the possible errors of the
-- Byron client.
data ByronClientCmdError
  = ByronCmdDelegationError !ByronDelegationError
  | ByronCmdGenesisError !ByronGenesisError
  | ByronCmdHelpersError !HelpersError
  | ByronCmdKeyFailure !ByronKeyFailure
  | ByronCmdQueryError !ByronQueryError
  | ByronCmdTxError !ByronTxError
  | ByronCmdTxSubmitError !(ApplyTxErr ByronBlock)
  | ByronCmdUpdateProposalError !ByronUpdateProposalError
  | ByronCmdVoteError !ByronVoteError
  deriving Int -> ByronClientCmdError -> ShowS
[ByronClientCmdError] -> ShowS
ByronClientCmdError -> String
(Int -> ByronClientCmdError -> ShowS)
-> (ByronClientCmdError -> String)
-> ([ByronClientCmdError] -> ShowS)
-> Show ByronClientCmdError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ByronClientCmdError] -> ShowS
$cshowList :: [ByronClientCmdError] -> ShowS
show :: ByronClientCmdError -> String
$cshow :: ByronClientCmdError -> String
showsPrec :: Int -> ByronClientCmdError -> ShowS
$cshowsPrec :: Int -> ByronClientCmdError -> ShowS
Show

renderByronClientCmdError :: ByronClientCmdError -> Text
renderByronClientCmdError :: ByronClientCmdError -> Text
renderByronClientCmdError ByronClientCmdError
err =
  case ByronClientCmdError
err of
    ByronCmdDelegationError ByronDelegationError
e -> ByronDelegationError -> Text
renderByronDelegationError ByronDelegationError
e
    ByronCmdGenesisError ByronGenesisError
e -> ByronGenesisError -> Text
renderByronGenesisError ByronGenesisError
e
    ByronCmdHelpersError HelpersError
e -> HelpersError -> Text
renderHelpersError HelpersError
e
    ByronCmdKeyFailure ByronKeyFailure
e -> ByronKeyFailure -> Text
renderByronKeyFailure ByronKeyFailure
e
    ByronCmdQueryError ByronQueryError
e -> ByronQueryError -> Text
renderByronQueryError ByronQueryError
e
    ByronCmdTxError ByronTxError
e -> ByronTxError -> Text
renderByronTxError ByronTxError
e
    ByronCmdTxSubmitError ApplyTxErr ByronBlock
e ->
      Text
"Error while submitting Byron tx: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
Text.pack (ApplyMempoolPayloadErr -> String
forall a b. (Show a, ConvertText String b) => a -> b
show ApplyMempoolPayloadErr
ApplyTxErr ByronBlock
e)
    ByronCmdUpdateProposalError ByronUpdateProposalError
e -> ByronUpdateProposalError -> Text
renderByronUpdateProposalError ByronUpdateProposalError
e
    ByronCmdVoteError ByronVoteError
e -> ByronVoteError -> Text
renderByronVoteError ByronVoteError
e

runByronClientCommand :: ByronCommand -> ExceptT ByronClientCmdError IO ()
runByronClientCommand :: ByronCommand -> ExceptT ByronClientCmdError IO ()
runByronClientCommand ByronCommand
c =
  case ByronCommand
c of
    NodeCmd NodeCmd
bc -> NodeCmd -> ExceptT ByronClientCmdError IO ()
runNodeCmd NodeCmd
bc
    Genesis NewDirectory
outDir GenesisParameters
params -> NewDirectory
-> GenesisParameters -> ExceptT ByronClientCmdError IO ()
runGenesisCommand NewDirectory
outDir GenesisParameters
params
    GetLocalNodeTip NetworkId
network -> (ByronQueryError -> ByronClientCmdError)
-> ExceptT ByronQueryError IO ()
-> ExceptT ByronClientCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT ByronQueryError -> ByronClientCmdError
ByronCmdQueryError (ExceptT ByronQueryError IO ()
 -> ExceptT ByronClientCmdError IO ())
-> ExceptT ByronQueryError IO ()
-> ExceptT ByronClientCmdError IO ()
forall a b. (a -> b) -> a -> b
$ NetworkId -> ExceptT ByronQueryError IO ()
runGetLocalNodeTip NetworkId
network
    ValidateCBOR CBORObject
cborObject String
fp -> CBORObject -> String -> ExceptT ByronClientCmdError IO ()
runValidateCBOR CBORObject
cborObject String
fp
    PrettyPrintCBOR String
fp -> String -> ExceptT ByronClientCmdError IO ()
runPrettyPrintCBOR String
fp
    PrettySigningKeyPublic ByronKeyFormat
bKeyFormat SigningKeyFile
skF -> ByronKeyFormat
-> SigningKeyFile -> ExceptT ByronClientCmdError IO ()
runPrettySigningKeyPublic ByronKeyFormat
bKeyFormat SigningKeyFile
skF
    MigrateDelegateKeyFrom SigningKeyFile
oldKey NewSigningKeyFile
nskf ->
       SigningKeyFile
-> NewSigningKeyFile -> ExceptT ByronClientCmdError IO ()
runMigrateDelegateKeyFrom SigningKeyFile
oldKey NewSigningKeyFile
nskf
    PrintGenesisHash GenesisFile
genFp -> GenesisFile -> ExceptT ByronClientCmdError IO ()
runPrintGenesisHash GenesisFile
genFp
    PrintSigningKeyAddress ByronKeyFormat
bKeyFormat NetworkId
networkid SigningKeyFile
skF -> ByronKeyFormat
-> NetworkId -> SigningKeyFile -> ExceptT ByronClientCmdError IO ()
runPrintSigningKeyAddress ByronKeyFormat
bKeyFormat NetworkId
networkid SigningKeyFile
skF
    Keygen NewSigningKeyFile
nskf -> NewSigningKeyFile -> ExceptT ByronClientCmdError IO ()
runKeygen NewSigningKeyFile
nskf
    ToVerification ByronKeyFormat
bKeyFormat SigningKeyFile
skFp NewVerificationKeyFile
nvkFp -> ByronKeyFormat
-> SigningKeyFile
-> NewVerificationKeyFile
-> ExceptT ByronClientCmdError IO ()
runToVerification ByronKeyFormat
bKeyFormat SigningKeyFile
skFp NewVerificationKeyFile
nvkFp
    SubmitTx NetworkId
network TxFile
fp -> NetworkId -> TxFile -> ExceptT ByronClientCmdError IO ()
runSubmitTx NetworkId
network TxFile
fp
    GetTxId TxFile
fp -> TxFile -> ExceptT ByronClientCmdError IO ()
runGetTxId TxFile
fp
    SpendGenesisUTxO GenesisFile
genFp NetworkId
nw ByronKeyFormat
era NewTxFile
nftx SigningKeyFile
ctKey Address ByronAddr
genRichAddr [TxOut ByronEra]
outs ->
      GenesisFile
-> NetworkId
-> ByronKeyFormat
-> NewTxFile
-> SigningKeyFile
-> Address ByronAddr
-> [TxOut ByronEra]
-> ExceptT ByronClientCmdError IO ()
runSpendGenesisUTxO GenesisFile
genFp NetworkId
nw ByronKeyFormat
era NewTxFile
nftx SigningKeyFile
ctKey Address ByronAddr
genRichAddr [TxOut ByronEra]
outs
    SpendUTxO NetworkId
nw ByronKeyFormat
era NewTxFile
nftx SigningKeyFile
ctKey [TxIn]
ins [TxOut ByronEra]
outs ->
      NetworkId
-> ByronKeyFormat
-> NewTxFile
-> SigningKeyFile
-> [TxIn]
-> [TxOut ByronEra]
-> ExceptT ByronClientCmdError IO ()
runSpendUTxO NetworkId
nw ByronKeyFormat
era NewTxFile
nftx SigningKeyFile
ctKey [TxIn]
ins [TxOut ByronEra]
outs


runNodeCmd :: NodeCmd -> ExceptT ByronClientCmdError IO ()
runNodeCmd :: NodeCmd -> ExceptT ByronClientCmdError IO ()
runNodeCmd (CreateVote NetworkId
nw SigningKeyFile
sKey String
upPropFp Bool
voteBool String
outputFp) =
  (ByronVoteError -> ByronClientCmdError)
-> ExceptT ByronVoteError IO ()
-> ExceptT ByronClientCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT ByronVoteError -> ByronClientCmdError
ByronCmdVoteError (ExceptT ByronVoteError IO () -> ExceptT ByronClientCmdError IO ())
-> ExceptT ByronVoteError IO ()
-> ExceptT ByronClientCmdError IO ()
forall a b. (a -> b) -> a -> b
$ NetworkId
-> SigningKeyFile
-> String
-> Bool
-> String
-> ExceptT ByronVoteError IO ()
runVoteCreation NetworkId
nw SigningKeyFile
sKey String
upPropFp Bool
voteBool String
outputFp

runNodeCmd (SubmitUpdateProposal NetworkId
network String
proposalFp) =
    (ByronUpdateProposalError -> ByronClientCmdError)
-> ExceptT ByronUpdateProposalError IO ()
-> ExceptT ByronClientCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT ByronUpdateProposalError -> ByronClientCmdError
ByronCmdUpdateProposalError
      (ExceptT ByronUpdateProposalError IO ()
 -> ExceptT ByronClientCmdError IO ())
-> ExceptT ByronUpdateProposalError IO ()
-> ExceptT ByronClientCmdError IO ()
forall a b. (a -> b) -> a -> b
$ NetworkId -> String -> ExceptT ByronUpdateProposalError IO ()
submitByronUpdateProposal NetworkId
network String
proposalFp

runNodeCmd (SubmitVote NetworkId
network String
voteFp) =
    (ByronVoteError -> ByronClientCmdError)
-> ExceptT ByronVoteError IO ()
-> ExceptT ByronClientCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT ByronVoteError -> ByronClientCmdError
ByronCmdVoteError (ExceptT ByronVoteError IO () -> ExceptT ByronClientCmdError IO ())
-> ExceptT ByronVoteError IO ()
-> ExceptT ByronClientCmdError IO ()
forall a b. (a -> b) -> a -> b
$ NetworkId -> String -> ExceptT ByronVoteError IO ()
submitByronVote NetworkId
network String
voteFp

runNodeCmd (UpdateProposal NetworkId
nw SigningKeyFile
sKey ProtocolVersion
pVer SoftwareVersion
sVer SystemTag
sysTag InstallerHash
insHash String
outputFp ByronProtocolParametersUpdate
params) =
  (ByronUpdateProposalError -> ByronClientCmdError)
-> ExceptT ByronUpdateProposalError IO ()
-> ExceptT ByronClientCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT ByronUpdateProposalError -> ByronClientCmdError
ByronCmdUpdateProposalError
    (ExceptT ByronUpdateProposalError IO ()
 -> ExceptT ByronClientCmdError IO ())
-> ExceptT ByronUpdateProposalError IO ()
-> ExceptT ByronClientCmdError IO ()
forall a b. (a -> b) -> a -> b
$ NetworkId
-> SigningKeyFile
-> ProtocolVersion
-> SoftwareVersion
-> SystemTag
-> InstallerHash
-> String
-> ByronProtocolParametersUpdate
-> ExceptT ByronUpdateProposalError IO ()
runProposalCreation NetworkId
nw SigningKeyFile
sKey ProtocolVersion
pVer SoftwareVersion
sVer SystemTag
sysTag InstallerHash
insHash String
outputFp ByronProtocolParametersUpdate
params

runGenesisCommand :: NewDirectory -> GenesisParameters -> ExceptT ByronClientCmdError IO ()
runGenesisCommand :: NewDirectory
-> GenesisParameters -> ExceptT ByronClientCmdError IO ()
runGenesisCommand NewDirectory
outDir GenesisParameters
params = do
  (GenesisData
genData, GeneratedSecrets
genSecrets) <- (ByronGenesisError -> ByronClientCmdError)
-> ExceptT ByronGenesisError IO (GenesisData, GeneratedSecrets)
-> ExceptT ByronClientCmdError IO (GenesisData, GeneratedSecrets)
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT ByronGenesisError -> ByronClientCmdError
ByronCmdGenesisError (ExceptT ByronGenesisError IO (GenesisData, GeneratedSecrets)
 -> ExceptT ByronClientCmdError IO (GenesisData, GeneratedSecrets))
-> ExceptT ByronGenesisError IO (GenesisData, GeneratedSecrets)
-> ExceptT ByronClientCmdError IO (GenesisData, GeneratedSecrets)
forall a b. (a -> b) -> a -> b
$ GenesisParameters
-> ExceptT ByronGenesisError IO (GenesisData, GeneratedSecrets)
mkGenesis GenesisParameters
params
  (ByronGenesisError -> ByronClientCmdError)
-> ExceptT ByronGenesisError IO ()
-> ExceptT ByronClientCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT ByronGenesisError -> ByronClientCmdError
ByronCmdGenesisError (ExceptT ByronGenesisError IO ()
 -> ExceptT ByronClientCmdError IO ())
-> ExceptT ByronGenesisError IO ()
-> ExceptT ByronClientCmdError IO ()
forall a b. (a -> b) -> a -> b
$ NewDirectory
-> GenesisData
-> GeneratedSecrets
-> ExceptT ByronGenesisError IO ()
dumpGenesis NewDirectory
outDir GenesisData
genData GeneratedSecrets
genSecrets

runValidateCBOR :: CBORObject -> FilePath -> ExceptT ByronClientCmdError IO ()
runValidateCBOR :: CBORObject -> String -> ExceptT ByronClientCmdError IO ()
runValidateCBOR CBORObject
cborObject String
fp = do
  LByteString
bs <- (HelpersError -> ByronClientCmdError)
-> ExceptT HelpersError IO LByteString
-> ExceptT ByronClientCmdError IO LByteString
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT HelpersError -> ByronClientCmdError
ByronCmdHelpersError (ExceptT HelpersError IO LByteString
 -> ExceptT ByronClientCmdError IO LByteString)
-> ExceptT HelpersError IO LByteString
-> ExceptT ByronClientCmdError IO LByteString
forall a b. (a -> b) -> a -> b
$ String -> ExceptT HelpersError IO LByteString
readCBOR String
fp
  Text
res <- Either ByronClientCmdError Text
-> ExceptT ByronClientCmdError IO Text
forall (m :: * -> *) x a. Monad m => Either x a -> ExceptT x m a
hoistEither (Either ByronClientCmdError Text
 -> ExceptT ByronClientCmdError IO Text)
-> (Either HelpersError Text -> Either ByronClientCmdError Text)
-> Either HelpersError Text
-> ExceptT ByronClientCmdError IO Text
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (HelpersError -> ByronClientCmdError)
-> Either HelpersError Text -> Either ByronClientCmdError Text
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first HelpersError -> ByronClientCmdError
ByronCmdHelpersError (Either HelpersError Text -> ExceptT ByronClientCmdError IO Text)
-> Either HelpersError Text -> ExceptT ByronClientCmdError IO Text
forall a b. (a -> b) -> a -> b
$ CBORObject -> LByteString -> Either HelpersError Text
validateCBOR CBORObject
cborObject LByteString
bs
  IO () -> ExceptT ByronClientCmdError IO ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ExceptT ByronClientCmdError IO ())
-> IO () -> ExceptT ByronClientCmdError IO ()
forall a b. (a -> b) -> a -> b
$ Text -> IO ()
putTextLn Text
res

runPrettyPrintCBOR :: FilePath -> ExceptT ByronClientCmdError IO ()
runPrettyPrintCBOR :: String -> ExceptT ByronClientCmdError IO ()
runPrettyPrintCBOR String
fp = do
  LByteString
bs <- (HelpersError -> ByronClientCmdError)
-> ExceptT HelpersError IO LByteString
-> ExceptT ByronClientCmdError IO LByteString
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT HelpersError -> ByronClientCmdError
ByronCmdHelpersError (ExceptT HelpersError IO LByteString
 -> ExceptT ByronClientCmdError IO LByteString)
-> ExceptT HelpersError IO LByteString
-> ExceptT ByronClientCmdError IO LByteString
forall a b. (a -> b) -> a -> b
$ String -> ExceptT HelpersError IO LByteString
readCBOR String
fp
  (HelpersError -> ByronClientCmdError)
-> ExceptT HelpersError IO () -> ExceptT ByronClientCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT HelpersError -> ByronClientCmdError
ByronCmdHelpersError (ExceptT HelpersError IO () -> ExceptT ByronClientCmdError IO ())
-> ExceptT HelpersError IO () -> ExceptT ByronClientCmdError IO ()
forall a b. (a -> b) -> a -> b
$ LByteString -> ExceptT HelpersError IO ()
pPrintCBOR LByteString
bs

runPrettySigningKeyPublic :: ByronKeyFormat -> SigningKeyFile -> ExceptT ByronClientCmdError IO ()
runPrettySigningKeyPublic :: ByronKeyFormat
-> SigningKeyFile -> ExceptT ByronClientCmdError IO ()
runPrettySigningKeyPublic ByronKeyFormat
bKeyFormat SigningKeyFile
skF = do
  SomeByronSigningKey
sK <- (ByronKeyFailure -> ByronClientCmdError)
-> ExceptT ByronKeyFailure IO SomeByronSigningKey
-> ExceptT ByronClientCmdError IO SomeByronSigningKey
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT ByronKeyFailure -> ByronClientCmdError
ByronCmdKeyFailure (ExceptT ByronKeyFailure IO SomeByronSigningKey
 -> ExceptT ByronClientCmdError IO SomeByronSigningKey)
-> ExceptT ByronKeyFailure IO SomeByronSigningKey
-> ExceptT ByronClientCmdError IO SomeByronSigningKey
forall a b. (a -> b) -> a -> b
$ ByronKeyFormat
-> SigningKeyFile -> ExceptT ByronKeyFailure IO SomeByronSigningKey
readByronSigningKey ByronKeyFormat
bKeyFormat SigningKeyFile
skF
  IO () -> ExceptT ByronClientCmdError IO ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ExceptT ByronClientCmdError IO ())
-> (VerificationKey ByronKey -> IO ())
-> VerificationKey ByronKey
-> ExceptT ByronClientCmdError IO ()
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Text -> IO ()
putTextLn (Text -> IO ())
-> (VerificationKey ByronKey -> Text)
-> VerificationKey ByronKey
-> IO ()
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. VerificationKey ByronKey -> Text
prettyPublicKey (VerificationKey ByronKey -> ExceptT ByronClientCmdError IO ())
-> VerificationKey ByronKey -> ExceptT ByronClientCmdError IO ()
forall a b. (a -> b) -> a -> b
$ SomeByronSigningKey -> VerificationKey ByronKey
byronWitnessToVerKey SomeByronSigningKey
sK

runMigrateDelegateKeyFrom
  :: SigningKeyFile
  -- ^ Legacy Byron signing key
  -> NewSigningKeyFile
  -> ExceptT ByronClientCmdError IO ()
runMigrateDelegateKeyFrom :: SigningKeyFile
-> NewSigningKeyFile -> ExceptT ByronClientCmdError IO ()
runMigrateDelegateKeyFrom oldKey :: SigningKeyFile
oldKey@(SigningKeyFile String
fp) (NewSigningKeyFile String
newKey) = do
  SomeByronSigningKey
sk <- (ByronKeyFailure -> ByronClientCmdError)
-> ExceptT ByronKeyFailure IO SomeByronSigningKey
-> ExceptT ByronClientCmdError IO SomeByronSigningKey
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT ByronKeyFailure -> ByronClientCmdError
ByronCmdKeyFailure (ExceptT ByronKeyFailure IO SomeByronSigningKey
 -> ExceptT ByronClientCmdError IO SomeByronSigningKey)
-> ExceptT ByronKeyFailure IO SomeByronSigningKey
-> ExceptT ByronClientCmdError IO SomeByronSigningKey
forall a b. (a -> b) -> a -> b
$ ByronKeyFormat
-> SigningKeyFile -> ExceptT ByronKeyFailure IO SomeByronSigningKey
readByronSigningKey ByronKeyFormat
LegacyByronKeyFormat SigningKeyFile
oldKey
  SomeByronSigningKey
migratedWitness <- case SomeByronSigningKey
sk of
                       AByronSigningKeyLegacy (ByronSigningKeyLegacy sKey) ->
                         SomeByronSigningKey
-> ExceptT ByronClientCmdError IO SomeByronSigningKey
forall (m :: * -> *) a. Monad m => a -> m a
return (SomeByronSigningKey
 -> ExceptT ByronClientCmdError IO SomeByronSigningKey)
-> (SigningKey ByronKey -> SomeByronSigningKey)
-> SigningKey ByronKey
-> ExceptT ByronClientCmdError IO SomeByronSigningKey
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. SigningKey ByronKey -> SomeByronSigningKey
AByronSigningKey (SigningKey ByronKey
 -> ExceptT ByronClientCmdError IO SomeByronSigningKey)
-> SigningKey ByronKey
-> ExceptT ByronClientCmdError IO SomeByronSigningKey
forall a b. (a -> b) -> a -> b
$ SigningKey -> SigningKey ByronKey
ByronSigningKey SigningKey
sKey
                       AByronSigningKey SigningKey ByronKey
_ ->
                         ByronClientCmdError
-> ExceptT ByronClientCmdError IO SomeByronSigningKey
forall (m :: * -> *) x a. Monad m => x -> ExceptT x m a
left (ByronClientCmdError
 -> ExceptT ByronClientCmdError IO SomeByronSigningKey)
-> (ByronKeyFailure -> ByronClientCmdError)
-> ByronKeyFailure
-> ExceptT ByronClientCmdError IO SomeByronSigningKey
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ByronKeyFailure -> ByronClientCmdError
ByronCmdKeyFailure (ByronKeyFailure
 -> ExceptT ByronClientCmdError IO SomeByronSigningKey)
-> ByronKeyFailure
-> ExceptT ByronClientCmdError IO SomeByronSigningKey
forall a b. (a -> b) -> a -> b
$ String -> ByronKeyFailure
CannotMigrateFromNonLegacySigningKey String
fp
  (HelpersError -> ByronClientCmdError)
-> ExceptT HelpersError IO () -> ExceptT ByronClientCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT HelpersError -> ByronClientCmdError
ByronCmdHelpersError (ExceptT HelpersError IO () -> ExceptT ByronClientCmdError IO ())
-> (ByteString -> ExceptT HelpersError IO ())
-> ByteString
-> ExceptT ByronClientCmdError IO ()
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. String -> ByteString -> ExceptT HelpersError IO ()
ensureNewFileLBS String
newKey (ByteString -> ExceptT ByronClientCmdError IO ())
-> ByteString -> ExceptT ByronClientCmdError IO ()
forall a b. (a -> b) -> a -> b
$ SomeByronSigningKey -> ByteString
serialiseByronWitness SomeByronSigningKey
migratedWitness

runPrintGenesisHash :: GenesisFile -> ExceptT ByronClientCmdError IO ()
runPrintGenesisHash :: GenesisFile -> ExceptT ByronClientCmdError IO ()
runPrintGenesisHash GenesisFile
genFp = do
    Config
genesis <- (ByronGenesisError -> ByronClientCmdError)
-> ExceptT ByronGenesisError IO Config
-> ExceptT ByronClientCmdError IO Config
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT ByronGenesisError -> ByronClientCmdError
ByronCmdGenesisError (ExceptT ByronGenesisError IO Config
 -> ExceptT ByronClientCmdError IO Config)
-> ExceptT ByronGenesisError IO Config
-> ExceptT ByronClientCmdError IO Config
forall a b. (a -> b) -> a -> b
$
                 GenesisFile -> NetworkId -> ExceptT ByronGenesisError IO Config
readGenesis GenesisFile
genFp NetworkId
dummyNetwork
    IO () -> ExceptT ByronClientCmdError IO ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ExceptT ByronClientCmdError IO ())
-> (Text -> IO ()) -> Text -> ExceptT ByronClientCmdError IO ()
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Text -> IO ()
putTextLn (Text -> ExceptT ByronClientCmdError IO ())
-> Text -> ExceptT ByronClientCmdError IO ()
forall a b. (a -> b) -> a -> b
$ Config -> Text
formatter Config
genesis
  where
    -- For this purpose of getting the hash, it does not matter what network
    -- value we use here.
    dummyNetwork :: NetworkId
    dummyNetwork :: NetworkId
dummyNetwork = NetworkId
Mainnet

    formatter :: Genesis.Config -> Text
    formatter :: Config -> Text
formatter = Format Text (AbstractHash Blake2b_256 Raw -> Text)
-> AbstractHash Blake2b_256 Raw -> Text
forall a. Format Text a -> a
F.sformat Format Text (AbstractHash Blake2b_256 Raw -> Text)
forall r algo a. Format r (AbstractHash algo a -> r)
Crypto.hashHexF
              (AbstractHash Blake2b_256 Raw -> Text)
-> (Config -> AbstractHash Blake2b_256 Raw) -> Config -> Text
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. GenesisHash -> AbstractHash Blake2b_256 Raw
Genesis.unGenesisHash
              (GenesisHash -> AbstractHash Blake2b_256 Raw)
-> (Config -> GenesisHash)
-> Config
-> AbstractHash Blake2b_256 Raw
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Config -> GenesisHash
Genesis.configGenesisHash

runPrintSigningKeyAddress
  :: ByronKeyFormat
  -> NetworkId
  -> SigningKeyFile
  -> ExceptT ByronClientCmdError IO ()
runPrintSigningKeyAddress :: ByronKeyFormat
-> NetworkId -> SigningKeyFile -> ExceptT ByronClientCmdError IO ()
runPrintSigningKeyAddress ByronKeyFormat
bKeyFormat NetworkId
networkid SigningKeyFile
skF = do
  SomeByronSigningKey
sK <- (ByronKeyFailure -> ByronClientCmdError)
-> ExceptT ByronKeyFailure IO SomeByronSigningKey
-> ExceptT ByronClientCmdError IO SomeByronSigningKey
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT ByronKeyFailure -> ByronClientCmdError
ByronCmdKeyFailure (ExceptT ByronKeyFailure IO SomeByronSigningKey
 -> ExceptT ByronClientCmdError IO SomeByronSigningKey)
-> ExceptT ByronKeyFailure IO SomeByronSigningKey
-> ExceptT ByronClientCmdError IO SomeByronSigningKey
forall a b. (a -> b) -> a -> b
$ ByronKeyFormat
-> SigningKeyFile -> ExceptT ByronKeyFailure IO SomeByronSigningKey
readByronSigningKey ByronKeyFormat
bKeyFormat SigningKeyFile
skF
  let sKeyAddr :: Text
sKeyAddr = Address ByronAddr -> Text
prettyAddress (Address ByronAddr -> Text)
-> (VerificationKey ByronKey -> Address ByronAddr)
-> VerificationKey ByronKey
-> Text
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. NetworkId -> VerificationKey ByronKey -> Address ByronAddr
makeByronAddress NetworkId
networkid (VerificationKey ByronKey -> Text)
-> VerificationKey ByronKey -> Text
forall a b. (a -> b) -> a -> b
$ SomeByronSigningKey -> VerificationKey ByronKey
byronWitnessToVerKey SomeByronSigningKey
sK
  IO () -> ExceptT ByronClientCmdError IO ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ExceptT ByronClientCmdError IO ())
-> IO () -> ExceptT ByronClientCmdError IO ()
forall a b. (a -> b) -> a -> b
$ Text -> IO ()
putTextLn Text
sKeyAddr

runKeygen :: NewSigningKeyFile -> ExceptT ByronClientCmdError IO ()
runKeygen :: NewSigningKeyFile -> ExceptT ByronClientCmdError IO ()
runKeygen (NewSigningKeyFile String
skF)  = do
  SigningKey ByronKey
sK <- IO (SigningKey ByronKey)
-> ExceptT ByronClientCmdError IO (SigningKey ByronKey)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (SigningKey ByronKey)
 -> ExceptT ByronClientCmdError IO (SigningKey ByronKey))
-> IO (SigningKey ByronKey)
-> ExceptT ByronClientCmdError IO (SigningKey ByronKey)
forall a b. (a -> b) -> a -> b
$ AsType ByronKey -> IO (SigningKey ByronKey)
forall keyrole.
Key keyrole =>
AsType keyrole -> IO (SigningKey keyrole)
generateSigningKey AsType ByronKey
AsByronKey
  (HelpersError -> ByronClientCmdError)
-> ExceptT HelpersError IO () -> ExceptT ByronClientCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT HelpersError -> ByronClientCmdError
ByronCmdHelpersError (ExceptT HelpersError IO () -> ExceptT ByronClientCmdError IO ())
-> (ByteString -> ExceptT HelpersError IO ())
-> ByteString
-> ExceptT ByronClientCmdError IO ()
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. String -> ByteString -> ExceptT HelpersError IO ()
ensureNewFileLBS String
skF (ByteString -> ExceptT ByronClientCmdError IO ())
-> ByteString -> ExceptT ByronClientCmdError IO ()
forall a b. (a -> b) -> a -> b
$ SigningKey ByronKey -> ByteString
forall a. SerialiseAsRawBytes a => a -> ByteString
serialiseToRawBytes SigningKey ByronKey
sK

runToVerification :: ByronKeyFormat -> SigningKeyFile -> NewVerificationKeyFile -> ExceptT ByronClientCmdError IO ()
runToVerification :: ByronKeyFormat
-> SigningKeyFile
-> NewVerificationKeyFile
-> ExceptT ByronClientCmdError IO ()
runToVerification ByronKeyFormat
bKeyFormat SigningKeyFile
skFp (NewVerificationKeyFile String
vkFp) = do
  SomeByronSigningKey
sk <- (ByronKeyFailure -> ByronClientCmdError)
-> ExceptT ByronKeyFailure IO SomeByronSigningKey
-> ExceptT ByronClientCmdError IO SomeByronSigningKey
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT ByronKeyFailure -> ByronClientCmdError
ByronCmdKeyFailure (ExceptT ByronKeyFailure IO SomeByronSigningKey
 -> ExceptT ByronClientCmdError IO SomeByronSigningKey)
-> ExceptT ByronKeyFailure IO SomeByronSigningKey
-> ExceptT ByronClientCmdError IO SomeByronSigningKey
forall a b. (a -> b) -> a -> b
$ ByronKeyFormat
-> SigningKeyFile -> ExceptT ByronKeyFailure IO SomeByronSigningKey
readByronSigningKey ByronKeyFormat
bKeyFormat SigningKeyFile
skFp
  let ByronVerificationKey vK = SomeByronSigningKey -> VerificationKey ByronKey
byronWitnessToVerKey SomeByronSigningKey
sk
  let vKey :: Text
vKey = Builder -> Text
Builder.toLazyText (Builder -> Text) -> Builder -> Text
forall a b. (a -> b) -> a -> b
$ VerificationKey -> Builder
Crypto.formatFullVerificationKey VerificationKey
vK
  (HelpersError -> ByronClientCmdError)
-> ExceptT HelpersError IO () -> ExceptT ByronClientCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT HelpersError -> ByronClientCmdError
ByronCmdHelpersError (ExceptT HelpersError IO () -> ExceptT ByronClientCmdError IO ())
-> ExceptT HelpersError IO () -> ExceptT ByronClientCmdError IO ()
forall a b. (a -> b) -> a -> b
$ (String -> Text -> IO ())
-> String -> Text -> ExceptT HelpersError IO ()
forall a.
(String -> a -> IO ()) -> String -> a -> ExceptT HelpersError IO ()
ensureNewFile String -> Text -> IO ()
TL.writeFile String
vkFp Text
vKey

runSubmitTx :: NetworkId -> TxFile -> ExceptT ByronClientCmdError IO ()
runSubmitTx :: NetworkId -> TxFile -> ExceptT ByronClientCmdError IO ()
runSubmitTx NetworkId
network TxFile
fp = do
    ATxAux ByteString
tx <- (ByronTxError -> ByronClientCmdError)
-> ExceptT ByronTxError IO (ATxAux ByteString)
-> ExceptT ByronClientCmdError IO (ATxAux ByteString)
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT ByronTxError -> ByronClientCmdError
ByronCmdTxError (ExceptT ByronTxError IO (ATxAux ByteString)
 -> ExceptT ByronClientCmdError IO (ATxAux ByteString))
-> ExceptT ByronTxError IO (ATxAux ByteString)
-> ExceptT ByronClientCmdError IO (ATxAux ByteString)
forall a b. (a -> b) -> a -> b
$ TxFile -> ExceptT ByronTxError IO (ATxAux ByteString)
readByronTx TxFile
fp
    (ByronTxError -> ByronClientCmdError)
-> ExceptT ByronTxError IO () -> ExceptT ByronClientCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT ByronTxError -> ByronClientCmdError
ByronCmdTxError (ExceptT ByronTxError IO () -> ExceptT ByronClientCmdError IO ())
-> ExceptT ByronTxError IO () -> ExceptT ByronClientCmdError IO ()
forall a b. (a -> b) -> a -> b
$
      NetworkId -> GenTx ByronBlock -> ExceptT ByronTxError IO ()
nodeSubmitTx NetworkId
network (ATxAux ByteString -> GenTx ByronBlock
normalByronTxToGenTx ATxAux ByteString
tx)

runGetTxId :: TxFile -> ExceptT ByronClientCmdError IO ()
runGetTxId :: TxFile -> ExceptT ByronClientCmdError IO ()
runGetTxId TxFile
fp = (ByronTxError -> ByronClientCmdError)
-> ExceptT ByronTxError IO () -> ExceptT ByronClientCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT ByronTxError -> ByronClientCmdError
ByronCmdTxError (ExceptT ByronTxError IO () -> ExceptT ByronClientCmdError IO ())
-> ExceptT ByronTxError IO () -> ExceptT ByronClientCmdError IO ()
forall a b. (a -> b) -> a -> b
$ do
    ATxAux ByteString
tx <- TxFile -> ExceptT ByronTxError IO (ATxAux ByteString)
readByronTx TxFile
fp
    let txbody :: TxBody ByronEra
txbody = Tx ByronEra -> TxBody ByronEra
forall era. Tx era -> TxBody era
getTxBody (ATxAux ByteString -> Tx ByronEra
ByronTx ATxAux ByteString
tx)
        txid :: TxId
txid   = TxBody ByronEra -> TxId
forall era. TxBody era -> TxId
getTxId TxBody ByronEra
txbody
    IO () -> ExceptT ByronTxError IO ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ExceptT ByronTxError IO ())
-> IO () -> ExceptT ByronTxError IO ()
forall a b. (a -> b) -> a -> b
$ ByteString -> IO ()
BS.putStrLn (ByteString -> IO ()) -> ByteString -> IO ()
forall a b. (a -> b) -> a -> b
$ TxId -> ByteString
forall a. SerialiseAsRawBytes a => a -> ByteString
serialiseToRawBytesHex TxId
txid

runSpendGenesisUTxO
  :: GenesisFile
  -> NetworkId
  -> ByronKeyFormat
  -> NewTxFile
  -> SigningKeyFile
  -> Address ByronAddr
  -> [TxOut ByronEra]
  -> ExceptT ByronClientCmdError IO ()
runSpendGenesisUTxO :: GenesisFile
-> NetworkId
-> ByronKeyFormat
-> NewTxFile
-> SigningKeyFile
-> Address ByronAddr
-> [TxOut ByronEra]
-> ExceptT ByronClientCmdError IO ()
runSpendGenesisUTxO GenesisFile
genesisFile NetworkId
nw ByronKeyFormat
bKeyFormat (NewTxFile String
ctTx) SigningKeyFile
ctKey Address ByronAddr
genRichAddr [TxOut ByronEra]
outs = do
    Config
genesis <- (ByronGenesisError -> ByronClientCmdError)
-> ExceptT ByronGenesisError IO Config
-> ExceptT ByronClientCmdError IO Config
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT ByronGenesisError -> ByronClientCmdError
ByronCmdGenesisError (ExceptT ByronGenesisError IO Config
 -> ExceptT ByronClientCmdError IO Config)
-> ExceptT ByronGenesisError IO Config
-> ExceptT ByronClientCmdError IO Config
forall a b. (a -> b) -> a -> b
$ GenesisFile -> NetworkId -> ExceptT ByronGenesisError IO Config
readGenesis GenesisFile
genesisFile NetworkId
nw
    SomeByronSigningKey
sk <- (ByronKeyFailure -> ByronClientCmdError)
-> ExceptT ByronKeyFailure IO SomeByronSigningKey
-> ExceptT ByronClientCmdError IO SomeByronSigningKey
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT ByronKeyFailure -> ByronClientCmdError
ByronCmdKeyFailure (ExceptT ByronKeyFailure IO SomeByronSigningKey
 -> ExceptT ByronClientCmdError IO SomeByronSigningKey)
-> ExceptT ByronKeyFailure IO SomeByronSigningKey
-> ExceptT ByronClientCmdError IO SomeByronSigningKey
forall a b. (a -> b) -> a -> b
$ ByronKeyFormat
-> SigningKeyFile -> ExceptT ByronKeyFailure IO SomeByronSigningKey
readByronSigningKey ByronKeyFormat
bKeyFormat SigningKeyFile
ctKey

    let tx :: Tx ByronEra
tx = Config
-> NetworkId
-> SomeByronSigningKey
-> Address ByronAddr
-> [TxOut ByronEra]
-> Tx ByronEra
txSpendGenesisUTxOByronPBFT Config
genesis NetworkId
nw SomeByronSigningKey
sk Address ByronAddr
genRichAddr [TxOut ByronEra]
outs
    (HelpersError -> ByronClientCmdError)
-> ExceptT HelpersError IO () -> ExceptT ByronClientCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT HelpersError -> ByronClientCmdError
ByronCmdHelpersError (ExceptT HelpersError IO () -> ExceptT ByronClientCmdError IO ())
-> (ByteString -> ExceptT HelpersError IO ())
-> ByteString
-> ExceptT ByronClientCmdError IO ()
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. String -> ByteString -> ExceptT HelpersError IO ()
ensureNewFileLBS String
ctTx (ByteString -> ExceptT ByronClientCmdError IO ())
-> ByteString -> ExceptT ByronClientCmdError IO ()
forall a b. (a -> b) -> a -> b
$ Tx ByronEra -> ByteString
forall a. SerialiseAsCBOR a => a -> ByteString
serialiseToCBOR Tx ByronEra
tx

runSpendUTxO
  :: NetworkId
  -> ByronKeyFormat
  -> NewTxFile
  -> SigningKeyFile
  -> [TxIn]
  -> [TxOut ByronEra]
  -> ExceptT ByronClientCmdError IO ()
runSpendUTxO :: NetworkId
-> ByronKeyFormat
-> NewTxFile
-> SigningKeyFile
-> [TxIn]
-> [TxOut ByronEra]
-> ExceptT ByronClientCmdError IO ()
runSpendUTxO NetworkId
nw ByronKeyFormat
bKeyFormat (NewTxFile String
ctTx) SigningKeyFile
ctKey [TxIn]
ins [TxOut ByronEra]
outs = do
    SomeByronSigningKey
sk <- (ByronKeyFailure -> ByronClientCmdError)
-> ExceptT ByronKeyFailure IO SomeByronSigningKey
-> ExceptT ByronClientCmdError IO SomeByronSigningKey
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT ByronKeyFailure -> ByronClientCmdError
ByronCmdKeyFailure (ExceptT ByronKeyFailure IO SomeByronSigningKey
 -> ExceptT ByronClientCmdError IO SomeByronSigningKey)
-> ExceptT ByronKeyFailure IO SomeByronSigningKey
-> ExceptT ByronClientCmdError IO SomeByronSigningKey
forall a b. (a -> b) -> a -> b
$ ByronKeyFormat
-> SigningKeyFile -> ExceptT ByronKeyFailure IO SomeByronSigningKey
readByronSigningKey ByronKeyFormat
bKeyFormat SigningKeyFile
ctKey

    let gTx :: Tx ByronEra
gTx = NetworkId
-> SomeByronSigningKey -> [TxIn] -> [TxOut ByronEra] -> Tx ByronEra
txSpendUTxOByronPBFT NetworkId
nw SomeByronSigningKey
sk [TxIn]
ins [TxOut ByronEra]
outs
    (HelpersError -> ByronClientCmdError)
-> ExceptT HelpersError IO () -> ExceptT ByronClientCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT HelpersError -> ByronClientCmdError
ByronCmdHelpersError (ExceptT HelpersError IO () -> ExceptT ByronClientCmdError IO ())
-> (ByteString -> ExceptT HelpersError IO ())
-> ByteString
-> ExceptT ByronClientCmdError IO ()
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. String -> ByteString -> ExceptT HelpersError IO ()
ensureNewFileLBS String
ctTx (ByteString -> ExceptT ByronClientCmdError IO ())
-> ByteString -> ExceptT ByronClientCmdError IO ()
forall a b. (a -> b) -> a -> b
$ Tx ByronEra -> ByteString
forall a. SerialiseAsCBOR a => a -> ByteString
serialiseToCBOR Tx ByronEra
gTx