{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RankNTypes #-}

{- HLINT ignore "Use newtype instead of data" -}

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

import Cardano.Api hiding (GenesisParameters, UpdateProposal, txId)
import Cardano.Api.Byron
  ( SigningKey (ByronSigningKey, ByronSigningKeyLegacy)
  , SomeByronSigningKey (..)
  , VerificationKey (ByronVerificationKey)
  )
import Cardano.Api.Byron qualified as Byron

import Cardano.CLI.Byron.Command
import Cardano.CLI.Byron.Delegation
import Cardano.CLI.Byron.Genesis
import Cardano.CLI.Byron.Key
import Cardano.CLI.Byron.Tx
import Cardano.CLI.Byron.UpdateProposal
import Cardano.CLI.Byron.Vote
import Cardano.CLI.Compatible.Exception
import Cardano.CLI.Helper
import Cardano.CLI.Type.Common
import Cardano.Crypto.Hashing qualified as Crypto
import Cardano.Crypto.Signing qualified as Crypto

import Data.ByteString.Char8 qualified as BS
import Data.Text (Text)
import Data.Text.IO qualified as Text
import Data.Text.Lazy.Builder qualified as Builder
import Data.Text.Lazy.IO qualified as TL
import Formatting qualified as F

-- | Data type that encompasses all the possible errors of the
-- Byron client.
data ByronClientCmdError
  = ByronCmdKeyFailure !ByronKeyFailure
  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
$cshowsPrec :: Int -> ByronClientCmdError -> ShowS
showsPrec :: Int -> ByronClientCmdError -> ShowS
$cshow :: ByronClientCmdError -> String
show :: ByronClientCmdError -> String
$cshowList :: [ByronClientCmdError] -> ShowS
showList :: [ByronClientCmdError] -> ShowS
Show

instance Error ByronClientCmdError where
  prettyError :: forall ann. ByronClientCmdError -> Doc ann
prettyError = ByronClientCmdError -> Doc ann
forall ann. ByronClientCmdError -> Doc ann
renderByronClientCmdError

renderByronClientCmdError :: ByronClientCmdError -> Doc ann
renderByronClientCmdError :: forall ann. ByronClientCmdError -> Doc ann
renderByronClientCmdError = \case
  ByronCmdKeyFailure ByronKeyFailure
e -> ByronKeyFailure -> Doc ann
forall ann. ByronKeyFailure -> Doc ann
renderByronKeyFailure ByronKeyFailure
e

runByronClientCommand :: ByronCommand -> CIO e ()
runByronClientCommand :: forall e. ByronCommand -> CIO e ()
runByronClientCommand ByronCommand
c =
  case ByronCommand
c of
    NodeCmds NodeCmds
bc -> NodeCmds -> CIO e ()
forall e. NodeCmds -> CIO e ()
runNodeCmds NodeCmds
bc
    Genesis NewDirectory
outDir GenesisParameters
params -> NewDirectory -> GenesisParameters -> CIO e ()
forall e. NewDirectory -> GenesisParameters -> CIO e ()
runGenesisCommand NewDirectory
outDir GenesisParameters
params
    ValidateCBOR CBORObject
cborObject String
fp -> CBORObject -> String -> CIO e ()
forall e. CBORObject -> String -> CIO e ()
runValidateCBOR CBORObject
cborObject String
fp
    PrettyPrintCBOR String
fp -> String -> CIO e ()
forall e. String -> CIO e ()
runPrettyPrintCBOR String
fp
    PrettySigningKeyPublic ByronKeyFormat
bKeyFormat SigningKeyFile 'In
skF -> ByronKeyFormat -> SigningKeyFile 'In -> CIO e ()
forall e. ByronKeyFormat -> SigningKeyFile 'In -> CIO e ()
runPrettySigningKeyPublic ByronKeyFormat
bKeyFormat SigningKeyFile 'In
skF
    MigrateDelegateKeyFrom SigningKeyFile 'In
oldKey NewSigningKeyFile
nskf ->
      SigningKeyFile 'In -> NewSigningKeyFile -> CIO e ()
forall e. SigningKeyFile 'In -> NewSigningKeyFile -> CIO e ()
runMigrateDelegateKeyFrom SigningKeyFile 'In
oldKey NewSigningKeyFile
nskf
    PrintGenesisHash GenesisFile
genFp -> GenesisFile -> CIO e ()
forall e. GenesisFile -> CIO e ()
runPrintGenesisHash GenesisFile
genFp
    PrintSigningKeyAddress ByronKeyFormat
bKeyFormat NetworkId
networkid SigningKeyFile 'In
skF -> ByronKeyFormat -> NetworkId -> SigningKeyFile 'In -> CIO e ()
forall e.
ByronKeyFormat -> NetworkId -> SigningKeyFile 'In -> CIO e ()
runPrintSigningKeyAddress ByronKeyFormat
bKeyFormat NetworkId
networkid SigningKeyFile 'In
skF
    Keygen NewSigningKeyFile
nskf -> NewSigningKeyFile -> CIO e ()
forall e. NewSigningKeyFile -> CIO e ()
runKeygen NewSigningKeyFile
nskf
    ToVerification ByronKeyFormat
bKeyFormat SigningKeyFile 'In
skFp NewVerificationKeyFile
nvkFp -> ByronKeyFormat
-> SigningKeyFile 'In -> NewVerificationKeyFile -> CIO e ()
forall e.
ByronKeyFormat
-> SigningKeyFile 'In -> NewVerificationKeyFile -> CIO e ()
runToVerification ByronKeyFormat
bKeyFormat SigningKeyFile 'In
skFp NewVerificationKeyFile
nvkFp
    SubmitTx SocketPath
socketPath NetworkId
network TxFile 'In
fp -> SocketPath -> NetworkId -> TxFile 'In -> CIO e ()
forall e. SocketPath -> NetworkId -> TxFile 'In -> CIO e ()
runSubmitTx SocketPath
socketPath NetworkId
network TxFile 'In
fp
    GetTxId TxFile 'In
fp -> TxFile 'In -> CIO e ()
forall e. TxFile 'In -> CIO e ()
runGetTxId TxFile 'In
fp
    SpendGenesisUTxO GenesisFile
genFp NetworkId
nw ByronKeyFormat
era NewTxFile
nftx SigningKeyFile 'In
ctKey Address ByronAddr
genRichAddr [TxOut CtxTx ByronEra]
outs ->
      GenesisFile
-> NetworkId
-> ByronKeyFormat
-> NewTxFile
-> SigningKeyFile 'In
-> Address ByronAddr
-> [TxOut CtxTx ByronEra]
-> CIO e ()
forall e.
GenesisFile
-> NetworkId
-> ByronKeyFormat
-> NewTxFile
-> SigningKeyFile 'In
-> Address ByronAddr
-> [TxOut CtxTx ByronEra]
-> CIO e ()
runSpendGenesisUTxO GenesisFile
genFp NetworkId
nw ByronKeyFormat
era NewTxFile
nftx SigningKeyFile 'In
ctKey Address ByronAddr
genRichAddr [TxOut CtxTx ByronEra]
outs
    SpendUTxO NetworkId
nw ByronKeyFormat
era NewTxFile
nftx SigningKeyFile 'In
ctKey [TxIn]
ins [TxOut CtxTx ByronEra]
outs ->
      NetworkId
-> ByronKeyFormat
-> NewTxFile
-> SigningKeyFile 'In
-> [TxIn]
-> [TxOut CtxTx ByronEra]
-> CIO e ()
forall e.
NetworkId
-> ByronKeyFormat
-> NewTxFile
-> SigningKeyFile 'In
-> [TxIn]
-> [TxOut CtxTx ByronEra]
-> CIO e ()
runSpendUTxO NetworkId
nw ByronKeyFormat
era NewTxFile
nftx SigningKeyFile 'In
ctKey [TxIn]
ins [TxOut CtxTx ByronEra]
outs

runNodeCmds :: NodeCmds -> CIO e ()
runNodeCmds :: forall e. NodeCmds -> CIO e ()
runNodeCmds (CreateVote NetworkId
nw SigningKeyFile 'In
sKey String
upPropFp Bool
voteBool String
outputFp) =
  NetworkId
-> SigningKeyFile 'In -> String -> Bool -> String -> CIO e ()
forall e.
NetworkId
-> SigningKeyFile 'In -> String -> Bool -> String -> CIO e ()
runVoteCreation NetworkId
nw SigningKeyFile 'In
sKey String
upPropFp Bool
voteBool String
outputFp
runNodeCmds (SubmitUpdateProposal SocketPath
nodeSocketPath NetworkId
network String
proposalFp) = do
  SocketPath -> NetworkId -> String -> CIO e ()
forall e. SocketPath -> NetworkId -> String -> CIO e ()
submitByronUpdateProposal SocketPath
nodeSocketPath NetworkId
network String
proposalFp
runNodeCmds (SubmitVote SocketPath
nodeSocketPath NetworkId
network String
voteFp) = do
  SocketPath -> NetworkId -> String -> CIO e ()
forall e. SocketPath -> NetworkId -> String -> CIO e ()
submitByronVote SocketPath
nodeSocketPath NetworkId
network String
voteFp
runNodeCmds (UpdateProposal NetworkId
nw SigningKeyFile 'In
sKey ProtocolVersion
pVer SoftwareVersion
sVer SystemTag
sysTag InstallerHash
insHash String
outputFp ByronProtocolParametersUpdate
params) =
  NetworkId
-> SigningKeyFile 'In
-> ProtocolVersion
-> SoftwareVersion
-> SystemTag
-> InstallerHash
-> String
-> ByronProtocolParametersUpdate
-> CIO e ()
forall e.
NetworkId
-> SigningKeyFile 'In
-> ProtocolVersion
-> SoftwareVersion
-> SystemTag
-> InstallerHash
-> String
-> ByronProtocolParametersUpdate
-> CIO e ()
runProposalCreation NetworkId
nw SigningKeyFile 'In
sKey ProtocolVersion
pVer SoftwareVersion
sVer SystemTag
sysTag InstallerHash
insHash String
outputFp ByronProtocolParametersUpdate
params

runGenesisCommand :: NewDirectory -> GenesisParameters -> CIO e ()
runGenesisCommand :: forall e. NewDirectory -> GenesisParameters -> CIO e ()
runGenesisCommand NewDirectory
outDir GenesisParameters
params = do
  (GenesisData
genData, GeneratedSecrets
genSecrets) <- GenesisParameters -> CIO e (GenesisData, GeneratedSecrets)
forall e.
GenesisParameters -> CIO e (GenesisData, GeneratedSecrets)
mkGenesis GenesisParameters
params
  ExceptT ByronGenesisError IO () -> RIO e ()
forall e (m :: * -> *) a.
(HasCallStack, MonadIO m, Show e, Typeable e, Error e) =>
ExceptT e IO a -> m a
fromExceptTCli (ExceptT ByronGenesisError IO () -> RIO e ())
-> ExceptT ByronGenesisError IO () -> RIO e ()
forall a b. (a -> b) -> a -> b
$ NewDirectory
-> GenesisData
-> GeneratedSecrets
-> ExceptT ByronGenesisError IO ()
dumpGenesis NewDirectory
outDir GenesisData
genData GeneratedSecrets
genSecrets

runValidateCBOR :: CBORObject -> FilePath -> CIO e ()
runValidateCBOR :: forall e. CBORObject -> String -> CIO e ()
runValidateCBOR CBORObject
cborObject String
fp = do
  ByteString
bs <- ExceptT HelpersError IO ByteString -> RIO e ByteString
forall e (m :: * -> *) a.
(HasCallStack, MonadIO m, Show e, Typeable e, Error e) =>
ExceptT e IO a -> m a
fromExceptTCli (ExceptT HelpersError IO ByteString -> RIO e ByteString)
-> ExceptT HelpersError IO ByteString -> RIO e ByteString
forall a b. (a -> b) -> a -> b
$ String -> ExceptT HelpersError IO ByteString
readCBOR String
fp
  Text
res <- Either HelpersError Text -> RIO e Text
forall e (m :: * -> *) a.
(HasCallStack, MonadIO m, Show e, Typeable e, Error e) =>
Either e a -> m a
fromEitherCli (Either HelpersError Text -> RIO e Text)
-> Either HelpersError Text -> RIO e Text
forall a b. (a -> b) -> a -> b
$ CBORObject -> ByteString -> Either HelpersError Text
validateCBOR CBORObject
cborObject ByteString
bs
  IO () -> RIO e ()
forall a. IO a -> RIO e a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> RIO e ()) -> IO () -> RIO e ()
forall a b. (a -> b) -> a -> b
$ Text -> IO ()
Text.putStrLn Text
res

