module Bcc.CLI.Sophie.Run.Node
  ( SophieNodeCmdError(SophieNodeCmdReadFileError)
  , renderSophieNodeCmdError
  , runNodeCmd
  , runNodeIssueOpCert
  , runNodeKeyGenCold
  , runNodeKeyGenKES
  , runNodeKeyGenVRF
  ) where

import           Bcc.Prelude hiding ((<.>))
import           Prelude (id)

import qualified Data.ByteString.Char8 as BS
import           Data.String (fromString)
import qualified Data.Text as Text

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

import           Bcc.Api
import           Bcc.Api.Sophie

import           Bcc.CLI.Sophie.Commands
import           Bcc.CLI.Sophie.Key (InputDecodeError, VerificationKeyOrFile,
                     readSigningKeyFileAnyOf, readVerificationKeyOrFile)
import           Bcc.CLI.Types (SigningKeyFile (..), VerificationKeyFile (..))

{- HLINT ignore "Reduce duplication" -}

data SophieNodeCmdError
  = SophieNodeCmdReadFileError !(FileError TextEnvelopeError)
  | SophieNodeCmdReadKeyFileError !(FileError InputDecodeError)
  | SophieNodeCmdWriteFileError !(FileError ())
  | SophieNodeCmdOperationalCertificateIssueError !OperationalCertIssueError
  | SophieNodeCmdVrfSigningKeyCreationError
      FilePath
      -- ^ Target path
      FilePath
      -- ^ Temp path
  deriving Int -> SophieNodeCmdError -> ShowS
[SophieNodeCmdError] -> ShowS
SophieNodeCmdError -> String
(Int -> SophieNodeCmdError -> ShowS)
-> (SophieNodeCmdError -> String)
-> ([SophieNodeCmdError] -> ShowS)
-> Show SophieNodeCmdError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SophieNodeCmdError] -> ShowS
$cshowList :: [SophieNodeCmdError] -> ShowS
show :: SophieNodeCmdError -> String
$cshow :: SophieNodeCmdError -> String
showsPrec :: Int -> SophieNodeCmdError -> ShowS
$cshowsPrec :: Int -> SophieNodeCmdError -> ShowS
Show

renderSophieNodeCmdError :: SophieNodeCmdError -> Text
renderSophieNodeCmdError :: SophieNodeCmdError -> Text
renderSophieNodeCmdError SophieNodeCmdError
err =
  case SophieNodeCmdError
err of
    SophieNodeCmdVrfSigningKeyCreationError String
targetPath String
tempPath ->
      String -> Text
Text.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ String
"Error creating VRF signing key file. Target path: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
targetPath
      String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
" Temporary path: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
tempPath

    SophieNodeCmdReadFileError FileError TextEnvelopeError
fileErr -> String -> Text
Text.pack (FileError TextEnvelopeError -> String
forall e. Error e => e -> String
displayError FileError TextEnvelopeError
fileErr)

    SophieNodeCmdReadKeyFileError FileError InputDecodeError
fileErr -> String -> Text
Text.pack (FileError InputDecodeError -> String
forall e. Error e => e -> String
displayError FileError InputDecodeError
fileErr)

    SophieNodeCmdWriteFileError FileError ()
fileErr -> String -> Text
Text.pack (FileError () -> String
forall e. Error e => e -> String
displayError FileError ()
fileErr)

    SophieNodeCmdOperationalCertificateIssueError OperationalCertIssueError
issueErr ->
      String -> Text
Text.pack (OperationalCertIssueError -> String
forall e. Error e => e -> String
displayError OperationalCertIssueError
issueErr)


runNodeCmd :: NodeCmd -> ExceptT SophieNodeCmdError IO ()
runNodeCmd :: NodeCmd -> ExceptT SophieNodeCmdError IO ()
runNodeCmd (NodeKeyGenCold VerificationKeyFile
vk SigningKeyFile
sk OpCertCounterFile
ctr) = VerificationKeyFile
-> SigningKeyFile
-> OpCertCounterFile
-> ExceptT SophieNodeCmdError IO ()
runNodeKeyGenCold VerificationKeyFile
vk SigningKeyFile
sk OpCertCounterFile
ctr
runNodeCmd (NodeKeyGenKES  VerificationKeyFile
vk SigningKeyFile
sk)     = VerificationKeyFile
-> SigningKeyFile -> ExceptT SophieNodeCmdError IO ()
runNodeKeyGenKES  VerificationKeyFile
vk SigningKeyFile
sk
runNodeCmd (NodeKeyGenVRF  VerificationKeyFile
vk SigningKeyFile
sk)     = VerificationKeyFile
-> SigningKeyFile -> ExceptT SophieNodeCmdError IO ()
runNodeKeyGenVRF  VerificationKeyFile
vk SigningKeyFile
sk
runNodeCmd (NodeKeyHashVRF VerificationKeyOrFile VrfKey
vk Maybe OutputFile
mOutFp) = VerificationKeyOrFile VrfKey
-> Maybe OutputFile -> ExceptT SophieNodeCmdError IO ()
runNodeKeyHashVRF VerificationKeyOrFile VrfKey
vk Maybe OutputFile
mOutFp
runNodeCmd (NodeNewCounter ColdVerificationKeyOrFile
vk Word
ctr OpCertCounterFile
out) = ColdVerificationKeyOrFile
-> Word -> OpCertCounterFile -> ExceptT SophieNodeCmdError IO ()
runNodeNewCounter ColdVerificationKeyOrFile
vk Word
ctr OpCertCounterFile
out
runNodeCmd (NodeIssueOpCert VerificationKeyOrFile KesKey
vk SigningKeyFile
sk OpCertCounterFile
ctr KESPeriod
p OutputFile
out) =
  VerificationKeyOrFile KesKey
-> SigningKeyFile
-> OpCertCounterFile
-> KESPeriod
-> OutputFile
-> ExceptT SophieNodeCmdError IO ()
runNodeIssueOpCert VerificationKeyOrFile KesKey
vk SigningKeyFile
sk OpCertCounterFile
ctr KESPeriod
p OutputFile
out



--
-- Node command implementations
--

runNodeKeyGenCold :: VerificationKeyFile
                  -> SigningKeyFile
                  -> OpCertCounterFile
                  -> ExceptT SophieNodeCmdError IO ()
runNodeKeyGenCold :: VerificationKeyFile
-> SigningKeyFile
-> OpCertCounterFile
-> ExceptT SophieNodeCmdError IO ()
runNodeKeyGenCold (VerificationKeyFile String
vkeyPath) (SigningKeyFile String
skeyPath)
                  (OpCertCounterFile String
ocertCtrPath) = do
    SigningKey StakePoolKey
skey <- IO (SigningKey StakePoolKey)
-> ExceptT SophieNodeCmdError IO (SigningKey StakePoolKey)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (SigningKey StakePoolKey)
 -> ExceptT SophieNodeCmdError IO (SigningKey StakePoolKey))
