Skip to content

Commit

Permalink
Merge pull request #4698 from input-output-hk/jordan/move-cli-signing…
Browse files Browse the repository at this point in the history
…-key-reading-to-api

Move signing key reading to cardano-api
  • Loading branch information
Jimbo4350 authored Dec 19, 2022
2 parents 3ffd819 + 45557f4 commit f24d5b1
Show file tree
Hide file tree
Showing 29 changed files with 159 additions and 188 deletions.
9 changes: 5 additions & 4 deletions cardano-api/cardano-api.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,11 @@ library
Cardano.Api.IPC.Monad
Cardano.Api.InMode
Cardano.Api.Json
Cardano.Api.Key
Cardano.Api.KeysByron
Cardano.Api.KeysPraos
Cardano.Api.KeysShelley
Cardano.Api.Keys.Byron
Cardano.Api.Keys.Class
Cardano.Api.Keys.Praos
Cardano.Api.Keys.Read
Cardano.Api.Keys.Shelley
Cardano.Api.LedgerEvent
Cardano.Api.LedgerState
Cardano.Api.Modes
Expand Down
14 changes: 11 additions & 3 deletions cardano-api/src/Cardano/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -512,10 +512,17 @@ module Cardano.Api (
TextEnvelopeCddlError(..),

-- *** Reading one of several key types
readKeyFile,
readKeyFileTextEnvelope,
readKeyFileAnyOf,

-- *** Read one of several types
FromSomeType(..),
deserialiseFromTextEnvelopeAnyOf,
readFileTextEnvelopeAnyOf,



-- * Errors
Error(..),
throwErrorAsException,
Expand Down Expand Up @@ -750,9 +757,10 @@ import Cardano.Api.HasTypeProxy
import Cardano.Api.InMode
import Cardano.Api.IPC
import Cardano.Api.IPC.Monad
import Cardano.Api.Key
import Cardano.Api.KeysByron
import Cardano.Api.KeysShelley
import Cardano.Api.Keys.Byron
import Cardano.Api.Keys.Class
import Cardano.Api.Keys.Read
import Cardano.Api.Keys.Shelley
import Cardano.Api.LedgerEvent
import Cardano.Api.LedgerState
import Cardano.Api.Modes
Expand Down
6 changes: 3 additions & 3 deletions cardano-api/src/Cardano/Api/Address.hs
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ import Cardano.Api.EraCast
import Cardano.Api.Eras
import Cardano.Api.Hash
import Cardano.Api.HasTypeProxy
import Cardano.Api.Key
import Cardano.Api.KeysByron
import Cardano.Api.KeysShelley
import Cardano.Api.Keys.Class
import Cardano.Api.Keys.Byron
import Cardano.Api.Keys.Shelley
import Cardano.Api.NetworkId
import Cardano.Api.Script
import Cardano.Api.SerialiseBech32
Expand Down
2 changes: 1 addition & 1 deletion cardano-api/src/Cardano/Api/Block.hs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ import qualified Cardano.Ledger.Era as Ledger
import Cardano.Api.Eras
import Cardano.Api.Hash
import Cardano.Api.HasTypeProxy
import Cardano.Api.KeysShelley
import Cardano.Api.Keys.Shelley
import Cardano.Api.Modes
import Cardano.Api.SerialiseRaw
import Cardano.Api.SerialiseUsing
Expand Down
2 changes: 1 addition & 1 deletion cardano-api/src/Cardano/Api/Byron.hs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ module Cardano.Api.Byron
import Cardano.Api
import Cardano.Api.Address
import Cardano.Api.IPC
import Cardano.Api.KeysByron
import Cardano.Api.Keys.Byron
import Cardano.Api.NetworkId
import Cardano.Api.SpecialByron
import Cardano.Api.Tx
Expand Down
6 changes: 3 additions & 3 deletions cardano-api/src/Cardano/Api/Certificate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ import qualified Cardano.Ledger.Shelley.TxBody as Shelley
import Cardano.Api.Address
import Cardano.Api.HasTypeProxy
import Cardano.Api.Hash
import Cardano.Api.KeysByron
import Cardano.Api.KeysPraos
import Cardano.Api.KeysShelley
import Cardano.Api.Keys.Byron
import Cardano.Api.Keys.Praos
import Cardano.Api.Keys.Shelley
import Cardano.Api.SerialiseCBOR
import Cardano.Api.SerialiseTextEnvelope
import Cardano.Api.StakePoolMetadata
Expand Down
8 changes: 4 additions & 4 deletions cardano-api/src/Cardano/Api/DeserialiseAnyOf.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ import Cardano.Api.SerialiseTextEnvelope

-- TODO: Think about if these belong
import Cardano.Api.Address
import Cardano.Api.Key
import Cardano.Api.KeysByron
import Cardano.Api.KeysPraos
import Cardano.Api.KeysShelley
import Cardano.Api.Keys.Class
import Cardano.Api.Keys.Byron
import Cardano.Api.Keys.Praos
import Cardano.Api.Keys.Shelley

------------------------------------------------------------------------------
-- Formatted/encoded input deserialisation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

-- | Byron key types and their 'Key' class instances
--
module Cardano.Api.KeysByron (
module Cardano.Api.Keys.Byron (

-- * Key types
ByronKey,
Expand Down Expand Up @@ -54,8 +54,8 @@ import qualified Cardano.Crypto.Wallet as Wallet

import Cardano.Api.HasTypeProxy
import Cardano.Api.Hash
import Cardano.Api.Key
import Cardano.Api.KeysShelley
import Cardano.Api.Keys.Class
import Cardano.Api.Keys.Shelley
import Cardano.Api.SerialiseCBOR
import Cardano.Api.SerialiseRaw
import Cardano.Api.SerialiseTextEnvelope
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilies #-}

module Cardano.Api.Key
module Cardano.Api.Keys.Class
( Key(..)
, generateSigningKey
, generateInsecureSigningKey
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

-- | Praos consensus key types and their 'Key' class instances
--
module Cardano.Api.KeysPraos (
module Cardano.Api.Keys.Praos (

-- * Key types
KesKey,
Expand All @@ -36,7 +36,7 @@ import Cardano.Ledger.Crypto (StandardCrypto)

import Cardano.Api.HasTypeProxy
import Cardano.Api.Hash
import Cardano.Api.Key
import Cardano.Api.Keys.Class
import Cardano.Api.SerialiseBech32
import Cardano.Api.SerialiseCBOR
import Cardano.Api.SerialiseRaw
Expand Down
87 changes: 87 additions & 0 deletions cardano-api/src/Cardano/Api/Keys/Read.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeFamilies #-}

module Cardano.Api.Keys.Read
( readKeyFile
, readKeyFileTextEnvelope
, readKeyFileAnyOf
) where

import Prelude

import Control.Exception
import Data.Bifunctor
import Data.ByteString as BS
import Data.List.NonEmpty (NonEmpty)

import Cardano.Api.DeserialiseAnyOf
import Cardano.Api.Error
import Cardano.Api.HasTypeProxy
import Cardano.Api.SerialiseBech32
import Cardano.Api.SerialiseTextEnvelope
import Cardano.Api.Utils

-- | Read a cryptographic key from a file.
--
-- The contents of the file can either be Bech32-encoded, hex-encoded, or in
-- the text envelope format.
readKeyFile
:: AsType a
-> NonEmpty (InputFormat a)
-> FilePath
-> IO (Either (FileError InputDecodeError) a)
readKeyFile asType acceptedFormats path = do
eContent <- fmap Right (readFileBlocking path) `catches` [Handler handler]
case eContent of
Left e -> return $ Left e
Right content ->
return . first (FileError path) $ deserialiseInput asType acceptedFormats content
where
handler :: IOException -> IO (Either (FileError InputDecodeError) BS.ByteString)
handler e = return . Left $ FileIOError path e

-- | Read a cryptographic key from a file.
--
-- The contents of the file must be in the text envelope format.
readKeyFileTextEnvelope
:: HasTextEnvelope a
=> AsType a
-> FilePath
-> IO (Either (FileError InputDecodeError) a)
readKeyFileTextEnvelope asType fp =
first toInputDecodeError <$> readFileTextEnvelope asType fp
where
toInputDecodeError
:: FileError TextEnvelopeError
-> FileError InputDecodeError
toInputDecodeError err =
case err of
FileIOError path ex -> FileIOError path ex
FileError path textEnvErr ->
FileError path (InputTextEnvelopeError textEnvErr)
FileErrorTempFile targetP tempP h ->
FileErrorTempFile targetP tempP h

-- | Read a cryptographic key from a file given that it is one of the provided
-- types.
--
-- The contents of the file can either be Bech32-encoded or in the text
-- envelope format.
readKeyFileAnyOf
:: forall b.
[FromSomeType SerialiseAsBech32 b]
-> [FromSomeType HasTextEnvelope b]
-> FilePath
-> IO (Either (FileError InputDecodeError) b)
readKeyFileAnyOf bech32Types textEnvTypes path = do
eContent <- fmap Right (readFileBlocking path) `catches` [Handler handler]
case eContent of
Left e -> return $ Left e
Right content ->
return . first (FileError path) $ deserialiseInputAnyOf bech32Types textEnvTypes content
where
handler :: IOException -> IO (Either (FileError InputDecodeError) BS.ByteString)
handler e = return . Left $ FileIOError path e

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

-- | Shelley key types and their 'Key' class instances
--
module Cardano.Api.KeysShelley (
module Cardano.Api.Keys.Shelley (

-- * Key types
PaymentKey,
Expand Down Expand Up @@ -56,7 +56,7 @@ import Cardano.Ledger.Crypto (StandardCrypto)
import Cardano.Api.Error
import Cardano.Api.Hash
import Cardano.Api.HasTypeProxy
import Cardano.Api.Key
import Cardano.Api.Keys.Class
import Cardano.Api.SerialiseBech32
import Cardano.Api.SerialiseCBOR
import Cardano.Api.SerialiseJSON
Expand Down
2 changes: 1 addition & 1 deletion cardano-api/src/Cardano/Api/LedgerEvent.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ where
import Cardano.Api.Address (StakeCredential, fromShelleyStakeCredential)
import Cardano.Api.Block (EpochNo)
import Cardano.Api.Certificate (Certificate)
import Cardano.Api.KeysShelley (Hash (StakePoolKeyHash), StakePoolKey)
import Cardano.Api.Keys.Shelley (Hash (StakePoolKeyHash), StakePoolKey)
import Cardano.Api.Value (Lovelace, fromShelleyDeltaLovelace, fromShelleyLovelace)
import qualified Cardano.Ledger.Coin as Ledger
import qualified Cardano.Ledger.Core as Ledger.Core
Expand Down
2 changes: 1 addition & 1 deletion cardano-api/src/Cardano/Api/LedgerState.hs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ import Cardano.Api.IPC (ConsensusModeParams (..),
LocalChainSyncClient (LocalChainSyncClientPipelined),
LocalNodeClientProtocols (..), LocalNodeClientProtocolsInMode,
LocalNodeConnectInfo (..), connectToLocalNode)
import Cardano.Api.KeysPraos
import Cardano.Api.Keys.Praos
import Cardano.Api.LedgerEvent (LedgerEvent, toLedgerEvent)
import Cardano.Api.Modes (CardanoMode, EpochSlots (..))
import qualified Cardano.Api.Modes as Api
Expand Down
8 changes: 4 additions & 4 deletions cardano-api/src/Cardano/Api/OperationalCertificate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ import Cardano.Api.Address
import Cardano.Api.Certificate
import Cardano.Api.Error
import Cardano.Api.HasTypeProxy
import Cardano.Api.Key
import Cardano.Api.KeysByron
import Cardano.Api.KeysPraos
import Cardano.Api.KeysShelley
import Cardano.Api.Keys.Class
import Cardano.Api.Keys.Byron
import Cardano.Api.Keys.Praos
import Cardano.Api.Keys.Shelley
import Cardano.Api.ProtocolParameters
import Cardano.Api.SerialiseCBOR
import Cardano.Api.SerialiseTextEnvelope
Expand Down
4 changes: 2 additions & 2 deletions cardano-api/src/Cardano/Api/ProtocolParameters.hs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ import Cardano.Api.Eras
import Cardano.Api.Error
import Cardano.Api.Hash
import Cardano.Api.HasTypeProxy
import Cardano.Api.KeysByron
import Cardano.Api.KeysShelley
import Cardano.Api.Keys.Byron
import Cardano.Api.Keys.Shelley
import Cardano.Api.Script
import Cardano.Api.SerialiseCBOR
import Cardano.Api.SerialiseRaw
Expand Down
2 changes: 1 addition & 1 deletion cardano-api/src/Cardano/Api/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ import Cardano.Api.Certificate
import Cardano.Api.EraCast
import Cardano.Api.Eras
import Cardano.Api.GenesisParameters
import Cardano.Api.KeysShelley
import Cardano.Api.Keys.Shelley
import Cardano.Api.Modes
import Cardano.Api.NetworkId
import Cardano.Api.Orphans ()
Expand Down
2 changes: 1 addition & 1 deletion cardano-api/src/Cardano/Api/Script.hs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ import Cardano.Api.Eras
import Cardano.Api.Error
import Cardano.Api.Hash
import Cardano.Api.HasTypeProxy
import Cardano.Api.KeysShelley
import Cardano.Api.Keys.Shelley
import Cardano.Api.ScriptData
import Cardano.Api.SerialiseCBOR
import Cardano.Api.SerialiseJSON
Expand Down
2 changes: 1 addition & 1 deletion cardano-api/src/Cardano/Api/ScriptData.hs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ import Cardano.Api.Eras
import Cardano.Api.Error
import Cardano.Api.HasTypeProxy
import Cardano.Api.Hash
import Cardano.Api.KeysShelley
import Cardano.Api.Keys.Shelley
import Cardano.Api.SerialiseCBOR
import Cardano.Api.SerialiseJSON
import Cardano.Api.SerialiseRaw
Expand Down
6 changes: 3 additions & 3 deletions cardano-api/src/Cardano/Api/Shelley.hs
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,9 @@ import Cardano.Api.Eras
import Cardano.Api.Genesis
import Cardano.Api.InMode
import Cardano.Api.IPC
import Cardano.Api.KeysByron
import Cardano.Api.KeysPraos
import Cardano.Api.KeysShelley
import Cardano.Api.Keys.Byron
import Cardano.Api.Keys.Praos
import Cardano.Api.Keys.Shelley
import Cardano.Api.LedgerState
import Cardano.Api.NetworkId
import Cardano.Api.OperationalCertificate
Expand Down
2 changes: 1 addition & 1 deletion cardano-api/src/Cardano/Api/SpecialByron.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import Data.Word
import Numeric.Natural

import Cardano.Api.HasTypeProxy
import Cardano.Api.KeysByron
import Cardano.Api.Keys.Byron
import Cardano.Api.NetworkId (NetworkId, toByronProtocolMagicId)
import Cardano.Api.SerialiseRaw

Expand Down
6 changes: 3 additions & 3 deletions cardano-api/src/Cardano/Api/StakePoolMetadata.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ import Cardano.Api.Eras
import Cardano.Api.Error
import Cardano.Api.Hash
import Cardano.Api.HasTypeProxy
import Cardano.Api.Key
import Cardano.Api.KeysByron
import Cardano.Api.KeysPraos
import Cardano.Api.Keys.Class
import Cardano.Api.Keys.Byron
import Cardano.Api.Keys.Praos
import Cardano.Api.Script
import Cardano.Api.SerialiseJSON
import Cardano.Api.SerialiseRaw
Expand Down
6 changes: 3 additions & 3 deletions cardano-api/src/Cardano/Api/Tx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ import Cardano.Api.Address
import Cardano.Api.Certificate
import Cardano.Api.Eras
import Cardano.Api.HasTypeProxy
import Cardano.Api.Key
import Cardano.Api.KeysByron
import Cardano.Api.KeysShelley
import Cardano.Api.Keys.Class
import Cardano.Api.Keys.Byron
import Cardano.Api.Keys.Shelley
import Cardano.Api.NetworkId
import Cardano.Api.SerialiseCBOR
import Cardano.Api.SerialiseTextEnvelope
Expand Down
4 changes: 2 additions & 2 deletions cardano-api/src/Cardano/Api/TxBody.hs
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@ import Cardano.Api.Eras
import Cardano.Api.Error
import Cardano.Api.Hash
import Cardano.Api.HasTypeProxy
import Cardano.Api.KeysByron
import Cardano.Api.KeysShelley
import Cardano.Api.Keys.Byron
import Cardano.Api.Keys.Shelley
import Cardano.Api.NetworkId
import Cardano.Api.ProtocolParameters
import Cardano.Api.Script
Expand Down
2 changes: 1 addition & 1 deletion cardano-api/test/Test/Cardano/Api/KeysByron.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Test.Cardano.Api.KeysByron
( tests
) where

import Cardano.Api (AsType(AsByronKey, AsSigningKey), Key(deterministicSigningKey))
import Cardano.Api (AsType (AsByronKey, AsSigningKey), Key (deterministicSigningKey))
import Cardano.Prelude ((<$>))
import Gen.Hedgehog.Roundtrip.CBOR (roundtrip_CBOR)
import Hedgehog (Property)
Expand Down
Loading

0 comments on commit f24d5b1

Please sign in to comment.