runPrettyPrintCBOR :: FilePath -> CIO e ()
runPrettyPrintCBOR :: forall e. String -> CIO e ()
runPrettyPrintCBOR String
fp = do
  ByteString
bs <- ExceptT HelpersError IO ByteString -> RIO e ByteString
forall e (m :: * -> *) a.
(HasCallStack, MonadIO m, Show e, Typeable e, Error e) =>
ExceptT e IO a -> m a
fromExceptTCli (ExceptT HelpersError IO ByteString -> RIO e ByteString)
-> ExceptT HelpersError IO ByteString -> RIO e ByteString
forall a b. (a -> b) -> a -> b
$ String -> ExceptT HelpersError IO ByteString
readCBOR String
fp
  ExceptT HelpersError IO () -> RIO e ()
forall e (m :: * -> *) a.
(HasCallStack, MonadIO m, Show e, Typeable e, Error e) =>
ExceptT e IO a -> m a
fromExceptTCli (ExceptT HelpersError IO () -> RIO e ())
-> ExceptT HelpersError IO () -> RIO e ()
forall a b. (a -> b) -> a -> b
$ ByteString -> ExceptT HelpersError IO ()
pPrintCBOR ByteString
bs

runPrettySigningKeyPublic
  :: ByronKeyFormat -> SigningKeyFile In -> CIO e ()
