{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}

module Cardano.CLI.Shelley.Run.Key
  ( ShelleyKeyCmdError
  , SomeSigningKey(..)
  , renderShelleyKeyCmdError
  , runKeyCmd

    -- * Exports for testing
  , decodeBech32
  ) where

import           Cardano.Prelude

import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as BSC
import qualified Data.Text as Text

import qualified Control.Exception as Exception
import           Control.Monad.Trans.Except.Extra (firstExceptT, hoistEither, left, newExceptT)

import qualified Codec.Binary.Bech32 as Bech32

import qualified Cardano.Crypto.DSIGN as DSIGN
import qualified Cardano.Crypto.Signing as Byron.Crypto
import qualified Cardano.Crypto.Signing as Byron
import qualified Cardano.Crypto.Signing as Crypto
import qualified Cardano.Crypto.Wallet as Crypto
import qualified Cardano.Ledger.Keys as Shelley

import           Cardano.Api
import           Cardano.Api.Byron hiding (SomeByronSigningKey (..))
import qualified Cardano.Api.Byron as ByronApi
import           Cardano.Api.Crypto.Ed25519Bip32 (xPrvFromBytes)
import           Cardano.Api.Shelley

import qualified Cardano.CLI.Byron.Key as Byron
import           Cardano.CLI.Helpers (textShow)
import           Cardano.CLI.Shelley.Commands
import           Cardano.CLI.Shelley.Key (InputDecodeError, readSigningKeyFileAnyOf)
import           Cardano.CLI.Types (SigningKeyFile (..), VerificationKeyFile (..))


data ShelleyKeyCmdError
  = ShelleyKeyCmdReadFileError !(FileError TextEnvelopeError)
  | ShelleyKeyCmdReadKeyFileError !(FileError InputDecodeError)
  | ShelleyKeyCmdWriteFileError !(FileError ())
  | ShelleyKeyCmdByronKeyFailure !Byron.ByronKeyFailure
  | ShelleyKeyCmdByronKeyParseError
      !Text
      -- ^ Text representation of the parse error. Unfortunately, the actual
      -- error type isn't exported.
  | ShelleyKeyCmdItnKeyConvError !ItnKeyConversionError
  | ShelleyKeyCmdWrongKeyTypeError
  | ShelleyKeyCmdCardanoAddressSigningKeyFileError
      !(FileError CardanoAddressSigningKeyConversionError)
  | ShelleyKeyCmdNonLegacyKey !FilePath
  deriving Int -> ShelleyKeyCmdError -> ShowS
[ShelleyKeyCmdError] -> ShowS
ShelleyKeyCmdError -> String
(Int -> ShelleyKeyCmdError -> ShowS)
-> (ShelleyKeyCmdError -> String)
-> ([ShelleyKeyCmdError] -> ShowS)
-> Show ShelleyKeyCmdError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ShelleyKeyCmdError] -> ShowS
$cshowList :: [ShelleyKeyCmdError] -> ShowS
show :: ShelleyKeyCmdError -> String
$cshow :: ShelleyKeyCmdError -> String
showsPrec :: Int -> ShelleyKeyCmdError -> ShowS
$cshowsPrec :: Int -> ShelleyKeyCmdError -> ShowS
Show

renderShelleyKeyCmdError :: ShelleyKeyCmdError -> Text
renderShelleyKeyCmdError :: ShelleyKeyCmdError -> Text
renderShelleyKeyCmdError ShelleyKeyCmdError
err =
  case ShelleyKeyCmdError
err of
    ShelleyKeyCmdReadFileError FileError TextEnvelopeError
fileErr -> String -> Text
Text.pack (FileError TextEnvelopeError -> String
forall e. Error e => e -> String
displayError FileError TextEnvelopeError
fileErr)
    ShelleyKeyCmdReadKeyFileError FileError InputDecodeError
fileErr -> String -> Text
Text.pack (FileError InputDecodeError -> String
forall e. Error e => e -> String
displayError FileError InputDecodeError
fileErr)
    ShelleyKeyCmdWriteFileError FileError ()
fileErr -> String -> Text
Text.pack (FileError () -> String
forall e. Error e => e -> String
displayError FileError ()
fileErr)
    ShelleyKeyCmdByronKeyFailure ByronKeyFailure
e -> ByronKeyFailure -> Text
Byron.renderByronKeyFailure ByronKeyFailure
e
    ShelleyKeyCmdByronKeyParseError Text
errTxt -> Text
errTxt
    ShelleyKeyCmdItnKeyConvError ItnKeyConversionError
convErr -> ItnKeyConversionError -> Text
renderConversionError ItnKeyConversionError
convErr
    ShelleyKeyCmdError
ShelleyKeyCmdWrongKeyTypeError -> String -> Text
Text.pack String
"Please use a signing key file \
                                   \when converting ITN BIP32 or Extended keys"
    ShelleyKeyCmdCardanoAddressSigningKeyFileError FileError CardanoAddressSigningKeyConversionError
fileErr ->
      String -> Text
Text.pack (FileError CardanoAddressSigningKeyConversionError -> String
forall e. Error e => e -> String
displayError FileError CardanoAddressSigningKeyConversionError
fileErr)
    ShelleyKeyCmdNonLegacyKey String
fp -> Text
"Signing key at: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
Text.pack String
fp Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" is not a legacy Byron signing key and should \
                                    \ not need to be converted."

runKeyCmd :: KeyCmd -> ExceptT ShelleyKeyCmdError IO ()
runKeyCmd :: KeyCmd -> ExceptT ShelleyKeyCmdError IO ()
runKeyCmd KeyCmd
cmd =
  case KeyCmd
cmd of
    KeyGetVerificationKey SigningKeyFile
skf VerificationKeyFile
vkf ->
      SigningKeyFile
-> VerificationKeyFile -> ExceptT ShelleyKeyCmdError IO ()
runGetVerificationKey SigningKeyFile
skf VerificationKeyFile
vkf

    KeyNonExtendedKey VerificationKeyFile
evkf VerificationKeyFile
vkf ->
      VerificationKeyFile
-> VerificationKeyFile -> ExceptT ShelleyKeyCmdError IO ()
runNonExtendedKey VerificationKeyFile
evkf VerificationKeyFile
vkf

    KeyConvertByronKey Maybe Text
mPassword ByronKeyType
keytype SomeKeyFile
skfOld OutputFile
skfNew ->
      Maybe Text
-> ByronKeyType
-> SomeKeyFile
-> OutputFile
-> ExceptT ShelleyKeyCmdError IO ()
runConvertByronKey Maybe Text
mPassword ByronKeyType
keytype SomeKeyFile
skfOld OutputFile
skfNew

    KeyConvertByronGenesisVKey VerificationKeyBase64
oldVk OutputFile
newVkf ->
      VerificationKeyBase64
-> OutputFile -> ExceptT ShelleyKeyCmdError IO ()
runConvertByronGenesisVerificationKey VerificationKeyBase64
oldVk OutputFile
newVkf

    KeyConvertITNStakeKey SomeKeyFile
itnKeyFile OutputFile
outFile ->
      SomeKeyFile -> OutputFile -> ExceptT ShelleyKeyCmdError IO ()
runConvertITNStakeKey SomeKeyFile
itnKeyFile OutputFile
outFile
    KeyConvertITNExtendedToStakeKey SomeKeyFile
itnPrivKeyFile OutputFile
outFile ->
      SomeKeyFile -> OutputFile -> ExceptT ShelleyKeyCmdError IO ()
runConvertITNExtendedToStakeKey SomeKeyFile
itnPrivKeyFile OutputFile
outFile
    KeyConvertITNBip32ToStakeKey SomeKeyFile
itnPrivKeyFile OutputFile
outFile ->
      SomeKeyFile -> OutputFile -> ExceptT ShelleyKeyCmdError IO ()
runConvertITNBip32ToStakeKey SomeKeyFile
itnPrivKeyFile OutputFile
outFile

    KeyConvertCardanoAddressSigningKey CardanoAddressKeyType
keyType SigningKeyFile
skfOld OutputFile
skfNew ->
      CardanoAddressKeyType
-> SigningKeyFile -> OutputFile -> ExceptT ShelleyKeyCmdError IO ()
runConvertCardanoAddressSigningKey CardanoAddressKeyType
keyType SigningKeyFile
skfOld OutputFile
skfNew

runGetVerificationKey :: SigningKeyFile
                      -> VerificationKeyFile
                      -> ExceptT ShelleyKeyCmdError IO ()
runGetVerificationKey :: SigningKeyFile
-> VerificationKeyFile -> ExceptT ShelleyKeyCmdError IO ()
runGetVerificationKey SigningKeyFile
skf (VerificationKeyFile String
vkf) = do
    SomeSigningKey
ssk <- (FileError InputDecodeError -> ShelleyKeyCmdError)
-> ExceptT (FileError InputDecodeError) IO SomeSigningKey
-> ExceptT ShelleyKeyCmdError IO SomeSigningKey
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError InputDecodeError -> ShelleyKeyCmdError
ShelleyKeyCmdReadKeyFileError (ExceptT (FileError InputDecodeError) IO SomeSigningKey
 -> ExceptT ShelleyKeyCmdError IO SomeSigningKey)
-> ExceptT (FileError InputDecodeError) IO SomeSigningKey
-> ExceptT ShelleyKeyCmdError IO SomeSigningKey
forall a b. (a -> b) -> a -> b
$
             SigningKeyFile
-> ExceptT (FileError InputDecodeError) IO SomeSigningKey
readSigningKeyFile SigningKeyFile
skf
    SomeSigningKey
-> (forall keyrole.
    Key keyrole =>
    SigningKey keyrole -> ExceptT ShelleyKeyCmdError IO ())
-> ExceptT ShelleyKeyCmdError IO ()
forall a.
SomeSigningKey
-> (forall keyrole. Key keyrole => SigningKey keyrole -> a) -> a
withSomeSigningKey SomeSigningKey
ssk ((forall keyrole.
  Key keyrole =>
  SigningKey keyrole -> ExceptT ShelleyKeyCmdError IO ())
 -> ExceptT ShelleyKeyCmdError IO ())
-> (forall keyrole.
    Key keyrole =>
    SigningKey keyrole -> ExceptT ShelleyKeyCmdError IO ())
-> ExceptT ShelleyKeyCmdError IO ()
forall a b. (a -> b) -> a -> b
$ \SigningKey keyrole
sk ->
      let vk :: VerificationKey keyrole
vk = SigningKey keyrole -> VerificationKey keyrole
forall keyrole.
Key keyrole =>
SigningKey keyrole -> VerificationKey keyrole
getVerificationKey SigningKey keyrole
sk in
      (FileError () -> ShelleyKeyCmdError)
-> ExceptT (FileError ()) IO () -> ExceptT ShelleyKeyCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError () -> ShelleyKeyCmdError
ShelleyKeyCmdWriteFileError (ExceptT (FileError ()) IO () -> ExceptT ShelleyKeyCmdError IO ())
-> (IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ())
-> IO (Either (FileError ()) ())
-> ExceptT ShelleyKeyCmdError 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 ShelleyKeyCmdError IO ())
-> IO (Either (FileError ()) ())
-> ExceptT ShelleyKeyCmdError IO ()
forall a b. (a -> b) -> a -> b
$
        String
-> Maybe TextEnvelopeDescr
-> VerificationKey keyrole
-> IO (Either (FileError ()) ())
forall a.
HasTextEnvelope a =>
String
-> Maybe TextEnvelopeDescr -> a -> IO (Either (FileError ()) ())
writeFileTextEnvelope String
vkf Maybe TextEnvelopeDescr
forall a. Maybe a
Nothing VerificationKey keyrole
vk


data SomeSigningKey
  = AByronSigningKey           (SigningKey ByronKey)
  | APaymentSigningKey         (SigningKey PaymentKey)
  | APaymentExtendedSigningKey (SigningKey PaymentExtendedKey)
  | AStakeSigningKey           (SigningKey StakeKey)
  | AStakeExtendedSigningKey   (SigningKey StakeExtendedKey)
  | AStakePoolSigningKey       (SigningKey StakePoolKey)
  | AGenesisSigningKey         (SigningKey GenesisKey)
  | AGenesisExtendedSigningKey (SigningKey GenesisExtendedKey)
  | AGenesisDelegateSigningKey (SigningKey GenesisDelegateKey)
  | AGenesisDelegateExtendedSigningKey
                               (SigningKey GenesisDelegateExtendedKey)
  | AGenesisUTxOSigningKey     (SigningKey GenesisUTxOKey)
  | AVrfSigningKey             (SigningKey VrfKey)
  | AKesSigningKey             (SigningKey KesKey)

withSomeSigningKey :: SomeSigningKey
                   -> (forall keyrole. Key keyrole => SigningKey keyrole -> a)
                   -> a
withSomeSigningKey :: SomeSigningKey
-> (forall keyrole. Key keyrole => SigningKey keyrole -> a) -> a
withSomeSigningKey SomeSigningKey
ssk forall keyrole. Key keyrole => SigningKey keyrole -> a
f =
    case SomeSigningKey
ssk of
      AByronSigningKey           SigningKey ByronKey
sk -> SigningKey ByronKey -> a
forall keyrole. Key keyrole => SigningKey keyrole -> a
f SigningKey ByronKey
sk
      APaymentSigningKey         SigningKey PaymentKey
sk -> SigningKey PaymentKey -> a
forall keyrole. Key keyrole => SigningKey keyrole -> a
f SigningKey PaymentKey
sk
      APaymentExtendedSigningKey SigningKey PaymentExtendedKey
sk -> SigningKey PaymentExtendedKey -> a
forall keyrole. Key keyrole => SigningKey keyrole -> a
f SigningKey PaymentExtendedKey
sk
      AStakeSigningKey           SigningKey StakeKey
sk -> SigningKey StakeKey -> a
forall keyrole. Key keyrole => SigningKey keyrole -> a
f SigningKey StakeKey
sk
      AStakeExtendedSigningKey   SigningKey StakeExtendedKey