-> IO (SigningKey StakePoolKey)
-> ExceptT SophieNodeCmdError IO (SigningKey StakePoolKey)
forall a b. (a -> b) -> a -> b
$ AsType StakePoolKey -> IO (SigningKey StakePoolKey)
forall keyrole.
Key keyrole =>
AsType keyrole -> IO (SigningKey keyrole)
generateSigningKey AsType StakePoolKey
AsStakePoolKey
    let vkey :: VerificationKey StakePoolKey
vkey = SigningKey StakePoolKey -> VerificationKey StakePoolKey
forall keyrole.
Key keyrole =>
SigningKey keyrole -> VerificationKey keyrole
getVerificationKey SigningKey StakePoolKey
skey
    (FileError () -> SophieNodeCmdError)
-> ExceptT (FileError ()) IO () -> ExceptT SophieNodeCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError () -> SophieNodeCmdError
SophieNodeCmdWriteFileError
      (ExceptT (FileError ()) IO () -> ExceptT SophieNodeCmdError IO ())
-> (IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ())
-> IO (Either (FileError ()) ())
-> ExceptT SophieNodeCmdError 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 SophieNodeCmdError IO ())
-> IO (Either (FileError ()) ())
-> ExceptT SophieNodeCmdError IO ()
forall a b. (a -> b) -> a -> b
$ String
-> Maybe TextEnvelopeDescr
-> SigningKey StakePoolKey
-> IO (Either (FileError ()) ())
forall a.
HasTextEnvelope a =>
String
-> Maybe TextEnvelopeDescr -> a -> IO (Either (FileError ()) ())
writeFileTextEnvelope String
skeyPath (TextEnvelopeDescr -> Maybe TextEnvelopeDescr
forall a. a -> Maybe a
Just TextEnvelopeDescr
skeyDesc) SigningKey StakePoolKey
skey
    (FileError () -> SophieNodeCmdError)
-> ExceptT (FileError ()) IO () -> ExceptT SophieNodeCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError () -> SophieNodeCmdError
SophieNodeCmdWriteFileError
      (ExceptT (FileError ()) IO () -> ExceptT SophieNodeCmdError IO ())
-> (IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ())
-> IO (Either (FileError ()) ())
-> ExceptT SophieNodeCmdError 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 SophieNodeCmdError IO ())
-> IO (Either (FileError ()) ())
-> ExceptT SophieNodeCmdError IO ()
forall a b. (a -> b) -> a -> b
$ String
-> Maybe TextEnvelopeDescr
-> VerificationKey StakePoolKey
-> IO (Either (FileError ()) ())
forall a.
HasTextEnvelope a =>
String
-> Maybe TextEnvelopeDescr -> a -> IO (Either (FileError ()) ())
writeFileTextEnvelope String
vkeyPath (TextEnvelopeDescr -> Maybe TextEnvelopeDescr
forall a. a -> Maybe a
Just TextEnvelopeDescr
vkeyDesc) VerificationKey StakePoolKey
vkey
    (FileError () -> SophieNodeCmdError)
-> ExceptT (FileError ()) IO () -> ExceptT SophieNodeCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError () -> SophieNodeCmdError
SophieNodeCmdWriteFileError
      (ExceptT (FileError ()) IO () -> ExceptT SophieNodeCmdError IO ())
-> (IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ())
-> IO (Either (FileError ()) ())
-> ExceptT SophieNodeCmdError 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 SophieNodeCmdError IO ())
-> IO (Either (FileError ()) ())
-> ExceptT SophieNodeCmdError IO ()
forall a b. (a -> b) -> a -> b
$ String
-> Maybe TextEnvelopeDescr
-> OperationalCertificateIssueCounter
-> IO (Either (FileError ()) ())
forall a.
HasTextEnvelope a =>
String
-> Maybe TextEnvelopeDescr -> a -> IO (Either (FileError ()) ())
writeFileTextEnvelope String
ocertCtrPath (TextEnvelopeDescr -> Maybe TextEnvelopeDescr
forall a. a -> Maybe a
Just TextEnvelopeDescr
ocertCtrDesc)
      (OperationalCertificateIssueCounter
 -> IO (Either (FileError ()) ()))
-> OperationalCertificateIssueCounter
-> IO (Either (FileError ()) ())
forall a b. (a -> b) -> a -> b
$ Word64
-> VerificationKey StakePoolKey
-> OperationalCertificateIssueCounter
OperationalCertificateIssueCounter Word64
initialCounter VerificationKey StakePoolKey
vkey
  where
    skeyDesc, vkeyDesc, ocertCtrDesc :: TextEnvelopeDescr
    skeyDesc :: TextEnvelopeDescr
skeyDesc = TextEnvelopeDescr
"Stake Pool Operator Signing Key"
    vkeyDesc :: TextEnvelopeDescr
vkeyDesc = TextEnvelopeDescr
"Stake Pool Operator Verification Key"
    ocertCtrDesc :: TextEnvelopeDescr
ocertCtrDesc = TextEnvelopeDescr
"Next certificate issue number: "
                TextEnvelopeDescr -> TextEnvelopeDescr -> TextEnvelopeDescr
forall a. Semigroup a => a -> a -> a
<> String -> TextEnvelopeDescr
forall a. IsString a => String -> a
fromString (Word64 -> String
forall a b. (Show a, ConvertText String b) => a -> b
show Word64
initialCounter)

    initialCounter :: Word64
    initialCounter :: Word64
initialCounter = Word64
0


runNodeKeyGenKES :: VerificationKeyFile
                 -> SigningKeyFile
                 -> ExceptT SophieNodeCmdError IO ()
runNodeKeyGenKES :: VerificationKeyFile
-> SigningKeyFile -> ExceptT SophieNodeCmdError IO ()
runNodeKeyGenKES (VerificationKeyFile String
vkeyPath) (SigningKeyFile String
skeyPath) = do
    SigningKey KesKey
skey <- IO (SigningKey KesKey)
-> ExceptT SophieNodeCmdError IO (SigningKey KesKey)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (SigningKey KesKey)
 -> ExceptT SophieNodeCmdError IO (SigningKey KesKey))
-> IO (SigningKey KesKey)
-> ExceptT SophieNodeCmdError IO (SigningKey KesKey)
forall a b. (a -> b) -> a -> b
$ AsType KesKey -> IO (SigningKey KesKey)
forall keyrole.
Key keyrole =>
AsType keyrole -> IO (SigningKey keyrole)
generateSigningKey AsType KesKey
AsKesKey
    let vkey :: VerificationKey KesKey
vkey = SigningKey KesKey -> VerificationKey KesKey
forall keyrole.
Key keyrole =>
SigningKey keyrole -> VerificationKey keyrole
getVerificationKey SigningKey KesKey
skey
    (FileError () -> SophieNodeCmdError)