runPrettySigningKeyPublic :: forall e. ByronKeyFormat -> SigningKeyFile 'In -> CIO e ()
runPrettySigningKeyPublic ByronKeyFormat
bKeyFormat SigningKeyFile 'In
skF = do
  SomeByronSigningKey
sK <- ExceptT ByronKeyFailure IO SomeByronSigningKey
-> RIO e SomeByronSigningKey
forall e (m :: * -> *) a.
(HasCallStack, MonadIO m, Show e, Typeable e, Error e) =>
ExceptT e IO a -> m a
fromExceptTCli (ExceptT ByronKeyFailure IO SomeByronSigningKey
 -> RIO e SomeByronSigningKey)
-> ExceptT ByronKeyFailure IO SomeByronSigningKey
-> RIO e SomeByronSigningKey
forall a b. (a -> b) -> a -> b
$ ByronKeyFormat
-> SigningKeyFile 'In
-> ExceptT ByronKeyFailure IO SomeByronSigningKey
readByronSigningKey ByronKeyFormat
bKeyFormat SigningKeyFile 'In
skF
  IO () -> RIO e ()
forall a. IO a -> RIO e a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> RIO e ())
-> (VerificationKey ByronKey -> IO ())
-> VerificationKey ByronKey
-> RIO e ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> IO ()
Text.putStrLn (Text -> IO ())
-> (VerificationKey ByronKey -> Text)
-> VerificationKey ByronKey
-> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. VerificationKey ByronKey -> Text
prettyPublicKey (VerificationKey ByronKey -> RIO e ())
-> VerificationKey ByronKey -> RIO e ()
forall a b. (a -> b) -> a -> b
$ SomeByronSigningKey -> VerificationKey ByronKey
byronWitnessToVerKey SomeByronSigningKey
sK