sk -> SigningKey StakeExtendedKey -> a
forall keyrole. Key keyrole => SigningKey keyrole -> a
f SigningKey StakeExtendedKey
sk
      AStakePoolSigningKey       SigningKey StakePoolKey
sk -> SigningKey StakePoolKey -> a
forall keyrole. Key keyrole => SigningKey keyrole -> a
f SigningKey StakePoolKey
sk
      AGenesisSigningKey         SigningKey GenesisKey
sk -> SigningKey GenesisKey -> a
forall keyrole. Key keyrole => SigningKey keyrole -> a
f SigningKey GenesisKey
sk
      AGenesisExtendedSigningKey SigningKey GenesisExtendedKey
sk -> SigningKey GenesisExtendedKey -> a
forall keyrole. Key keyrole => SigningKey keyrole -> a
f SigningKey GenesisExtendedKey
sk
      AGenesisDelegateSigningKey SigningKey GenesisDelegateKey
sk -> SigningKey GenesisDelegateKey -> a
forall keyrole. Key keyrole => SigningKey keyrole -> a
f SigningKey GenesisDelegateKey
sk
      AGenesisDelegateExtendedSigningKey
                                 SigningKey GenesisDelegateExtendedKey
sk -> SigningKey GenesisDelegateExtendedKey -> a
forall keyrole. Key keyrole => SigningKey keyrole -> a
f SigningKey GenesisDelegateExtendedKey
sk
      AGenesisUTxOSigningKey     SigningKey GenesisUTxOKey
sk -> SigningKey GenesisUTxOKey -> a
forall keyrole. Key keyrole => SigningKey keyrole -> a
f SigningKey GenesisUTxOKey
sk
      AVrfSigningKey             SigningKey VrfKey
sk -> SigningKey VrfKey -> a
forall keyrole. Key keyrole => SigningKey keyrole -> a
f SigningKey VrfKey
sk
      AKesSigningKey             SigningKey KesKey
sk -> SigningKey KesKey -> a
forall keyrole. Key keyrole => SigningKey keyrole -> a
f SigningKey KesKey
sk

readSigningKeyFile
  :: SigningKeyFile
  -> ExceptT (FileError InputDecodeError) IO SomeSigningKey
readSigningKeyFile :: SigningKeyFile
-> ExceptT (FileError InputDecodeError) IO SomeSigningKey
readSigningKeyFile SigningKeyFile
skFile =
    IO (Either (FileError InputDecodeError) SomeSigningKey)
-> ExceptT (FileError InputDecodeError) IO SomeSigningKey
forall (m :: * -> *) x a. m (Either x a) -> ExceptT x m a
newExceptT (IO (Either (FileError InputDecodeError) SomeSigningKey)
 -> ExceptT (FileError InputDecodeError) IO SomeSigningKey)
-> IO (Either (FileError InputDecodeError) SomeSigningKey)
-> ExceptT (FileError InputDecodeError) IO SomeSigningKey
forall a b. (a -> b) -> a -> b
$
      [FromSomeType SerialiseAsBech32 SomeSigningKey]
-> [FromSomeType HasTextEnvelope SomeSigningKey]
-> SigningKeyFile
-> IO (Either (FileError InputDecodeError) SomeSigningKey)
forall b.
[FromSomeType SerialiseAsBech32 b]
-> [FromSomeType HasTextEnvelope b]
-> SigningKeyFile
-> IO (Either (FileError InputDecodeError) b)
readSigningKeyFileAnyOf [FromSomeType SerialiseAsBech32 SomeSigningKey]
bech32FileTypes [FromSomeType HasTextEnvelope SomeSigningKey]
textEnvFileTypes SigningKeyFile
skFile
  where
    textEnvFileTypes :: [FromSomeType HasTextEnvelope SomeSigningKey]
textEnvFileTypes =
      [ AsType (SigningKey ByronKey)
-> (SigningKey ByronKey -> SomeSigningKey)
-> FromSomeType HasTextEnvelope SomeSigningKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType ByronKey -> AsType (SigningKey ByronKey)
forall a. AsType a -> AsType (SigningKey a)
AsSigningKey AsType ByronKey
AsByronKey)
                      SigningKey ByronKey -> SomeSigningKey
AByronSigningKey
      , AsType (SigningKey PaymentKey)
-> (SigningKey PaymentKey -> SomeSigningKey)
-> FromSomeType HasTextEnvelope SomeSigningKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType PaymentKey -> AsType (SigningKey PaymentKey)
forall a. AsType a -> AsType (SigningKey a)
AsSigningKey AsType PaymentKey
AsPaymentKey)
                      SigningKey PaymentKey -> SomeSigningKey
APaymentSigningKey
      , AsType (SigningKey PaymentExtendedKey)
-> (SigningKey PaymentExtendedKey -> SomeSigningKey)
-> FromSomeType HasTextEnvelope SomeSigningKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType PaymentExtendedKey -> AsType (SigningKey PaymentExtendedKey)
forall a. AsType a -> AsType (SigningKey a)
AsSigningKey AsType PaymentExtendedKey
AsPaymentExtendedKey)
                      SigningKey PaymentExtendedKey -> SomeSigningKey
APaymentExtendedSigningKey
      , AsType (SigningKey StakeKey)
-> (SigningKey StakeKey -> SomeSigningKey)
-> FromSomeType HasTextEnvelope SomeSigningKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType StakeKey -> AsType (SigningKey StakeKey)
forall a. AsType a -> AsType (SigningKey a)
AsSigningKey AsType StakeKey
AsStakeKey)
                      SigningKey StakeKey -> SomeSigningKey
AStakeSigningKey
      , AsType (SigningKey StakeExtendedKey)
-> (SigningKey StakeExtendedKey -> SomeSigningKey)
-> FromSomeType HasTextEnvelope SomeSigningKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType StakeExtendedKey -> AsType (SigningKey StakeExtendedKey)
forall a. AsType a -> AsType (SigningKey a)
AsSigningKey AsType StakeExtendedKey
AsStakeExtendedKey)
                      SigningKey StakeExtendedKey -> SomeSigningKey
AStakeExtendedSigningKey
      , AsType (SigningKey StakePoolKey)
-> (SigningKey StakePoolKey -> SomeSigningKey)
-> FromSomeType HasTextEnvelope SomeSigningKey
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 -> SomeSigningKey
AStakePoolSigningKey
      , AsType (SigningKey GenesisKey)
-> (SigningKey GenesisKey -> SomeSigningKey)
-> FromSomeType HasTextEnvelope SomeSigningKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType GenesisKey -> AsType (SigningKey GenesisKey)
forall a. AsType a -> AsType (SigningKey a)
AsSigningKey AsType GenesisKey
AsGenesisKey)
                      SigningKey GenesisKey -> SomeSigningKey
AGenesisSigningKey
      , AsType (SigningKey GenesisExtendedKey)
-> (SigningKey GenesisExtendedKey -> SomeSigningKey)
-> FromSomeType HasTextEnvelope SomeSigningKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType GenesisExtendedKey -> AsType (SigningKey GenesisExtendedKey)
forall a. AsType a -> AsType (SigningKey a)
AsSigningKey AsType GenesisExtendedKey
AsGenesisExtendedKey)
                      SigningKey GenesisExtendedKey -> SomeSigningKey
AGenesisExtendedSigningKey
      , AsType (SigningKey GenesisDelegateKey)
-> (SigningKey GenesisDelegateKey -> SomeSigningKey)
-> FromSomeType HasTextEnvelope SomeSigningKey
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 GenesisDelegateKey -> SomeSigningKey
AGenesisDelegateSigningKey
      , AsType (SigningKey GenesisDelegateExtendedKey)
-> (SigningKey GenesisDelegateExtendedKey -> SomeSigningKey)
-> FromSomeType HasTextEnvelope SomeSigningKey
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 -> SomeSigningKey
AGenesisDelegateExtendedSigningKey
      , AsType (SigningKey GenesisUTxOKey)
-> (SigningKey GenesisUTxOKey -> SomeSigningKey)
-> FromSomeType HasTextEnvelope SomeSigningKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType GenesisUTxOKey -> AsType (SigningKey GenesisUTxOKey)
forall a. AsType a -> AsType (SigningKey a)
AsSigningKey AsType GenesisUTxOKey
AsGenesisUTxOKey)
                      SigningKey GenesisUTxOKey -> SomeSigningKey
AGenesisUTxOSigningKey
      , AsType (SigningKey VrfKey)
-> (SigningKey VrfKey -> SomeSigningKey)
-> FromSomeType HasTextEnvelope SomeSigningKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType VrfKey -> AsType (SigningKey VrfKey)
forall a. AsType a -> AsType (SigningKey a)
AsSigningKey AsType VrfKey
AsVrfKey)
                      SigningKey VrfKey -> SomeSigningKey
AVrfSigningKey
      , AsType (SigningKey KesKey)
-> (SigningKey KesKey -> SomeSigningKey)
-> FromSomeType HasTextEnvelope SomeSigningKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType KesKey -> AsType (SigningKey KesKey)
forall a. AsType a -> AsType (SigningKey a)
AsSigningKey AsType KesKey
AsKesKey)
                      SigningKey KesKey -> SomeSigningKey
AKesSigningKey
      ]

    bech32FileTypes :: [FromSomeType SerialiseAsBech32 SomeSigningKey]
bech32FileTypes =
      [ AsType (SigningKey PaymentKey)
-> (SigningKey PaymentKey -> SomeSigningKey)
-> FromSomeType SerialiseAsBech32 SomeSigningKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType PaymentKey -> AsType (SigningKey PaymentKey)
forall a. AsType a -> AsType (SigningKey a)
AsSigningKey AsType PaymentKey
AsPaymentKey)
                      SigningKey PaymentKey -> SomeSigningKey
APaymentSigningKey
      , AsType (SigningKey PaymentExtendedKey)
-> (SigningKey PaymentExtendedKey -> SomeSigningKey)
-> FromSomeType SerialiseAsBech32 SomeSigningKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType PaymentExtendedKey -> AsType (SigningKey PaymentExtendedKey)
forall a. AsType a -> AsType (SigningKey a)
AsSigningKey AsType PaymentExtendedKey
AsPaymentExtendedKey)
                      SigningKey PaymentExtendedKey -> SomeSigningKey
APaymentExtendedSigningKey
      , AsType (SigningKey StakeKey)
-> (SigningKey StakeKey -> SomeSigningKey)
-> FromSomeType SerialiseAsBech32 SomeSigningKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType StakeKey -> AsType (SigningKey StakeKey)
forall a. AsType a -> AsType (SigningKey a)
AsSigningKey AsType StakeKey
AsStakeKey)
                      SigningKey StakeKey -> SomeSigningKey
AStakeSigningKey
      , AsType (SigningKey StakeExtendedKey)
-> (SigningKey StakeExtendedKey -> SomeSigningKey)
-> FromSomeType SerialiseAsBech32 SomeSigningKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType StakeExtendedKey -> AsType (SigningKey StakeExtendedKey)
forall a. AsType a -> AsType (SigningKey a)
AsSigningKey AsType StakeExtendedKey
AsStakeExtendedKey)
                      SigningKey StakeExtendedKey -> SomeSigningKey
AStakeExtendedSigningKey
      , AsType (SigningKey StakePoolKey)
-> (SigningKey StakePoolKey -> SomeSigningKey)
-> FromSomeType SerialiseAsBech32 SomeSigningKey
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 -> SomeSigningKey
AStakePoolSigningKey
      , AsType (SigningKey VrfKey)
-> (SigningKey VrfKey -> SomeSigningKey)
-> FromSomeType SerialiseAsBech32 SomeSigningKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType VrfKey -> AsType (SigningKey VrfKey)
forall a. AsType a -> AsType (SigningKey a)
AsSigningKey AsType VrfKey
AsVrfKey)
                      SigningKey VrfKey -> SomeSigningKey
AVrfSigningKey
      , AsType (SigningKey KesKey)
-> (SigningKey KesKey -> SomeSigningKey)
-> FromSomeType SerialiseAsBech32 SomeSigningKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType KesKey -> AsType (SigningKey KesKey)
forall a. AsType a -> AsType (SigningKey a)
AsSigningKey AsType KesKey
AsKesKey)
                      SigningKey KesKey -> SomeSigningKey
AKesSigningKey
      ]


runNonExtendedKey :: VerificationKeyFile
                  -> VerificationKeyFile
                  -> ExceptT ShelleyKeyCmdError IO ()
runNonExtendedKey :: VerificationKeyFile
-> VerificationKeyFile -> ExceptT ShelleyKeyCmdError IO ()
runNonExtendedKey VerificationKeyFile
evkf (VerificationKeyFile String
vkf) = do
    SomeExtendedVerificationKey
evk <- (FileError TextEnvelopeError -> ShelleyKeyCmdError)
-> ExceptT
     (FileError TextEnvelopeError) IO SomeExtendedVerificationKey
-> ExceptT ShelleyKeyCmdError IO SomeExtendedVerificationKey
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError TextEnvelopeError -> ShelleyKeyCmdError
ShelleyKeyCmdReadFileError (ExceptT
   (FileError TextEnvelopeError) IO SomeExtendedVerificationKey
 -> ExceptT ShelleyKeyCmdError IO SomeExtendedVerificationKey)
-> ExceptT
     (FileError TextEnvelopeError) IO SomeExtendedVerificationKey
-> ExceptT ShelleyKeyCmdError IO SomeExtendedVerificationKey
forall a b. (a -> b) -> a -> b
$
             VerificationKeyFile
-> ExceptT
     (FileError TextEnvelopeError) IO SomeExtendedVerificationKey
readExtendedVerificationKeyFile VerificationKeyFile
evkf
    SomeExtendedVerificationKey
-> (forall keyrole.
    Key keyrole =>
    VerificationKey keyrole -> ExceptT ShelleyKeyCmdError IO ())
