{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}

module Cardano.CLI.EraIndependent.Address.Run
  ( runAddressCmds
  , runAddressBuildCmd
  , runAddressKeyGenCmd
  , runAddressKeyHashCmd
  , buildShelleyAddress
  , generateAndWriteKeyFiles
  )
where

import Cardano.Api

import Cardano.CLI.Compatible.Exception
import Cardano.CLI.EraIndependent.Address.Command
import Cardano.CLI.EraIndependent.Address.Info.Run
import Cardano.CLI.EraIndependent.Key.Run qualified as Key
import Cardano.CLI.Read
import Cardano.CLI.Type.Common
import Cardano.CLI.Type.Error.AddressCmdError
import Cardano.CLI.Type.Key
  ( PaymentVerifier (..)
  , StakeIdentifier (..)
  , StakeVerifier (..)
  , VerificationKeyTextOrFile
  , generateKeyPair
  , readVerificationKeyOrHashOrFile
  , readVerificationKeyTextOrFileAnyOf
  )

import Control.Monad (void)
import Data.ByteString.Char8 qualified as BS
import Data.Function
import Data.Text.IO qualified as Text
import Vary (Vary)
import Vary qualified

runAddressCmds
  :: ()
  => AddressCmds
  -> CIO e ()
runAddressCmds :: forall e. AddressCmds -> CIO e ()
runAddressCmds = \case
  AddressKeyGen Vary '[FormatBech32, FormatTextEnvelope]
fmt AddressKeyType
kt VerificationKeyFile 'Out
vkf SigningKeyFile 'Out
skf ->
    Vary '[FormatBech32, FormatTextEnvelope]
-> AddressKeyType
-> VerificationKeyFile 'Out
-> SigningKeyFile 'Out
-> CIO e ()
forall e.
Vary '[FormatBech32, FormatTextEnvelope]
-> AddressKeyType
-> VerificationKeyFile 'Out
-> SigningKeyFile 'Out
-> CIO e ()
runAddressKeyGenCmd Vary '[FormatBech32, FormatTextEnvelope]
fmt AddressKeyType
kt VerificationKeyFile 'Out
vkf SigningKeyFile 'Out
skf
  AddressKeyHash VerificationKeyTextOrFile
