Skip to content

Commit

Permalink
Merge pull request #816 from IntersectMBO/smelc/share-code-in-comitte…
Browse files Browse the repository at this point in the history
…e-parsers

Share code in parsers of committee commands
  • Loading branch information
smelc authored Jul 4, 2024
2 parents e29e335 + daddf0d commit a1dc1a7
Showing 1 changed file with 34 additions and 65 deletions.
99 changes: 34 additions & 65 deletions cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -526,13 +526,18 @@ pTransferAmt =
, Opt.help "The amount to transfer."
]

pHexHash
:: SerialiseAsRawBytes (Hash a) => AsType a -> ReadM (Hash a)
pHexHash a =
pHexHash :: ()
=> SerialiseAsRawBytes (Hash a)
=> AsType a
-> Maybe String -- | Optional prefix to the error message
-> ReadM (Hash a)
pHexHash a mErrPrefix =
Opt.eitherReader $
first (docToString . prettyError)
first (\e -> errPrefix <> (docToString $ prettyError e))
. deserialiseFromRawBytesHex (AsHash a)
. BSC.pack
where
errPrefix = maybe "" ((<>) ": ") mErrPrefix

pBech32KeyHash :: SerialiseAsBech32 (Hash a) => AsType a -> ReadM (Hash a)
pBech32KeyHash a =
Expand Down Expand Up @@ -714,17 +719,11 @@ pScriptHash longOptionName helpText =

pRemoveCommitteeColdVerificationKeyHash :: Parser (Hash CommitteeColdKey)
pRemoveCommitteeColdVerificationKeyHash =
Opt.option (Opt.eitherReader deserialiseFromHex) $ mconcat
Opt.option deserialiseColdCCKeyHashFromHex $ mconcat
[ Opt.long "remove-cc-cold-verification-key-hash"
, Opt.metavar "STRING"
, Opt.help "Constitutional Committee key hash (hex-encoded)."
]
where
deserialiseFromHex :: String -> Either String (Hash CommitteeColdKey)
deserialiseFromHex =
first (\e -> docToString $ "Invalid Constitutional Committee cold key hash: " <> prettyError e)
. deserialiseFromRawBytesHex (AsHash AsCommitteeColdKey)
. BSC.pack

pRemoveCommitteeColdVerificationKeyOrFile :: Parser (VerificationKeyOrFile CommitteeColdKey)
pRemoveCommitteeColdVerificationKeyOrFile =
Expand All @@ -735,17 +734,21 @@ pRemoveCommitteeColdVerificationKeyOrFile =

pRemoveCommitteeColdVerificationKey :: Parser (VerificationKey CommitteeColdKey)
pRemoveCommitteeColdVerificationKey =
Opt.option (Opt.eitherReader deserialiseFromHex) $ mconcat
Opt.option (Opt.eitherReader deserialiseColdCCKeyFromHex) $ mconcat
[ Opt.long "remove-cc-cold-verification-key"
, Opt.metavar "STRING"
, Opt.help "Constitutional Committee cold key (hex-encoded)."
]
where
deserialiseFromHex :: String -> Either String (VerificationKey CommitteeColdKey)
deserialiseFromHex =
first (\e -> docToString $ "Invalid Constitutional Committee cold key: " <> prettyError e)
. deserialiseFromRawBytesHex (AsVerificationKey AsCommitteeColdKey)
. BSC.pack

deserialiseColdCCKeyFromHex :: String -> Either String (VerificationKey CommitteeColdKey)
deserialiseColdCCKeyFromHex =
first (\e -> docToString $ "Invalid Constitutional Committee cold key: " <> prettyError e)
. deserialiseFromRawBytesHex (AsVerificationKey AsCommitteeColdKey)
. BSC.pack

deserialiseColdCCKeyHashFromHex :: ReadM (Hash CommitteeColdKey)
deserialiseColdCCKeyHashFromHex =
pHexHash AsCommitteeColdKey (Just "Invalid Constitutional Committee cold key hash")

pRemoveCommitteeColdVerificationKeyFile :: Parser (File (VerificationKey keyrole) In)
pRemoveCommitteeColdVerificationKeyFile =
Expand Down Expand Up @@ -774,31 +777,19 @@ pCommitteeColdVerificationKeyOrFile =