-> ExceptT ShelleyKeyCmdError IO ()
forall a.
SomeExtendedVerificationKey
-> (forall keyrole. Key keyrole => VerificationKey keyrole -> a)
-> a
withNonExtendedKey SomeExtendedVerificationKey
evk ((forall keyrole.
  Key keyrole =>
  VerificationKey keyrole -> ExceptT ShelleyKeyCmdError IO ())
 -> ExceptT ShelleyKeyCmdError IO ())
-> (forall keyrole.
    Key keyrole =>
    VerificationKey keyrole -> ExceptT ShelleyKeyCmdError IO ())
-> ExceptT ShelleyKeyCmdError IO ()
forall a b. (a -> b) -> a -> b
$ \VerificationKey keyrole
vk ->
      (FileError () -> ShelleyKeyCmdError)
-> ExceptT (FileError ()) IO () -> ExceptT ShelleyKeyCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError () -> ShelleyKeyCmdError
ShelleyKeyCmdWriteFileError (ExceptT (FileError ()) IO () -> ExceptT ShelleyKeyCmdError IO ())
-> (IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ())
-> IO (Either (FileError ()) ())
-> ExceptT ShelleyKeyCmdError 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 ShelleyKeyCmdError IO ())
-> IO (Either (FileError ()) ())
-> ExceptT ShelleyKeyCmdError IO ()
forall a b. (a -> b) -> a -> b
$
        String
-> Maybe TextEnvelopeDescr
-> VerificationKey keyrole
-> IO (Either (FileError ()) ())
forall a.
HasTextEnvelope a =>
String
-> Maybe TextEnvelopeDescr -> a -> IO (Either (FileError ()) ())
writeFileTextEnvelope String
vkf Maybe TextEnvelopeDescr
forall a. Maybe a
Nothing VerificationKey keyrole
vk

withNonExtendedKey :: SomeExtendedVerificationKey
                   -> (forall keyrole. Key keyrole => VerificationKey keyrole -> a)
                   -> a
withNonExtendedKey :: SomeExtendedVerificationKey
-> (forall keyrole. Key keyrole => VerificationKey keyrole -> a)
-> a
withNonExtendedKey (APaymentExtendedVerificationKey VerificationKey PaymentExtendedKey
vk) forall keyrole. Key keyrole => VerificationKey keyrole -> a
f =
    VerificationKey PaymentKey -> a
forall keyrole. Key keyrole => VerificationKey keyrole -> a
f (VerificationKey PaymentExtendedKey -> VerificationKey PaymentKey
forall keyroleA keyroleB.
CastVerificationKeyRole keyroleA keyroleB =>
VerificationKey keyroleA -> VerificationKey keyroleB
castVerificationKey VerificationKey PaymentExtendedKey
vk :: VerificationKey PaymentKey)

withNonExtendedKey (AStakeExtendedVerificationKey VerificationKey StakeExtendedKey
vk) forall keyrole. Key keyrole => VerificationKey keyrole -> a
f =
    VerificationKey StakeKey -> a
forall keyrole. Key keyrole => VerificationKey keyrole -> a
f (VerificationKey StakeExtendedKey -> VerificationKey StakeKey
forall keyroleA keyroleB.
CastVerificationKeyRole keyroleA keyroleB =>
VerificationKey keyroleA -> VerificationKey keyroleB
castVerificationKey VerificationKey StakeExtendedKey
vk :: VerificationKey StakeKey)

withNonExtendedKey (AGenesisExtendedVerificationKey VerificationKey GenesisExtendedKey
vk) forall keyrole. Key keyrole => VerificationKey keyrole -> a
f =
    VerificationKey GenesisKey -> a
forall keyrole. Key keyrole => VerificationKey keyrole -> a
f (VerificationKey GenesisExtendedKey -> VerificationKey GenesisKey
forall keyroleA keyroleB.
CastVerificationKeyRole keyroleA keyroleB =>
VerificationKey keyroleA -> VerificationKey keyroleB
castVerificationKey VerificationKey GenesisExtendedKey
vk :: VerificationKey GenesisKey)

withNonExtendedKey (AGenesisDelegateExtendedVerificationKey VerificationKey GenesisDelegateExtendedKey
vk) forall keyrole. Key keyrole => VerificationKey keyrole -> a
f =
    VerificationKey GenesisDelegateKey -> a
forall keyrole. Key keyrole => VerificationKey keyrole -> a
f (VerificationKey GenesisDelegateExtendedKey
-> VerificationKey GenesisDelegateKey
forall keyroleA keyroleB.
CastVerificationKeyRole keyroleA keyroleB =>
VerificationKey keyroleA -> VerificationKey keyroleB
castVerificationKey VerificationKey GenesisDelegateExtendedKey
vk :: VerificationKey GenesisDelegateKey)


data SomeExtendedVerificationKey
  = APaymentExtendedVerificationKey (VerificationKey PaymentExtendedKey)
  | AStakeExtendedVerificationKey   (VerificationKey StakeExtendedKey)
  | AGenesisExtendedVerificationKey (VerificationKey GenesisExtendedKey)
  | AGenesisDelegateExtendedVerificationKey
                                    (VerificationKey GenesisDelegateExtendedKey)

readExtendedVerificationKeyFile
  :: VerificationKeyFile
  -> ExceptT (FileError TextEnvelopeError) IO SomeExtendedVerificationKey
readExtendedVerificationKeyFile :: VerificationKeyFile
-> ExceptT
     (FileError TextEnvelopeError) IO SomeExtendedVerificationKey
readExtendedVerificationKeyFile (VerificationKeyFile String
evkfile) =
    IO
  (Either (FileError TextEnvelopeError) SomeExtendedVerificationKey)
-> ExceptT
     (FileError TextEnvelopeError) IO SomeExtendedVerificationKey
forall (m :: * -> *) x a. m (Either x a) -> ExceptT x m a
newExceptT (IO
   (Either (FileError TextEnvelopeError) SomeExtendedVerificationKey)
 -> ExceptT
      (FileError TextEnvelopeError) IO SomeExtendedVerificationKey)
-> IO
     (Either (FileError TextEnvelopeError) SomeExtendedVerificationKey)
-> ExceptT
     (FileError TextEnvelopeError) IO SomeExtendedVerificationKey
forall a b. (a -> b) -> a -> b
$ [FromSomeType HasTextEnvelope SomeExtendedVerificationKey]
-> String
-> IO
     (Either (FileError TextEnvelopeError) SomeExtendedVerificationKey)
forall b.
[FromSomeType HasTextEnvelope b]
-> String -> IO (Either (FileError TextEnvelopeError) b)
readFileTextEnvelopeAnyOf [FromSomeType HasTextEnvelope SomeExtendedVerificationKey]
fileTypes String
evkfile
  where
    fileTypes :: [FromSomeType HasTextEnvelope SomeExtendedVerificationKey]
fileTypes =
      [ AsType (VerificationKey PaymentExtendedKey)
-> (VerificationKey PaymentExtendedKey
    -> SomeExtendedVerificationKey)
-> FromSomeType HasTextEnvelope SomeExtendedVerificationKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType PaymentExtendedKey
-> AsType (VerificationKey PaymentExtendedKey)
forall a. AsType a -> AsType (VerificationKey a)
AsVerificationKey AsType PaymentExtendedKey
AsPaymentExtendedKey)
                      VerificationKey PaymentExtendedKey -> SomeExtendedVerificationKey
APaymentExtendedVerificationKey
      , AsType (VerificationKey StakeExtendedKey)
-> (VerificationKey StakeExtendedKey
    -> SomeExtendedVerificationKey)
-> FromSomeType HasTextEnvelope SomeExtendedVerificationKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType StakeExtendedKey
-> AsType (VerificationKey StakeExtendedKey)
forall a. AsType a -> AsType (VerificationKey a)
AsVerificationKey AsType StakeExtendedKey
AsStakeExtendedKey)
                      VerificationKey StakeExtendedKey -> SomeExtendedVerificationKey
AStakeExtendedVerificationKey
      , AsType (VerificationKey GenesisExtendedKey)
-> (VerificationKey GenesisExtendedKey
    -> SomeExtendedVerificationKey)
-> FromSomeType HasTextEnvelope SomeExtendedVerificationKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType GenesisExtendedKey
-> AsType (VerificationKey GenesisExtendedKey)
forall a. AsType a -> AsType (VerificationKey a)
AsVerificationKey AsType GenesisExtendedKey
AsGenesisExtendedKey)
                      VerificationKey GenesisExtendedKey -> SomeExtendedVerificationKey
AGenesisExtendedVerificationKey
      , AsType (VerificationKey GenesisDelegateExtendedKey)
-> (VerificationKey GenesisDelegateExtendedKey
    -> SomeExtendedVerificationKey)
-> FromSomeType HasTextEnvelope SomeExtendedVerificationKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType GenesisDelegateExtendedKey
-> AsType (VerificationKey GenesisDelegateExtendedKey)
forall a. AsType a -> AsType (VerificationKey a)
AsVerificationKey AsType GenesisDelegateExtendedKey
AsGenesisDelegateExtendedKey)
                      VerificationKey GenesisDelegateExtendedKey
-> SomeExtendedVerificationKey
AGenesisDelegateExtendedVerificationKey
      ]


runConvertByronKey
  :: Maybe Text      -- ^ Password (if applicable)
  -> ByronKeyType
  -> SomeKeyFile     -- ^ Input file: old format
  -> OutputFile      -- ^ Output file: new format
  -> ExceptT ShelleyKeyCmdError IO ()
runConvertByronKey :: Maybe Text
-> ByronKeyType
-> SomeKeyFile
-> OutputFile
-> ExceptT ShelleyKeyCmdError IO ()
runConvertByronKey Maybe Text
mPwd (ByronPaymentKey ByronKeyFormat
format) (ASigningKeyFile SigningKeyFile
skeyPathOld) =
    Maybe Text
-> ByronKeyFormat
-> (SigningKey -> SigningKey ByronKey)
-> SigningKeyFile
-> OutputFile
-> ExceptT ShelleyKeyCmdError IO ()
forall keyrole.
Key keyrole =>
Maybe Text
-> ByronKeyFormat
-> (SigningKey -> SigningKey keyrole)
-> SigningKeyFile
-> OutputFile
-> ExceptT ShelleyKeyCmdError IO ()
convertByronSigningKey Maybe Text
mPwd ByronKeyFormat
format SigningKey -> SigningKey ByronKey
convert SigningKeyFile
skeyPathOld
  where
    convert :: Byron.SigningKey -> SigningKey ByronKey
    convert :: SigningKey -> SigningKey ByronKey
convert = SigningKey -> SigningKey ByronKey
ByronSigningKey

runConvertByronKey Maybe Text
mPwd (ByronGenesisKey ByronKeyFormat
format) (ASigningKeyFile SigningKeyFile
skeyPathOld) =
    Maybe Text
-> ByronKeyFormat
-> (SigningKey -> SigningKey GenesisExtendedKey)
-> SigningKeyFile
-> OutputFile
-> ExceptT ShelleyKeyCmdError IO ()
forall keyrole.
Key keyrole =>
Maybe Text
-> ByronKeyFormat
-> (SigningKey -> SigningKey keyrole)
-> SigningKeyFile
-> OutputFile
-> ExceptT ShelleyKeyCmdError IO ()
convertByronSigningKey Maybe Text
mPwd ByronKeyFormat
format SigningKey -> SigningKey GenesisExtendedKey
convert SigningKeyFile
skeyPathOld
  where
    convert :: Byron.SigningKey -> SigningKey GenesisExtendedKey
    convert :: SigningKey -> SigningKey GenesisExtendedKey
convert (Byron.SigningKey XPrv
xsk) = XPrv -> SigningKey GenesisExtendedKey
GenesisExtendedSigningKey XPrv
xsk

runConvertByronKey Maybe Text
mPwd (ByronDelegateKey ByronKeyFormat
format) (ASigningKeyFile SigningKeyFile
skeyPathOld) =
    Maybe Text
-> ByronKeyFormat
-> (SigningKey -> SigningKey GenesisDelegateExtendedKey)
-> SigningKeyFile
-> OutputFile
-> ExceptT ShelleyKeyCmdError IO ()
forall keyrole.
Key keyrole =>
Maybe Text
-> ByronKeyFormat
-> (SigningKey -> SigningKey keyrole)
-> SigningKeyFile
-> OutputFile
-> ExceptT ShelleyKeyCmdError IO ()
convertByronSigningKey Maybe Text
mPwd ByronKeyFormat
format SigningKey -> SigningKey GenesisDelegateExtendedKey
convert SigningKeyFile
skeyPathOld
  where
    convert :: Byron.SigningKey -> SigningKey GenesisDelegateExtendedKey
    convert :: SigningKey -> SigningKey GenesisDelegateExtendedKey
convert (Byron.SigningKey XPrv
xsk) = XPrv -> SigningKey GenesisDelegateExtendedKey
GenesisDelegateExtendedSigningKey XPrv
xsk

runConvertByronKey Maybe Text
_ (ByronPaymentKey ByronKeyFormat
NonLegacyByronKeyFormat)
                     (AVerificationKeyFile VerificationKeyFile
vkeyPathOld) =
    (VerificationKey -> VerificationKey ByronKey)
-> VerificationKeyFile
-> OutputFile
-> ExceptT ShelleyKeyCmdError IO ()
forall keyrole.
Key keyrole =>
(VerificationKey -> VerificationKey keyrole)
-> VerificationKeyFile
-> OutputFile
-> ExceptT ShelleyKeyCmdError IO ()
convertByronVerificationKey VerificationKey -> VerificationKey ByronKey
convert VerificationKeyFile
vkeyPathOld
  where
    convert :: Byron.VerificationKey -> VerificationKey ByronKey
    convert :: VerificationKey -> VerificationKey ByronKey
convert = VerificationKey -> VerificationKey ByronKey
ByronVerificationKey

runConvertByronKey Maybe Text
_ (ByronGenesisKey ByronKeyFormat
NonLegacyByronKeyFormat)
                     (AVerificationKeyFile VerificationKeyFile
vkeyPathOld) =
    (VerificationKey -> VerificationKey GenesisExtendedKey)
