Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Share code in parsers of committee commands #816

Merged
merged 6 commits into from
Jul 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
, 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 =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, you could also use this function in:
pAddCommitteeColdVerificationKeyHash, pGenesisVerificationKeyHash, pGenesisDelegateVerificationKeyHash, pVrfVerificationKeyHash, and pStakePoolMetadataHash, I think.

Opt.eitherReader $
first (docToString . prettyError)
first (\e -> errPrefix <> (docToString $ prettyError e))
. deserialiseFromRawBytesHex (AsHash a)
. BSC.pack
where
errPrefix = maybe "" ((<>) ": ") mErrPrefix

Check notice

Code scanning / HLint

Use section Note

cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs:540:26-36: Suggestion: Use section
  
Found:
  ((<>) ": ")
  
Perhaps:
  (": " <>)

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

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 @@

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 =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would also be nice to have also something like pHexHash for verification keys (or even generalize it further, not sure if it is possble), and that could also be used in several places in this file: pGenesisDelegateVerificationKey, pAddCommitteeColdVerificationKey, deserialiseColdCCKeyFromHex, deserialiseHotCCKeyFromHex, and pGenesisVerificationKey.

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 @@

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 @@
]

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 @@

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 @@
. 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")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pHexHash AsCommitteeHotKey (Just "Invalid Consitutional Committee hot key hash")
pHexHash AsCommitteeHotKey (Just "Invalid Constituional Committee hot key hash")

Consitutional -> Constitutional

The typo was already there before the PR, but it would be good to do a search and replace since you are at it


pCommitteeHotVerificationKeyFile :: String -> Parser (VerificationKeyFile In)
pCommitteeHotVerificationKeyFile longFlag =
Expand All @@ -906,23 +881,17 @@
-- | 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 @@
-- | 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 @@
, 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 @@

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
Loading