-> ExceptT (FileError ()) IO () -> ExceptT SophieNodeCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError () -> SophieNodeCmdError
SophieNodeCmdWriteFileError
      (ExceptT (FileError ()) IO () -> ExceptT SophieNodeCmdError IO ())
-> (IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ())
-> IO (Either (FileError ()) ())
-> ExceptT SophieNodeCmdError 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 SophieNodeCmdError IO ())
-> IO (Either (FileError ()) ())
-> ExceptT SophieNodeCmdError IO ()
forall a b. (a -> b) -> a -> b
$ String
-> Maybe TextEnvelopeDescr
-> SigningKey KesKey
-> IO (Either (FileError ()) ())
forall a.
HasTextEnvelope a =>
String
-> Maybe TextEnvelopeDescr -> a -> IO (Either (FileError ()) ())
writeFileTextEnvelope String
skeyPath (TextEnvelopeDescr -> Maybe TextEnvelopeDescr
forall a. a -> Maybe a
Just TextEnvelopeDescr
skeyDesc) SigningKey KesKey
skey
    (FileError () -> SophieNodeCmdError)
-> ExceptT (FileError ()) IO () -> ExceptT SophieNodeCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError () -> SophieNodeCmdError
SophieNodeCmdWriteFileError
      (ExceptT (FileError ()) IO () -> ExceptT SophieNodeCmdError IO ())
-> (IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ())
-> IO (Either (FileError ()) ())
-> ExceptT SophieNodeCmdError 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 SophieNodeCmdError IO ())
-> IO (Either (FileError ()) ())
-> ExceptT SophieNodeCmdError IO ()
forall a b. (a -> b) -> a -> b
$ String
-> Maybe TextEnvelopeDescr
-> VerificationKey KesKey
-> IO (Either (FileError ()) ())
forall a.
HasTextEnvelope a =>
String
-> Maybe TextEnvelopeDescr -> a -> IO (Either (FileError ()) ())
writeFileTextEnvelope String
vkeyPath (TextEnvelopeDescr -> Maybe TextEnvelopeDescr
forall a. a -> Maybe a
Just TextEnvelopeDescr
vkeyDesc) VerificationKey KesKey
vkey
  where
    skeyDesc, vkeyDesc :: TextEnvelopeDescr
    skeyDesc :: TextEnvelopeDescr
skeyDesc = TextEnvelopeDescr
"KES Signing Key"
    vkeyDesc :: TextEnvelopeDescr
vkeyDesc = TextEnvelopeDescr
"KES Verification Key"

runNodeKeyGenVRF :: VerificationKeyFile -> SigningKeyFile
                 -> ExceptT SophieNodeCmdError IO ()
runNodeKeyGenVRF :: VerificationKeyFile
-> SigningKeyFile -> ExceptT SophieNodeCmdError IO ()
runNodeKeyGenVRF (VerificationKeyFile String
vkeyPath) (SigningKeyFile String
skeyPath) = do
    SigningKey VrfKey
skey <- IO (SigningKey VrfKey)
-> ExceptT SophieNodeCmdError IO (SigningKey VrfKey)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (SigningKey VrfKey)
 -> ExceptT SophieNodeCmdError IO (SigningKey VrfKey))
-> IO (SigningKey VrfKey)
-> ExceptT SophieNodeCmdError IO (SigningKey VrfKey)
forall a b. (a -> b) -> a -> b
$ AsType VrfKey -> IO (SigningKey VrfKey)
forall keyrole.
Key keyrole =>
AsType keyrole -> IO (SigningKey keyrole)
generateSigningKey AsType VrfKey
AsVrfKey
    let vkey :: VerificationKey VrfKey
vkey = SigningKey VrfKey -> VerificationKey VrfKey
forall keyrole.
Key keyrole =>
SigningKey keyrole -> VerificationKey keyrole
getVerificationKey SigningKey VrfKey
skey
    (FileError () -> SophieNodeCmdError)
-> ExceptT (FileError ()) IO () -> ExceptT SophieNodeCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError () -> SophieNodeCmdError
SophieNodeCmdWriteFileError
      (ExceptT (FileError ()) IO () -> ExceptT SophieNodeCmdError IO ())
-> (IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ())
-> IO (Either (FileError ()) ())
-> ExceptT SophieNodeCmdError 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 SophieNodeCmdError IO ())
-> IO (Either (FileError ()) ())
-> ExceptT SophieNodeCmdError IO ()
forall a b. (a -> b) -> a -> b
$ String
-> Maybe TextEnvelopeDescr
-> SigningKey VrfKey
-> IO (Either (FileError ()) ())
forall a.
HasTextEnvelope a =>
String
-> Maybe TextEnvelopeDescr -> a -> IO (Either (FileError ()) ())
writeFileTextEnvelopeWithOwnerPermissions String
skeyPath (TextEnvelopeDescr -> Maybe TextEnvelopeDescr
forall a. a -> Maybe a
Just TextEnvelopeDescr
skeyDesc) SigningKey VrfKey
skey
    (FileError () -> SophieNodeCmdError)
-> ExceptT (FileError ()) IO () -> ExceptT SophieNodeCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError () -> SophieNodeCmdError
SophieNodeCmdWriteFileError
      (ExceptT (FileError ()) IO () -> ExceptT SophieNodeCmdError IO ())
-> (IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ())
-> IO (Either (FileError ()) ())
-> ExceptT SophieNodeCmdError 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 SophieNodeCmdError IO ())
-> IO (Either (FileError ()) ())
-> ExceptT SophieNodeCmdError IO ()
forall a b. (a -> b) -> a -> b
$ String
-> Maybe TextEnvelopeDescr
-> VerificationKey VrfKey
-> IO (Either (FileError ()) ())
forall a.
HasTextEnvelope a =>
String
-> Maybe TextEnvelopeDescr -> a -> IO (Either (FileError ()) ())
writeFileTextEnvelope String
vkeyPath (TextEnvelopeDescr -> Maybe TextEnvelopeDescr
forall a. a -> Maybe a
Just TextEnvelopeDescr
vkeyDesc) VerificationKey VrfKey
vkey
  where
    skeyDesc, vkeyDesc :: TextEnvelopeDescr
    skeyDesc :: TextEnvelopeDescr
skeyDesc = TextEnvelopeDescr
"VRF Signing Key"
    vkeyDesc :: TextEnvelopeDescr
vkeyDesc = TextEnvelopeDescr
"VRF Verification Key"

runNodeKeyHashVRF :: VerificationKeyOrFile VrfKey
                  -> Maybe OutputFile
                  -> ExceptT SophieNodeCmdError IO ()