pCommitteeColdVerificationKey :: Parser (VerificationKey CommitteeColdKey)
pCommitteeColdVerificationKey =
Opt.option (Opt.eitherReader deserialiseFromHex) $ mconcat
Opt.option (Opt.eitherReader deserialiseColdCCKeyFromHex) $ mconcat
[ Opt.long "cold-verification-key"
, Opt.metavar "STRING"
, Opt.help "Constitutional Committee cold key (hex-encoded)."
]
where
deserialiseFromHex :: String -> Either String (VerificationKey CommitteeColdKey)
deserialiseFromHex =
first (\e -> docToString $ "Invalid Constitutional Committee cold key: " <> prettyError e)
. deserialiseFromRawBytesHex (AsVerificationKey AsCommitteeColdKey)
. BSC.pack

pCommitteeColdVerificationKeyHash :: Parser (Hash CommitteeColdKey)
pCommitteeColdVerificationKeyHash =
Opt.option (Opt.eitherReader deserialiseFromHex) $ mconcat
Opt.option deserialiseColdCCKeyHashFromHex $ mconcat
[ Opt.long "cold-verification-key-hash"
, Opt.metavar "STRING"
, Opt.help "Constitutional Committee key hash (hex-encoded)."
]
where
deserialiseFromHex :: String -> Either String (Hash CommitteeColdKey)
deserialiseFromHex =
first (\e -> docToString $ "Invalid Consitutional Committee cold key hash: " <> prettyError e)
. deserialiseFromRawBytesHex (AsHash AsCommitteeColdKey)
. BSC.pack

pCommitteeColdVerificationKeyFile :: Parser (File (VerificationKey keyrole) In)
pCommitteeColdVerificationKeyFile =
Expand Down Expand Up @@ -843,12 +834,7 @@ pAnyVerificationKeySource helpText =
]

pCommitteeHotKey :: Parser (VerificationKey CommitteeHotKey)
pCommitteeHotKey =
Opt.option (Opt.eitherReader deserialiseHotCCKeyFromHex) $ mconcat
[ Opt.long "hot-key"
, Opt.metavar "STRING"
, Opt.help "Constitutional Committee hot key (hex-encoded)."
]
pCommitteeHotKey = pCommitteeHotVerificationKey "hot-key"

pCommitteeHotVerificationKeyOrFile :: Parser (VerificationKeyOrFile CommitteeHotKey)
pCommitteeHotVerificationKeyOrFile =
Expand All @@ -859,17 +845,11 @@ pCommitteeHotVerificationKeyOrFile =

pCommitteeHotVerificationKeyHash :: Parser (Hash CommitteeHotKey)
pCommitteeHotVerificationKeyHash =
Opt.option (Opt.eitherReader deserialiseFromHex) $ mconcat
Opt.option deserialiseHotCCKeyHashFromHex $ mconcat
[ Opt.long "hot-verification-key-hash"
, Opt.metavar "STRING"
, Opt.help "Constitutional Committee key hash (hex-encoded)."
]
where
deserialiseFromHex :: String -> Either String (Hash CommitteeHotKey)
deserialiseFromHex =
first (\e -> docToString $ "Invalid Consitutional Committee hot key hash: " <> prettyError e)
. deserialiseFromRawBytesHex (AsHash AsCommitteeHotKey)
. BSC.pack

pCommitteeHotVerificationKey :: String -> Parser (VerificationKey CommitteeHotKey)
pCommitteeHotVerificationKey longFlag =
Expand All @@ -885,14 +865,9 @@ deserialiseHotCCKeyFromHex =
. deserialiseFromRawBytesHex (AsVerificationKey AsCommitteeHotKey)
. BSC.pack

