Skip to content

Commit

Permalink
Merge pull request #360 from input-output-hk/newhoggy/command-arg-typ…
Browse files Browse the repository at this point in the history
…es-for-key-commands

Command argument types for `key` commands
  • Loading branch information
newhoggy authored Oct 10, 2023
2 parents 0fef8a4 + 965314c commit eb0e52b
Show file tree
Hide file tree
Showing 8 changed files with 673 additions and 526 deletions.
1 change: 1 addition & 0 deletions cardano-cli/cardano-cli.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ library
Cardano.CLI.Legacy.Commands.TextView
Cardano.CLI.Legacy.Commands.Transaction
Cardano.CLI.Legacy.Options
Cardano.CLI.Legacy.Options.Key
Cardano.CLI.Legacy.Run
Cardano.CLI.Legacy.Run.Address
Cardano.CLI.Legacy.Run.Genesis
Expand Down
122 changes: 85 additions & 37 deletions cardano-cli/src/Cardano/CLI/EraBased/Commands/Key.hs
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE LambdaCase #-}

module Cardano.CLI.EraBased.Commands.Key
( KeyCmds (..)
, KeyVerificationKeyCmdArgs(..)
, KeyNonExtendedKeyCmdArgs(..)
, KeyConvertByronKeyCmdArgs(..)
, KeyConvertByronGenesisVKeyCmdArgs(..)
, KeyConvertITNKeyCmdArgs(..)
, KeyConvertITNExtendedKeyCmdArgs(..)
, KeyConvertITNBip32KeyCmdArgs(..)
, KeyConvertCardanoAddressKeyCmdArgs(..)
, renderKeyCmds
) where

Expand All @@ -13,50 +22,89 @@ import Cardano.CLI.Types.Common
import Data.Text (Text)

data KeyCmds era
= KeyGetVerificationKey
(SigningKeyFile In)
(VerificationKeyFile Out)
| KeyNonExtendedKey
(VerificationKeyFile In)
(VerificationKeyFile Out)
| KeyConvertByronKey
(Maybe Text)
ByronKeyType
(SomeKeyFile In)
(File () Out)
| KeyConvertByronGenesisVKey
VerificationKeyBase64
(File () Out)
| KeyConvertITNStakeKey
(SomeKeyFile In)
(File () Out)
| KeyConvertITNExtendedToStakeKey
(SomeKeyFile In)
(File () Out)
| KeyConvertITNBip32ToStakeKey
(SomeKeyFile In)
(File () Out)
| KeyConvertCardanoAddressSigningKey
CardanoAddressKeyType
(SigningKeyFile In)
(File () Out)
= KeyVerificationKeyCmd !KeyVerificationKeyCmdArgs
| KeyNonExtendedKeyCmd !KeyNonExtendedKeyCmdArgs
| KeyConvertByronKeyCmd !KeyConvertByronKeyCmdArgs
| KeyConvertByronGenesisVKeyCmd !KeyConvertByronGenesisVKeyCmdArgs
| KeyConvertITNKeyCmd !KeyConvertITNKeyCmdArgs
| KeyConvertITNExtendedKeyCmd !KeyConvertITNExtendedKeyCmdArgs
| KeyConvertITNBip32KeyCmd !KeyConvertITNBip32KeyCmdArgs
| KeyConvertCardanoAddressKeyCmd !KeyConvertCardanoAddressKeyCmdArgs
deriving Show

-- | Get a verification key from a signing key. This supports all key types
data KeyVerificationKeyCmdArgs = KeyVerificationKeyCmdArgs
{ skeyFile :: !(SigningKeyFile In) -- ^ Input filepath of the signing key
, vkeyFile :: !(VerificationKeyFile Out) -- ^ Output filepath of the verification key
} deriving Show

-- | Get a non-extended verification key from an extended verification key. This
-- supports all extended key types.
data KeyNonExtendedKeyCmdArgs = KeyNonExtendedKeyCmdArgs
{ extendedVkeyFileIn :: !(VerificationKeyFile In) -- ^ Input filepath of the ed25519-bip32 verification key
, nonExtendedVkeyFileOut :: !(VerificationKeyFile Out) -- ^ Output filepath of the verification key
} deriving Show