-> VerificationKeyFile
-> OutputFile
-> ExceptT ShelleyKeyCmdError IO ()
forall keyrole.
Key keyrole =>
(VerificationKey -> VerificationKey keyrole)
-> VerificationKeyFile
-> OutputFile
-> ExceptT ShelleyKeyCmdError IO ()
convertByronVerificationKey VerificationKey -> VerificationKey GenesisExtendedKey
convert VerificationKeyFile
vkeyPathOld
  where
    convert :: Byron.VerificationKey -> VerificationKey GenesisExtendedKey
    convert :: VerificationKey -> VerificationKey GenesisExtendedKey
convert (Byron.VerificationKey XPub
xvk) = XPub -> VerificationKey GenesisExtendedKey
GenesisExtendedVerificationKey XPub
xvk

runConvertByronKey Maybe Text
_ (ByronDelegateKey ByronKeyFormat
NonLegacyByronKeyFormat)
                     (AVerificationKeyFile VerificationKeyFile
vkeyPathOld) =
    (VerificationKey -> VerificationKey GenesisDelegateExtendedKey)
-> VerificationKeyFile
-> OutputFile
-> ExceptT ShelleyKeyCmdError IO ()
forall keyrole.
Key keyrole =>
(VerificationKey -> VerificationKey keyrole)
-> VerificationKeyFile
-> OutputFile
-> ExceptT ShelleyKeyCmdError IO ()
convertByronVerificationKey VerificationKey -> VerificationKey GenesisDelegateExtendedKey
convert VerificationKeyFile
vkeyPathOld
  where
    convert :: Byron.VerificationKey
            -> VerificationKey GenesisDelegateExtendedKey
    convert :: VerificationKey -> VerificationKey GenesisDelegateExtendedKey
convert (Byron.VerificationKey XPub
xvk) =
      XPub -> VerificationKey GenesisDelegateExtendedKey
GenesisDelegateExtendedVerificationKey XPub
xvk

runConvertByronKey Maybe Text
_ (ByronPaymentKey  ByronKeyFormat
LegacyByronKeyFormat)
                      AVerificationKeyFile{} =
    ExceptT ShelleyKeyCmdError IO ()
-> OutputFile -> ExceptT ShelleyKeyCmdError IO ()
forall a b. a -> b -> a
const ExceptT ShelleyKeyCmdError IO ()
forall e a. ExceptT e IO a
legacyVerificationKeysNotSupported

runConvertByronKey Maybe Text
_ (ByronGenesisKey  ByronKeyFormat
LegacyByronKeyFormat)
                      AVerificationKeyFile{} =
    ExceptT ShelleyKeyCmdError IO ()
-> OutputFile -> ExceptT ShelleyKeyCmdError IO ()
forall a b. a -> b -> a
const ExceptT ShelleyKeyCmdError IO ()
forall e a. ExceptT e IO a
legacyVerificationKeysNotSupported

runConvertByronKey Maybe Text
_ (ByronDelegateKey ByronKeyFormat
LegacyByronKeyFormat)
                      AVerificationKeyFile{} =
    ExceptT ShelleyKeyCmdError IO ()
-> OutputFile -> ExceptT ShelleyKeyCmdError IO ()
forall a b. a -> b -> a
const ExceptT ShelleyKeyCmdError IO ()
forall e a. ExceptT e IO a
legacyVerificationKeysNotSupported

legacyVerificationKeysNotSupported :: ExceptT e IO a
legacyVerificationKeysNotSupported :: ExceptT e IO a
legacyVerificationKeysNotSupported =
    IO a -> ExceptT e IO a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO a -> ExceptT e IO a) -> IO a -> ExceptT e IO a
forall a b. (a -> b) -> a -> b
$ do
      String -> IO ()
forall a (m :: * -> *). (Print a, MonadIO m) => a -> m ()
putStrLn (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ String
"convert keys: byron legacy format not supported for "
              String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"verification keys. Convert the signing key and then get the "
              String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"verification key."
      IO a
forall a. IO a
exitFailure


convertByronSigningKey
  :: forall keyrole.
     Key keyrole
  => Maybe Text          -- ^ Password (if applicable)
  -> ByronKeyFormat
  -> (Byron.SigningKey -> SigningKey keyrole)
  -> SigningKeyFile      -- ^ Input file: old format
  -> OutputFile          -- ^ Output file: new format
  -> ExceptT ShelleyKeyCmdError IO ()
convertByronSigningKey :: Maybe Text
-> ByronKeyFormat
-> (SigningKey -> SigningKey keyrole)
-> SigningKeyFile
-> OutputFile
-> ExceptT ShelleyKeyCmdError IO ()
convertByronSigningKey Maybe Text
mPwd ByronKeyFormat
byronFormat SigningKey -> SigningKey keyrole
convert
                       SigningKeyFile
skeyPathOld
                       (OutputFile String
skeyPathNew) = do


    SomeByronSigningKey
sKey <- (ByronKeyFailure -> ShelleyKeyCmdError)
-> ExceptT ByronKeyFailure IO SomeByronSigningKey
-> ExceptT ShelleyKeyCmdError IO SomeByronSigningKey
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT ByronKeyFailure -> ShelleyKeyCmdError
ShelleyKeyCmdByronKeyFailure
              (ExceptT ByronKeyFailure IO SomeByronSigningKey
 -> ExceptT ShelleyKeyCmdError IO SomeByronSigningKey)
-> ExceptT ByronKeyFailure IO SomeByronSigningKey
-> ExceptT ShelleyKeyCmdError IO SomeByronSigningKey
forall a b. (a -> b) -> a -> b
$ ByronKeyFormat
-> SigningKeyFile -> ExceptT ByronKeyFailure IO SomeByronSigningKey
Byron.readByronSigningKey ByronKeyFormat
byronFormat SigningKeyFile
skeyPathOld

    -- Account for password protected legacy Byron keys
    SigningKey
unprotectedSk <- case SomeByronSigningKey
sKey of
                       ByronApi.AByronSigningKeyLegacy (ByronSigningKeyLegacy sk@(Crypto.SigningKey xprv)) ->
                         case Maybe Text
mPwd of
                           -- Change password to empty string
                           Just Text
pwd -> SigningKey -> ExceptT ShelleyKeyCmdError IO SigningKey
forall (m :: * -> *) a. Monad m => a -> m a
return (SigningKey -> ExceptT ShelleyKeyCmdError IO SigningKey)
-> (XPrv -> SigningKey)
-> XPrv
-> ExceptT ShelleyKeyCmdError IO SigningKey
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. XPrv -> SigningKey
Crypto.SigningKey
                                         (XPrv -> ExceptT ShelleyKeyCmdError IO SigningKey)
-> XPrv -> ExceptT ShelleyKeyCmdError IO SigningKey
forall a b. (a -> b) -> a -> b
$ ByteString -> ByteString -> XPrv -> XPrv
forall oldPassPhrase newPassPhrase.
(ByteArrayAccess oldPassPhrase, ByteArrayAccess newPassPhrase) =>
oldPassPhrase -> newPassPhrase -> XPrv -> XPrv
Crypto.xPrvChangePass (Text -> ByteString
encodeUtf8 Text
pwd) (Text -> ByteString
encodeUtf8 Text
"") XPrv
xprv
                           Maybe Text
Nothing -> SigningKey -> ExceptT ShelleyKeyCmdError IO SigningKey
forall (m :: * -> *) a. Monad m => a -> m a
return SigningKey
sk
                       ByronApi.AByronSigningKey (ByronSigningKey sk) -> SigningKey -> ExceptT ShelleyKeyCmdError IO SigningKey
forall (m :: * -> *) a. Monad m => a -> m a
return SigningKey
sk


    let sk' :: SigningKey keyrole
        sk' :: SigningKey keyrole
sk' = SigningKey -> SigningKey keyrole
convert SigningKey
unprotectedSk

    (FileError () -> ShelleyKeyCmdError)
-> ExceptT (FileError ()) IO () -> ExceptT ShelleyKeyCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError () -> ShelleyKeyCmdError
ShelleyKeyCmdWriteFileError (ExceptT (FileError ()) IO () -> ExceptT ShelleyKeyCmdError IO ())
-> (IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ())
-> IO (Either (FileError ()) ())
-> ExceptT ShelleyKeyCmdError 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 ShelleyKeyCmdError IO ())
-> IO (Either (FileError ()) ())
-> ExceptT ShelleyKeyCmdError IO ()
forall a b. (a -> b) -> a -> b
$
      String
-> Maybe TextEnvelopeDescr
-> SigningKey keyrole
-> IO (Either (FileError ()) ())
forall a.
HasTextEnvelope a =>
String
-> Maybe TextEnvelopeDescr -> a -> IO (Either (FileError ()) ())
writeFileTextEnvelope String
skeyPathNew Maybe TextEnvelopeDescr
forall a. Maybe a
Nothing SigningKey keyrole
sk'

convertByronVerificationKey
  :: forall keyrole.
     Key keyrole
  => (Byron.VerificationKey -> VerificationKey keyrole)
  -> VerificationKeyFile -- ^ Input file: old format
  -> OutputFile          -- ^ Output file: new format
  -> ExceptT ShelleyKeyCmdError IO ()
convertByronVerificationKey :: (VerificationKey -> VerificationKey keyrole)
-> VerificationKeyFile
-> OutputFile
-> ExceptT ShelleyKeyCmdError IO ()
convertByronVerificationKey VerificationKey -> VerificationKey keyrole
convert
                            (VerificationKeyFile String
vkeyPathOld)
                            (OutputFile String
vkeyPathNew) = do

    VerificationKey
vk <- (ByronKeyFailure -> ShelleyKeyCmdError)
-> ExceptT ByronKeyFailure IO VerificationKey
-> ExceptT ShelleyKeyCmdError IO VerificationKey
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT ByronKeyFailure -> ShelleyKeyCmdError
ShelleyKeyCmdByronKeyFailure (ExceptT ByronKeyFailure IO VerificationKey
 -> ExceptT ShelleyKeyCmdError IO VerificationKey)
-> ExceptT ByronKeyFailure IO VerificationKey
-> ExceptT ShelleyKeyCmdError IO VerificationKey
forall a b. (a -> b) -> a -> b
$
            VerificationKeyFile -> ExceptT ByronKeyFailure IO VerificationKey
Byron.readPaymentVerificationKey (String -> VerificationKeyFile
Byron.VerificationKeyFile String
vkeyPathOld)

    let vk' :: VerificationKey keyrole
        vk' :: VerificationKey keyrole
vk' = VerificationKey -> VerificationKey keyrole
convert VerificationKey
vk

    (FileError () -> ShelleyKeyCmdError)
-> ExceptT (FileError ()) IO () -> ExceptT ShelleyKeyCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError () -> ShelleyKeyCmdError
ShelleyKeyCmdWriteFileError (ExceptT (FileError ()) IO () -> ExceptT ShelleyKeyCmdError IO ())
-> (IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ())
-> IO (Either (FileError ()) ())
-> ExceptT ShelleyKeyCmdError 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 ShelleyKeyCmdError IO ())
-> IO (Either (FileError ()) ())
-> ExceptT ShelleyKeyCmdError IO ()
forall a b. (a -> b) -> a -> b
$
      String
-> Maybe TextEnvelopeDescr
-> VerificationKey keyrole
-> IO (Either (FileError ()) ())
forall a.
HasTextEnvelope a =>
String
-> Maybe TextEnvelopeDescr -> a -> IO (Either (FileError ()) ())
writeFileTextEnvelope String
vkeyPathNew Maybe TextEnvelopeDescr
forall a. Maybe a
Nothing VerificationKey keyrole
vk'


runConvertByronGenesisVerificationKey
  :: VerificationKeyBase64  -- ^ Input key raw old format
  -> OutputFile             -- ^ Output file: new format
  -> ExceptT ShelleyKeyCmdError IO ()
runConvertByronGenesisVerificationKey :: VerificationKeyBase64
-> OutputFile -> ExceptT ShelleyKeyCmdError IO ()
runConvertByronGenesisVerificationKey (VerificationKeyBase64 String
b64ByronVKey)
                                      (OutputFile String
vkeyPathNew) = do

    VerificationKey
vk <- (VerificationKeyParseError -> ShelleyKeyCmdError)
-> ExceptT VerificationKeyParseError IO VerificationKey
-> ExceptT ShelleyKeyCmdError IO VerificationKey
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT (Text -> ShelleyKeyCmdError
ShelleyKeyCmdByronKeyParseError (Text -> ShelleyKeyCmdError)
-> (VerificationKeyParseError -> Text)
-> VerificationKeyParseError
-> ShelleyKeyCmdError
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. VerificationKeyParseError -> Text
forall a b. (Show a, ConvertText String b) => a -> b
show)
        (ExceptT VerificationKeyParseError IO VerificationKey
 -> ExceptT ShelleyKeyCmdError IO VerificationKey)
-> (String -> ExceptT VerificationKeyParseError IO VerificationKey)
-> String
-> ExceptT ShelleyKeyCmdError IO VerificationKey
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Either VerificationKeyParseError VerificationKey
-> ExceptT VerificationKeyParseError IO VerificationKey
forall (m :: * -> *) x a. Monad m => Either x a -> ExceptT x m a
hoistEither
        (Either VerificationKeyParseError VerificationKey
 -> ExceptT VerificationKeyParseError IO VerificationKey)
-> (String -> Either VerificationKeyParseError VerificationKey)
-> String
-> ExceptT VerificationKeyParseError IO VerificationKey
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Text -> Either VerificationKeyParseError VerificationKey
Byron.Crypto.parseFullVerificationKey
        (Text -> Either VerificationKeyParseError VerificationKey)
-> (String -> Text)
-> String
-> Either VerificationKeyParseError VerificationKey
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. String -> Text
Text.pack
        (String -> ExceptT ShelleyKeyCmdError IO VerificationKey)
