Skip to content

Commit 4c52af2

Browse files
committed
Add sub parsers
1 parent 8e23329 commit 4c52af2

File tree

10 files changed

+270
-26
lines changed

10 files changed

+270
-26
lines changed

cardano-cli/cardano-cli.cabal

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,10 @@ library
187187
Cardano.CLI.Orphan
188188
Cardano.CLI.Parser
189189
Cardano.CLI.Read
190+
Cardano.CLI.Read.Committee.ColdKey
191+
Cardano.CLI.Read.Committee.HotKey
190192
Cardano.CLI.Read.DRep
193+
Cardano.CLI.Read.GovernanceActionId
191194
Cardano.CLI.Render
192195
Cardano.CLI.Run
193196
Cardano.CLI.TopHandler

cardano-cli/src/Cardano/CLI/EraIndependent/Cip/Cip129/Command.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ renderCip129Command :: Cip129 -> Text
1818
renderCip129Command (Cip129DRep{}) = "cip-129 drep"
1919
renderCip129Command (Cip129CommitteeHotKey{}) = "cip-129 committee-hot-key"
2020
renderCip129Command (Cip129CommitteeColdKey{}) = "cip-129 committee-cold-key"
21-
renderCip129Command (Cip129GovernanceAction{}) = "cip-129 governance-action"
21+
renderCip129Command (Cip129GovernanceAction{}) = "cip-129 governance-action-id"
Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,64 @@
11
{-# LANGUAGE DataKinds #-}
22

33
module Cardano.CLI.EraIndependent.Cip.Cip129.Conversion
4-
( encodeCip129DrepVerficationKeyText
4+
( encodeCip129DrepVerficationKeyText
5+
, encodeCip129CommitteeColdVerficationKeyText
6+
, encodeCip129CommitteeHotVerficationKeyText
7+
, encodeCip129GovernanceActionIdText
58
)
69
where
710

8-
import Cardano.CLI.Read.DRep
9-
import Data.Text
10-
import Cardano.Api.Ledger qualified as L
1111
import Cardano.Api.Ledger (StandardCrypto)
12+
import Cardano.Api.Ledger qualified as L
1213
import Cardano.Api.Shelley
1314

15+
import Cardano.CLI.Read.Committee.ColdKey
16+
import Cardano.CLI.Read.Committee.HotKey
17+
import Cardano.CLI.Read.DRep
18+
19+
import Data.Text
1420

1521
encodeCip129DrepVerficationKeyText :: AnyDrepVerificationKey -> Text
16-
encodeCip129DrepVerficationKeyText = serialiseToBech32CIP129 . anyDrepVerificationKeyToCredential
17-
18-
19-
anyDrepVerificationKeyToCredential :: AnyDrepVerificationKey -> L.Credential L.DRepRole StandardCrypto
20-
anyDrepVerificationKeyToCredential drepKey =
21-
case drepKey of
22-
AnyDrepVerificationKey vk ->
23-
let DRepKeyHash hash = verificationKeyHash vk
24-
in L.KeyHashObj hash
25-
AnyDrepExtendedVerificationKey vk ->
26-
let DRepExtendedKeyHash hash = verificationKeyHash vk
27-
in L.KeyHashObj hash
22+
encodeCip129DrepVerficationKeyText = serialiseToBech32CIP129 . anyDrepVerificationKeyToCredential
23+
24+
anyDrepVerificationKeyToCredential
25+
:: AnyDrepVerificationKey -> L.Credential L.DRepRole StandardCrypto
26+
anyDrepVerificationKeyToCredential drepKey =
27+
case drepKey of
28+
AnyDrepVerificationKey vk ->
29+
let DRepKeyHash hash = verificationKeyHash vk
30+
in L.KeyHashObj hash
31+
AnyDrepExtendedVerificationKey vk ->
32+
let DRepExtendedKeyHash hash = verificationKeyHash vk
33+
in L.KeyHashObj hash
34+
35+
encodeCip129CommitteeHotVerficationKeyText :: AnyCommitteeHotVerificationKey -> Text
36+
encodeCip129CommitteeHotVerficationKeyText = serialiseToBech32CIP129 . anyCommitteeHotVerificationKeyToCredential
37+
38+
anyCommitteeHotVerificationKeyToCredential
39+
:: AnyCommitteeHotVerificationKey -> L.Credential L.HotCommitteeRole StandardCrypto
40+
anyCommitteeHotVerificationKeyToCredential committeeHotKey =
41+
case committeeHotKey of
42+
AnyCommitteeHotVerificationKey vk ->
43+
let CommitteeHotKeyHash hash = verificationKeyHash vk
44+
in L.KeyHashObj hash
45+
AnyCommitteeHotExtendedVerificationKey vk ->
46+
let CommitteeHotExtendedKeyHash hash = verificationKeyHash vk
47+
in L.KeyHashObj hash
48+
49+
encodeCip129CommitteeColdVerficationKeyText :: AnyCommitteeColdVerificationKey -> Text
50+
encodeCip129CommitteeColdVerficationKeyText = serialiseToBech32CIP129 . anyCommitteeColdVerificationKeyToCredential
51+
52+
anyCommitteeColdVerificationKeyToCredential
53+
:: AnyCommitteeColdVerificationKey -> L.Credential L.ColdCommitteeRole StandardCrypto
54+
anyCommitteeColdVerificationKeyToCredential committeeColdKey =
55+
case committeeColdKey of
56+
AnyCommitteeColdVerificationKey vk ->
57+
let CommitteeColdKeyHash hash = verificationKeyHash vk
58+
in L.KeyHashObj hash
59+
AnyCommitteeColdExtendedVerificationKey vk ->
60+
let CommitteeColdExtendedKeyHash hash = verificationKeyHash vk
61+
in L.KeyHashObj hash
62+
63+
encodeCip129GovernanceActionIdText :: L.GovActionId StandardCrypto -> Text
64+
encodeCip129GovernanceActionIdText = serialiseToBech32CIP129

cardano-cli/src/Cardano/CLI/EraIndependent/Cip/Cip129/Run.hs

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@ import Cardano.CLI.EraIndependent.Cip.Cip129.Conversion
1313
import Cardano.CLI.EraIndependent.Cip.Common
1414
import Cardano.CLI.Orphan ()
1515
import Cardano.CLI.Read
16+
import Cardano.CLI.Read.Committee.ColdKey
17+
import Cardano.CLI.Read.Committee.HotKey
1618
import Cardano.CLI.Read.DRep
19+
import Cardano.CLI.Read.GovernanceActionId
1720

21+
import Data.ByteString (ByteString)
1822
import Data.ByteString qualified as BS
1923
import Data.ByteString.Char8 qualified as BSC
2024
import Data.Text.Encoding qualified as Text
@@ -32,11 +36,43 @@ runCip129 (Cip129DRep inp out) = do
3236
InputBech32Text t -> do
3337
fromEitherCli . Valid.toEither $ readDRepBech32VerificationKeyText t
3438
let cip129Output = Text.encodeUtf8 $ encodeCip129DrepVerficationKeyText k
35-
case out of
36-
OutputText ->
37-
liftIO $ BSC.hPutStrLn stdout cip129Output
38-
OutputFile (File fp) ->
39-
liftIO $ BS.writeFile fp cip129Output
40-
runCip129 (Cip129CommitteeHotKey _inp _out) = undefined
41-
runCip129 (Cip129CommitteeColdKey _inp _out) = undefined
42-
runCip129 (Cip129GovernanceAction _inp _out) = undefined
39+
renderOutput cip129Output out
40+
runCip129 (Cip129CommitteeHotKey inp out) = do
41+
k <- case inp of
42+
InputTextEnvelopeFile (File textEnvFp) -> do
43+
f <- liftIO $ fileOrPipe textEnvFp
44+
fromEitherIOCli $ readCommitteeHotVerificationKeyFile f
45+
InputHexText t ->
46+
fromEitherCli . Valid.toEither $ readCommitteeHotHexVerificationKeyText t
47+
InputBech32Text t ->
48+
fromEitherCli . Valid.toEither $ readCommitteeHotBech32VerificationKeyText t
49+
let cip129Output = Text.encodeUtf8 $ encodeCip129CommitteeHotVerficationKeyText k
50+
renderOutput cip129Output out
51+
runCip129 (Cip129CommitteeColdKey inp out) = do
52+
k <- case inp of
53+
InputTextEnvelopeFile (File textEnvFp) -> do
54+
f <- liftIO $ fileOrPipe textEnvFp
55+
fromEitherIOCli $ readCommitteeColdVerificationKeyFile f
56+
InputHexText t ->
57+
fromEitherCli . Valid.toEither $ readCommitteeColdHexVerificationKeyText t
58+
InputBech32Text t ->
59+
fromEitherCli . Valid.toEither $ readCommitteeColdBech32VerificationKeyText t
60+
let cip129Output = Text.encodeUtf8 $ encodeCip129CommitteeColdVerficationKeyText k
61+
renderOutput cip129Output out
62+
runCip129 (Cip129GovernanceAction inp out) =
63+
case inp of
64+
InputHexText t -> do
65+
govId <- fromEitherCli $ readGoveranceActionIdHexText t
66+
let cip129Output = Text.encodeUtf8 $ encodeCip129GovernanceActionIdText govId
67+
renderOutput cip129Output out
68+
InputBech32Text{} ->
69+
throwCliError $ InputError "Bech32 encoded Governance Action Id is not supported"
70+
InputTextEnvelopeFile{} ->
71+
throwCliError $ InputError "TextEnvelope encoded Governance Action Id is not supported"
72+
73+
renderOutput :: ByteString -> Output -> CIO e ()
74+
renderOutput cip129Output out = case out of
75+
OutputText -> do
76+
liftIO $ BSC.hPutStrLn stdout cip129Output
77+
OutputFile (File fp) -> do
78+
liftIO $ BS.writeFile fp cip129Output

cardano-cli/src/Cardano/CLI/EraIndependent/Cip/Common.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
module Cardano.CLI.EraIndependent.Cip.Common
55
( -- * Input related
66
Input (..)
7+
, InputError (..)
78
, pInputFile
89
, pInputHexText
910
, pInputBech32Text
@@ -28,6 +29,11 @@ data Input
2829
| InputHexText Text
2930
| InputBech32Text Text
3031

32+
newtype InputError = InputError Text deriving Show
33+
34+
instance Error InputError where
35+
prettyError (InputError err) = pretty err
36+
3137
pInputFile :: String -> String -> Opt.Parser Input
3238
pInputFile optName desc =
3339
InputTextEnvelopeFile <$> pFileInDirection optName desc

cardano-cli/src/Cardano/CLI/Option.hs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ parseClientCommand envCli =
8989
, parseDebug envCli
9090
, backwardsCompatibilityCommands envCli
9191
, parseDisplayVersion (opts envCli)
92-
, CipFormatCmds <$> pCip129
92+
, parseCipFormatCmds
9393
, parseCompatibilityCommands envCli
9494
]
9595

@@ -103,6 +103,12 @@ parseByron mNetworkId =
103103
, command' "byron" "Byron specific commands" $ parseByronCommands mNetworkId
104104
]
105105

106+
parseCipFormatCmds :: Parser ClientCommand
107+
parseCipFormatCmds =
108+
subParser "cip-format" $
109+
Opt.info (CipFormatCmds <$> pCip129) $
110+
Opt.progDesc "Group of commands related to CIP changes."
111+
106112
parseHash :: Parser ClientCommand
107113
parseHash = HashCmds <$> pHashCmds
108114

cardano-cli/src/Cardano/CLI/Orphan.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,17 @@ import Cardano.Api.Ledger qualified as L
1313
import Cardano.Api.Shelley (VotesMergingConflict, scriptDataToJsonDetailedSchema)
1414

1515
import Data.Aeson
16+
import Text.Parsec qualified as Text
1617

1718
instance Error [Bech32DecodeError] where
1819
prettyError errs = mconcat $ map prettyError errs
1920

2021
instance Error [RawBytesHexError] where
2122
prettyError errs = mconcat $ map prettyError errs
2223

24+
instance Error Text.ParseError where
25+
prettyError = pretty . show
26+
2327
instance Error (VotesMergingConflict era) where
2428
prettyError = pretty . show
2529

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
{-# LANGUAGE FlexibleInstances #-}
2+
{-# LANGUAGE GADTs #-}
3+
{-# LANGUAGE StandaloneDeriving #-}
4+
5+
module Cardano.CLI.Read.Committee.ColdKey
6+
( AnyCommitteeColdVerificationKey (..)
7+
8+
-- * Read bech32 or hex encoded Committee Hot verification key
9+
, readCommitteeColdBech32VerificationKeyText
10+
, readCommitteeColdHexVerificationKeyText
11+
12+
-- * Read TextEnvelope Committee Hot verification key file
13+
, readCommitteeColdVerificationKeyFile
14+
)
15+
where
16+
17+
import Cardano.Api.Shelley
18+
19+
import Cardano.CLI.Read
20+
import Cardano.Prelude qualified as Text
21+
22+
import Prelude
23+
24+
import Data.Text (Text)
25+
import Data.Validation
26+
27+
data AnyCommitteeColdVerificationKey where
28+
AnyCommitteeColdVerificationKey :: VerificationKey CommitteeColdKey -> AnyCommitteeColdVerificationKey
29+
AnyCommitteeColdExtendedVerificationKey
30+
:: VerificationKey CommitteeColdExtendedKey -> AnyCommitteeColdVerificationKey
31+
32+
deriving instance Show AnyCommitteeColdVerificationKey
33+
34+
readCommitteeColdBech32VerificationKeyText
35+
:: Text -> Validation [Bech32DecodeError] AnyCommitteeColdVerificationKey
36+
readCommitteeColdBech32VerificationKeyText committeeColdText =
37+
let vkey =
38+
liftError return $
39+
AnyCommitteeColdVerificationKey
40+
<$> deserialiseFromBech32 (AsVerificationKey AsCommitteeColdKey) committeeColdText
41+
extendedVkey =
42+
liftError return $
43+
AnyCommitteeColdExtendedVerificationKey
44+
<$> deserialiseFromBech32 (AsVerificationKey AsCommitteeColdExtendedKey) committeeColdText
45+
in vkey <> extendedVkey
46+
47+
readCommitteeColdHexVerificationKeyText
48+
:: Text -> Validation [RawBytesHexError] AnyCommitteeColdVerificationKey
49+
readCommitteeColdHexVerificationKeyText committeeColdText =
50+
let committeeColdBs = Text.encodeUtf8 committeeColdText
51+
vkey =
52+
liftError return $
53+
AnyCommitteeColdVerificationKey
54+
<$> deserialiseFromRawBytesHex (AsVerificationKey AsCommitteeColdKey) committeeColdBs
55+
extendedVkey =
56+
liftError return $
57+
AnyCommitteeColdExtendedVerificationKey
58+
<$> deserialiseFromRawBytesHex (AsVerificationKey AsCommitteeColdExtendedKey) committeeColdBs
59+
in vkey <> extendedVkey
60+
61+
readCommitteeColdVerificationKeyFile
62+
:: FileOrPipe -> IO (Either (FileError TextEnvelopeError) AnyCommitteeColdVerificationKey)
63+
readCommitteeColdVerificationKeyFile = readFileOrPipeTextEnvelopeAnyOf types
64+
where
65+
types =
66+
[ FromSomeType (AsVerificationKey AsCommitteeColdKey) AnyCommitteeColdVerificationKey
67+
, FromSomeType (AsVerificationKey AsCommitteeColdExtendedKey) AnyCommitteeColdExtendedVerificationKey
68+
]
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
{-# LANGUAGE FlexibleInstances #-}
2+
{-# LANGUAGE GADTs #-}
3+
{-# LANGUAGE StandaloneDeriving #-}
4+
5+
module Cardano.CLI.Read.Committee.HotKey
6+
( AnyCommitteeHotVerificationKey (..)
7+
8+
-- * Read bech32 or hex encoded Committee Hot verification key
9+
, readCommitteeHotBech32VerificationKeyText
10+
, readCommitteeHotHexVerificationKeyText
11+
12+
-- * Read TextEnvelope Committee Hot verification key file
13+
, readCommitteeHotVerificationKeyFile
14+
)
15+
where
16+
17+
import Cardano.Api.Shelley
18+
19+
import Cardano.CLI.Read
20+
import Cardano.Prelude qualified as Text
21+
22+
import Prelude
23+
24+
import Data.Text (Text)
25+
import Data.Validation
26+
27+
data AnyCommitteeHotVerificationKey where
28+
AnyCommitteeHotVerificationKey :: VerificationKey CommitteeHotKey -> AnyCommitteeHotVerificationKey
29+
AnyCommitteeHotExtendedVerificationKey
30+
:: VerificationKey CommitteeHotExtendedKey -> AnyCommitteeHotVerificationKey
31+
32+
deriving instance Show AnyCommitteeHotVerificationKey
33+
34+
readCommitteeHotBech32VerificationKeyText
35+
:: Text -> Validation [Bech32DecodeError] AnyCommitteeHotVerificationKey
36+
readCommitteeHotBech32VerificationKeyText committeeHot =
37+
let vkey =
38+
liftError return $
39+
AnyCommitteeHotVerificationKey
40+
<$> deserialiseFromBech32 (AsVerificationKey AsCommitteeHotKey) committeeHot
41+
extendedVkey =
42+
liftError return $
43+
AnyCommitteeHotExtendedVerificationKey
44+
<$> deserialiseFromBech32 (AsVerificationKey AsCommitteeHotExtendedKey) committeeHot
45+
in vkey <> extendedVkey
46+
47+
readCommitteeHotHexVerificationKeyText
48+
:: Text -> Validation [RawBytesHexError] AnyCommitteeHotVerificationKey
49+
readCommitteeHotHexVerificationKeyText committeeHotText =
50+
let committeeHotBs = Text.encodeUtf8 committeeHotText
51+
vkey =
52+
liftError return $
53+
AnyCommitteeHotVerificationKey
54+
<$> deserialiseFromRawBytesHex (AsVerificationKey AsCommitteeHotKey) committeeHotBs
55+
extendedVkey =
56+
liftError return $
57+
AnyCommitteeHotExtendedVerificationKey
58+
<$> deserialiseFromRawBytesHex (AsVerificationKey AsCommitteeHotExtendedKey) committeeHotBs
59+
in vkey <> extendedVkey
60+
61+
readCommitteeHotVerificationKeyFile
62+
:: FileOrPipe -> IO (Either (FileError TextEnvelopeError) AnyCommitteeHotVerificationKey)
63+
readCommitteeHotVerificationKeyFile = readFileOrPipeTextEnvelopeAnyOf types
64+
where
65+
types =
66+
[ FromSomeType (AsVerificationKey AsCommitteeHotKey) AnyCommitteeHotVerificationKey
67+
, FromSomeType (AsVerificationKey AsCommitteeHotExtendedKey) AnyCommitteeHotExtendedVerificationKey
68+
]
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module Cardano.CLI.Read.GovernanceActionId
2+
( readGoveranceActionIdHexText
3+
) where
4+
5+
import Cardano.Api.Shelley
6+
import qualified Cardano.Api.Ledger as L
7+
import Cardano.CLI.EraBased.Common.Option ( parseTxIn )
8+
import qualified Text.Parsec as Text
9+
import qualified Data.Text as Text
10+
import Data.Text (Text)
11+
12+
13+
readGoveranceActionIdHexText :: Text -> Either Text.ParseError (L.GovActionId L.StandardCrypto)
14+
readGoveranceActionIdHexText hexText = do
15+
TxIn txid (TxIx index) <- Text.parse parseTxIn "" $ Text.unpack hexText
16+
return $ createGovernanceActionId txid $ fromIntegral index

0 commit comments

Comments
 (0)