runMigrateDelegateKeyFrom
  :: SigningKeyFile In
  -- ^ Legacy Byron signing key
  -> NewSigningKeyFile
  -> CIO e ()
runMigrateDelegateKeyFrom :: forall e. SigningKeyFile 'In -> NewSigningKeyFile -> CIO e ()
runMigrateDelegateKeyFrom oldKey :: SigningKeyFile 'In
oldKey@(File String
fp) (NewSigningKeyFile String
newKey) = do
  SomeByronSigningKey
sk <- ExceptT ByronKeyFailure IO SomeByronSigningKey
-> RIO e SomeByronSigningKey
forall e (m :: * -> *) a.
(HasCallStack, MonadIO m, Show e, Typeable e, Error e) =>
ExceptT e IO a -> m a
fromExceptTCli (ExceptT ByronKeyFailure IO SomeByronSigningKey
 -> RIO e SomeByronSigningKey)
-> ExceptT ByronKeyFailure IO SomeByronSigningKey
-> RIO e SomeByronSigningKey
forall a b. (a -> b) -> a -> b
$ ByronKeyFormat
-> SigningKeyFile 'In
-> ExceptT ByronKeyFailure IO SomeByronSigningKey
readByronSigningKey ByronKeyFormat
LegacyByronKeyFormat SigningKeyFile 'In
oldKey
  SomeByronSigningKey
migratedWitness <- case SomeByronSigningKey
sk of
    AByronSigningKeyLegacy (ByronSigningKeyLegacy SigningKey
sKey) ->
      SomeByronSigningKey -> RIO e SomeByronSigningKey