-- | Convert a Byron payment, genesis or genesis delegate key (signing or
-- verification) to a corresponding Shelley-format key.
data KeyConvertByronKeyCmdArgs = KeyConvertByronKeyCmdArgs
{ mPassword :: !(Maybe Text) -- ^ Password for signing key (if applicable)
, byronKeyType :: !ByronKeyType -- ^ The byron key type of the input file
, someKeyFileIn :: !(SomeKeyFile In) -- ^ Input file containing the byron key
, someKeyFileOut :: !(File () Out) -- ^ The output file to which the Shelley-format key will be written
} deriving Show

-- Convert a Base64-encoded Byron genesis verification key to a Shelley genesis
-- verification key
data KeyConvertByronGenesisVKeyCmdArgs = KeyConvertByronGenesisVKeyCmdArgs
{ vkey :: !VerificationKeyBase64 -- ^ Base64 string for the Byron genesis verification key
, vkeyFileOut :: !(File () Out) -- ^ The output file
} deriving Show

-- | Convert an Incentivized Testnet (ITN) non-extended (Ed25519) signing or
-- verification key to a corresponding Shelley stake key
data KeyConvertITNKeyCmdArgs = KeyConvertITNKeyCmdArgs
{ itnKeyFile :: !(SomeKeyFile In) -- ^ Filepath of the ITN key (signing or verification)
, outFile :: !(File () Out) -- ^ The output file
} deriving Show

-- | Convert an Incentivized Testnet (ITN) extended (Ed25519Extended) signing key
-- to a corresponding Shelley stake signing key
data KeyConvertITNExtendedKeyCmdArgs = KeyConvertITNExtendedKeyCmdArgs
{ itnPrivKeyFile :: !(SomeKeyFile In) -- ^ Filepath of the ITN signing key
, outFile :: !(File () Out) -- ^ The output file
} deriving Show

-- | Convert an Incentivized Testnet (ITN) BIP32 (Ed25519Bip32) signing key to a
-- corresponding Shelley stake signing key
data KeyConvertITNBip32KeyCmdArgs = KeyConvertITNBip32KeyCmdArgs
{ itnPrivKeyFile :: !(SomeKeyFile In) -- ^ Filepath of the ITN signing key
, outFile :: !(File () Out) -- ^ The output file
} deriving Show

-- | Convert a cardano-address extended signing key to a corresponding
-- Shelley-format key
data KeyConvertCardanoAddressKeyCmdArgs = KeyConvertCardanoAddressKeyCmdArgs
{ cardanoAddressKeyType :: !CardanoAddressKeyType -- ^ Address key type of th signing key input file
, skeyFileIn :: !(SigningKeyFile In) -- ^ Input filepath of the signing key
, skeyFileOut :: !(File () Out) -- ^ The output file
} deriving Show