runNodeKeyHashVRF :: VerificationKeyOrFile VrfKey
-> Maybe OutputFile -> ExceptT SophieNodeCmdError IO ()
runNodeKeyHashVRF VerificationKeyOrFile VrfKey
verKeyOrFile Maybe OutputFile
mOutputFp = do
  VerificationKey VrfKey
vkey <- (FileError InputDecodeError -> SophieNodeCmdError)
-> ExceptT (FileError InputDecodeError) IO (VerificationKey VrfKey)
-> ExceptT SophieNodeCmdError IO (VerificationKey VrfKey)
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError InputDecodeError -> SophieNodeCmdError
SophieNodeCmdReadKeyFileError
    (ExceptT (FileError InputDecodeError) IO (VerificationKey VrfKey)
 -> ExceptT SophieNodeCmdError IO (VerificationKey VrfKey))
-> (IO
      (Either (FileError InputDecodeError) (VerificationKey VrfKey))
    -> ExceptT
         (FileError InputDecodeError) IO (VerificationKey VrfKey))
-> IO
     (Either (FileError InputDecodeError) (VerificationKey VrfKey))
-> ExceptT SophieNodeCmdError IO (VerificationKey VrfKey)
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 VrfKey))
-> ExceptT (FileError InputDecodeError) IO (VerificationKey VrfKey)
forall (m :: * -> *) x a. m (Either x a) -> ExceptT x m a
newExceptT
    (IO (Either (FileError InputDecodeError) (VerificationKey VrfKey))
 -> ExceptT SophieNodeCmdError IO (VerificationKey VrfKey))
-> IO
     (Either (FileError InputDecodeError) (VerificationKey VrfKey))
-> ExceptT SophieNodeCmdError IO (VerificationKey VrfKey)
forall a b. (a -> b) -> a -> b
$ AsType VrfKey
-> VerificationKeyOrFile VrfKey
-> IO
     (Either (FileError InputDecodeError) (VerificationKey VrfKey))
forall keyrole.
(HasTextEnvelope (VerificationKey keyrole),
 SerialiseAsBech32 (VerificationKey keyrole)) =>
AsType keyrole
-> VerificationKeyOrFile keyrole
-> IO
     (Either (FileError InputDecodeError) (VerificationKey keyrole))
readVerificationKeyOrFile AsType VrfKey
AsVrfKey VerificationKeyOrFile VrfKey
verKeyOrFile

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

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


runNodeNewCounter :: ColdVerificationKeyOrFile
                  -> Word
                  -> OpCertCounterFile
                  -> ExceptT SophieNodeCmdError IO ()
runNodeNewCounter :: ColdVerificationKeyOrFile
-> Word -> OpCertCounterFile -> ExceptT SophieNodeCmdError IO ()
runNodeNewCounter ColdVerificationKeyOrFile
coldVerKeyOrFile Word
counter
                  (OpCertCounterFile String
ocertCtrPath) = do

    VerificationKey StakePoolKey
vkey <- (FileError TextEnvelopeError -> SophieNodeCmdError)
-> ExceptT
     (FileError TextEnvelopeError) IO (VerificationKey StakePoolKey)
-> ExceptT SophieNodeCmdError IO (VerificationKey StakePoolKey)
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError TextEnvelopeError -> SophieNodeCmdError
SophieNodeCmdReadFileError (ExceptT
   (FileError TextEnvelopeError) IO (VerificationKey StakePoolKey)
 -> ExceptT SophieNodeCmdError IO (VerificationKey StakePoolKey))
-> (IO
      (Either
         (FileError TextEnvelopeError) (VerificationKey StakePoolKey))
    -> ExceptT
         (FileError TextEnvelopeError) IO (VerificationKey StakePoolKey))
-> IO
     (Either
        (FileError TextEnvelopeError) (VerificationKey StakePoolKey))
-> ExceptT SophieNodeCmdError IO (VerificationKey StakePoolKey)
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 TextEnvelopeError) (VerificationKey StakePoolKey))
-> ExceptT
     (FileError TextEnvelopeError) IO (VerificationKey StakePoolKey)
forall (m :: * -> *) x a. m (Either x a) -> ExceptT x m a
newExceptT (IO
   (Either
      (FileError TextEnvelopeError) (VerificationKey StakePoolKey))
 -> ExceptT SophieNodeCmdError IO (VerificationKey StakePoolKey))
-> IO
     (Either
        (FileError TextEnvelopeError) (VerificationKey StakePoolKey))
-> ExceptT SophieNodeCmdError IO (VerificationKey StakePoolKey)
forall a b. (a -> b) -> a -> b
$
      ColdVerificationKeyOrFile
-> IO
     (Either
        (FileError TextEnvelopeError) (VerificationKey StakePoolKey))
readColdVerificationKeyOrFile ColdVerificationKeyOrFile
coldVerKeyOrFile

    let ocertIssueCounter :: OperationalCertificateIssueCounter
ocertIssueCounter =
          Word64
-> VerificationKey StakePoolKey
-> OperationalCertificateIssueCounter
OperationalCertificateIssueCounter (Word -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word
counter) VerificationKey StakePoolKey
vkey

    (FileError () -> SophieNodeCmdError)
-> ExceptT (FileError ()) IO () -> ExceptT SophieNodeCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError () -> SophieNodeCmdError
SophieNodeCmdWriteFileError (ExceptT (FileError ()) IO () -> ExceptT SophieNodeCmdError IO ())
-> (IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ())
-> IO (Either (FileError ()) ())
-> ExceptT SophieNodeCmdError 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 SophieNodeCmdError IO ())
-> IO (Either (FileError ()) ())
-> ExceptT SophieNodeCmdError IO ()
forall a b. (a -> b) -> a -> b
$
      String
-> Maybe TextEnvelopeDescr
-> OperationalCertificateIssueCounter
-> IO (Either (FileError ()) ())
forall a.
HasTextEnvelope a =>
String
-> Maybe TextEnvelopeDescr -> a -> IO (Either (FileError ()) ())
writeFileTextEnvelope String
ocertCtrPath Maybe TextEnvelopeDescr
forall a. Maybe a
Nothing OperationalCertificateIssueCounter
ocertIssueCounter


runNodeIssueOpCert :: VerificationKeyOrFile KesKey
                   -- ^ This is the hot KES verification key.
                   -> SigningKeyFile
                   -- ^ This is the cold signing key.
                   -> OpCertCounterFile
                   -- ^ Counter that establishes the precedence
                   -- of the operational certificate.
                   -> KESPeriod
                   -- ^ Start of the validity period for this certificate.
                   -> OutputFile
                   -> ExceptT SophieNodeCmdError IO ()
runNodeIssueOpCert :: VerificationKeyOrFile KesKey
-> SigningKeyFile
-> OpCertCounterFile
-> KESPeriod
-> OutputFile
-> ExceptT SophieNodeCmdError IO ()
runNodeIssueOpCert VerificationKeyOrFile KesKey
kesVerKeyOrFile
                   SigningKeyFile
