Skip to content

Commit

Permalink
Merge #2103
Browse files Browse the repository at this point in the history
2103: Cardano Node TODOs 2020-11-12 r=Jimbo4350 a=Jimbo4350

- Add prettyPrintJSON function 
- Replace CardanoEra with ByronKeyFormat

Co-authored-by: Jordan Millar <jordan.millar@iohk.io>
  • Loading branch information
iohk-bors[bot] and Jimbo4350 committed Dec 8, 2020
2 parents 52dcece + 17ed5c5 commit fefe4b5
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 123 deletions.
18 changes: 9 additions & 9 deletions cardano-api/src/Cardano/Api/SerialiseJSON.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Cardano.Api.SerialiseJSON
( serialiseToJSON
, ToJSON(..)
, deserialiseFromJSON
, prettyPrintJSON
, FromJSON(..)
, JsonDecodeError(..)
, readFileJSON
Expand All @@ -13,19 +14,17 @@ module Cardano.Api.SerialiseJSON

import Prelude

import Control.Monad.Trans.Except (runExceptT)
import Control.Monad.Trans.Except.Extra (firstExceptT, handleIOExceptT, hoistEither)
import Data.Aeson (FromJSON (..), ToJSON (..))
import qualified Data.Aeson as Aeson
import Data.Aeson.Encode.Pretty (encodePretty)
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as LBS

import Data.Aeson (FromJSON (..), ToJSON (..))
import qualified Data.Aeson as Aeson

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

import Cardano.Api.HasTypeProxy
import Cardano.Api.Error
import Cardano.Api.HasTypeProxy


newtype JsonDecodeError = JsonDecodeError String
Expand All @@ -38,7 +37,8 @@ instance Error JsonDecodeError where
serialiseToJSON :: ToJSON a => a -> ByteString
serialiseToJSON = LBS.toStrict . Aeson.encode

--TODO: add prettyPrintJSON :: ToJSON a => a -> ByteString
prettyPrintJSON :: ToJSON a => a -> ByteString
prettyPrintJSON = LBS.toStrict . encodePretty

deserialiseFromJSON :: FromJSON a
=> AsType a
Expand Down
25 changes: 13 additions & 12 deletions cardano-cli/src/Cardano/CLI/Byron/Commands.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import Cardano.CLI.Types

import Cardano.Chain.Common (Address (..))
import Cardano.Chain.UTxO (TxIn (..), TxOut (..))
import Cardano.CLI.Shelley.Commands (ByronKeyFormat)

data ByronCommand =

Expand All @@ -36,44 +37,44 @@ data ByronCommand =
| Genesis
NewDirectory
GenesisParameters
CardanoEra
ByronKeyFormat
| PrintGenesisHash
GenesisFile

--- Key Related Commands ---
| Keygen
CardanoEra
ByronKeyFormat
NewSigningKeyFile
PasswordRequirement
| ToVerification
CardanoEra
ByronKeyFormat
SigningKeyFile
NewVerificationKeyFile

| PrettySigningKeyPublic
CardanoEra
ByronKeyFormat
SigningKeyFile

| MigrateDelegateKeyFrom
CardanoEra
-- ^ Old CardanoEra
ByronKeyFormat
-- ^ Old ByronKeyFormat
SigningKeyFile
-- ^ Old key
CardanoEra
-- ^ New CardanoEra
ByronKeyFormat
-- ^ New ByronKeyFormat
NewSigningKeyFile
-- ^ New Key

| PrintSigningKeyAddress
CardanoEra
ByronKeyFormat
NetworkId
SigningKeyFile

--- Delegation Related Commands ---

| IssueDelegationCertificate
NetworkId
CardanoEra
ByronKeyFormat
EpochNumber
-- ^ The epoch from which the delegation is valid.
SigningKeyFile
Expand Down Expand Up @@ -101,7 +102,7 @@ data ByronCommand =
| SpendGenesisUTxO
GenesisFile
NetworkId
CardanoEra
ByronKeyFormat
NewTxFile
-- ^ Filepath of the newly created transaction.
SigningKeyFile
Expand All @@ -112,7 +113,7 @@ data ByronCommand =
-- ^ Tx output.
| SpendUTxO
NetworkId
CardanoEra
ByronKeyFormat
NewTxFile
-- ^ Filepath of the newly created transaction.
SigningKeyFile
Expand Down
17 changes: 7 additions & 10 deletions cardano-cli/src/Cardano/CLI/Byron/Delegation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ import qualified Cardano.CLI.Byron.Legacy as Legacy
import Cardano.Crypto (ProtocolMagicId, SigningKey)
import qualified Cardano.Crypto as Crypto

import Cardano.CLI.Byron.Key (ByronKeyFailure, CardanoEra (..), renderByronKeyFailure,
serialiseSigningKey)
import Cardano.CLI.Byron.Key (ByronKeyFailure, renderByronKeyFailure, serialiseSigningKey)
import Cardano.CLI.Helpers (textShow)
import Cardano.CLI.Shelley.Commands (ByronKeyFormat (..))
import Cardano.CLI.Types (CertificateFile (..))

data ByronDelegationError
Expand Down Expand Up @@ -117,11 +117,8 @@ checkDlgCert cert magic issuerVK' delegateVK' =
serialiseDelegationCert :: Dlg.Certificate -> LB.ByteString
serialiseDelegationCert = canonicalEncodePretty

serialiseDelegateKey :: CardanoEra -> SigningKey -> Either ByronDelegationError LB.ByteString
serialiseDelegateKey ByronEraLegacy sk = pure
. toLazyByteString
. Legacy.encodeLegacyDelegateKey
$ Legacy.LegacyDelegateKey sk
serialiseDelegateKey ByronEra sk =
first ByronDelegationKeyError $
serialiseSigningKey ByronEra sk
serialiseDelegateKey :: ByronKeyFormat -> SigningKey -> Either ByronDelegationError LB.ByteString
serialiseDelegateKey LegacyByronKeyFormat sk =
pure . toLazyByteString . Legacy.encodeLegacyDelegateKey $ Legacy.LegacyDelegateKey sk
serialiseDelegateKey NonLegacyByronKeyFormat sk =
first ByronDelegationKeyError $ serialiseSigningKey NonLegacyByronKeyFormat sk
6 changes: 2 additions & 4 deletions cardano-cli/src/Cardano/CLI/Byron/Genesis.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ import qualified Cardano.Crypto as Crypto
import Cardano.CLI.Byron.Delegation
import Cardano.CLI.Byron.Key
import Cardano.CLI.Helpers (textShow)
import Cardano.CLI.Shelley.Commands (ByronKeyFormat (..))
import Cardano.CLI.Types (GenesisFile (..))

data ByronGenesisError
= ByronDelegationCertSerializationError !ByronDelegationError
| ByronDelegationKeySerializationError ByronDelegationError
| ByronGenesisCardanoEraNotSupported !CardanoEra
| GenesisGenerationError !Genesis.GenesisDataGenerationError
| GenesisOutputDirAlreadyExists FilePath
| GenesisReadError !FilePath !Genesis.GenesisDataError
Expand All @@ -80,8 +80,6 @@ renderByronGenesisError err =
"Error creating genesis delegation: " <> textShow genDelegError
GenesisGenerationError genDataGenError ->
"Error generating genesis: " <> textShow genDataGenError
ByronGenesisCardanoEraNotSupported era ->
"Error while serialising genesis, " <> textShow era <> " is not supported."
GenesisOutputDirAlreadyExists genOutDir ->
"Genesis output directory already exists: " <> textShow genOutDir
GenesisReadError genFp genDataError ->
Expand Down Expand Up @@ -172,7 +170,7 @@ readGenesis (GenesisFile file) nw =
-- thrown if the directory already exists, or the genesis has delegate keys that
-- are not delegated to.
dumpGenesis
:: CardanoEra
:: ByronKeyFormat
-> NewDirectory
-> Genesis.GenesisData
-> Genesis.GeneratedSecrets
Expand Down
32 changes: 12 additions & 20 deletions cardano-cli/src/Cardano/CLI/Byron/Key.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ module Cardano.CLI.Byron.Key
, NewSigningKeyFile(..)
, NewVerificationKeyFile(..)
, VerificationKeyFile(..)
, CardanoEra(..)
, serialiseSigningKey
, deserialiseSigningKey
, keygen
Expand Down Expand Up @@ -43,6 +42,7 @@ import qualified Cardano.Chain.Common as Common
import qualified Cardano.Chain.Genesis as Genesis
import qualified Cardano.CLI.Byron.Legacy as Legacy
import Cardano.CLI.Helpers (textShow)
import Cardano.CLI.Shelley.Commands (ByronKeyFormat (..))
import Cardano.CLI.Types
import Cardano.Crypto (SigningKey (..))
import qualified Cardano.Crypto.Random as Crypto
Expand Down Expand Up @@ -85,27 +85,21 @@ data PasswordRequirement
type PasswordPrompt = String

-- | Some commands have variants or file formats that depend on the era.
--
-- TODO: this looks like it's only used for Byron era keys, so could be renamed
--
data CardanoEra = ByronEraLegacy | ByronEra
deriving Show

serialiseSigningKey
:: CardanoEra
:: ByronKeyFormat
-> Crypto.SigningKey
-> Either ByronKeyFailure LB.ByteString
serialiseSigningKey ByronEraLegacy (Crypto.SigningKey k) = pure $ toLazyByteString (Crypto.toCBORXPrv k)
serialiseSigningKey ByronEra (Crypto.SigningKey k) = pure $ toLazyByteString (Crypto.toCBORXPrv k)
serialiseSigningKey _ (Crypto.SigningKey k) = pure $ toLazyByteString (Crypto.toCBORXPrv k)

deserialiseSigningKey :: CardanoEra -> FilePath -> LB.ByteString
deserialiseSigningKey :: ByronKeyFormat -> FilePath -> LB.ByteString
-> Either ByronKeyFailure SigningKey
deserialiseSigningKey ByronEraLegacy fp delSkey =
deserialiseSigningKey LegacyByronKeyFormat fp delSkey =
case deserialiseFromBytes Legacy.decodeLegacyDelegateKey delSkey of
Left deSerFail -> Left $ SigningKeyDeserialisationFailed fp deSerFail
Right (_, Legacy.LegacyDelegateKey sKey ) -> pure sKey

deserialiseSigningKey ByronEra fp delSkey =
deserialiseSigningKey NonLegacyByronKeyFormat fp delSkey =
case deserialiseFromBytes Crypto.fromCBORXPrv delSkey of
Left deSerFail -> Left $ SigningKeyDeserialisationFailed fp deSerFail
Right (_, sKey) -> Right $ SigningKey sKey
Expand All @@ -123,12 +117,12 @@ prettyPublicKey vk =
-- TODO: we need to support password-protected secrets.
-- | Read signing key from a file. Throw an error if the file can't be read or
-- fails to deserialise.
readEraSigningKey :: CardanoEra -> SigningKeyFile -> ExceptT ByronKeyFailure IO SigningKey
readEraSigningKey era (SigningKeyFile fp) = do
readEraSigningKey :: ByronKeyFormat -> SigningKeyFile -> ExceptT ByronKeyFailure IO SigningKey
readEraSigningKey bKeyFormat (SigningKeyFile fp) = do
sK <- handleIOExceptT (ReadSigningKeyFailure fp . T.pack . displayException) $ LB.readFile fp

-- Signing Key
hoistEither $ deserialiseSigningKey era fp sK
hoistEither $ deserialiseSigningKey bKeyFormat fp sK

-- | Read verification key from a file. Throw an error if the file can't be read
-- or the key fails to deserialise.
Expand All @@ -141,12 +135,10 @@ readPaymentVerificationKey (VerificationKeyFile fp) = do
firstExceptT (VerificationKeyDeserialisationFailed fp . T.pack . show) eVk


serialisePoorKey :: CardanoEra -> Genesis.PoorSecret
serialisePoorKey :: ByronKeyFormat -> Genesis.PoorSecret
-> Either ByronKeyFailure LB.ByteString
serialisePoorKey ByronEraLegacy ps =
serialiseSigningKey ByronEraLegacy $ Genesis.poorSecretToKey ps
serialisePoorKey ByronEra ps =
serialiseSigningKey ByronEra $ Genesis.poorSecretToKey ps
serialisePoorKey bKeyFormat ps =
serialiseSigningKey bKeyFormat $ Genesis.poorSecretToKey ps

-- | Generate a cryptographically random signing key,
-- protected with a (potentially empty) passphrase.
Expand Down
33 changes: 17 additions & 16 deletions cardano-cli/src/Cardano/CLI/Byron/Parsers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import Cardano.CLI.Byron.Key
import Cardano.CLI.Byron.Tx
import Cardano.CLI.Byron.UpdateProposal
import Cardano.CLI.Run (ClientCommand (ByronCommand))
import Cardano.CLI.Shelley.Commands (ByronKeyFormat (..))
import Cardano.CLI.Types

command' :: String -> String -> Parser a -> Mod CommandFields a
Expand Down Expand Up @@ -144,7 +145,7 @@ parseDelegationRelatedValues =
\ delegator to sign blocks on behalf of the issuer"
$ IssueDelegationCertificate
<$> pNetworkId
<*> parseCardanoEra
<*> parseByronKeyFormat
<*> ( EpochNumber
<$> parseIntegral
"since-epoch"
Expand Down Expand Up @@ -210,7 +211,7 @@ parseGenesisRelatedValues =
"genesis-output-dir"
"Non-existent directory where genesis JSON file and secrets shall be placed."
<*> parseGenesisParameters
<*> parseCardanoEra
<*> parseByronKeyFormat
, command' "print-genesis-hash" "Compute hash of a genesis file."
$ PrintGenesisHash
<$> parseGenesisFile "genesis-json"
Expand All @@ -223,14 +224,14 @@ parseKeyRelatedValues =
mconcat
[ command' "keygen" "Generate a signing key."
$ Keygen
<$> parseCardanoEra
<$> parseByronKeyFormat
<*> parseNewSigningKeyFile "secret"
<*> parsePassword
, command'
"to-verification"
"Extract a verification key in its base64 form."
$ ToVerification
<$> parseCardanoEra
<$> parseByronKeyFormat
<*> parseSigningKeyFile
"secret"
"Signing key file to extract the verification part from."
Expand All @@ -239,15 +240,15 @@ parseKeyRelatedValues =
"signing-key-public"
"Pretty-print a signing key's verification key (not a secret)."
$ PrettySigningKeyPublic
<$> parseCardanoEra
<$> parseByronKeyFormat
<*> parseSigningKeyFile
"secret"
"Signing key to pretty-print."
, command'
"signing-key-address"
"Print address of a signing key."
$ PrintSigningKeyAddress
<$> parseCardanoEra
<$> parseByronKeyFormat
<*> pNetworkId
<*> parseSigningKeyFile
"secret"
Expand All @@ -256,9 +257,9 @@ parseKeyRelatedValues =
"migrate-delegate-key-from"
"Migrate a delegate key from an older version."
$ MigrateDelegateKeyFrom
<$> parseCardanoEra -- Old CardanoEra
<$> parseByronKeyFormat -- Old Byron key format
<*> parseSigningKeyFile "from" "Signing key file to migrate."
<*> parseCardanoEra -- New CardanoEra
<*> parseByronKeyFormat -- New Byron key format
<*> parseNewSigningKeyFile "to"
]

Expand Down Expand Up @@ -341,7 +342,7 @@ parseTxRelatedValues =
$ SpendGenesisUTxO
<$> parseGenesisFile "genesis-json"
<*> pNetworkId
<*> parseCardanoEra
<*> parseByronKeyFormat
<*> parseNewTxFile "tx"
<*> parseSigningKeyFile
"wallet-key"
Expand All @@ -356,7 +357,7 @@ parseTxRelatedValues =
"Write a file with a signed transaction, spending normal UTxO."
$ SpendUTxO
<$> pNetworkId
<*> parseCardanoEra
<*> parseByronKeyFormat
<*> parseNewTxFile "tx"
<*> parseSigningKeyFile
"wallet-key"
Expand Down Expand Up @@ -619,19 +620,19 @@ parseAddress opt desc =
option (cliParseBase58Address <$> str)
$ long opt <> metavar "ADDR" <> help desc

parseCardanoEra :: Parser CardanoEra
parseCardanoEra = asum
[ flag' ByronEraLegacy $
parseByronKeyFormat :: Parser ByronKeyFormat
parseByronKeyFormat = asum
[ flag' LegacyByronKeyFormat $
long "byron-legacy-formats"
<> help "Byron/cardano-sl formats and compatibility"

, flag' ByronEra $
, flag' NonLegacyByronKeyFormat $
long "byron-formats"
<> help "Byron era formats and compatibility"

-- And hidden compatibility flag aliases:
, flag' ByronEraLegacy $ hidden <> long "byron-legacy"
, flag' ByronEra $ hidden <> long "real-pbft"
, flag' LegacyByronKeyFormat $ hidden <> long "byron-legacy"
, flag' NonLegacyByronKeyFormat $ hidden <> long "real-pbft"
]

parseCertificateFile :: String -> String -> Parser CertificateFile
Expand Down
Loading

0 comments on commit fefe4b5

Please sign in to comment.