-> String -> ExceptT ShelleyKeyCmdError IO VerificationKey
forall a b. (a -> b) -> a -> b
$ String
b64ByronVKey

    let vk' :: VerificationKey GenesisKey
        vk' :: VerificationKey GenesisKey
vk' = VerificationKey -> VerificationKey GenesisKey
convert VerificationKey
vk

    (FileError () -> ShelleyKeyCmdError)
-> ExceptT (FileError ()) IO () -> ExceptT ShelleyKeyCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError () -> ShelleyKeyCmdError
ShelleyKeyCmdWriteFileError (ExceptT (FileError ()) IO () -> ExceptT ShelleyKeyCmdError IO ())
-> (IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ())
-> IO (Either (FileError ()) ())
-> ExceptT ShelleyKeyCmdError 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 ShelleyKeyCmdError IO ())
-> IO (Either (FileError ()) ())
-> ExceptT ShelleyKeyCmdError IO ()
forall a b. (a -> b) -> a -> b
$
      String
-> Maybe TextEnvelopeDescr
-> VerificationKey GenesisKey
-> IO (Either (FileError ()) ())
forall a.
HasTextEnvelope a =>
String
-> Maybe TextEnvelopeDescr -> a -> IO (Either (FileError ()) ())
writeFileTextEnvelope String
vkeyPathNew Maybe TextEnvelopeDescr
forall a. Maybe a
Nothing VerificationKey GenesisKey
vk'
  where
    convert :: Byron.VerificationKey -> VerificationKey GenesisKey
    convert :: VerificationKey -> VerificationKey GenesisKey
convert (Byron.VerificationKey XPub
xvk) =
      VerificationKey GenesisExtendedKey -> VerificationKey GenesisKey
forall keyroleA keyroleB.
CastVerificationKeyRole keyroleA keyroleB =>
VerificationKey keyroleA -> VerificationKey keyroleB
castVerificationKey (XPub -> VerificationKey GenesisExtendedKey
GenesisExtendedVerificationKey XPub
xvk)


--------------------------------------------------------------------------------
-- ITN verification/signing key conversion to Haskell verficiation/signing keys
--------------------------------------------------------------------------------

runConvertITNStakeKey
  :: SomeKeyFile
  -> OutputFile
  -> ExceptT ShelleyKeyCmdError IO ()
runConvertITNStakeKey :: SomeKeyFile -> OutputFile -> ExceptT ShelleyKeyCmdError IO ()
runConvertITNStakeKey (AVerificationKeyFile (VerificationKeyFile String
vk)) (OutputFile String
outFile) = do
  Text
bech32publicKey <- (ItnKeyConversionError -> ShelleyKeyCmdError)
-> ExceptT ItnKeyConversionError IO Text
-> ExceptT ShelleyKeyCmdError IO Text
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT ItnKeyConversionError -> ShelleyKeyCmdError
ShelleyKeyCmdItnKeyConvError (ExceptT ItnKeyConversionError IO Text
 -> ExceptT ShelleyKeyCmdError IO Text)
-> (IO (Either ItnKeyConversionError Text)
    -> ExceptT ItnKeyConversionError IO Text)
-> IO (Either ItnKeyConversionError Text)
-> ExceptT ShelleyKeyCmdError IO Text
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. IO (Either ItnKeyConversionError Text)
-> ExceptT ItnKeyConversionError IO Text
forall (m :: * -> *) x a. m (Either x a) -> ExceptT x m a
newExceptT (IO (Either ItnKeyConversionError Text)
 -> ExceptT ShelleyKeyCmdError IO Text)
-> IO (Either ItnKeyConversionError Text)
-> ExceptT ShelleyKeyCmdError IO Text
forall a b. (a -> b) -> a -> b
$
                     String -> IO (Either ItnKeyConversionError Text)
readFileITNKey String
vk
  VerificationKey StakeKey
vkey <- Either ShelleyKeyCmdError (VerificationKey StakeKey)
-> ExceptT ShelleyKeyCmdError IO (VerificationKey StakeKey)
forall (m :: * -> *) x a. Monad m => Either x a -> ExceptT x m a
hoistEither
    (Either ShelleyKeyCmdError (VerificationKey StakeKey)
 -> ExceptT ShelleyKeyCmdError IO (VerificationKey StakeKey))
-> (Either ItnKeyConversionError (VerificationKey StakeKey)
    -> Either ShelleyKeyCmdError (VerificationKey StakeKey))
-> Either ItnKeyConversionError (VerificationKey StakeKey)
-> ExceptT ShelleyKeyCmdError IO (VerificationKey StakeKey)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (ItnKeyConversionError -> ShelleyKeyCmdError)
-> Either ItnKeyConversionError (VerificationKey StakeKey)
-> Either ShelleyKeyCmdError (VerificationKey StakeKey)
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first ItnKeyConversionError -> ShelleyKeyCmdError
ShelleyKeyCmdItnKeyConvError
    (Either ItnKeyConversionError (VerificationKey StakeKey)
 -> ExceptT ShelleyKeyCmdError IO (VerificationKey StakeKey))
-> Either ItnKeyConversionError (VerificationKey StakeKey)
-> ExceptT ShelleyKeyCmdError IO (VerificationKey StakeKey)
forall a b. (a -> b) -> a -> b
$ Text -> Either ItnKeyConversionError (VerificationKey StakeKey)
convertITNVerificationKey Text
bech32publicKey
  (FileError () -> ShelleyKeyCmdError)
-> ExceptT (FileError ()) IO () -> ExceptT ShelleyKeyCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError () -> ShelleyKeyCmdError
ShelleyKeyCmdWriteFileError (ExceptT (FileError ()) IO () -> ExceptT ShelleyKeyCmdError IO ())
-> (IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ())
-> IO (Either (FileError ()) ())
-> ExceptT ShelleyKeyCmdError 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 ShelleyKeyCmdError IO ())
-> IO (Either (FileError ()) ())
-> ExceptT ShelleyKeyCmdError IO ()
forall a b. (a -> b) -> a -> b
$
    String
-> Maybe TextEnvelopeDescr
-> VerificationKey StakeKey
-> IO (Either (FileError ()) ())
forall a.
HasTextEnvelope a =>
String
-> Maybe TextEnvelopeDescr -> a -> IO (Either (FileError ()) ())
writeFileTextEnvelope String
outFile Maybe TextEnvelopeDescr
forall a. Maybe a
Nothing VerificationKey StakeKey
vkey

runConvertITNStakeKey (ASigningKeyFile (SigningKeyFile String
sk)) (OutputFile String
outFile) = do
  Text
bech32privateKey <- (ItnKeyConversionError -> ShelleyKeyCmdError)
-> ExceptT ItnKeyConversionError IO Text
-> ExceptT ShelleyKeyCmdError IO Text
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT ItnKeyConversionError -> ShelleyKeyCmdError
ShelleyKeyCmdItnKeyConvError (ExceptT ItnKeyConversionError IO Text
 -> ExceptT ShelleyKeyCmdError IO Text)
-> (IO (Either ItnKeyConversionError Text)
    -> ExceptT ItnKeyConversionError IO Text)
-> IO (Either ItnKeyConversionError Text)
-> ExceptT ShelleyKeyCmdError IO Text
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. IO (Either ItnKeyConversionError Text)
-> ExceptT ItnKeyConversionError IO Text
forall (m :: * -> *) x a. m (Either x a) -> ExceptT x m a
newExceptT (IO (Either ItnKeyConversionError Text)
 -> ExceptT ShelleyKeyCmdError IO Text)
-> IO (Either ItnKeyConversionError Text)
-> ExceptT ShelleyKeyCmdError IO Text
forall a b. (a -> b) -> a -> b
$
                      String -> IO (Either ItnKeyConversionError Text)
readFileITNKey String
sk
  SigningKey StakeKey
skey <- Either ShelleyKeyCmdError (SigningKey StakeKey)
-> ExceptT ShelleyKeyCmdError IO (SigningKey StakeKey)
forall (m :: * -> *) x a. Monad m => Either x a -> ExceptT x m a
hoistEither
    (Either ShelleyKeyCmdError (SigningKey StakeKey)
 -> ExceptT ShelleyKeyCmdError IO (SigningKey StakeKey))
-> (Either ItnKeyConversionError (SigningKey StakeKey)
    -> Either ShelleyKeyCmdError (SigningKey StakeKey))
-> Either ItnKeyConversionError (SigningKey StakeKey)
-> ExceptT ShelleyKeyCmdError IO (SigningKey StakeKey)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (ItnKeyConversionError -> ShelleyKeyCmdError)
-> Either ItnKeyConversionError (SigningKey StakeKey)
-> Either ShelleyKeyCmdError (SigningKey StakeKey)
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first ItnKeyConversionError -> ShelleyKeyCmdError
ShelleyKeyCmdItnKeyConvError
    (Either ItnKeyConversionError (SigningKey StakeKey)
 -> ExceptT ShelleyKeyCmdError IO (SigningKey StakeKey))
-> Either ItnKeyConversionError (SigningKey StakeKey)
-> ExceptT ShelleyKeyCmdError IO (SigningKey StakeKey)
forall a b. (a -> b) -> a -> b
$ Text -> Either ItnKeyConversionError (SigningKey StakeKey)
convertITNSigningKey Text
bech32privateKey
  (FileError () -> ShelleyKeyCmdError)
-> ExceptT (FileError ()) IO () -> ExceptT ShelleyKeyCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError () -> ShelleyKeyCmdError
ShelleyKeyCmdWriteFileError (ExceptT (FileError ()) IO () -> ExceptT ShelleyKeyCmdError IO ())
-> (IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ())
-> IO (Either (FileError ()) ())
-> ExceptT ShelleyKeyCmdError 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 ShelleyKeyCmdError IO ())
-> IO (Either (FileError ()) ())
-> ExceptT ShelleyKeyCmdError IO ()
forall a b. (a -> b) -> a -> b
$
    String
-> Maybe TextEnvelopeDescr
-> SigningKey StakeKey
-> IO (Either (FileError ()) ())
forall a.
HasTextEnvelope a =>
String
-> Maybe TextEnvelopeDescr -> a -> IO (Either (FileError ()) ())
writeFileTextEnvelope String
outFile Maybe TextEnvelopeDescr
forall a. Maybe a
Nothing SigningKey StakeKey
skey

runConvertITNExtendedToStakeKey :: SomeKeyFile -> OutputFile -> ExceptT ShelleyKeyCmdError IO ()
runConvertITNExtendedToStakeKey :: SomeKeyFile -> OutputFile -> ExceptT ShelleyKeyCmdError IO ()
runConvertITNExtendedToStakeKey (AVerificationKeyFile VerificationKeyFile
_) OutputFile
_ = ShelleyKeyCmdError -> ExceptT ShelleyKeyCmdError IO ()
forall (m :: * -> *) x a. Monad m => x -> ExceptT x m a
left ShelleyKeyCmdError
ShelleyKeyCmdWrongKeyTypeError
runConvertITNExtendedToStakeKey (ASigningKeyFile (SigningKeyFile String
sk)) (OutputFile String
outFile) = do
  Text
bech32privateKey <- (ItnKeyConversionError -> ShelleyKeyCmdError)
-> ExceptT ItnKeyConversionError IO Text
-> ExceptT ShelleyKeyCmdError IO Text
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT ItnKeyConversionError -> ShelleyKeyCmdError
ShelleyKeyCmdItnKeyConvError (ExceptT ItnKeyConversionError IO Text
 -> ExceptT ShelleyKeyCmdError IO Text)
-> (IO (Either ItnKeyConversionError Text)
    -> ExceptT ItnKeyConversionError IO Text)
-> IO (Either ItnKeyConversionError Text)
-> ExceptT ShelleyKeyCmdError IO Text
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. IO (Either ItnKeyConversionError Text)
-> ExceptT ItnKeyConversionError IO Text
forall (m :: * -> *) x a. m (Either x a) -> ExceptT x m a
newExceptT (IO (Either ItnKeyConversionError Text)
 -> ExceptT ShelleyKeyCmdError IO Text)
-> IO (Either ItnKeyConversionError Text)
-> ExceptT ShelleyKeyCmdError IO Text
forall a b. (a -> b) -> a -> b
$ String -> IO (Either ItnKeyConversionError Text)
readFileITNKey String
sk
  SigningKey StakeExtendedKey
skey <- Either ShelleyKeyCmdError (SigningKey StakeExtendedKey)
-> ExceptT ShelleyKeyCmdError IO (SigningKey StakeExtendedKey)
forall (m :: * -> *) x a. Monad m => Either x a -> ExceptT x m a
hoistEither (Either ShelleyKeyCmdError (SigningKey StakeExtendedKey)
 -> ExceptT ShelleyKeyCmdError IO (SigningKey StakeExtendedKey))
-> (Either ItnKeyConversionError (SigningKey StakeExtendedKey)
    -> Either ShelleyKeyCmdError (SigningKey StakeExtendedKey))
-> Either ItnKeyConversionError (SigningKey StakeExtendedKey)
-> ExceptT ShelleyKeyCmdError IO (SigningKey StakeExtendedKey)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (ItnKeyConversionError -> ShelleyKeyCmdError)
-> Either ItnKeyConversionError (SigningKey StakeExtendedKey)
-> Either ShelleyKeyCmdError (SigningKey StakeExtendedKey)
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first ItnKeyConversionError -> ShelleyKeyCmdError
ShelleyKeyCmdItnKeyConvError
            (Either ItnKeyConversionError (SigningKey StakeExtendedKey)
 -> ExceptT ShelleyKeyCmdError IO (SigningKey StakeExtendedKey))
-> Either ItnKeyConversionError (SigningKey StakeExtendedKey)
-> ExceptT ShelleyKeyCmdError IO (SigningKey StakeExtendedKey)
forall a b. (a -> b) -> a -> b
$ Text -> Either ItnKeyConversionError (SigningKey StakeExtendedKey)
convertITNExtendedSigningKey Text
bech32privateKey
  (FileError () -> ShelleyKeyCmdError)