stakePoolSKeyFile
                   (OpCertCounterFile String
ocertCtrPath)
                   KESPeriod
kesPeriod
                   (OutputFile String
certFile) = do

    OperationalCertificateIssueCounter
ocertIssueCounter <- (FileError TextEnvelopeError -> SophieNodeCmdError)
-> ExceptT
     (FileError TextEnvelopeError) IO OperationalCertificateIssueCounter
-> ExceptT SophieNodeCmdError IO OperationalCertificateIssueCounter
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError TextEnvelopeError -> SophieNodeCmdError
SophieNodeCmdReadFileError
      (ExceptT
   (FileError TextEnvelopeError) IO OperationalCertificateIssueCounter
 -> ExceptT
      SophieNodeCmdError IO OperationalCertificateIssueCounter)
-> (IO
      (Either
         (FileError TextEnvelopeError) OperationalCertificateIssueCounter)
    -> ExceptT
         (FileError TextEnvelopeError)
         IO
         OperationalCertificateIssueCounter)
-> IO
     (Either
        (FileError TextEnvelopeError) OperationalCertificateIssueCounter)
-> ExceptT SophieNodeCmdError IO OperationalCertificateIssueCounter
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 TextEnvelopeError) OperationalCertificateIssueCounter)
-> ExceptT
     (FileError TextEnvelopeError) IO OperationalCertificateIssueCounter
forall (m :: * -> *) x a. m (Either x a) -> ExceptT x m a
newExceptT
      (IO
   (Either
      (FileError TextEnvelopeError) OperationalCertificateIssueCounter)
 -> ExceptT
      SophieNodeCmdError IO OperationalCertificateIssueCounter)
-> IO
     (Either
        (FileError TextEnvelopeError) OperationalCertificateIssueCounter)
-> ExceptT SophieNodeCmdError IO OperationalCertificateIssueCounter
forall a b. (a -> b) -> a -> b
$ AsType OperationalCertificateIssueCounter
-> String
-> IO
     (Either
        (FileError TextEnvelopeError) OperationalCertificateIssueCounter)
forall a.
HasTextEnvelope a =>
AsType a -> String -> IO (Either (FileError TextEnvelopeError) a)
readFileTextEnvelope AsType OperationalCertificateIssueCounter
AsOperationalCertificateIssueCounter String
ocertCtrPath

    VerificationKey KesKey
verKeyKes <- (FileError InputDecodeError -> SophieNodeCmdError)
-> ExceptT (FileError InputDecodeError) IO (VerificationKey KesKey)
-> ExceptT SophieNodeCmdError IO (VerificationKey KesKey)
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError InputDecodeError -> SophieNodeCmdError
SophieNodeCmdReadKeyFileError
      (ExceptT (FileError InputDecodeError) IO (VerificationKey KesKey)
 -> ExceptT SophieNodeCmdError IO (VerificationKey KesKey))
-> (IO
      (Either (FileError InputDecodeError) (VerificationKey KesKey))
    -> ExceptT
         (FileError InputDecodeError) IO (VerificationKey KesKey))
-> IO
     (Either (FileError InputDecodeError) (VerificationKey KesKey))
-> ExceptT SophieNodeCmdError IO (VerificationKey KesKey)
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 KesKey))
-> ExceptT (FileError InputDecodeError) IO (VerificationKey KesKey)
forall (m :: * -> *) x a. m (Either x a) -> ExceptT x m a
newExceptT
      (IO (Either (FileError InputDecodeError) (VerificationKey KesKey))
 -> ExceptT SophieNodeCmdError IO (VerificationKey KesKey))
-> IO
     (Either (FileError InputDecodeError) (VerificationKey KesKey))
-> ExceptT SophieNodeCmdError IO (VerificationKey KesKey)
forall a b. (a -> b) -> a -> b
$ AsType KesKey
-> VerificationKeyOrFile KesKey
-> IO
     (Either (FileError InputDecodeError) (VerificationKey KesKey))
forall keyrole.
(HasTextEnvelope (VerificationKey keyrole),
 SerialiseAsBech32 (VerificationKey keyrole)) =>
AsType keyrole
-> VerificationKeyOrFile keyrole
-> IO
     (Either (FileError InputDecodeError) (VerificationKey keyrole))
readVerificationKeyOrFile AsType KesKey
AsKesKey VerificationKeyOrFile KesKey
kesVerKeyOrFile

    Either
  (SigningKey StakePoolKey) (SigningKey GenesisDelegateExtendedKey)
signKey <- (FileError InputDecodeError -> SophieNodeCmdError)
-> ExceptT
     (FileError InputDecodeError)
     IO
     (Either
        (SigningKey StakePoolKey) (SigningKey GenesisDelegateExtendedKey))
-> ExceptT
     SophieNodeCmdError
     IO
     (Either
        (SigningKey StakePoolKey) (SigningKey GenesisDelegateExtendedKey))
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError InputDecodeError -> SophieNodeCmdError
SophieNodeCmdReadKeyFileError
      (ExceptT
   (FileError InputDecodeError)
   IO
   (Either
      (SigningKey StakePoolKey) (SigningKey GenesisDelegateExtendedKey))
 -> ExceptT
      SophieNodeCmdError
      IO
      (Either
         (SigningKey StakePoolKey) (SigningKey GenesisDelegateExtendedKey)))
-> (IO
      (Either
         (FileError InputDecodeError)
         (Either
            (SigningKey StakePoolKey) (SigningKey GenesisDelegateExtendedKey)))
    -> ExceptT
         (FileError InputDecodeError)
         IO
         (Either
            (SigningKey StakePoolKey) (SigningKey GenesisDelegateExtendedKey)))
-> IO
     (Either
        (FileError InputDecodeError)
        (Either
           (SigningKey StakePoolKey) (SigningKey GenesisDelegateExtendedKey)))
-> ExceptT
     SophieNodeCmdError
     IO
     (Either
        (SigningKey StakePoolKey) (SigningKey GenesisDelegateExtendedKey))
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)
     (Either
        (SigningKey StakePoolKey) (SigningKey GenesisDelegateExtendedKey)))
-> ExceptT
     (FileError InputDecodeError)
     IO
     (Either
        (SigningKey StakePoolKey) (SigningKey GenesisDelegateExtendedKey))
forall (m :: * -> *) x a. m (Either x a) -> ExceptT x m a
newExceptT
      (IO
   (Either
      (FileError InputDecodeError)
      (Either
         (SigningKey StakePoolKey) (SigningKey GenesisDelegateExtendedKey)))
 -> ExceptT
      SophieNodeCmdError
      IO
      (Either
         (SigningKey StakePoolKey) (SigningKey GenesisDelegateExtendedKey)))