renderKeyCmds :: KeyCmds era -> Text
renderKeyCmds = \case
KeyGetVerificationKey {} ->
KeyVerificationKeyCmd {} ->
"key verification-key"
KeyNonExtendedKey {} ->
KeyNonExtendedKeyCmd {} ->
"key non-extended-key"
KeyConvertByronKey {} ->
KeyConvertByronKeyCmd {} ->
"key convert-byron-key"
KeyConvertByronGenesisVKey {} ->
"key convert-byron-genesis-key"
KeyConvertITNStakeKey {} ->
KeyConvertByronGenesisVKeyCmd {} ->
"key convert-byron-genesis-vkey"
KeyConvertITNKeyCmd {} ->
"key convert-itn-key"
KeyConvertITNExtendedToStakeKey {} ->
KeyConvertITNExtendedKeyCmd {} ->
"key convert-itn-extended-key"
KeyConvertITNBip32ToStakeKey {} ->
KeyConvertITNBip32KeyCmd {} ->
"key convert-itn-bip32-key"
KeyConvertCardanoAddressSigningKey {} ->
"key convert-cardano-address-signing-key"
KeyConvertCardanoAddressKeyCmd {} ->
"key convert-cardano-address-key"
110 changes: 59 additions & 51 deletions cardano-cli/src/Cardano/CLI/EraBased/Options/Key.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ pKeyCmds =
)
[ Just
$ subParser "verification-key"
$ Opt.info pKeyGetVerificationKey
$ Opt.info pKeyVerificationKeyCmd
$ Opt.progDesc
$ mconcat
[ "Get a verification key from a signing key. This "
, " supports all key types."
]
, Just
$ subParser "non-extended-key"
$ Opt.info pKeyNonExtendedKey
$ Opt.info pKeyNonExtendedKeyCmd
$ Opt.progDesc
$ mconcat
[ "Get a non-extended verification key from an "
Expand All @@ -48,7 +48,7 @@ pKeyCmds =
]
, Just
$ subParser "convert-byron-key"
$ Opt.info pKeyConvertByronKey
$ Opt.info pKeyConvertByronKeyCmd
$ Opt.progDesc
$ mconcat
[ "Convert a Byron payment, genesis or genesis "
Expand All @@ -57,7 +57,7 @@ pKeyCmds =
]
, Just
$ subParser "convert-byron-genesis-vkey"
$ Opt.info pKeyConvertByronGenesisVKey
$ Opt.info pKeyConvertByronGenesisKeyCmd
$ Opt.progDesc
$ mconcat
[ "Convert a Base64-encoded Byron genesis "
Expand All @@ -66,7 +66,7 @@ pKeyCmds =
]
, Just
$ subParser "convert-itn-key"
$ Opt.info pKeyConvertITNKey
$ Opt.info pKeyConvertITNKeyCmd
$ Opt.progDesc
$ mconcat
[ "Convert an Incentivized Testnet (ITN) non-extended "
Expand All @@ -75,7 +75,7 @@ pKeyCmds =
]
, Just
$ subParser "convert-itn-extended-key"
$ Opt.info pKeyConvertITNExtendedKey
$ Opt.info pKeyConvertITNExtendedKeyCmd
$ Opt.progDesc
$ mconcat
[ "Convert an Incentivized Testnet (ITN) extended "
Expand All @@ -84,7 +84,7 @@ pKeyCmds =
]
, Just
$ subParser "convert-itn-bip32-key"
$ Opt.info pKeyConvertITNBip32Key
$ Opt.info pKeyConvertITNBip32KeyCmd
$ Opt.progDesc
$ mconcat
[ "Convert an Incentivized Testnet (ITN) BIP32 "
Expand All @@ -93,33 +93,36 @@ pKeyCmds =
]
, Just
$ subParser "convert-cardano-address-key"
$ Opt.info pKeyConvertCardanoAddressSigningKey
$ Opt.info pKeyConvertCardanoAddressKeyCmd
$ Opt.progDesc
$ mconcat
[ "Convert a cardano-address extended signing key "
, "to a corresponding Shelley-format key."
]
]

pKeyGetVerificationKey :: Parser (KeyCmds era)
pKeyGetVerificationKey =
KeyGetVerificationKey
<$> pSigningKeyFileIn
<*> pVerificationKeyFileOut
pKeyVerificationKeyCmd :: Parser (KeyCmds era)
pKeyVerificationKeyCmd =
fmap KeyVerificationKeyCmd $
KeyVerificationKeyCmdArgs
<$> pSigningKeyFileIn
<*> pVerificationKeyFileOut

pKeyNonExtendedKey :: Parser (KeyCmds era)
pKeyNonExtendedKey =
KeyNonExtendedKey
<$> pExtendedVerificationKeyFileIn
<*> pVerificationKeyFileOut
pKeyNonExtendedKeyCmd :: Parser (KeyCmds era)
pKeyNonExtendedKeyCmd =
fmap KeyNonExtendedKeyCmd $
KeyNonExtendedKeyCmdArgs
<$> pExtendedVerificationKeyFileIn
<*> pVerificationKeyFileOut