-> ExceptT (FileError ()) IO () -> ExceptT ShelleyKeyCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError () -> ShelleyKeyCmdError
ShelleyKeyCmdWriteFileError (ExceptT (FileError ()) IO () -> ExceptT ShelleyKeyCmdError IO ())
-> (IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ())
-> IO (Either (FileError ()) ())
-> ExceptT ShelleyKeyCmdError 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 ShelleyKeyCmdError IO ())
-> IO (Either (FileError ()) ())
-> ExceptT ShelleyKeyCmdError IO ()
forall a b. (a -> b) -> a -> b
$ String
-> Maybe TextEnvelopeDescr
-> SigningKey StakeExtendedKey
-> IO (Either (FileError ()) ())
forall a.
HasTextEnvelope a =>
String
-> Maybe TextEnvelopeDescr -> a -> IO (Either (FileError ()) ())
writeFileTextEnvelope String
outFile Maybe TextEnvelopeDescr
forall a. Maybe a
Nothing SigningKey StakeExtendedKey
skey

runConvertITNBip32ToStakeKey :: SomeKeyFile -> OutputFile -> ExceptT ShelleyKeyCmdError IO ()
runConvertITNBip32ToStakeKey :: SomeKeyFile -> OutputFile -> ExceptT ShelleyKeyCmdError IO ()
runConvertITNBip32ToStakeKey (AVerificationKeyFile VerificationKeyFile
_) OutputFile
_ = ShelleyKeyCmdError -> ExceptT ShelleyKeyCmdError IO ()
forall (m :: * -> *) x a. Monad m => x -> ExceptT x m a
left ShelleyKeyCmdError
ShelleyKeyCmdWrongKeyTypeError
runConvertITNBip32ToStakeKey (ASigningKeyFile (SigningKeyFile String
sk)) (OutputFile String
outFile) = do
  Text
bech32privateKey <- (ItnKeyConversionError -> ShelleyKeyCmdError)
-> ExceptT ItnKeyConversionError IO Text
-> ExceptT ShelleyKeyCmdError IO Text
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT ItnKeyConversionError -> ShelleyKeyCmdError
ShelleyKeyCmdItnKeyConvError (ExceptT ItnKeyConversionError IO Text
 -> ExceptT ShelleyKeyCmdError IO Text)
-> (IO (Either ItnKeyConversionError Text)
    -> ExceptT ItnKeyConversionError IO Text)
-> IO (Either ItnKeyConversionError Text)
-> ExceptT ShelleyKeyCmdError IO Text
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. IO (Either ItnKeyConversionError Text)
-> ExceptT ItnKeyConversionError IO Text
forall (m :: * -> *) x a. m (Either x a) -> ExceptT x m a
newExceptT (IO (Either ItnKeyConversionError Text)
 -> ExceptT ShelleyKeyCmdError IO Text)
-> IO (Either ItnKeyConversionError Text)
-> ExceptT ShelleyKeyCmdError IO Text
forall a b. (a -> b) -> a -> b
$ String -> IO (Either ItnKeyConversionError Text)
readFileITNKey String
sk
  SigningKey StakeExtendedKey
skey <- Either ShelleyKeyCmdError (SigningKey StakeExtendedKey)
-> ExceptT ShelleyKeyCmdError IO (SigningKey StakeExtendedKey)
forall (m :: * -> *) x a. Monad m => Either x a -> ExceptT x m a
hoistEither (Either ShelleyKeyCmdError (SigningKey StakeExtendedKey)
 -> ExceptT ShelleyKeyCmdError IO (SigningKey StakeExtendedKey))
-> (Either ItnKeyConversionError (SigningKey StakeExtendedKey)
    -> Either ShelleyKeyCmdError (SigningKey StakeExtendedKey))
-> Either ItnKeyConversionError (SigningKey StakeExtendedKey)
-> ExceptT ShelleyKeyCmdError IO (SigningKey StakeExtendedKey)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (ItnKeyConversionError -> ShelleyKeyCmdError)
-> Either ItnKeyConversionError (SigningKey StakeExtendedKey)
-> Either ShelleyKeyCmdError (SigningKey StakeExtendedKey)
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first ItnKeyConversionError -> ShelleyKeyCmdError
ShelleyKeyCmdItnKeyConvError
            (Either ItnKeyConversionError (SigningKey StakeExtendedKey)
 -> ExceptT ShelleyKeyCmdError IO (SigningKey StakeExtendedKey))
-> Either ItnKeyConversionError (SigningKey StakeExtendedKey)
-> ExceptT ShelleyKeyCmdError IO (SigningKey StakeExtendedKey)
forall a b. (a -> b) -> a -> b
$ Text -> Either ItnKeyConversionError (SigningKey StakeExtendedKey)
convertITNBIP32SigningKey Text
bech32privateKey
  (FileError () -> ShelleyKeyCmdError)
-> ExceptT (FileError ()) IO () -> ExceptT ShelleyKeyCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError () -> ShelleyKeyCmdError
ShelleyKeyCmdWriteFileError (ExceptT (FileError ()) IO () -> ExceptT ShelleyKeyCmdError IO ())
-> (IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ())
-> IO (Either (FileError ()) ())
-> ExceptT ShelleyKeyCmdError 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 ShelleyKeyCmdError IO ())
-> IO (Either (FileError ()) ())
-> ExceptT ShelleyKeyCmdError IO ()
forall a b. (a -> b) -> a -> b
$ String
-> Maybe TextEnvelopeDescr
-> SigningKey StakeExtendedKey
-> IO (Either (FileError ()) ())
forall a.
HasTextEnvelope a =>
String
-> Maybe TextEnvelopeDescr -> a -> IO (Either (FileError ()) ())
writeFileTextEnvelope String
outFile Maybe TextEnvelopeDescr
forall a. Maybe a
Nothing SigningKey StakeExtendedKey
skey

-- | An error that can occur while converting an Incentivized Testnet (ITN)
-- key.
data ItnKeyConversionError
  = ItnKeyBech32DecodeError !Bech32DecodeError
  | ItnReadBech32FileError !FilePath !IOException
  | ItnSigningKeyDeserialisationError !ByteString
  | ItnVerificationKeyDeserialisationError !ByteString
  deriving Int -> ItnKeyConversionError -> ShowS
[ItnKeyConversionError] -> ShowS
ItnKeyConversionError -> String
(Int -> ItnKeyConversionError -> ShowS)
-> (ItnKeyConversionError -> String)
-> ([ItnKeyConversionError] -> ShowS)
-> Show ItnKeyConversionError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ItnKeyConversionError] -> ShowS
$cshowList :: [ItnKeyConversionError] -> ShowS
show :: ItnKeyConversionError -> String
$cshow :: ItnKeyConversionError -> String
showsPrec :: Int -> ItnKeyConversionError -> ShowS
$cshowsPrec :: Int -> ItnKeyConversionError -> ShowS
Show

-- | Render an error message for an 'ItnKeyConversionError'.
renderConversionError :: ItnKeyConversionError -> Text
renderConversionError :: ItnKeyConversionError -> Text
renderConversionError ItnKeyConversionError
err =
  case ItnKeyConversionError
err of
    ItnKeyBech32DecodeError Bech32DecodeError
decErr ->
      Text
"Error decoding Bech32 key: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
Text.pack (Bech32DecodeError -> String
forall e. Error e => e -> String
displayError Bech32DecodeError
decErr)
    ItnReadBech32FileError String
fp IOException
readErr ->
      Text
"Error reading Bech32 key at: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
forall a. Show a => a -> Text
textShow String
fp
                        Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" Error: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
Text.pack (IOException -> String
forall e. Exception e => e -> String
displayException IOException
readErr)
    ItnSigningKeyDeserialisationError ByteString
_sKey ->
      -- Sensitive data, such as the signing key, is purposely not included in
      -- the error message.
      Text
"Error deserialising signing key."
    ItnVerificationKeyDeserialisationError ByteString
vKey ->
      Text
"Error deserialising verification key: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
forall a. Show a => a -> Text
textShow (ByteString -> String
BSC.unpack ByteString
vKey)

-- | Convert public ed25519 key to a Shelley stake verification key
convertITNVerificationKey :: Text -> Either ItnKeyConversionError (VerificationKey StakeKey)
convertITNVerificationKey :: Text -> Either ItnKeyConversionError (VerificationKey StakeKey)
convertITNVerificationKey Text
pubKey = do
  (HumanReadablePart
_, DataPart
_, ByteString
keyBS) <- (Bech32DecodeError -> ItnKeyConversionError)
-> Either
     Bech32DecodeError (HumanReadablePart, DataPart, ByteString)
-> Either
     ItnKeyConversionError (HumanReadablePart, DataPart, ByteString)
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first Bech32DecodeError -> ItnKeyConversionError
ItnKeyBech32DecodeError (Text
-> Either
     Bech32DecodeError (HumanReadablePart, DataPart, ByteString)
decodeBech32 Text
pubKey)
  case ByteString -> Maybe (VerKeyDSIGN Ed25519DSIGN)
forall v. DSIGNAlgorithm v => ByteString -> Maybe (VerKeyDSIGN v)
DSIGN.rawDeserialiseVerKeyDSIGN ByteString
keyBS of
    Just VerKeyDSIGN Ed25519DSIGN
verKey -> VerificationKey StakeKey
-> Either ItnKeyConversionError (VerificationKey StakeKey)
forall a b. b -> Either a b
Right (VerificationKey StakeKey
 -> Either ItnKeyConversionError (VerificationKey StakeKey))