-> IO
     (Either
        (FileError InputDecodeError)
        (Either
           (SigningKey StakePoolKey) (SigningKey GenesisDelegateExtendedKey)))
-> ExceptT
     SophieNodeCmdError
     IO
     (Either
        (SigningKey StakePoolKey) (SigningKey GenesisDelegateExtendedKey))
forall a b. (a -> b) -> a -> b
$ [FromSomeType
   SerialiseAsBech32
   (Either
      (SigningKey StakePoolKey) (SigningKey GenesisDelegateExtendedKey))]
-> [FromSomeType
      HasTextEnvelope
      (Either
         (SigningKey StakePoolKey) (SigningKey GenesisDelegateExtendedKey))]
-> SigningKeyFile
-> IO
     (Either
        (FileError InputDecodeError)
        (Either
           (SigningKey StakePoolKey) (SigningKey GenesisDelegateExtendedKey)))
forall b.
[FromSomeType SerialiseAsBech32 b]
-> [FromSomeType HasTextEnvelope b]
-> SigningKeyFile
-> IO (Either (FileError InputDecodeError) b)
readSigningKeyFileAnyOf
          [FromSomeType
   SerialiseAsBech32
   (Either
      (SigningKey StakePoolKey) (SigningKey GenesisDelegateExtendedKey))]
bech32PossibleBlockIssuers
          [FromSomeType
   HasTextEnvelope
   (Either
      (SigningKey StakePoolKey) (SigningKey GenesisDelegateExtendedKey))]
textEnvPossibleBlockIssuers
          SigningKeyFile
stakePoolSKeyFile

    (OperationalCertificate
ocert, OperationalCertificateIssueCounter
nextOcertCtr) <-
      (OperationalCertIssueError -> SophieNodeCmdError)
-> ExceptT
     OperationalCertIssueError
     IO
     (OperationalCertificate, OperationalCertificateIssueCounter)
-> ExceptT
     SophieNodeCmdError
     IO
     (OperationalCertificate, OperationalCertificateIssueCounter)
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT OperationalCertIssueError -> SophieNodeCmdError
SophieNodeCmdOperationalCertificateIssueError
        (ExceptT
   OperationalCertIssueError
   IO
   (OperationalCertificate, OperationalCertificateIssueCounter)
 -> ExceptT
      SophieNodeCmdError
      IO
      (OperationalCertificate, OperationalCertificateIssueCounter))
-> (Either
      OperationalCertIssueError
      (OperationalCertificate, OperationalCertificateIssueCounter)
    -> ExceptT
         OperationalCertIssueError
         IO
         (OperationalCertificate, OperationalCertificateIssueCounter))
-> Either
     OperationalCertIssueError
     (OperationalCertificate, OperationalCertificateIssueCounter)
-> ExceptT
     SophieNodeCmdError
     IO
     (OperationalCertificate, OperationalCertificateIssueCounter)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Either
  OperationalCertIssueError
  (OperationalCertificate, OperationalCertificateIssueCounter)
-> ExceptT
     OperationalCertIssueError
     IO
     (OperationalCertificate, OperationalCertificateIssueCounter)
forall (m :: * -> *) x a. Monad m => Either x a -> ExceptT x m a
hoistEither
        (Either
   OperationalCertIssueError
   (OperationalCertificate, OperationalCertificateIssueCounter)
 -> ExceptT
      SophieNodeCmdError
      IO
      (OperationalCertificate, OperationalCertificateIssueCounter))
-> Either
     OperationalCertIssueError
     (OperationalCertificate, OperationalCertificateIssueCounter)
-> ExceptT
     SophieNodeCmdError
     IO
     (OperationalCertificate, OperationalCertificateIssueCounter)
forall a b. (a -> b) -> a -> b
$ VerificationKey KesKey
-> Either
     (SigningKey StakePoolKey) (SigningKey GenesisDelegateExtendedKey)
-> KESPeriod
-> OperationalCertificateIssueCounter
-> Either
     OperationalCertIssueError
     (OperationalCertificate, OperationalCertificateIssueCounter)
issueOperationalCertificate
            VerificationKey KesKey
verKeyKes
            Either
  (SigningKey StakePoolKey) (SigningKey GenesisDelegateExtendedKey)
signKey
            KESPeriod
kesPeriod
            OperationalCertificateIssueCounter
ocertIssueCounter

    -- Write the counter first, to reduce the chance of ending up with
    -- a new cert but without updating the counter.
    (FileError () -> SophieNodeCmdError)
-> ExceptT (FileError ()) IO () -> ExceptT SophieNodeCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError () -> SophieNodeCmdError
SophieNodeCmdWriteFileError
      (ExceptT (FileError ()) IO () -> ExceptT SophieNodeCmdError IO ())
-> (IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ())
-> IO (Either (FileError ()) ())
-> ExceptT SophieNodeCmdError 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 SophieNodeCmdError IO ())
-> IO (Either (FileError ()) ())
-> ExceptT SophieNodeCmdError IO ()
forall a b. (a -> b) -> a -> b
$ String
-> Maybe TextEnvelopeDescr
-> OperationalCertificateIssueCounter
-> IO (Either (FileError ()) ())
forall a.
HasTextEnvelope a =>
String
-> Maybe TextEnvelopeDescr -> a -> IO (Either (FileError ()) ())
writeFileTextEnvelope
        String
ocertCtrPath
        (TextEnvelopeDescr -> Maybe TextEnvelopeDescr
forall a. a -> Maybe a
Just (TextEnvelopeDescr -> Maybe TextEnvelopeDescr)
-> TextEnvelopeDescr -> Maybe TextEnvelopeDescr
forall a b. (a -> b) -> a -> b
$ Word64 -> TextEnvelopeDescr
ocertCtrDesc (Word64 -> TextEnvelopeDescr) -> Word64 -> TextEnvelopeDescr
forall a b. (a -> b) -> a -> b
$ OperationalCertificateIssueCounter -> Word64
getCounter OperationalCertificateIssueCounter
nextOcertCtr)
        OperationalCertificateIssueCounter
nextOcertCtr

    (FileError () -> SophieNodeCmdError)
-> ExceptT (FileError ()) IO () -> ExceptT SophieNodeCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError () -> SophieNodeCmdError
SophieNodeCmdWriteFileError
      (ExceptT (FileError ()) IO () -> ExceptT SophieNodeCmdError IO ())
-> (IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ())
-> IO (Either (FileError ()) ())
-> ExceptT SophieNodeCmdError 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 SophieNodeCmdError IO ())
-> IO (Either (FileError ()) ())
-> ExceptT SophieNodeCmdError IO ()
forall a b. (a -> b) -> a -> b
$ String
-> Maybe TextEnvelopeDescr
-> OperationalCertificate
-> IO (Either (FileError ()) ())
forall a.
HasTextEnvelope a =>
String
-> Maybe TextEnvelopeDescr -> a -> IO (Either (FileError ()) ())
writeFileTextEnvelope String
certFile Maybe TextEnvelopeDescr
forall a. Maybe a
Nothing OperationalCertificate
ocert
  where
    getCounter :: OperationalCertificateIssueCounter -> Word64
    getCounter :: OperationalCertificateIssueCounter -> Word64