forall a. a -> RIO e a
forall (m :: * -> *) a. Monad m => a -> m a
return (SomeByronSigningKey -> RIO e SomeByronSigningKey)
-> (SigningKey ByronKey -> SomeByronSigningKey)
-> SigningKey ByronKey
-> RIO e SomeByronSigningKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SigningKey ByronKey -> SomeByronSigningKey
AByronSigningKey (SigningKey ByronKey -> RIO e SomeByronSigningKey)
-> SigningKey ByronKey -> RIO e SomeByronSigningKey
forall a b. (a -> b) -> a -> b
$ SigningKey -> SigningKey ByronKey
ByronSigningKey SigningKey
sKey
    AByronSigningKey SigningKey ByronKey
_ ->
      ByronClientCmdError -> RIO e SomeByronSigningKey
forall e (m :: * -> *) a.
(HasCallStack, Show e, Typeable e, Error e, MonadIO m) =>
e -> m a
throwCliError (ByronClientCmdError -> RIO e SomeByronSigningKey)
-> (ByronKeyFailure -> ByronClientCmdError)
-> ByronKeyFailure
-> RIO e SomeByronSigningKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByronKeyFailure -> ByronClientCmdError
ByronCmdKeyFailure (ByronKeyFailure -> RIO e SomeByronSigningKey)
-> ByronKeyFailure -> RIO e SomeByronSigningKey
forall a b. (a -> b) -> a -> b
$ String -> ByronKeyFailure
CannotMigrateFromNonLegacySigningKey String
fp
  ExceptT HelpersError IO () -> RIO e ()
forall e (m :: * -> *) a.
(HasCallStack, MonadIO m, Show e, Typeable e, Error e) =>
ExceptT e IO a -> m a
fromExceptTCli (ExceptT HelpersError IO () -> RIO e ())
-> ExceptT HelpersError IO () -> RIO e ()
forall a b. (a -> b) -> a -> b
$ String -> ByteString -> ExceptT HelpersError IO ()
ensureNewFileLBS String
newKey (ByteString -> ExceptT HelpersError IO ())
-> ByteString -> ExceptT HelpersError IO ()
forall a b. (a -> b) -> a -> b
$ SomeByronSigningKey -> ByteString
serialiseByronWitness SomeByronSigningKey
migratedWitness

runPrintGenesisHash :: GenesisFile -> CIO e ()
runPrintGenesisHash :: forall e. GenesisFile -> CIO e ()
runPrintGenesisHash GenesisFile
genFp = do
  Config
genesis <-
    ExceptT ByronGenesisError IO Config -> RIO e Config
forall e (m :: * -> *) a.
(HasCallStack, MonadIO m, Show e, Typeable e, Error e) =>
ExceptT e IO a -> m a
fromExceptTCli (ExceptT ByronGenesisError IO Config -> RIO e Config)
-> ExceptT ByronGenesisError IO Config -> RIO e Config
forall a b. (a -> b) -> a -> b
$ GenesisFile -> NetworkId -> ExceptT ByronGenesisError IO Config
readGenesis GenesisFile
genFp NetworkId
dummyNetwork
  IO () -> RIO e ()
forall a. IO a -> RIO e a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> RIO e ()) -> (Text -> IO ()) -> Text -> RIO e ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> IO ()
Text.putStrLn (Text -> RIO e ()) -> Text -> RIO e ()
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 :: Byron.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 b c a. (b -> c) -> (a -> b) -> a -> c
. GenesisHash -> AbstractHash Blake2b_256 Raw
Byron.unGenesisHash
      (GenesisHash -> AbstractHash Blake2b_256 Raw)
-> (Config -> GenesisHash)
-> Config
-> AbstractHash Blake2b_256 Raw
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Config -> GenesisHash
Byron.configGenesisHash

runPrintSigningKeyAddress
  :: ByronKeyFormat
  -> NetworkId
  -> SigningKeyFile In
  -> CIO e ()
runPrintSigningKeyAddress :: forall e.
ByronKeyFormat -> NetworkId -> SigningKeyFile 'In -> CIO e ()
runPrintSigningKeyAddress ByronKeyFormat
bKeyFormat NetworkId
networkid SigningKeyFile 'In
skF = do
  SomeByronSigningKey
sK <- ExceptT ByronKeyFailure IO SomeByronSigningKey
-> RIO e SomeByronSigningKey
forall e (m :: * -> *) a.
(HasCallStack, MonadIO m, Show e, Typeable e, Error e) =>
ExceptT e IO a -> m a
fromExceptTCli (ExceptT ByronKeyFailure IO SomeByronSigningKey
 -> RIO e SomeByronSigningKey)