-> (VKey 'Staking StandardCrypto -> VerificationKey StakeKey)
-> VKey 'Staking StandardCrypto
-> Either ItnKeyConversionError (VerificationKey StakeKey)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. VKey 'Staking StandardCrypto -> VerificationKey StakeKey
StakeVerificationKey (VKey 'Staking StandardCrypto
 -> Either ItnKeyConversionError (VerificationKey StakeKey))
-> VKey 'Staking StandardCrypto
-> Either ItnKeyConversionError (VerificationKey StakeKey)
forall a b. (a -> b) -> a -> b
$ VerKeyDSIGN (DSIGN StandardCrypto) -> VKey 'Staking StandardCrypto
forall (kd :: KeyRole) crypto.
VerKeyDSIGN (DSIGN crypto) -> VKey kd crypto
Shelley.VKey VerKeyDSIGN (DSIGN StandardCrypto)
VerKeyDSIGN Ed25519DSIGN
verKey
    Maybe (VerKeyDSIGN Ed25519DSIGN)
Nothing -> ItnKeyConversionError
-> Either ItnKeyConversionError (VerificationKey StakeKey)
forall a b. a -> Either a b
Left (ItnKeyConversionError
 -> Either ItnKeyConversionError (VerificationKey StakeKey))
-> ItnKeyConversionError
-> Either ItnKeyConversionError (VerificationKey StakeKey)
forall a b. (a -> b) -> a -> b
$ ByteString -> ItnKeyConversionError
ItnVerificationKeyDeserialisationError ByteString
keyBS

-- | Convert private ed22519 key to a Shelley signing key.
convertITNSigningKey :: Text -> Either ItnKeyConversionError (SigningKey StakeKey)
convertITNSigningKey :: Text -> Either ItnKeyConversionError (SigningKey StakeKey)
convertITNSigningKey Text
privKey = do
  (HumanReadablePart
_, DataPart
_, ByteString
keyBS) <- (Bech32DecodeError -> ItnKeyConversionError)
-> Either
     Bech32DecodeError (HumanReadablePart, DataPart, ByteString)
-> Either
     ItnKeyConversionError (HumanReadablePart, DataPart, ByteString)
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first Bech32DecodeError -> ItnKeyConversionError
ItnKeyBech32DecodeError (Text
-> Either
     Bech32DecodeError (HumanReadablePart, DataPart, ByteString)
decodeBech32 Text
privKey)
  case ByteString -> Maybe (SignKeyDSIGN Ed25519DSIGN)
forall v. DSIGNAlgorithm v => ByteString -> Maybe (SignKeyDSIGN v)
DSIGN.rawDeserialiseSignKeyDSIGN ByteString
keyBS of
    Just SignKeyDSIGN Ed25519DSIGN
signKey -> SigningKey StakeKey
-> Either ItnKeyConversionError (SigningKey StakeKey)
forall a b. b -> Either a b
Right (SigningKey StakeKey
 -> Either ItnKeyConversionError (SigningKey StakeKey))
-> SigningKey StakeKey
-> Either ItnKeyConversionError (SigningKey StakeKey)
forall a b. (a -> b) -> a -> b
$ SignKeyDSIGN StandardCrypto -> SigningKey StakeKey
StakeSigningKey SignKeyDSIGN StandardCrypto
SignKeyDSIGN Ed25519DSIGN
signKey
    Maybe (SignKeyDSIGN Ed25519DSIGN)
Nothing -> ItnKeyConversionError
-> Either ItnKeyConversionError (SigningKey StakeKey)
forall a b. a -> Either a b
Left (ItnKeyConversionError
 -> Either ItnKeyConversionError (SigningKey StakeKey))
-> ItnKeyConversionError
-> Either ItnKeyConversionError (SigningKey StakeKey)
forall a b. (a -> b) -> a -> b
$ ByteString -> ItnKeyConversionError
ItnSigningKeyDeserialisationError ByteString
keyBS

-- | Convert extended private ed22519 key to a Shelley signing key
-- Extended private key = 64 bytes,
-- Public key = 32 bytes.
convertITNExtendedSigningKey :: Text -> Either ItnKeyConversionError (SigningKey StakeExtendedKey)
convertITNExtendedSigningKey :: Text -> Either ItnKeyConversionError (SigningKey StakeExtendedKey)
convertITNExtendedSigningKey Text
privKey = do
  (HumanReadablePart
_, DataPart
_, ByteString
privkeyBS) <- (Bech32DecodeError -> ItnKeyConversionError)
-> Either
     Bech32DecodeError (HumanReadablePart, DataPart, ByteString)
-> Either
     ItnKeyConversionError (HumanReadablePart, DataPart, ByteString)
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first Bech32DecodeError -> ItnKeyConversionError
ItnKeyBech32DecodeError (Text
-> Either
     Bech32DecodeError (HumanReadablePart, DataPart, ByteString)
decodeBech32 Text
privKey)
  let dummyChainCode :: ByteString
dummyChainCode = Int -> Word8 -> ByteString
BS.replicate Int
32 Word8
0
  case ByteString -> Maybe XPrv
xPrvFromBytes (ByteString -> Maybe XPrv) -> ByteString -> Maybe XPrv
forall a b. (a -> b) -> a -> b
$ [ByteString] -> ByteString
BS.concat [ByteString
privkeyBS, ByteString
dummyChainCode] of
    Just XPrv
xprv -> SigningKey StakeExtendedKey
-> Either ItnKeyConversionError (SigningKey StakeExtendedKey)
forall a b. b -> Either a b
Right (SigningKey StakeExtendedKey
 -> Either ItnKeyConversionError (SigningKey StakeExtendedKey))
-> SigningKey StakeExtendedKey
-> Either ItnKeyConversionError (SigningKey StakeExtendedKey)
forall a b. (a -> b) -> a -> b
$ XPrv -> SigningKey StakeExtendedKey
StakeExtendedSigningKey XPrv
xprv
    Maybe XPrv
Nothing -> ItnKeyConversionError
-> Either ItnKeyConversionError (SigningKey StakeExtendedKey)
forall a b. a -> Either a b
Left (ItnKeyConversionError
 -> Either ItnKeyConversionError (SigningKey StakeExtendedKey))
-> ItnKeyConversionError
-> Either ItnKeyConversionError (SigningKey StakeExtendedKey)
forall a b. (a -> b) -> a -> b
$ ByteString -> ItnKeyConversionError
ItnSigningKeyDeserialisationError ByteString
privkeyBS

-- BIP32 Private key = 96 bytes (64 bytes extended private key + 32 bytes chaincode)
-- BIP32 Public Key = 64 Bytes
convertITNBIP32SigningKey :: Text -> Either ItnKeyConversionError (SigningKey StakeExtendedKey)
convertITNBIP32SigningKey :: Text -> Either ItnKeyConversionError (SigningKey StakeExtendedKey)
convertITNBIP32SigningKey Text
privKey = do
  (HumanReadablePart
_, DataPart
_, ByteString
privkeyBS) <- (Bech32DecodeError -> ItnKeyConversionError)
-> Either
     Bech32DecodeError (HumanReadablePart, DataPart, ByteString)
-> Either
     ItnKeyConversionError (HumanReadablePart, DataPart, ByteString)
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first Bech32DecodeError -> ItnKeyConversionError
ItnKeyBech32DecodeError (Text
-> Either
     Bech32DecodeError (HumanReadablePart, DataPart, ByteString)
decodeBech32 Text
privKey)
  case ByteString -> Maybe XPrv
xPrvFromBytes ByteString
privkeyBS of
    Just XPrv
xprv -> SigningKey StakeExtendedKey
-> Either ItnKeyConversionError (SigningKey StakeExtendedKey)
forall a b. b -> Either a b
Right (SigningKey StakeExtendedKey
 -> Either ItnKeyConversionError (SigningKey StakeExtendedKey))
-> SigningKey StakeExtendedKey
-> Either ItnKeyConversionError (SigningKey StakeExtendedKey)
forall a b. (a -> b) -> a -> b
$ XPrv -> SigningKey StakeExtendedKey
StakeExtendedSigningKey XPrv
xprv
    Maybe XPrv
Nothing -> ItnKeyConversionError
-> Either ItnKeyConversionError (SigningKey StakeExtendedKey)
forall a b. a -> Either a b
Left (ItnKeyConversionError
 -> Either ItnKeyConversionError (SigningKey StakeExtendedKey))
-> ItnKeyConversionError
-> Either ItnKeyConversionError (SigningKey StakeExtendedKey)
forall a b. (a -> b) -> a -> b
$ ByteString -> ItnKeyConversionError
ItnSigningKeyDeserialisationError ByteString
privkeyBS

readFileITNKey :: FilePath -> IO (Either ItnKeyConversionError Text)
readFileITNKey :: String -> IO (Either ItnKeyConversionError Text)
readFileITNKey String
fp = do
  Either IOException Text
eStr <- IO Text -> IO (Either IOException Text)
forall e a. Exception e => IO a -> IO (Either e a)
Exception.try (IO Text -> IO (Either IOException Text))
-> IO Text -> IO (Either IOException Text)
forall a b. (a -> b) -> a -> b
$ String -> IO Text
readFile String
fp
  case Either IOException Text
eStr of
    Left IOException
e -> Either ItnKeyConversionError Text
-> IO (Either ItnKeyConversionError Text)
forall (m :: * -> *) a. Monad m => a -> m a
return (Either ItnKeyConversionError Text
 -> IO (Either ItnKeyConversionError Text))
-> (ItnKeyConversionError -> Either ItnKeyConversionError Text)
-> ItnKeyConversionError
-> IO (Either ItnKeyConversionError Text)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ItnKeyConversionError -> Either ItnKeyConversionError Text
forall a b. a -> Either a b
Left (ItnKeyConversionError -> IO (Either ItnKeyConversionError Text))
-> ItnKeyConversionError -> IO (Either ItnKeyConversionError Text)
forall a b. (a -> b) -> a -> b
$ String -> IOException -> ItnKeyConversionError
ItnReadBech32FileError String
fp IOException
e
    Right Text
str -> Either ItnKeyConversionError Text
-> IO (Either ItnKeyConversionError Text)
forall (m :: * -> *) a. Monad m => a -> m a
return (Either ItnKeyConversionError Text
 -> IO (Either ItnKeyConversionError Text))
-> ([Text] -> Either ItnKeyConversionError Text)
-> [Text]
-> IO (Either ItnKeyConversionError Text)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Text -> Either ItnKeyConversionError Text
forall a b. b -> Either a b
Right (Text -> Either ItnKeyConversionError Text)
-> ([Text] -> Text) -> [Text] -> Either ItnKeyConversionError Text
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. [Text] -> Text
Text.concat ([Text] -> IO (Either ItnKeyConversionError Text))
-> [Text] -> IO (Either ItnKeyConversionError Text)
forall a b. (a -> b) -> a -> b
$ Text -> [Text]
Text.words Text
str

--------------------------------------------------------------------------------
-- `cardano-address` extended signing key conversions
--------------------------------------------------------------------------------

runConvertCardanoAddressSigningKey
  :: CardanoAddressKeyType
  -> SigningKeyFile
  -> OutputFile
  -> ExceptT ShelleyKeyCmdError IO ()
runConvertCardanoAddressSigningKey :: CardanoAddressKeyType
-> SigningKeyFile -> OutputFile -> ExceptT ShelleyKeyCmdError IO ()
runConvertCardanoAddressSigningKey CardanoAddressKeyType
keyType SigningKeyFile
skFile (OutputFile String
outFile) = do
  SomeCardanoAddressSigningKey
sKey <- (FileError CardanoAddressSigningKeyConversionError
 -> ShelleyKeyCmdError)
-> ExceptT
     (FileError CardanoAddressSigningKeyConversionError)
     IO
     SomeCardanoAddressSigningKey
-> ExceptT ShelleyKeyCmdError IO SomeCardanoAddressSigningKey
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError CardanoAddressSigningKeyConversionError
-> ShelleyKeyCmdError
ShelleyKeyCmdCardanoAddressSigningKeyFileError
    (ExceptT
   (FileError CardanoAddressSigningKeyConversionError)
   IO
   SomeCardanoAddressSigningKey
 -> ExceptT ShelleyKeyCmdError IO SomeCardanoAddressSigningKey)
-> (IO
      (Either
         (FileError CardanoAddressSigningKeyConversionError)
         SomeCardanoAddressSigningKey)
    -> ExceptT
         (FileError CardanoAddressSigningKeyConversionError)
         IO
         SomeCardanoAddressSigningKey)
-> IO
     (Either
        (FileError CardanoAddressSigningKeyConversionError)
        SomeCardanoAddressSigningKey)
-> ExceptT ShelleyKeyCmdError IO SomeCardanoAddressSigningKey
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 CardanoAddressSigningKeyConversionError)
     SomeCardanoAddressSigningKey)
-> ExceptT
     (FileError CardanoAddressSigningKeyConversionError)
     IO
     SomeCardanoAddressSigningKey
forall (m :: * -> *) x a. m (Either x a) -> ExceptT x m a
newExceptT
    (IO
   (Either
      (FileError CardanoAddressSigningKeyConversionError)
      SomeCardanoAddressSigningKey)
 -> ExceptT ShelleyKeyCmdError IO SomeCardanoAddressSigningKey)
-> IO
     (Either
        (FileError CardanoAddressSigningKeyConversionError)
        SomeCardanoAddressSigningKey)
-> ExceptT ShelleyKeyCmdError IO SomeCardanoAddressSigningKey
forall a b. (a -> b) -> a -> b
$ CardanoAddressKeyType
-> SigningKeyFile
-> IO
     (Either
        (FileError CardanoAddressSigningKeyConversionError)
        SomeCardanoAddressSigningKey)
readSomeCardanoAddressSigningKeyFile CardanoAddressKeyType
keyType SigningKeyFile
skFile
  (FileError () -> ShelleyKeyCmdError)
-> ExceptT (FileError ()) IO () -> ExceptT ShelleyKeyCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError () -> ShelleyKeyCmdError
ShelleyKeyCmdWriteFileError (ExceptT (FileError ()) IO () -> ExceptT ShelleyKeyCmdError IO ())
-> (IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ())
-> IO (Either (FileError ()) ())
-> ExceptT ShelleyKeyCmdError 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 ShelleyKeyCmdError IO ())
-> IO (Either (FileError ()) ())
-> ExceptT ShelleyKeyCmdError IO ()
forall a b. (a -> b) -> a -> b
$ String
-> SomeCardanoAddressSigningKey -> IO (Either (FileError ()) ())
writeSomeCardanoAddressSigningKeyFile String
outFile SomeCardanoAddressSigningKey
sKey

-- | Some kind of signing key that was converted from a @cardano-address@
-- signing key.
data SomeCardanoAddressSigningKey
  = ACardanoAddrShelleyPaymentSigningKey !(SigningKey PaymentExtendedKey)
  | ACardanoAddrShelleyStakeSigningKey !(SigningKey StakeExtendedKey)
  | ACardanoAddrByronSigningKey !(SigningKey ByronKey)

-- | An error that can occur while converting a @cardano-address@ extended
-- signing key.
data CardanoAddressSigningKeyConversionError
  = CardanoAddressSigningKeyBech32DecodeError !Bech32DecodeError
  -- ^ There was an error in decoding the string as Bech32.
  | CardanoAddressSigningKeyDeserialisationError !ByteString
  -- ^ There was an error in converting the @cardano-address@ extended signing
  -- key.
  deriving (Int -> CardanoAddressSigningKeyConversionError -> ShowS
[CardanoAddressSigningKeyConversionError] -> ShowS
CardanoAddressSigningKeyConversionError -> String
(Int -> CardanoAddressSigningKeyConversionError -> ShowS)
-> (CardanoAddressSigningKeyConversionError -> String)
-> ([CardanoAddressSigningKeyConversionError] -> ShowS)
-> Show CardanoAddressSigningKeyConversionError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CardanoAddressSigningKeyConversionError] -> ShowS
$cshowList :: [CardanoAddressSigningKeyConversionError] -> ShowS
show :: CardanoAddressSigningKeyConversionError -> String
$cshow :: CardanoAddressSigningKeyConversionError -> String
showsPrec :: Int -> CardanoAddressSigningKeyConversionError -> ShowS
$cshowsPrec :: Int -> CardanoAddressSigningKeyConversionError -> ShowS
Show, CardanoAddressSigningKeyConversionError
-> CardanoAddressSigningKeyConversionError -> Bool
(CardanoAddressSigningKeyConversionError
 -> CardanoAddressSigningKeyConversionError -> Bool)
-> (CardanoAddressSigningKeyConversionError
    -> CardanoAddressSigningKeyConversionError -> Bool)
-> Eq CardanoAddressSigningKeyConversionError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CardanoAddressSigningKeyConversionError
-> CardanoAddressSigningKeyConversionError -> Bool
$c/= :: CardanoAddressSigningKeyConversionError
-> CardanoAddressSigningKeyConversionError -> Bool
== :: CardanoAddressSigningKeyConversionError
-> CardanoAddressSigningKeyConversionError -> Bool
$c== :: CardanoAddressSigningKeyConversionError
-> CardanoAddressSigningKeyConversionError -> Bool
Eq)

instance Error CardanoAddressSigningKeyConversionError where
  displayError :: CardanoAddressSigningKeyConversionError -> String
displayError = Text -> String
Text.unpack (Text -> String)
-> (CardanoAddressSigningKeyConversionError -> Text)
-> CardanoAddressSigningKeyConversionError
-> String
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. CardanoAddressSigningKeyConversionError -> Text
renderCardanoAddressSigningKeyConversionError

-- | Render an error message for a 'CardanoAddressSigningKeyConversionError'.
renderCardanoAddressSigningKeyConversionError
  :: CardanoAddressSigningKeyConversionError
  -> Text
renderCardanoAddressSigningKeyConversionError :: CardanoAddressSigningKeyConversionError -> Text
renderCardanoAddressSigningKeyConversionError CardanoAddressSigningKeyConversionError
err =
  case CardanoAddressSigningKeyConversionError
err of
    CardanoAddressSigningKeyBech32DecodeError Bech32DecodeError
decErr ->
      String -> Text
Text.pack (Bech32DecodeError -> String
forall e. Error e => e -> String
displayError Bech32DecodeError
decErr)
    CardanoAddressSigningKeyDeserialisationError ByteString
_bs ->
      -- Sensitive data, such as the signing key, is purposely not included in
      -- the error message.
      Text
"Error deserialising cardano-address signing key."