getCounter (OperationalCertificateIssueCounter Word64
n VerificationKey StakePoolKey
_) = Word64
n

    ocertCtrDesc :: Word64 -> TextEnvelopeDescr
    ocertCtrDesc :: Word64 -> TextEnvelopeDescr
ocertCtrDesc Word64
n = TextEnvelopeDescr
"Next certificate issue number: " TextEnvelopeDescr -> TextEnvelopeDescr -> TextEnvelopeDescr
forall a. Semigroup a => a -> a -> a
<> String -> TextEnvelopeDescr
forall a. IsString a => String -> a
fromString (Word64 -> String
forall a b. (Show a, ConvertText String b) => a -> b
show Word64
n)

    textEnvPossibleBlockIssuers
      :: [FromSomeType HasTextEnvelope
                       (Either (SigningKey StakePoolKey)
                               (SigningKey GenesisDelegateExtendedKey))]
    textEnvPossibleBlockIssuers :: [FromSomeType
   HasTextEnvelope
   (Either
      (SigningKey StakePoolKey) (SigningKey GenesisDelegateExtendedKey))]
textEnvPossibleBlockIssuers =
      [ AsType (SigningKey StakePoolKey)
-> (SigningKey StakePoolKey
    -> Either
         (SigningKey StakePoolKey) (SigningKey GenesisDelegateExtendedKey))
-> FromSomeType
     HasTextEnvelope
     (Either
        (SigningKey StakePoolKey) (SigningKey GenesisDelegateExtendedKey))
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType StakePoolKey -> AsType (SigningKey StakePoolKey)
forall a. AsType a -> AsType (SigningKey a)
AsSigningKey AsType StakePoolKey
AsStakePoolKey)        SigningKey StakePoolKey
-> Either
     (SigningKey StakePoolKey) (SigningKey GenesisDelegateExtendedKey)
forall a b. a -> Either a b
Left
      , AsType (SigningKey GenesisDelegateKey)
-> (SigningKey GenesisDelegateKey
    -> Either
         (SigningKey StakePoolKey) (SigningKey GenesisDelegateExtendedKey))
-> FromSomeType
     HasTextEnvelope
     (Either
        (SigningKey StakePoolKey) (SigningKey GenesisDelegateExtendedKey))
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType GenesisDelegateKey -> AsType (SigningKey GenesisDelegateKey)
forall a. AsType a -> AsType (SigningKey a)
AsSigningKey AsType GenesisDelegateKey
AsGenesisDelegateKey) (SigningKey StakePoolKey
-> Either
     (SigningKey StakePoolKey) (SigningKey GenesisDelegateExtendedKey)
forall a b. a -> Either a b
Left (SigningKey StakePoolKey
 -> Either
      (SigningKey StakePoolKey) (SigningKey GenesisDelegateExtendedKey))
-> (SigningKey GenesisDelegateKey -> SigningKey StakePoolKey)
-> SigningKey GenesisDelegateKey
-> Either
     (SigningKey StakePoolKey) (SigningKey GenesisDelegateExtendedKey)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. SigningKey GenesisDelegateKey -> SigningKey StakePoolKey
forall keyroleA keyroleB.
CastSigningKeyRole keyroleA keyroleB =>
SigningKey keyroleA -> SigningKey keyroleB
castSigningKey)
      , AsType (SigningKey GenesisDelegateExtendedKey)
-> (SigningKey GenesisDelegateExtendedKey
    -> Either
         (SigningKey StakePoolKey) (SigningKey GenesisDelegateExtendedKey))
-> FromSomeType
     HasTextEnvelope
     (Either
        (SigningKey StakePoolKey) (SigningKey GenesisDelegateExtendedKey))
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType GenesisDelegateExtendedKey
-> AsType (SigningKey GenesisDelegateExtendedKey)
forall a. AsType a -> AsType (SigningKey a)
AsSigningKey AsType GenesisDelegateExtendedKey
AsGenesisDelegateExtendedKey) SigningKey GenesisDelegateExtendedKey
-> Either
     (SigningKey StakePoolKey) (SigningKey GenesisDelegateExtendedKey)
forall a b. b -> Either a b
Right
      ]
    bech32PossibleBlockIssuers
      :: [FromSomeType SerialiseAsBech32
                       (Either (SigningKey StakePoolKey)
                               (SigningKey GenesisDelegateExtendedKey))]
    bech32PossibleBlockIssuers :: [FromSomeType
   SerialiseAsBech32
   (Either
      (SigningKey StakePoolKey) (SigningKey GenesisDelegateExtendedKey))]
bech32PossibleBlockIssuers =
      [AsType (SigningKey StakePoolKey)
-> (SigningKey StakePoolKey
    -> Either
         (SigningKey StakePoolKey) (SigningKey GenesisDelegateExtendedKey))
-> FromSomeType
     SerialiseAsBech32
     (Either
        (SigningKey StakePoolKey) (SigningKey GenesisDelegateExtendedKey))
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType StakePoolKey -> AsType (SigningKey StakePoolKey)
forall a. AsType a -> AsType (SigningKey a)
AsSigningKey AsType StakePoolKey
AsStakePoolKey) SigningKey StakePoolKey
-> Either
     (SigningKey StakePoolKey) (SigningKey GenesisDelegateExtendedKey)
forall a b. a -> Either a b
Left]

-- | Read a cold verification key or file.
--
-- If a filepath is provided, it will be interpreted as a text envelope
-- formatted file.
readColdVerificationKeyOrFile
  :: ColdVerificationKeyOrFile
  -> IO (Either (FileError TextEnvelopeError) (VerificationKey StakePoolKey))
readColdVerificationKeyOrFile :: ColdVerificationKeyOrFile
-> IO
     (Either
        (FileError TextEnvelopeError) (VerificationKey StakePoolKey))
readColdVerificationKeyOrFile ColdVerificationKeyOrFile
coldVerKeyOrFile =
  case ColdVerificationKeyOrFile
coldVerKeyOrFile of
    ColdStakePoolVerificationKey VerificationKey StakePoolKey
vk -> Either (FileError TextEnvelopeError) (VerificationKey StakePoolKey)
-> IO
     (Either
        (FileError TextEnvelopeError) (VerificationKey StakePoolKey))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (VerificationKey StakePoolKey
-> Either
     (FileError TextEnvelopeError) (VerificationKey StakePoolKey)
forall a b. b -> Either a b
Right VerificationKey StakePoolKey
vk)
    ColdGenesisDelegateVerificationKey VerificationKey GenesisDelegateKey
