Skip to content

Commit

Permalink
Expand upon Cardano.CLI.Shelley.Key to support Bech32 serialisation
Browse files Browse the repository at this point in the history
  • Loading branch information
intricate authored and newhoggy committed Mar 21, 2023
1 parent 7f52046 commit 9190a97
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions cardano-cli/src/Cardano/CLI/Shelley/Key.hs
Expand Up @@ -7,6 +7,21 @@
-- | Shelley CLI option data types and functions for cryptographic keys.
module Cardano.CLI.Shelley.Key
( VerificationKeyOrFile (..)
, InputFormat (..)
, InputDecodeError (..)
, deserialiseInput
, deserialiseInputAnyOf
, renderInputDecodeError

, OutputDirection (..)
, writeOutputBech32

, readKeyFileAnyOf
, readKeyFileTextEnvelope

, readSigningKeyFile
, readSigningKeyFileAnyOf

, readVerificationKeyOrFile
, readVerificationKeyOrTextEnvFile

Expand All @@ -28,15 +43,85 @@ module Cardano.CLI.Shelley.Key
import Cardano.Api

import Control.Monad.IO.Class (MonadIO (..))
import Control.Monad.Trans.Except (runExceptT)
import Control.Monad.Trans.Except.Extra (handleIOExceptT)
import Data.Bifunctor (Bifunctor (..))
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as BSC
import qualified Data.List.NonEmpty as NE
import Data.Text (Text)
import qualified Data.Text as Text
import qualified Data.Text.Encoding as Text

import Cardano.CLI.Types

------------------------------------------------------------------------------
-- Formatted/encoded input deserialisation
------------------------------------------------------------------------------

------------------------------------------------------------------------------
-- Formatted/encoded output serialisation
------------------------------------------------------------------------------

-- | Where to write some output.
data OutputDirection
= OutputDirectionStdout
-- ^ Write output to @stdout@.
| OutputDirectionFile !FilePath
-- ^ Write output to a provided file.
deriving (Eq, Show)

writeOutputBech32
:: SerialiseAsBech32 a
=> OutputDirection
-> a
-> IO (Either (FileError ()) ())
writeOutputBech32 outputDirection a =
case outputDirection of
OutputDirectionStdout -> Right <$> BSC.putStrLn outputBs
OutputDirectionFile fp ->
runExceptT $ handleIOExceptT (FileIOError fp) $
BS.writeFile fp outputBs
where
outputBs :: ByteString
outputBs = Text.encodeUtf8 (serialiseToBech32 a)

------------------------------------------------------------------------------
-- Signing key deserialisation
------------------------------------------------------------------------------

-- | Read a signing key from a file.
--
-- The contents of the file can either be Bech32-encoded, hex-encoded, or in
-- the text envelope format.
readSigningKeyFile
:: forall keyrole.
( HasTextEnvelope (SigningKey keyrole)
, SerialiseAsBech32 (SigningKey keyrole)
)
=> AsType keyrole
-> SigningKeyFile
-> IO (Either (FileError InputDecodeError) (SigningKey keyrole))
readSigningKeyFile asType (SigningKeyFile fp) =
readKeyFile
(AsSigningKey asType)
(NE.fromList [InputFormatBech32, InputFormatHex, InputFormatTextEnvelope])
fp

-- | Read a signing key from a file given that it is one of the provided types
-- of signing key.
--
-- The contents of the file can either be Bech32-encoded or in the text
-- envelope format.
readSigningKeyFileAnyOf
:: forall b.
[FromSomeType SerialiseAsBech32 b]
-> [FromSomeType HasTextEnvelope b]
-> SigningKeyFile
-> IO (Either (FileError InputDecodeError) b)
readSigningKeyFileAnyOf bech32Types textEnvTypes (SigningKeyFile fp) =
readKeyFileAnyOf bech32Types textEnvTypes fp

------------------------------------------------------------------------------
-- Verification key deserialisation
Expand Down

0 comments on commit 9190a97

Please sign in to comment.