-> ExceptT ByronKeyFailure IO SomeByronSigningKey
-> RIO e SomeByronSigningKey
forall a b. (a -> b) -> a -> b
$ ByronKeyFormat
-> SigningKeyFile 'In
-> ExceptT ByronKeyFailure IO SomeByronSigningKey
readByronSigningKey ByronKeyFormat
bKeyFormat SigningKeyFile 'In
skF
  let sKeyAddr :: Text
sKeyAddr = Address ByronAddr -> Text
prettyAddress (Address ByronAddr -> Text)
-> (VerificationKey ByronKey -> Address ByronAddr)
-> VerificationKey ByronKey
-> Text
forall b c a. (b -> c) -> (a -> b) -> 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 () -> RIO e ()
forall a. IO a -> RIO e a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> RIO e ()) -> IO () -> RIO e ()
forall a b. (a -> b) -> a -> b
$ Text -> IO ()
Text.putStrLn Text
sKeyAddr

runKeygen :: NewSigningKeyFile -> CIO e ()
runKeygen :: forall e. NewSigningKeyFile -> CIO e ()
runKeygen (NewSigningKeyFile String
skF) = do
  SigningKey ByronKey
sK <- AsType ByronKey -> RIO e (SigningKey ByronKey)
forall (m :: * -> *) keyrole.
(MonadIO m, Key keyrole) =>
AsType keyrole -> m (SigningKey keyrole)
generateSigningKey AsType ByronKey
AsByronKey
  ExceptT HelpersError IO () -> RIO e ()
forall e (m :: * -> *) a.
(HasCallStack, MonadIO m, Show e, Typeable e, Error e) =>
ExceptT e IO a -> m a
fromExceptTCli (ExceptT HelpersError IO () -> RIO e ())
-> ExceptT HelpersError IO () -> RIO e ()
forall a b. (a -> b) -> a -> b
$ String -> ByteString -> ExceptT HelpersError IO ()
ensureNewFileLBS String
skF (ByteString -> ExceptT HelpersError IO ())
-> ByteString -> ExceptT HelpersError IO ()
forall a b. (a -> b) -> a -> b
$ SigningKey ByronKey -> ByteString
forall a. SerialiseAsRawBytes a => a -> ByteString
serialiseToRawBytes SigningKey ByronKey
sK

runToVerification
  :: ByronKeyFormat -> SigningKeyFile In -> NewVerificationKeyFile -> CIO e ()
runToVerification :: forall e.
ByronKeyFormat
-> SigningKeyFile 'In -> NewVerificationKeyFile -> CIO e ()
runToVerification ByronKeyFormat
bKeyFormat SigningKeyFile 'In
skFp (NewVerificationKeyFile String
vkFp) = do
  SomeByronSigningKey
sk <- ExceptT ByronKeyFailure IO SomeByronSigningKey
-> RIO e SomeByronSigningKey
forall e (m :: * -> *) a.
(HasCallStack, MonadIO m, Show e, Typeable e, Error e) =>
ExceptT e IO a -> m a
fromExceptTCli (ExceptT ByronKeyFailure IO SomeByronSigningKey
 -> RIO e SomeByronSigningKey)
-> ExceptT ByronKeyFailure IO SomeByronSigningKey
-> RIO e SomeByronSigningKey
forall a b. (a -> b) -> a -> b
$ ByronKeyFormat
-> SigningKeyFile 'In
-> ExceptT ByronKeyFailure IO SomeByronSigningKey
readByronSigningKey ByronKeyFormat
bKeyFormat SigningKeyFile 'In
skFp
  let ByronVerificationKey VerificationKey
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
  ExceptT HelpersError IO () -> RIO e ()
forall e (m :: * -> *) a.
(HasCallStack, MonadIO m, Show e, Typeable e, Error e) =>
ExceptT e IO a -> m a
fromExceptTCli (ExceptT HelpersError IO () -> RIO e ())
-> ExceptT HelpersError IO () -> RIO e ()
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 :: SocketPath -> NetworkId -> TxFile In -> CIO e ()
runSubmitTx :: forall e. SocketPath -> NetworkId -> TxFile 'In -> CIO e ()
runSubmitTx SocketPath
nodeSocketPath NetworkId
network TxFile 'In
fp = do
  ATxAux ByteString
tx <- ExceptT ByronTxError IO (ATxAux ByteString)
-> RIO e (ATxAux ByteString)
forall e (m :: * -> *) a.
(HasCallStack, MonadIO m, Show e, Typeable e, Error e) =>
ExceptT e IO a -> m a
fromExceptTCli (ExceptT ByronTxError IO (ATxAux ByteString)
 -> RIO e (ATxAux ByteString))