vkf Maybe (File () 'Out)
mOFp ->
    VerificationKeyTextOrFile -> Maybe (File () 'Out) -> CIO e ()
forall e.
VerificationKeyTextOrFile -> Maybe (File () 'Out) -> CIO e ()
runAddressKeyHashCmd VerificationKeyTextOrFile
vkf Maybe (File () 'Out)
mOFp
  AddressBuild PaymentVerifier
paymentVerifier Maybe StakeIdentifier
mbStakeVerifier NetworkId
nw Maybe (File () 'Out)
mOutFp ->
    PaymentVerifier
-> Maybe StakeIdentifier
-> NetworkId
-> Maybe (File () 'Out)
-> CIO e ()
forall e.
PaymentVerifier
-> Maybe StakeIdentifier
-> NetworkId
-> Maybe (File () 'Out)
-> CIO e ()
runAddressBuildCmd PaymentVerifier
paymentVerifier Maybe StakeIdentifier
mbStakeVerifier NetworkId
nw Maybe (File () 'Out)
mOutFp
  AddressInfo Text
txt Maybe (File () 'Out)
mOFp ->
    Text -> Maybe (File () 'Out) -> CIO e ()
forall e. Text -> Maybe (File () 'Out) -> CIO e ()
runAddressInfoCmd Text
txt Maybe (File () 'Out)
mOFp

runAddressKeyGenCmd
  :: Vary [FormatBech32, FormatTextEnvelope]
  -> AddressKeyType
  -> VerificationKeyFile Out
  -> SigningKeyFile Out
  -> CIO e ()
runAddressKeyGenCmd :: forall e.
Vary '[FormatBech32, FormatTextEnvelope]
-> AddressKeyType
-> VerificationKeyFile 'Out
-> SigningKeyFile 'Out
-> CIO e ()
runAddressKeyGenCmd Vary '[FormatBech32, FormatTextEnvelope]
fmt AddressKeyType
kt VerificationKeyFile 'Out
vkf SigningKeyFile 'Out
skf = case AddressKeyType
kt of
  AddressKeyType
AddressKeyShelley -> RIO e (VerificationKey PaymentKey, SigningKey PaymentKey)
-> RIO e ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (RIO e (VerificationKey PaymentKey, SigningKey PaymentKey)
 -> RIO e ())
-> RIO e (VerificationKey PaymentKey, SigningKey PaymentKey)
-> RIO e ()
forall a b. (a -> b) -> a -> b
$ Vary '[FormatBech32, FormatTextEnvelope]
-> AsType PaymentKey
-> VerificationKeyFile 'Out
-> SigningKeyFile 'Out
-> CIO e (VerificationKey PaymentKey, SigningKey PaymentKey)
forall keyrole e.
(Key keyrole, HasTypeProxy keyrole,
 SerialiseAsBech32 (SigningKey keyrole),
 SerialiseAsBech32 (VerificationKey keyrole)) =>
Vary '[FormatBech32, FormatTextEnvelope]
-> AsType keyrole
-> VerificationKeyFile 'Out
-> SigningKeyFile 'Out
-> CIO e (VerificationKey keyrole, SigningKey keyrole)
generateAndWriteKeyFiles Vary '[FormatBech32, FormatTextEnvelope]
fmt AsType PaymentKey
AsPaymentKey VerificationKeyFile 'Out
vkf SigningKeyFile 'Out
skf
  AddressKeyType
AddressKeyShelleyExtended -> RIO
  e
  (VerificationKey PaymentExtendedKey, SigningKey PaymentExtendedKey)
-> RIO e ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (RIO
   e
   (VerificationKey PaymentExtendedKey, SigningKey PaymentExtendedKey)
 -> RIO e ())
-> RIO
     e
     (VerificationKey PaymentExtendedKey, SigningKey PaymentExtendedKey)
-> RIO e ()
forall a b. (a -> b) -> a -> b
$ Vary '[FormatBech32, FormatTextEnvelope]
-> AsType PaymentExtendedKey
-> VerificationKeyFile 'Out
-> SigningKeyFile 'Out
-> CIO
     e
     (VerificationKey PaymentExtendedKey, SigningKey PaymentExtendedKey)
forall keyrole e.
(Key keyrole, HasTypeProxy keyrole,
 SerialiseAsBech32 (SigningKey keyrole),
 SerialiseAsBech32 (VerificationKey keyrole)) =>
Vary '[FormatBech32, FormatTextEnvelope]
-> AsType keyrole
-> VerificationKeyFile 'Out
-> SigningKeyFile 'Out
-> CIO e (VerificationKey keyrole, SigningKey keyrole)
generateAndWriteKeyFiles Vary '[FormatBech32, FormatTextEnvelope]
fmt AsType PaymentExtendedKey
AsPaymentExtendedKey VerificationKeyFile 'Out
vkf SigningKeyFile 'Out
skf
  AddressKeyType
AddressKeyByron -> AsType ByronKey
-> VerificationKeyFile 'Out -> SigningKeyFile 'Out -> CIO e ()
forall keyrole e.
(Key keyrole, HasTypeProxy keyrole) =>
AsType keyrole
-> VerificationKeyFile 'Out -> SigningKeyFile 'Out -> CIO e ()
generateAndWriteByronKeyFiles AsType ByronKey
AsByronKey VerificationKeyFile 'Out
vkf SigningKeyFile 'Out
skf

generateAndWriteByronKeyFiles
  :: Key keyrole
  => HasTypeProxy keyrole
  => AsType keyrole
  -> VerificationKeyFile Out
  -> SigningKeyFile Out
  -> CIO e ()
generateAndWriteByronKeyFiles :: forall keyrole e.
(Key keyrole, HasTypeProxy keyrole) =>
AsType keyrole
-> VerificationKeyFile 'Out -> SigningKeyFile 'Out -> CIO e ()
generateAndWriteByronKeyFiles AsType keyrole
asType' VerificationKeyFile 'Out
vkf SigningKeyFile 'Out
skf = do
  (VerificationKey keyrole -> SigningKey keyrole -> RIO e ())
-> (VerificationKey keyrole, SigningKey keyrole) -> RIO e ()
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (VerificationKeyFile 'Out
-> SigningKeyFile 'Out
-> VerificationKey keyrole
-> SigningKey keyrole
-> CIO e ()
forall keyrole e.
Key keyrole =>
VerificationKeyFile 'Out
-> SigningKeyFile 'Out
-> VerificationKey keyrole
-> SigningKey keyrole
-> CIO e ()
writeByronPaymentKeyFiles VerificationKeyFile 'Out
vkf SigningKeyFile 'Out
skf) ((VerificationKey keyrole, SigningKey keyrole) -> RIO e ())
-> RIO e (VerificationKey keyrole, SigningKey keyrole) -> RIO e ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< AsType keyrole
-> RIO e (VerificationKey keyrole, SigningKey keyrole)
forall keyrole (m :: * -> *).
(MonadIO m, Key keyrole, HasTypeProxy keyrole) =>
AsType keyrole -> m (VerificationKey keyrole, SigningKey keyrole)
generateKeyPair AsType keyrole
asType'

generateAndWriteKeyFiles
  :: ()
  => Key keyrole
  => HasTypeProxy keyrole
  => SerialiseAsBech32 (SigningKey keyrole)
  => SerialiseAsBech32 (VerificationKey keyrole)
  => Vary [FormatBech32, FormatTextEnvelope]
  -> AsType keyrole
  -> VerificationKeyFile Out
  -> SigningKeyFile Out
  -> CIO e (VerificationKey keyrole, SigningKey keyrole)
generateAndWriteKeyFiles :: forall keyrole e.
(Key keyrole, HasTypeProxy keyrole,
 SerialiseAsBech32 (SigningKey keyrole),
 SerialiseAsBech32 (VerificationKey keyrole)) =>
Vary '[FormatBech32, FormatTextEnvelope]
-> AsType keyrole
-> VerificationKeyFile 'Out
-> SigningKeyFile 'Out
-> CIO e (VerificationKey keyrole, SigningKey keyrole)
generateAndWriteKeyFiles Vary '[FormatBech32, FormatTextEnvelope]
fmt AsType keyrole
asType' VerificationKeyFile 'Out
vkf SigningKeyFile 'Out
skf = do
  (VerificationKey keyrole
vk, SigningKey keyrole
sk) <- AsType keyrole
-> RIO e (VerificationKey keyrole, SigningKey keyrole)
forall keyrole (m :: * -> *).
(MonadIO m, Key keyrole, HasTypeProxy keyrole) =>
AsType keyrole -> m (VerificationKey keyrole, SigningKey keyrole)
generateKeyPair AsType keyrole
asType'
  Vary '[FormatBech32, FormatTextEnvelope]
-> VerificationKeyFile 'Out
-> SigningKeyFile 'Out
-> VerificationKey keyrole
-> SigningKey keyrole
-> CIO e ()
forall keyrole e.
(Key keyrole, SerialiseAsBech32 (SigningKey keyrole),
 SerialiseAsBech32 (VerificationKey keyrole)) =>
Vary '[FormatBech32, FormatTextEnvelope]
-> VerificationKeyFile 'Out
-> SigningKeyFile 'Out
-> VerificationKey keyrole
-> SigningKey keyrole
-> CIO e ()
writePaymentKeyFiles Vary '[FormatBech32, FormatTextEnvelope]
fmt VerificationKeyFile 'Out
vkf SigningKeyFile 'Out
skf VerificationKey keyrole
vk SigningKey keyrole
sk
  (VerificationKey keyrole, SigningKey keyrole)
-> RIO e (VerificationKey keyrole, SigningKey keyrole)
forall a. a -> RIO e a
forall (m :: * -> *) a. Monad m => a -> m a
return (VerificationKey keyrole
vk, SigningKey keyrole
sk)

writePaymentKeyFiles
  :: Key keyrole
  => SerialiseAsBech32 (SigningKey keyrole)
  => SerialiseAsBech32 (VerificationKey keyrole)
  => Vary [FormatBech32, FormatTextEnvelope]
  -> VerificationKeyFile Out
  -> SigningKeyFile Out
  -> VerificationKey keyrole
  -> SigningKey keyrole
  -> CIO e ()
writePaymentKeyFiles :: forall keyrole e.
(Key keyrole, SerialiseAsBech32 (SigningKey keyrole),
 SerialiseAsBech32 (VerificationKey keyrole)) =>
Vary '[FormatBech32, FormatTextEnvelope]
-> VerificationKeyFile 'Out
-> SigningKeyFile 'Out
-> VerificationKey keyrole
-> SigningKey keyrole
-> CIO e ()
writePaymentKeyFiles Vary '[FormatBech32, FormatTextEnvelope]
fmt VerificationKeyFile 'Out
vkeyPath SigningKeyFile 'Out
skeyPath VerificationKey keyrole
vkey SigningKey keyrole
skey = do
  Vary '[FormatBech32, FormatTextEnvelope]
fmt
    Vary '[FormatBech32, FormatTextEnvelope]
-> (Vary '[FormatBech32, FormatTextEnvelope] -> RIO e ())
-> RIO e ()
forall a b. a -> (a -> b) -> b
& ( (Vary '[FormatBech32, FormatTextEnvelope] -> RIO e ())
-> Vary '[FormatBech32, FormatTextEnvelope] -> RIO e ()
forall a. a -> a
id
          ((Vary '[FormatBech32, FormatTextEnvelope] -> RIO e ())
 -> Vary '[FormatBech32, FormatTextEnvelope] -> RIO e ())
-> ((Vary '[] -> RIO e ())
    -> Vary '[FormatBech32, FormatTextEnvelope] -> RIO e ())
-> (Vary '[] -> RIO e ())
-> Vary '[FormatBech32, FormatTextEnvelope]
-> RIO e ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (FormatBech32 -> RIO e ())
-> (Vary '[FormatTextEnvelope] -> RIO e ())
-> Vary '[FormatBech32, FormatTextEnvelope]
-> RIO e ()
forall a b (l :: [*]).
(a -> b) -> (Vary l -> b) -> Vary (a : l) -> b
Vary.on
            ( \FormatBech32
FormatBech32 -> do
                forall e (m :: * -> *) a.
(HasCallStack, MonadIO m, Show e, Typeable e, Error e) =>
IO (Either e a) -> m a
fromEitherIOCli @(FileError ()) (IO (Either (FileError ()) ()) -> RIO e ())
-> IO (Either (FileError ()) ()) -> RIO e ()
forall a b. (a -> b) -> a -> b
$
                  SigningKeyFile 'Out -> Text -> IO (Either (FileError ()) ())
forall (m :: * -> *) content e.
MonadIO m =>
File content 'Out -> Text -> m (Either (FileError e) ())
writeTextFile SigningKeyFile 'Out
skeyPath (Text -> IO (Either (FileError ()) ()))
-> Text -> IO (Either (FileError ()) ())
forall a b. (a -> b) -> a -> b
$
                    SigningKey keyrole -> Text
forall a. SerialiseAsBech32 a => a -> Text
serialiseToBech32 SigningKey keyrole
skey
            )
          ((Vary '[FormatTextEnvelope] -> RIO e ())
 -> Vary '[FormatBech32, FormatTextEnvelope] -> RIO e ())
-> ((Vary '[] -> RIO e ())
    -> Vary '[FormatTextEnvelope] -> RIO e ())
-> (Vary '[] -> RIO e ())
-> Vary '[FormatBech32, FormatTextEnvelope]
-> RIO e ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (FormatTextEnvelope -> RIO e ())
-> (Vary '[] -> RIO e ()) -> Vary '[FormatTextEnvelope] -> RIO e ()
forall a b (l :: [*]).
(a -> b) -> (Vary l -> b) -> Vary (a : l) -> b
Vary.on
            ( \FormatTextEnvelope
FormatTextEnvelope -> do
                forall e (m :: * -> *) a.
(HasCallStack, MonadIO m, Show e, Typeable e, Error e) =>
IO (Either e a) -> m a
fromEitherIOCli @(FileError ()) (IO (Either (FileError ()) ()) -> RIO e ())
-> IO (Either (FileError ()) ()) -> RIO e ()
forall a b. (a -> b) -> a -> b
$
                  SigningKeyFile 'Out -> ByteString -> IO (Either (FileError ()) ())
forall (m :: * -> *) content e.
MonadIO m =>
File content 'Out -> ByteString -> m (Either (FileError e) ())
writeLazyByteStringFile SigningKeyFile 'Out
skeyPath (ByteString -> IO (Either (FileError ()) ()))
-> ByteString -> IO (Either (FileError ()) ())
forall a b. (a -> b) -> a -> b
$
                    Maybe TextEnvelopeDescr -> SigningKey keyrole -> ByteString
forall a.
HasTextEnvelope a =>
Maybe TextEnvelopeDescr -> a -> ByteString
textEnvelopeToJSON (TextEnvelopeDescr -> Maybe TextEnvelopeDescr
forall a. a -> Maybe a
Just TextEnvelopeDescr
skeyDesc) SigningKey keyrole
skey
            )
          ((Vary '[] -> RIO e ())
 -> Vary '[FormatBech32, FormatTextEnvelope] -> RIO e ())
-> (Vary '[] -> RIO e ())
-> Vary '[FormatBech32, FormatTextEnvelope]
-> RIO e ()
forall a b. (a -> b) -> a -> b
$ Vary '[] -> RIO e ()
forall anything. Vary '[] -> anything
Vary.exhaustiveCase
      )

  Vary '[FormatBech32, FormatTextEnvelope]
fmt
    Vary '[FormatBech32, FormatTextEnvelope]
-> (Vary '[FormatBech32, FormatTextEnvelope] -> RIO e ())
-> RIO e ()
forall a b. a -> (a -> b) -> b
& ( (Vary '[FormatBech32, FormatTextEnvelope] -> RIO e ())
-> Vary '[FormatBech32, FormatTextEnvelope] -> RIO e ()
forall a. a -> a
id
          ((Vary '[FormatBech32, FormatTextEnvelope] -> RIO e ())
 -> Vary '[FormatBech32, FormatTextEnvelope] -> RIO e ())
-> ((Vary '[] -> RIO e ())
    -> Vary '[FormatBech32, FormatTextEnvelope] -> RIO e ())
-> (Vary '[] -> RIO e ())
-> Vary '[FormatBech32, FormatTextEnvelope]
-> RIO e ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (FormatBech32 -> RIO e ())
-> (Vary '[FormatTextEnvelope] -> RIO e ())
-> Vary '[FormatBech32, FormatTextEnvelope]
-> RIO e ()
forall a b (l :: [*]).
(a -> b) -> (Vary l -> b) -> Vary (a : l) -> b
Vary.on
            ( \FormatBech32
FormatBech32 -> do
                forall e (m :: * -> *) a.
(HasCallStack, MonadIO m, Show e, Typeable e, Error e) =>
IO (Either e a) -> m a
fromEitherIOCli @(FileError ()) (IO (Either (FileError ()) ()) -> RIO e ())
-> IO (Either (FileError ()) ()) -> RIO e ()
forall a b. (a -> b) -> a -> b
$
                  VerificationKeyFile 'Out -> Text -> IO (Either (FileError ()) ())
forall (m :: * -> *) content e.
MonadIO m =>
File content 'Out -> Text -> m (Either (FileError e) ())
writeTextFile VerificationKeyFile 'Out
vkeyPath (Text -> IO (Either (FileError ()) ()))
-> Text -> IO (Either (FileError ()) ())
forall a b. (a -> b) -> a -> b
$
                    VerificationKey keyrole -> Text
forall a. SerialiseAsBech32 a => a -> Text
serialiseToBech32 VerificationKey keyrole
vkey
            )
          ((Vary '[FormatTextEnvelope] -> RIO e ())
 -> Vary '[FormatBech32, FormatTextEnvelope] -> RIO e ())
-> ((Vary '[] -> RIO e ())
    -> Vary '[FormatTextEnvelope] -> RIO e ())
-> (Vary '[] -> RIO e ())
-> Vary '[FormatBech32, FormatTextEnvelope]
-> RIO e ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (FormatTextEnvelope -> RIO e ())
-> (Vary '[] -> RIO e ()) -> Vary '[FormatTextEnvelope] -> RIO e ()
forall a b (l :: [*]).
(a -> b) -> (Vary l -> b) -> Vary (a : l) -> b
Vary.on
            ( \FormatTextEnvelope
FormatTextEnvelope -> do
                forall e (m :: * -> *) a.
(HasCallStack, MonadIO m, Show e, Typeable e, Error e) =>
IO (Either e a) -> m a
fromEitherIOCli @(FileError ()) (IO (Either (FileError ()) ()) -> RIO e ())
-> IO (Either (FileError ()) ()) -> RIO e ()
forall a b. (a -> b) -> a -> b
$
                  VerificationKeyFile 'Out
-> ByteString -> IO (Either (FileError ()) ())
forall (m :: * -> *) content e.
MonadIO m =>
File content 'Out -> ByteString -> m (Either (FileError e) ())
writeLazyByteStringFile VerificationKeyFile 'Out
vkeyPath (ByteString -> IO (Either (FileError ()) ()))
-> ByteString -> IO (Either (FileError ()) ())
forall a b. (a -> b) -> a -> b
$
                    Maybe TextEnvelopeDescr -> VerificationKey keyrole -> ByteString
forall a.
HasTextEnvelope a =>
Maybe TextEnvelopeDescr -> a -> ByteString
textEnvelopeToJSON (TextEnvelopeDescr -> Maybe TextEnvelopeDescr
forall a. a -> Maybe a
Just TextEnvelopeDescr
Key.paymentVkeyDesc) VerificationKey keyrole
vkey
            )
          ((Vary '[] -> RIO e ())
 -> Vary '[FormatBech32, FormatTextEnvelope] -> RIO e ())
-> (Vary '[] -> RIO e ())
-> Vary '[FormatBech32, FormatTextEnvelope]
-> RIO e ()
forall a b. (a -> b) -> a -> b
$ Vary '[] -> RIO e ()
forall anything. Vary '[] -> anything
Vary.exhaustiveCase
      )
 where
  skeyDesc :: TextEnvelopeDescr
  skeyDesc :: TextEnvelopeDescr
skeyDesc = TextEnvelopeDescr
"Payment Signing Key"

writeByronPaymentKeyFiles
  :: Key keyrole
  => VerificationKeyFile Out
  -> SigningKeyFile Out
  -> VerificationKey keyrole
  -> SigningKey keyrole
  -> CIO e ()
writeByronPaymentKeyFiles :: forall keyrole e.
Key keyrole =>
VerificationKeyFile 'Out
-> SigningKeyFile 'Out
-> VerificationKey keyrole
-> SigningKey keyrole
-> CIO e ()
writeByronPaymentKeyFiles VerificationKeyFile 'Out
vkeyPath SigningKeyFile 'Out
skeyPath VerificationKey keyrole
vkey SigningKey keyrole
skey = do
  -- No bech32 encoding for Byron keys
  forall e (m :: * -> *) a.
(HasCallStack, MonadIO m, Show e, Typeable e, Error e) =>
IO (Either e a) -> m a
fromEitherIOCli @(FileError ()) (IO (Either (FileError ()) ()) -> RIO e ())
-> IO (Either (FileError ()) ()) -> RIO e ()
forall a b. (a -> b) -> a -> b
$
    SigningKeyFile 'Out -> ByteString -> IO (Either (FileError ()) ())
forall (m :: * -> *) content e.
MonadIO m =>
File content 'Out -> ByteString -> m (Either (FileError e) ())
writeLazyByteStringFile SigningKeyFile 'Out
skeyPath (ByteString -> IO (Either (FileError ()) ()))
-> ByteString -> IO (Either (FileError ()) ())
forall a b. (a -> b) -> a -> b
$
      Maybe TextEnvelopeDescr -> SigningKey keyrole -> ByteString
forall a.
HasTextEnvelope a =>
Maybe TextEnvelopeDescr -> a -> ByteString
textEnvelopeToJSON (TextEnvelopeDescr -> Maybe TextEnvelopeDescr
forall a. a -> Maybe a
Just TextEnvelopeDescr
skeyDesc) SigningKey keyrole
skey

  forall e (m :: * -> *) a.
(HasCallStack, MonadIO m, Show e, Typeable e, Error e) =>
IO (Either e a) -> m a
fromEitherIOCli @(FileError ()) (IO (Either (FileError ()) ()) -> RIO e ())
-> IO (Either (FileError ()) ()) -> RIO e ()
forall a b. (a -> b) -> a -> b
$
    VerificationKeyFile 'Out
-> ByteString -> IO (Either (FileError ()) ())
forall (m :: * -> *) content e.
MonadIO m =>
File content 'Out -> ByteString -> m (Either (FileError e) ())
writeLazyByteStringFile VerificationKeyFile 'Out
vkeyPath (ByteString -> IO (Either (FileError ()) ()))
-> ByteString -> IO (Either (FileError ()) ())
forall a b. (a -> b) -> a -> b
$
      Maybe TextEnvelopeDescr -> VerificationKey keyrole -> ByteString
forall a.
HasTextEnvelope a =>
Maybe TextEnvelopeDescr -> a -> ByteString
textEnvelopeToJSON (TextEnvelopeDescr -> Maybe TextEnvelopeDescr
forall a. a -> Maybe a
Just TextEnvelopeDescr
Key.paymentVkeyDesc) VerificationKey keyrole
vkey
 where
  skeyDesc :: TextEnvelopeDescr
  skeyDesc :: TextEnvelopeDescr
skeyDesc = TextEnvelopeDescr
"Payment Signing Key"

runAddressKeyHashCmd
  :: VerificationKeyTextOrFile
  -> Maybe (File () Out)
  -> CIO e ()
runAddressKeyHashCmd :: forall e.
VerificationKeyTextOrFile -> Maybe (File () 'Out) -> CIO e ()
runAddressKeyHashCmd VerificationKeyTextOrFile
vkeyTextOrFile Maybe (File () 'Out)
mOutputFp = do
  SomeAddressVerificationKey
vkey <-
    IO
  (Either VerificationKeyTextOrFileError SomeAddressVerificationKey)
-> RIO e SomeAddressVerificationKey
forall e (m :: * -> *) a.
(HasCallStack, MonadIO m, Show e, Typeable e, Error e) =>
IO (Either e a) -> m a
fromEitherIOCli (IO
   (Either VerificationKeyTextOrFileError SomeAddressVerificationKey)
 -> RIO e SomeAddressVerificationKey)
-> IO
     (Either VerificationKeyTextOrFileError SomeAddressVerificationKey)
-> RIO e SomeAddressVerificationKey
forall a b. (a -> b) -> a -> b
$
      VerificationKeyTextOrFile
-> IO
     (Either VerificationKeyTextOrFileError SomeAddressVerificationKey)
readVerificationKeyTextOrFileAnyOf VerificationKeyTextOrFile
vkeyTextOrFile

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

  case Maybe (File () 'Out)
mOutputFp of
    Just (File FilePath
fpath) -> 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
$ FilePath -> ByteString -> IO ()
BS.writeFile FilePath
fpath ByteString
hexKeyHash
    Maybe (File () 'Out)
Nothing -> 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
$ ByteString -> IO ()
BS.putStrLn ByteString
hexKeyHash

runAddressBuildCmd
  :: PaymentVerifier
  -> Maybe StakeIdentifier
  -> NetworkId
  -> Maybe (File () Out)
  -> CIO e ()
runAddressBuildCmd :: forall e.
PaymentVerifier
-> Maybe StakeIdentifier
-> NetworkId
-> Maybe (File () 'Out)
-> CIO e ()
runAddressBuildCmd PaymentVerifier
paymentVerifier Maybe StakeIdentifier
mbStakeVerifier NetworkId
nw Maybe (File () 'Out)
mOutFp = do
  Text
outText <- case PaymentVerifier
paymentVerifier of
    PaymentVerifierKey VerificationKeyTextOrFile
payVkeyTextOrFile -> do
      SomeAddressVerificationKey
payVKey <-
        IO
  (Either VerificationKeyTextOrFileError SomeAddressVerificationKey)
-> RIO e SomeAddressVerificationKey
forall e (m :: * -> *) a.
(HasCallStack, MonadIO m, Show e, Typeable e, Error e) =>
IO (Either e a) -> m a
fromEitherIOCli (IO
   (Either VerificationKeyTextOrFileError SomeAddressVerificationKey)
 -> RIO e SomeAddressVerificationKey)
-> IO
     (Either VerificationKeyTextOrFileError SomeAddressVerificationKey)
-> RIO e SomeAddressVerificationKey
forall a b. (a -> b) -> a -> b
$
          VerificationKeyTextOrFile
-> IO
     (Either VerificationKeyTextOrFileError SomeAddressVerificationKey)
readVerificationKeyTextOrFileAnyOf VerificationKeyTextOrFile
payVkeyTextOrFile

      AddressAny
addr <- case SomeAddressVerificationKey
payVKey of
        AByronVerificationKey VerificationKey ByronKey
vk ->
          AddressAny -> RIO e AddressAny
forall a. a -> RIO e a
forall (m :: * -> *) a. Monad m => a -> m a
return (Address ByronAddr -> AddressAny
AddressByron (NetworkId -> VerificationKey ByronKey -> Address ByronAddr
makeByronAddress NetworkId
nw VerificationKey ByronKey
vk))
        APaymentVerificationKey VerificationKey PaymentKey
vk ->
          Address ShelleyAddr -> AddressAny
AddressShelley (Address ShelleyAddr -> AddressAny)
-> RIO e (Address ShelleyAddr) -> RIO e AddressAny
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> VerificationKey PaymentKey
-> Maybe StakeIdentifier
-> NetworkId
-> CIO e (Address ShelleyAddr)
forall e.
VerificationKey PaymentKey
-> Maybe StakeIdentifier
-> NetworkId
-> CIO e (Address ShelleyAddr)
buildShelleyAddress VerificationKey PaymentKey
vk Maybe StakeIdentifier
mbStakeVerifier NetworkId
nw
        APaymentExtendedVerificationKey VerificationKey PaymentExtendedKey
vk ->
          Address ShelleyAddr -> AddressAny
AddressShelley (Address ShelleyAddr -> AddressAny)
-> RIO e (Address ShelleyAddr) -> RIO e AddressAny
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> VerificationKey PaymentKey
-> Maybe StakeIdentifier
-> NetworkId
-> CIO e (Address ShelleyAddr)
forall e.
VerificationKey PaymentKey
-> Maybe StakeIdentifier
-> NetworkId
-> CIO e (Address ShelleyAddr)
buildShelleyAddress (VerificationKey PaymentExtendedKey -> VerificationKey PaymentKey
forall keyroleA keyroleB.
CastVerificationKeyRole keyroleA keyroleB =>
VerificationKey keyroleA -> VerificationKey keyroleB
castVerificationKey VerificationKey PaymentExtendedKey
vk) Maybe StakeIdentifier
mbStakeVerifier NetworkId
nw
        AGenesisUTxOVerificationKey VerificationKey GenesisUTxOKey
vk ->
          Address ShelleyAddr -> AddressAny
AddressShelley (Address ShelleyAddr -> AddressAny)
-> RIO e (Address ShelleyAddr) -> RIO e AddressAny
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> VerificationKey PaymentKey
-> Maybe StakeIdentifier
-> NetworkId
-> CIO e (Address ShelleyAddr)
forall e.
VerificationKey PaymentKey
-> Maybe StakeIdentifier
-> NetworkId
-> CIO e (Address ShelleyAddr)
buildShelleyAddress (VerificationKey GenesisUTxOKey -> VerificationKey PaymentKey
forall keyroleA keyroleB.
CastVerificationKeyRole keyroleA keyroleB =>
VerificationKey keyroleA -> VerificationKey keyroleB
castVerificationKey VerificationKey GenesisUTxOKey
vk) Maybe StakeIdentifier
mbStakeVerifier NetworkId
nw
        SomeAddressVerificationKey
nonPaymentKey ->
          AddressCmdError -> RIO e AddressAny
forall e (m :: * -> *) a.
(HasCallStack, Show e, Typeable e, Error e, MonadIO m) =>
e -> m a
throwCliError (AddressCmdError -> RIO e AddressAny)
-> AddressCmdError -> RIO e AddressAny
forall a b. (a -> b) -> a -> b
$ SomeAddressVerificationKey -> AddressCmdError
AddressCmdExpectedPaymentVerificationKey SomeAddressVerificationKey
nonPaymentKey
      Text -> RIO e Text
forall a. a -> RIO e a
forall (m :: * -> *) a. Monad m => a -> m a
return (Text -> RIO e Text) -> Text -> RIO e Text
forall a b. (a -> b) -> a -> b
$ AddressAny -> Text
forall addr. SerialiseAddress addr => addr -> Text
serialiseAddress (AddressAny
addr :: AddressAny)
    PaymentVerifierScriptFile (File FilePath
fp) -> do
      ScriptInAnyLang ScriptLanguage lang
_lang Script lang
script <-
        FilePath -> RIO e ScriptInAnyLang
forall (m :: * -> *). MonadIO m => FilePath -> m ScriptInAnyLang
readFileScriptInAnyLang FilePath
fp

      let payCred :: PaymentCredential
payCred = ScriptHash -> PaymentCredential
PaymentCredentialByScript (Script lang -> ScriptHash
forall lang. Script lang -> ScriptHash
hashScript Script lang
script)

      StakeAddressReference
stakeAddressReference <-
        RIO e StakeAddressReference
-> (StakeIdentifier -> RIO e StakeAddressReference)
-> Maybe StakeIdentifier
-> RIO e StakeAddressReference
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (StakeAddressReference -> RIO e StakeAddressReference
forall a. a -> RIO e a
forall (m :: * -> *) a. Monad m => a -> m a
return StakeAddressReference
NoStakeAddress) StakeIdentifier -> RIO e StakeAddressReference
StakeIdentifier -> CIO e StakeAddressReference
forall e. StakeIdentifier -> CIO e StakeAddressReference
makeStakeAddressRef Maybe StakeIdentifier
mbStakeVerifier

      Text -> RIO e Text
forall a. a -> RIO e a
forall (m :: * -> *) a. Monad m => a -> m a
return (Text -> RIO e Text) -> Text -> RIO e Text
forall a b. (a -> b) -> a -> b
$ Address ShelleyAddr -> Text
forall addr. SerialiseAddress addr => addr -> Text
serialiseAddress (Address ShelleyAddr -> Text)
-> (StakeAddressReference -> Address ShelleyAddr)
-> StakeAddressReference
-> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NetworkId
-> PaymentCredential
-> StakeAddressReference
-> Address ShelleyAddr
makeShelleyAddress NetworkId
nw PaymentCredential
payCred (StakeAddressReference -> Text) -> StakeAddressReference -> Text
forall a b. (a -> b) -> a -> b
$ StakeAddressReference
stakeAddressReference

  case Maybe (File () 'Out)
mOutFp of
    Just (File FilePath
fpath) -> 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
$ FilePath -> Text -> IO ()
Text.writeFile FilePath
fpath Text
outText
    Maybe (File () 'Out)
Nothing -> 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.putStr Text
outText

makeStakeAddressRef
  :: StakeIdentifier
  -> CIO e StakeAddressReference
makeStakeAddressRef :: forall e. StakeIdentifier -> CIO e StakeAddressReference
makeStakeAddressRef StakeIdentifier
stakeIdentifier =
  case StakeIdentifier
stakeIdentifier of
    StakeIdentifierVerifier StakeVerifier
stakeVerifier ->
      case StakeVerifier
stakeVerifier of
        StakeVerifierKey VerificationKeyOrHashOrFile StakeKey
stkVkeyOrFile -> do
          Hash StakeKey
stakeVKeyHash <-
            VerificationKeyOrHashOrFile StakeKey -> CIO e (Hash StakeKey)
forall keyrole e.
(Key keyrole, SerialiseAsBech32 (VerificationKey keyrole)) =>
VerificationKeyOrHashOrFile keyrole -> CIO e (Hash keyrole)
readVerificationKeyOrHashOrFile VerificationKeyOrHashOrFile StakeKey
stkVkeyOrFile
          StakeAddressReference -> RIO e StakeAddressReference
forall a. a -> RIO e a
forall (m :: * -> *) a. Monad m => a -> m a
return (StakeAddressReference -> RIO e StakeAddressReference)
-> (StakeCredential -> StakeAddressReference)
-> StakeCredential
-> RIO e StakeAddressReference
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StakeCredential -> StakeAddressReference
StakeAddressByValue (StakeCredential -> RIO e StakeAddressReference)
-> StakeCredential -> RIO e StakeAddressReference
forall a b. (a -> b) -> a -> b
$ Hash StakeKey -> StakeCredential
StakeCredentialByKey Hash StakeKey
stakeVKeyHash
        StakeVerifierScriptFile (File FilePath
fp) -> do
          ScriptInAnyLang ScriptLanguage lang
_lang Script lang
script <-
            FilePath -> RIO e ScriptInAnyLang
forall (m :: * -> *). MonadIO m => FilePath -> m ScriptInAnyLang
readFileScriptInAnyLang FilePath
fp

          let stakeCred :: StakeCredential
stakeCred = ScriptHash -> StakeCredential
StakeCredentialByScript (Script lang -> ScriptHash
forall lang. Script lang -> ScriptHash
hashScript Script lang
script)
          StakeAddressReference -> RIO e StakeAddressReference
forall a. a -> RIO e a
forall (m :: * -> *) a. Monad m => a -> m a
return (StakeCredential -> StakeAddressReference
StakeAddressByValue StakeCredential
stakeCred)
    StakeIdentifierAddress StakeAddress
stakeAddr ->
      StakeAddressReference -> RIO e StakeAddressReference
forall a. a -> RIO e a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (StakeAddressReference -> RIO e StakeAddressReference)
-> StakeAddressReference -> RIO e StakeAddressReference
forall a b. (a -> b) -> a -> b
$ StakeCredential -> StakeAddressReference
StakeAddressByValue (StakeCredential -> StakeAddressReference)
-> StakeCredential -> StakeAddressReference
forall a b. (a -> b) -> a -> b
$ StakeAddress -> StakeCredential
stakeAddressCredential StakeAddress
stakeAddr

buildShelleyAddress
  :: VerificationKey PaymentKey
  -> Maybe StakeIdentifier
  -> NetworkId
  -> CIO e (Address ShelleyAddr)
buildShelleyAddress :: forall e.
VerificationKey PaymentKey
-> Maybe StakeIdentifier
-> NetworkId
-> CIO e (Address ShelleyAddr)
buildShelleyAddress VerificationKey PaymentKey
vkey Maybe StakeIdentifier
mbStakeVerifier NetworkId
nw =
  NetworkId
-> PaymentCredential
-> StakeAddressReference
-> Address ShelleyAddr
makeShelleyAddress NetworkId
nw (Hash PaymentKey -> PaymentCredential
PaymentCredentialByKey (VerificationKey PaymentKey -> Hash PaymentKey
forall keyrole.
Key keyrole =>
VerificationKey keyrole -> Hash keyrole
verificationKeyHash VerificationKey PaymentKey
vkey))
    (StakeAddressReference -> Address ShelleyAddr)
-> RIO e StakeAddressReference -> RIO e (Address ShelleyAddr)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> RIO e StakeAddressReference
-> (StakeIdentifier -> RIO e StakeAddressReference)
-> Maybe StakeIdentifier
-> RIO e StakeAddressReference
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (StakeAddressReference -> RIO e StakeAddressReference
forall a. a -> RIO e a
forall (m :: * -> *) a. Monad m => a -> m a
return StakeAddressReference
NoStakeAddress) StakeIdentifier -> RIO e StakeAddressReference
StakeIdentifier -> CIO e StakeAddressReference
forall e. StakeIdentifier -> CIO e StakeAddressReference
makeStakeAddressRef Maybe StakeIdentifier
mbStakeVerifier