pKeyConvertByronKey :: Parser (KeyCmds era)
pKeyConvertByronKey =
KeyConvertByronKey
<$> optional pPassword
<*> pByronKeyType
<*> pByronKeyFile
<*> pOutputFile
pKeyConvertByronKeyCmd :: Parser (KeyCmds era)
pKeyConvertByronKeyCmd =
fmap KeyConvertByronKeyCmd $
KeyConvertByronKeyCmdArgs
<$> optional pPassword
<*> pByronKeyType
<*> pByronKeyFile
<*> pOutputFile

pPassword :: Parser Text
pPassword =
Expand Down Expand Up @@ -183,11 +186,12 @@ pByronVerificationKeyFile =
, Opt.completer (Opt.bashCompleter "file")
]

pKeyConvertByronGenesisVKey :: Parser (KeyCmds era)
pKeyConvertByronGenesisVKey =
KeyConvertByronGenesisVKey
<$> pByronGenesisVKeyBase64
<*> pOutputFile
pKeyConvertByronGenesisKeyCmd :: Parser (KeyCmds era)
pKeyConvertByronGenesisKeyCmd =
fmap KeyConvertByronGenesisVKeyCmd $
KeyConvertByronGenesisVKeyCmdArgs
<$> pByronGenesisVKeyBase64
<*> pOutputFile

pByronGenesisVKeyBase64 :: Parser VerificationKeyBase64
pByronGenesisVKeyBase64 =
Expand All @@ -197,23 +201,26 @@ pByronGenesisVKeyBase64 =
, Opt.help "Base64 string for the Byron genesis verification key."
]

pKeyConvertITNKey :: Parser (KeyCmds era)
pKeyConvertITNKey =
KeyConvertITNStakeKey
<$> pITNKeyFIle
<*> pOutputFile
pKeyConvertITNKeyCmd :: Parser (KeyCmds era)
pKeyConvertITNKeyCmd =
fmap KeyConvertITNKeyCmd $
KeyConvertITNKeyCmdArgs
<$> pITNKeyFIle
<*> pOutputFile

pKeyConvertITNExtendedKey :: Parser (KeyCmds era)
pKeyConvertITNExtendedKey =
KeyConvertITNExtendedToStakeKey
<$> pITNSigningKeyFile
<*> pOutputFile
pKeyConvertITNExtendedKeyCmd :: Parser (KeyCmds era)
pKeyConvertITNExtendedKeyCmd =
fmap KeyConvertITNExtendedKeyCmd $
KeyConvertITNExtendedKeyCmdArgs
<$> pITNSigningKeyFile
<*> pOutputFile

pKeyConvertITNBip32Key :: Parser (KeyCmds era)
pKeyConvertITNBip32Key =
KeyConvertITNBip32ToStakeKey
<$> pITNSigningKeyFile
<*> pOutputFile
pKeyConvertITNBip32KeyCmd :: Parser (KeyCmds era)
pKeyConvertITNBip32KeyCmd =
fmap KeyConvertITNBip32KeyCmd $
KeyConvertITNBip32KeyCmdArgs
<$> pITNSigningKeyFile
<*> pOutputFile

pITNKeyFIle :: Parser (SomeKeyFile direction)
pITNKeyFIle =
Expand All @@ -240,12 +247,13 @@ pITNVerificationKeyFile =
, Opt.completer (Opt.bashCompleter "file")
]

pKeyConvertCardanoAddressSigningKey :: Parser (KeyCmds era)
pKeyConvertCardanoAddressSigningKey =
KeyConvertCardanoAddressSigningKey
<$> pCardanoAddressKeyType
<*> pSigningKeyFileIn
<*> pOutputFile
pKeyConvertCardanoAddressKeyCmd :: Parser (KeyCmds era)
pKeyConvertCardanoAddressKeyCmd =
fmap KeyConvertCardanoAddressKeyCmd $
KeyConvertCardanoAddressKeyCmdArgs
<$> pCardanoAddressKeyType
<*> pSigningKeyFileIn
<*> pOutputFile

pCardanoAddressKeyType :: Parser CardanoAddressKeyType
pCardanoAddressKeyType =
Expand Down
Loading

0 comments on commit eb0e52b

Please sign in to comment.