vk -> Either (FileError TextEnvelopeError) (VerificationKey StakePoolKey)
-> IO
     (Either
        (FileError TextEnvelopeError) (VerificationKey StakePoolKey))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either
   (FileError TextEnvelopeError) (VerificationKey StakePoolKey)
 -> IO
      (Either
         (FileError TextEnvelopeError) (VerificationKey StakePoolKey)))
-> Either
     (FileError TextEnvelopeError) (VerificationKey StakePoolKey)
-> IO
     (Either
        (FileError TextEnvelopeError) (VerificationKey StakePoolKey))
forall a b. (a -> b) -> a -> b
$ VerificationKey StakePoolKey
-> Either
     (FileError TextEnvelopeError) (VerificationKey StakePoolKey)
forall a b. b -> Either a b
Right (VerificationKey GenesisDelegateKey -> VerificationKey StakePoolKey
forall keyroleA keyroleB.
CastVerificationKeyRole keyroleA keyroleB =>
VerificationKey keyroleA -> VerificationKey keyroleB
castVerificationKey VerificationKey GenesisDelegateKey
vk)
    ColdGenesisVestedDelegateVerificationKey VerificationKey GenesisVestedDelegateKey
vk -> Either (FileError TextEnvelopeError) (VerificationKey StakePoolKey)
-> IO
     (Either
        (FileError TextEnvelopeError) (VerificationKey StakePoolKey))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either
   (FileError TextEnvelopeError) (VerificationKey StakePoolKey)
 -> IO
      (Either
         (FileError TextEnvelopeError) (VerificationKey StakePoolKey)))
-> Either
     (FileError TextEnvelopeError) (VerificationKey StakePoolKey)
-> IO
     (Either
        (FileError TextEnvelopeError) (VerificationKey StakePoolKey))
forall a b. (a -> b) -> a -> b
$ VerificationKey StakePoolKey
-> Either
     (FileError TextEnvelopeError) (VerificationKey StakePoolKey)
forall a b. b -> Either a b
Right (VerificationKey GenesisVestedDelegateKey
-> VerificationKey StakePoolKey
forall keyroleA keyroleB.
CastVerificationKeyRole keyroleA keyroleB =>
VerificationKey keyroleA -> VerificationKey keyroleB
castVerificationKey VerificationKey GenesisVestedDelegateKey
vk)
    ColdVestedDelegateVerificationKey VerificationKey VestedDelegateKey
vk -> Either (FileError TextEnvelopeError) (VerificationKey StakePoolKey)
-> IO
     (Either
        (FileError TextEnvelopeError) (VerificationKey StakePoolKey))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either
   (FileError TextEnvelopeError) (VerificationKey StakePoolKey)
 -> IO
      (Either
         (FileError TextEnvelopeError) (VerificationKey StakePoolKey)))
-> Either
     (FileError TextEnvelopeError) (VerificationKey StakePoolKey)
-> IO
     (Either
        (FileError TextEnvelopeError) (VerificationKey StakePoolKey))
forall a b. (a -> b) -> a -> b
$ VerificationKey StakePoolKey
-> Either
     (FileError TextEnvelopeError) (VerificationKey StakePoolKey)
forall a b. b -> Either a b
Right (VerificationKey VestedDelegateKey -> VerificationKey StakePoolKey
forall keyroleA keyroleB.
CastVerificationKeyRole keyroleA keyroleB =>
VerificationKey keyroleA -> VerificationKey keyroleB
castVerificationKey VerificationKey VestedDelegateKey
vk)
    ColdVerificationKeyFile (VerificationKeyFile String
fp) ->
      [FromSomeType HasTextEnvelope (VerificationKey StakePoolKey)]
-> String
-> IO
     (Either
        (FileError TextEnvelopeError) (VerificationKey StakePoolKey))
forall b.
[FromSomeType HasTextEnvelope b]
-> String -> IO (Either (FileError TextEnvelopeError) b)
readFileTextEnvelopeAnyOf
        [ AsType (VerificationKey StakePoolKey)
-> (VerificationKey StakePoolKey -> VerificationKey StakePoolKey)
-> FromSomeType HasTextEnvelope (VerificationKey StakePoolKey)
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType StakePoolKey -> AsType (VerificationKey StakePoolKey)
forall a. AsType a -> AsType (VerificationKey a)
AsVerificationKey AsType StakePoolKey
AsStakePoolKey) VerificationKey StakePoolKey -> VerificationKey StakePoolKey
forall a. a -> a
id
        , AsType (VerificationKey GenesisDelegateKey)
-> (VerificationKey GenesisDelegateKey
    -> VerificationKey StakePoolKey)
-> FromSomeType HasTextEnvelope (VerificationKey StakePoolKey)
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType GenesisDelegateKey
-> AsType (VerificationKey GenesisDelegateKey)
forall a. AsType a -> AsType (VerificationKey a)
AsVerificationKey AsType GenesisDelegateKey
AsGenesisDelegateKey) VerificationKey GenesisDelegateKey -> VerificationKey StakePoolKey
forall keyroleA keyroleB.
CastVerificationKeyRole keyroleA keyroleB =>
VerificationKey keyroleA -> VerificationKey keyroleB
castVerificationKey
        , AsType (VerificationKey GenesisVestedDelegateKey)
-> (VerificationKey GenesisVestedDelegateKey
    -> VerificationKey StakePoolKey)
-> FromSomeType HasTextEnvelope (VerificationKey StakePoolKey)
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType GenesisVestedDelegateKey
-> AsType (VerificationKey GenesisVestedDelegateKey)
forall a. AsType a -> AsType (VerificationKey a)
AsVerificationKey AsType GenesisVestedDelegateKey
AsGenesisVestedDelegateKey) VerificationKey GenesisVestedDelegateKey
-> VerificationKey StakePoolKey
forall keyroleA keyroleB.
CastVerificationKeyRole keyroleA keyroleB =>
VerificationKey keyroleA -> VerificationKey keyroleB
castVerificationKey
        , AsType (VerificationKey VestedDelegateKey)
-> (VerificationKey VestedDelegateKey
    -> VerificationKey StakePoolKey)
-> FromSomeType HasTextEnvelope (VerificationKey StakePoolKey)
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType VestedDelegateKey
-> AsType (VerificationKey VestedDelegateKey)
forall a. AsType a -> AsType (VerificationKey a)
AsVerificationKey AsType VestedDelegateKey
AsVestedDelegateKey) VerificationKey VestedDelegateKey -> VerificationKey StakePoolKey
forall keyroleA keyroleB.
CastVerificationKeyRole keyroleA keyroleB =>
VerificationKey keyroleA -> VerificationKey keyroleB
castVerificationKey
        ]
        String
fp