-> ExceptT ByronTxError IO (ATxAux ByteString)
-> RIO e (ATxAux ByteString)
forall a b. (a -> b) -> a -> b
$ TxFile 'In -> ExceptT ByronTxError IO (ATxAux ByteString)
readByronTx TxFile 'In
fp

  ExceptT ByronTxError IO () -> RIO e ()
forall e (m :: * -> *) a.
(HasCallStack, MonadIO m, Show e, Typeable e, Error e) =>
ExceptT e IO a -> m a
fromExceptTCli (ExceptT ByronTxError IO () -> RIO e ())
-> ExceptT ByronTxError IO () -> RIO e ()
forall a b. (a -> b) -> a -> b
$ SocketPath
-> NetworkId -> GenTx ByronBlock -> ExceptT ByronTxError IO ()
nodeSubmitTx SocketPath
nodeSocketPath NetworkId
network (ATxAux ByteString -> GenTx ByronBlock
normalByronTxToGenTx ATxAux ByteString
tx)

runGetTxId :: TxFile In -> CIO e ()
runGetTxId :: forall e. TxFile 'In -> CIO e ()
runGetTxId TxFile 'In
fp = do
  ATxAux ByteString
tx <- ExceptT ByronTxError IO (ATxAux ByteString)
-> RIO e (ATxAux ByteString)
forall e (m :: * -> *) a.
(HasCallStack, MonadIO m, Show e, Typeable e, Error e) =>
ExceptT e IO a -> m a
fromExceptTCli (ExceptT ByronTxError IO (ATxAux ByteString)
 -> RIO e (ATxAux ByteString))
-> ExceptT ByronTxError IO (ATxAux ByteString)
-> RIO e (ATxAux ByteString)
forall a b. (a -> b) -> a -> b
$ TxFile 'In -> ExceptT ByronTxError IO (ATxAux ByteString)
readByronTx TxFile 'In
fp
  let txId :: TxId
txId = ATxAux ByteString -> TxId
getTxIdByron ATxAux ByteString
tx
  IO () -> RIO e ()
forall a. IO a -> RIO e a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> RIO e ())
-> (ByteString -> IO ()) -> ByteString -> RIO e ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> IO ()
BS.putStrLn (ByteString -> RIO e ()) -> ByteString -> RIO e ()
forall a b. (a -> b) -> a -> b
$ TxId -> ByteString
forall a. SerialiseAsRawBytes a => a -> ByteString
serialiseToRawBytesHex TxId
txId

runSpendGenesisUTxO
  :: GenesisFile
  -> NetworkId
  -> ByronKeyFormat
  -> NewTxFile
  -> SigningKeyFile In
  -> Address ByronAddr
  -> [TxOut CtxTx ByronEra]
  -> CIO e ()
runSpendGenesisUTxO :: forall e.
GenesisFile
-> NetworkId
-> ByronKeyFormat
-> NewTxFile
-> SigningKeyFile 'In
-> Address ByronAddr
-> [TxOut CtxTx ByronEra]
-> CIO e ()
runSpendGenesisUTxO GenesisFile
genesisFile NetworkId
nw ByronKeyFormat
bKeyFormat (NewTxFile String
ctTx) SigningKeyFile 'In
ctKey Address ByronAddr
genRichAddr [TxOut CtxTx ByronEra]
outs = do
  Config
genesis <- ExceptT ByronGenesisError IO Config -> RIO e Config
forall e (m :: * -> *) a.
(HasCallStack, MonadIO m, Show e, Typeable e, Error e) =>
ExceptT e IO a -> m a
fromExceptTCli (ExceptT ByronGenesisError IO Config -> RIO e Config)
-> ExceptT ByronGenesisError IO Config -> RIO e Config
forall a b. (a -> b) -> a -> b
$ GenesisFile -> NetworkId -> ExceptT ByronGenesisError IO Config
readGenesis GenesisFile
genesisFile NetworkId
nw
  SomeByronSigningKey
sk <- ExceptT ByronKeyFailure IO SomeByronSigningKey
-> RIO e SomeByronSigningKey
forall e (m :: * -> *) a.
(HasCallStack, MonadIO m, Show e, Typeable e, Error e) =>
ExceptT e IO a -> m a
fromExceptTCli (ExceptT ByronKeyFailure IO SomeByronSigningKey
 -> RIO e SomeByronSigningKey)