pCommitteeHotKeyFile :: Parser (VerificationKeyFile In)
pCommitteeHotKeyFile =
fmap File $ Opt.strOption $ mconcat
[ Opt.long "hot-key-file"
, Opt.metavar "FILE"
, Opt.help "Filepath of the Consitutional Committee hot key."
, Opt.completer (Opt.bashCompleter "file")
]
deserialiseHotCCKeyHashFromHex :: ReadM (Hash CommitteeHotKey)
deserialiseHotCCKeyHashFromHex =
pHexHash AsCommitteeHotKey (Just "Invalid Consitutional Committee hot key hash")

pCommitteeHotVerificationKeyFile :: String -> Parser (VerificationKeyFile In)
pCommitteeHotVerificationKeyFile longFlag =
Expand All @@ -906,23 +881,17 @@ pCommitteeHotVerificationKeyFile longFlag =
-- | The first argument is the optional prefix.
pCommitteeHotKeyHash :: Maybe String -> Parser (Hash CommitteeHotKey)
pCommitteeHotKeyHash prefix =
Opt.option (Opt.eitherReader deserialiseFromHex) $ mconcat
Opt.option deserialiseHotCCKeyHashFromHex $ mconcat
[ Opt.long $ prefixFlag prefix "hot-key-hash"
, Opt.metavar "STRING"
, Opt.help "Constitutional Committee key hash (hex-encoded)."
]
where
deserialiseFromHex :: String -> Either String (Hash CommitteeHotKey)
deserialiseFromHex =
first (\e -> docToString $ "Invalid Consitutional Committee hot key hash: " <> prettyError e)
. deserialiseFromRawBytesHex (AsHash AsCommitteeHotKey)
. BSC.pack

pCommitteeHotKeyOrHashOrFile :: Parser (VerificationKeyOrHashOrFile CommitteeHotKey)
pCommitteeHotKeyOrHashOrFile =
asum
[ VerificationKeyOrFile . VerificationKeyValue <$> pCommitteeHotKey
, VerificationKeyOrFile . VerificationKeyFilePath <$> pCommitteeHotKeyFile
, VerificationKeyOrFile . VerificationKeyFilePath <$> pCommitteeHotVerificationKeyFile "hot-key-file"
, VerificationKeyHash <$> pCommitteeHotKeyHash Nothing
]

Expand Down Expand Up @@ -998,7 +967,7 @@ pStakeVerificationKeyOrHashOrFile prefix = asum
-- | First argument is the optional prefix
pStakeVerificationKeyHash :: Maybe String -> Parser (Hash StakeKey)
pStakeVerificationKeyHash prefix =
Opt.option (pHexHash AsStakeKey) $ mconcat
Opt.option (pHexHash AsStakeKey Nothing) $ mconcat
[ Opt.long $ prefixFlag prefix "stake-key-hash"
, Opt.metavar "HASH"
, Opt.help "Stake verification key hash (hex-encoded)."
Expand Down Expand Up @@ -2416,10 +2385,10 @@ pAddress =
, Opt.help "A Cardano address"
]

-- | First argument is the prefix to use
-- | First argument is the prefix for the option's flag to use
pStakePoolVerificationKeyHash :: Maybe String -> Parser (Hash StakePoolKey)
pStakePoolVerificationKeyHash prefix =
Opt.option (pBech32KeyHash AsStakePoolKey <|> pHexHash AsStakePoolKey) $ mconcat
Opt.option (pBech32KeyHash AsStakePoolKey <|> pHexHash AsStakePoolKey Nothing) $ mconcat
[ Opt.long $ prefixFlag prefix "stake-pool-id"
, Opt.metavar "STAKE_POOL_ID"
, Opt.help
Expand Down Expand Up @@ -3292,7 +3261,7 @@ pAllOrOnlyDRepHashSource = pAll <|> pOnly

pDRepVerificationKeyHash :: Parser (Hash DRepKey)
pDRepVerificationKeyHash =
Opt.option (pBech32KeyHash AsDRepKey <|> pHexHash AsDRepKey) $ mconcat
Opt.option (pBech32KeyHash AsDRepKey <|> pHexHash AsDRepKey Nothing) $ mconcat
[ Opt.long "drep-key-hash"
, Opt.metavar "HASH"
, Opt.help "DRep verification key hash (either Bech32-encoded or hex-encoded)."
Expand Down

0 comments on commit a1dc1a7

Please sign in to comment.