-- | Decode a Bech32-encoded string.
decodeBech32
  :: Text
  -> Either Bech32DecodeError (Bech32.HumanReadablePart, Bech32.DataPart, ByteString)
decodeBech32 :: Text
-> Either
     Bech32DecodeError (HumanReadablePart, DataPart, ByteString)
decodeBech32 Text
bech32Str =
  case Text -> Either DecodingError (HumanReadablePart, DataPart)
Bech32.decodeLenient Text
bech32Str of
    Left DecodingError
err -> Bech32DecodeError
-> Either
     Bech32DecodeError (HumanReadablePart, DataPart, ByteString)
forall a b. a -> Either a b
Left (DecodingError -> Bech32DecodeError
Bech32DecodingError DecodingError
err)
    Right (HumanReadablePart
hrPart, DataPart
dataPart) ->
      case DataPart -> Maybe ByteString
Bech32.dataPartToBytes DataPart
dataPart of
        Maybe ByteString
Nothing ->
          Bech32DecodeError
-> Either
     Bech32DecodeError (HumanReadablePart, DataPart, ByteString)
forall a b. a -> Either a b
Left (Bech32DecodeError
 -> Either
      Bech32DecodeError (HumanReadablePart, DataPart, ByteString))
-> Bech32DecodeError
-> Either
     Bech32DecodeError (HumanReadablePart, DataPart, ByteString)
forall a b. (a -> b) -> a -> b
$ Text -> Bech32DecodeError
Bech32DataPartToBytesError (DataPart -> Text
Bech32.dataPartToText DataPart
dataPart)
        Just ByteString
bs -> (HumanReadablePart, DataPart, ByteString)
-> Either
     Bech32DecodeError (HumanReadablePart, DataPart, ByteString)
forall a b. b -> Either a b
Right (HumanReadablePart
hrPart, DataPart
dataPart, ByteString
bs)

-- | Convert a Ed25519 BIP32 extended signing key (96 bytes) to a @cardano-crypto@
-- style extended signing key.
--
-- Note that both the ITN and @cardano-address@ use this key format.
convertBip32SigningKey
  :: ByteString
  -> Either CardanoAddressSigningKeyConversionError Crypto.XPrv
convertBip32SigningKey :: ByteString -> Either CardanoAddressSigningKeyConversionError XPrv
convertBip32SigningKey ByteString
signingKeyBs =
  case ByteString -> Maybe XPrv
xPrvFromBytes ByteString
signingKeyBs of
    Just XPrv
xPrv -> XPrv -> Either CardanoAddressSigningKeyConversionError XPrv
forall a b. b -> Either a b
Right XPrv
xPrv
    Maybe XPrv
Nothing ->
      CardanoAddressSigningKeyConversionError
-> Either CardanoAddressSigningKeyConversionError XPrv
forall a b. a -> Either a b
Left (CardanoAddressSigningKeyConversionError
 -> Either CardanoAddressSigningKeyConversionError XPrv)
-> CardanoAddressSigningKeyConversionError
-> Either CardanoAddressSigningKeyConversionError XPrv
forall a b. (a -> b) -> a -> b
$ ByteString -> CardanoAddressSigningKeyConversionError
CardanoAddressSigningKeyDeserialisationError ByteString
signingKeyBs

-- | Read a file containing a Bech32-encoded Ed25519 BIP32 extended signing
-- key.
readBech32Bip32SigningKeyFile
  :: SigningKeyFile
  -> IO (Either (FileError CardanoAddressSigningKeyConversionError) Crypto.XPrv)
readBech32Bip32SigningKeyFile :: SigningKeyFile
-> IO
     (Either (FileError CardanoAddressSigningKeyConversionError) XPrv)
readBech32Bip32SigningKeyFile (SigningKeyFile String
fp) = do
  Either IOException Text
eStr <- IO Text -> IO (Either IOException Text)
forall e a. Exception e => IO a -> IO (Either e a)
Exception.try (IO Text -> IO (Either IOException Text))
-> IO Text -> IO (Either IOException Text)
forall a b. (a -> b) -> a -> b
$ String -> IO Text
readFile String
fp
  case Either IOException Text
eStr of
    Left IOException
e -> Either (FileError CardanoAddressSigningKeyConversionError) XPrv
-> IO
     (Either (FileError CardanoAddressSigningKeyConversionError) XPrv)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either (FileError CardanoAddressSigningKeyConversionError) XPrv
 -> IO
      (Either (FileError CardanoAddressSigningKeyConversionError) XPrv))
-> (FileError CardanoAddressSigningKeyConversionError
    -> Either (FileError CardanoAddressSigningKeyConversionError) XPrv)
-> FileError CardanoAddressSigningKeyConversionError
-> IO
     (Either (FileError CardanoAddressSigningKeyConversionError) XPrv)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. FileError CardanoAddressSigningKeyConversionError
-> Either (FileError CardanoAddressSigningKeyConversionError) XPrv
forall a b. a -> Either a b
Left (FileError CardanoAddressSigningKeyConversionError
 -> IO
      (Either (FileError CardanoAddressSigningKeyConversionError) XPrv))
-> FileError CardanoAddressSigningKeyConversionError
-> IO
     (Either (FileError CardanoAddressSigningKeyConversionError) XPrv)
forall a b. (a -> b) -> a -> b
$ String
-> IOException -> FileError CardanoAddressSigningKeyConversionError
forall e. String -> IOException -> FileError e
FileIOError String
fp IOException
e
    Right Text
str ->
      case Text
-> Either
     Bech32DecodeError (HumanReadablePart, DataPart, ByteString)
decodeBech32 ([Text] -> Text
Text.concat ([Text] -> Text) -> [Text] -> Text
forall a b. (a -> b) -> a -> b
$ Text -> [Text]
Text.words Text
str) of
        Left Bech32DecodeError
err ->
          Either (FileError CardanoAddressSigningKeyConversionError) XPrv
-> IO
     (Either (FileError CardanoAddressSigningKeyConversionError) XPrv)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either (FileError CardanoAddressSigningKeyConversionError) XPrv
 -> IO
      (Either (FileError CardanoAddressSigningKeyConversionError) XPrv))
-> Either (FileError CardanoAddressSigningKeyConversionError) XPrv
-> IO
     (Either (FileError CardanoAddressSigningKeyConversionError) XPrv)
forall a b. (a -> b) -> a -> b
$ FileError CardanoAddressSigningKeyConversionError
-> Either (FileError CardanoAddressSigningKeyConversionError) XPrv
forall a b. a -> Either a b
Left (FileError CardanoAddressSigningKeyConversionError
 -> Either (FileError CardanoAddressSigningKeyConversionError) XPrv)
-> FileError CardanoAddressSigningKeyConversionError
-> Either (FileError CardanoAddressSigningKeyConversionError) XPrv
forall a b. (a -> b) -> a -> b
$
            String
-> CardanoAddressSigningKeyConversionError
-> FileError CardanoAddressSigningKeyConversionError
forall e. String -> e -> FileError e
FileError String
fp (Bech32DecodeError -> CardanoAddressSigningKeyConversionError
CardanoAddressSigningKeyBech32DecodeError Bech32DecodeError
err)
        Right (HumanReadablePart
_hrPart, DataPart
_dataPart, ByteString
bs) ->
          Either (FileError CardanoAddressSigningKeyConversionError) XPrv
-> IO
     (Either (FileError CardanoAddressSigningKeyConversionError) XPrv)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either (FileError CardanoAddressSigningKeyConversionError) XPrv
 -> IO
      (Either (FileError CardanoAddressSigningKeyConversionError) XPrv))
-> Either (FileError CardanoAddressSigningKeyConversionError) XPrv
-> IO
     (Either (FileError CardanoAddressSigningKeyConversionError) XPrv)
forall a b. (a -> b) -> a -> b
$ (CardanoAddressSigningKeyConversionError
 -> FileError CardanoAddressSigningKeyConversionError)
-> Either CardanoAddressSigningKeyConversionError XPrv
-> Either (FileError CardanoAddressSigningKeyConversionError) XPrv
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (String
-> CardanoAddressSigningKeyConversionError
-> FileError CardanoAddressSigningKeyConversionError
forall e. String -> e -> FileError e
FileError String
fp) (ByteString -> Either CardanoAddressSigningKeyConversionError XPrv
convertBip32SigningKey ByteString
bs)

-- | Read a file containing a Bech32-encoded @cardano-address@ extended
-- signing key.
readSomeCardanoAddressSigningKeyFile
  :: CardanoAddressKeyType
  -> SigningKeyFile
  -> IO (Either (FileError CardanoAddressSigningKeyConversionError) SomeCardanoAddressSigningKey)
readSomeCardanoAddressSigningKeyFile :: CardanoAddressKeyType
-> SigningKeyFile
-> IO
     (Either
        (FileError CardanoAddressSigningKeyConversionError)
        SomeCardanoAddressSigningKey)
readSomeCardanoAddressSigningKeyFile CardanoAddressKeyType
keyType SigningKeyFile
skFile = do
    Either (FileError CardanoAddressSigningKeyConversionError) XPrv
xPrv <- SigningKeyFile
-> IO
     (Either (FileError CardanoAddressSigningKeyConversionError) XPrv)
readBech32Bip32SigningKeyFile SigningKeyFile
skFile
    Either
  (FileError CardanoAddressSigningKeyConversionError)
  SomeCardanoAddressSigningKey
-> IO
     (Either
        (FileError CardanoAddressSigningKeyConversionError)
        SomeCardanoAddressSigningKey)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (XPrv -> SomeCardanoAddressSigningKey
toSomeCardanoAddressSigningKey (XPrv -> SomeCardanoAddressSigningKey)
-> Either (FileError CardanoAddressSigningKeyConversionError) XPrv
-> Either
     (FileError CardanoAddressSigningKeyConversionError)
     SomeCardanoAddressSigningKey
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Either (FileError CardanoAddressSigningKeyConversionError) XPrv
xPrv)
  where
    toSomeCardanoAddressSigningKey :: Crypto.XPrv -> SomeCardanoAddressSigningKey
    toSomeCardanoAddressSigningKey :: XPrv -> SomeCardanoAddressSigningKey
toSomeCardanoAddressSigningKey XPrv
xPrv =
      case CardanoAddressKeyType
keyType of
        CardanoAddressKeyType
CardanoAddressShelleyPaymentKey ->
          SigningKey PaymentExtendedKey -> SomeCardanoAddressSigningKey
ACardanoAddrShelleyPaymentSigningKey
            (XPrv -> SigningKey PaymentExtendedKey
PaymentExtendedSigningKey XPrv
xPrv)
        CardanoAddressKeyType
CardanoAddressShelleyStakeKey ->
          SigningKey StakeExtendedKey -> SomeCardanoAddressSigningKey
ACardanoAddrShelleyStakeSigningKey (XPrv -> SigningKey StakeExtendedKey
StakeExtendedSigningKey XPrv
xPrv)
        CardanoAddressKeyType
CardanoAddressIcarusPaymentKey ->
          SigningKey ByronKey -> SomeCardanoAddressSigningKey
ACardanoAddrByronSigningKey (SigningKey ByronKey -> SomeCardanoAddressSigningKey)
-> SigningKey ByronKey -> SomeCardanoAddressSigningKey
forall a b. (a -> b) -> a -> b
$
            SigningKey -> SigningKey ByronKey
ByronSigningKey (XPrv -> SigningKey
Byron.SigningKey XPrv
xPrv)
        CardanoAddressKeyType
CardanoAddressByronPaymentKey ->
          SigningKey ByronKey -> SomeCardanoAddressSigningKey
ACardanoAddrByronSigningKey (SigningKey ByronKey -> SomeCardanoAddressSigningKey)
-> SigningKey ByronKey -> SomeCardanoAddressSigningKey
forall a b. (a -> b) -> a -> b
$
            SigningKey -> SigningKey ByronKey
ByronSigningKey (XPrv -> SigningKey
Byron.SigningKey XPrv
xPrv)

-- | Write a text envelope formatted file containing a @cardano-address@
-- extended signing key, but converted to a format supported by @cardano-cli@.
writeSomeCardanoAddressSigningKeyFile
  :: FilePath
  -> SomeCardanoAddressSigningKey
  -> IO (Either (FileError ()) ())
writeSomeCardanoAddressSigningKeyFile :: String
-> SomeCardanoAddressSigningKey -> IO (Either (FileError ()) ())
writeSomeCardanoAddressSigningKeyFile String
outFile SomeCardanoAddressSigningKey
skey =
  case SomeCardanoAddressSigningKey
skey of
    ACardanoAddrShelleyPaymentSigningKey SigningKey PaymentExtendedKey
sk ->
      String
-> Maybe TextEnvelopeDescr
-> SigningKey PaymentExtendedKey
-> IO (Either (FileError ()) ())
forall a.
HasTextEnvelope a =>
String
-> Maybe TextEnvelopeDescr -> a -> IO (Either (FileError ()) ())
writeFileTextEnvelope String
outFile Maybe TextEnvelopeDescr
forall a. Maybe a
Nothing SigningKey PaymentExtendedKey
sk
    ACardanoAddrShelleyStakeSigningKey SigningKey StakeExtendedKey
sk ->
      String
-> Maybe TextEnvelopeDescr
-> SigningKey StakeExtendedKey
-> IO (Either (FileError ()) ())
forall a.
HasTextEnvelope a =>
String
-> Maybe TextEnvelopeDescr -> a -> IO (Either (FileError ()) ())
writeFileTextEnvelope String
outFile Maybe TextEnvelopeDescr
forall a. Maybe a
Nothing SigningKey StakeExtendedKey
sk
    ACardanoAddrByronSigningKey SigningKey ByronKey
sk ->
      String
-> Maybe TextEnvelopeDescr
-> SigningKey ByronKey
-> IO (Either (FileError ()) ())
forall a.
HasTextEnvelope a =>
String
-> Maybe TextEnvelopeDescr -> a -> IO (Either (FileError ()) ())
writeFileTextEnvelope String
outFile Maybe TextEnvelopeDescr
forall a. Maybe a
Nothing SigningKey ByronKey
sk