-> ExceptT ByronKeyFailure IO SomeByronSigningKey
-> RIO e SomeByronSigningKey
forall a b. (a -> b) -> a -> b
$ ByronKeyFormat
-> SigningKeyFile 'In
-> ExceptT ByronKeyFailure IO SomeByronSigningKey
readByronSigningKey ByronKeyFormat
bKeyFormat SigningKeyFile 'In
ctKey

  let tx :: ATxAux ByteString
tx = Config
-> NetworkId
-> SomeByronSigningKey
-> Address ByronAddr
-> [TxOut CtxTx ByronEra]
-> ATxAux ByteString
txSpendGenesisUTxOByronPBFT Config
genesis NetworkId
nw SomeByronSigningKey
sk Address ByronAddr
genRichAddr [TxOut CtxTx ByronEra]
outs
  ExceptT HelpersError IO () -> RIO e ()
forall e (m :: * -> *) a.
(HasCallStack, MonadIO m, Show e, Typeable e, Error e) =>
ExceptT e IO a -> m a
fromExceptTCli (ExceptT HelpersError IO () -> RIO e ())
-> ExceptT HelpersError IO () -> RIO e ()
forall a b. (a -> b) -> a -> b
$
    String -> ByteString -> ExceptT HelpersError IO ()
ensureNewFileLBS String
ctTx (ByteString -> ExceptT HelpersError IO ())
-> ByteString -> ExceptT HelpersError IO ()
forall a b. (a -> b) -> a -> b
$
      TextEnvelope -> ByteString
teRawCBOR (TextEnvelope -> ByteString) -> TextEnvelope -> ByteString
forall a b. (a -> b) -> a -> b
$
        ATxAux ByteString -> TextEnvelope
serializeByronTx ATxAux ByteString
tx

-- Construct a Byron era tx
runSpendUTxO
  :: NetworkId
  -> ByronKeyFormat
  -> NewTxFile
  -> SigningKeyFile In
  -> [TxIn]
  -> [TxOut CtxTx ByronEra]
  -> CIO e ()
runSpendUTxO :: forall e.
NetworkId
-> ByronKeyFormat
-> NewTxFile
-> SigningKeyFile 'In
-> [TxIn]
-> [TxOut CtxTx ByronEra]
-> CIO e ()
runSpendUTxO NetworkId
nw ByronKeyFormat
bKeyFormat (NewTxFile String
ctTx) SigningKeyFile 'In
ctKey [TxIn]
ins [TxOut CtxTx ByronEra]
outs = do
  SomeByronSigningKey
sk <- ExceptT ByronKeyFailure IO SomeByronSigningKey
-> RIO e SomeByronSigningKey
forall e (m :: * -> *) a.
(HasCallStack, MonadIO m, Show e, Typeable e, Error e) =>
ExceptT e IO a -> m a
fromExceptTCli (ExceptT ByronKeyFailure IO SomeByronSigningKey
 -> RIO e SomeByronSigningKey)
-> ExceptT ByronKeyFailure IO SomeByronSigningKey
-> RIO e SomeByronSigningKey
forall a b. (a -> b) -> a -> b
$ ByronKeyFormat
-> SigningKeyFile 'In
-> ExceptT ByronKeyFailure IO SomeByronSigningKey
readByronSigningKey ByronKeyFormat
bKeyFormat SigningKeyFile 'In
ctKey

  let gTx :: ATxAux ByteString
gTx = NetworkId
-> SomeByronSigningKey
-> [TxIn]
-> [TxOut CtxTx ByronEra]
-> ATxAux ByteString
txSpendUTxOByronPBFT NetworkId
nw SomeByronSigningKey
sk [TxIn]
ins [TxOut CtxTx ByronEra]
outs
  ExceptT HelpersError IO () -> RIO e ()
forall e (m :: * -> *) a.
(HasCallStack, MonadIO m, Show e, Typeable e, Error e) =>
ExceptT e IO a -> m a
fromExceptTCli (ExceptT HelpersError IO () -> RIO e ())
-> ExceptT HelpersError IO () -> RIO e ()
forall a b. (a -> b) -> a -> b
$
    String -> ByteString -> ExceptT HelpersError IO ()
ensureNewFileLBS String
ctTx (ByteString -> ExceptT HelpersError IO ())
-> ByteString -> ExceptT HelpersError IO ()
forall a b. (a -> b) -> a -> b
$
      TextEnvelope -> ByteString
teRawCBOR (TextEnvelope -> ByteString) -> TextEnvelope -> ByteString
forall a b. (a -> b) -> a -> b
$
        ATxAux ByteString -> TextEnvelope
serializeByronTx ATxAux ByteString
gTx