From 82e64231919e415ad1ef705dd5954f7e2da35924 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Hurlin?= Date: Mon, 1 Jul 2024 10:52:44 +0200 Subject: [PATCH 1/6] Share code 1 --- cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs b/cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs index 859462a986..7d62497274 100644 --- a/cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs +++ b/cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs @@ -843,12 +843,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 = From d722a0d1c1ae64fc28dc408c4a545d44ae54106b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Hurlin?= Date: Mon, 1 Jul 2024 10:56:59 +0200 Subject: [PATCH 2/6] Share code 2 --- .../Cardano/CLI/EraBased/Options/Common.hs | 22 +++++++------------ 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs b/cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs index 7d62497274..6f99d3e665 100644 --- a/cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs +++ b/cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs @@ -854,17 +854,11 @@ pCommitteeHotVerificationKeyOrFile = pCommitteeHotVerificationKeyHash :: Parser (Hash CommitteeHotKey) pCommitteeHotVerificationKeyHash = - Opt.option (Opt.eitherReader deserialiseFromHex) $ mconcat + Opt.option (Opt.eitherReader 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 = @@ -880,6 +874,12 @@ deserialiseHotCCKeyFromHex = . deserialiseFromRawBytesHex (AsVerificationKey AsCommitteeHotKey) . BSC.pack +deserialiseHotCCKeyHashFromHex :: String -> Either String (Hash CommitteeHotKey) +deserialiseHotCCKeyHashFromHex = + first (\e -> docToString $ "Invalid Consitutional Committee hot key hash: " <> prettyError e) + . deserialiseFromRawBytesHex (AsHash AsCommitteeHotKey) + . BSC.pack + pCommitteeHotKeyFile :: Parser (VerificationKeyFile In) pCommitteeHotKeyFile = fmap File $ Opt.strOption $ mconcat @@ -901,17 +901,11 @@ 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 (Opt.eitherReader 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 = From a6be275ba00a672b9b3a461fc782cd48203501cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Hurlin?= Date: Mon, 1 Jul 2024 11:09:17 +0200 Subject: [PATCH 3/6] Share code 3 --- .../src/Cardano/CLI/EraBased/Options/Common.hs | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs b/cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs index 6f99d3e665..6321a5f6f1 100644 --- a/cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs +++ b/cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs @@ -880,15 +880,6 @@ deserialiseHotCCKeyHashFromHex = . deserialiseFromRawBytesHex (AsHash 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") - ] - pCommitteeHotVerificationKeyFile :: String -> Parser (VerificationKeyFile In) pCommitteeHotVerificationKeyFile longFlag = fmap File $ Opt.strOption $ mconcat @@ -911,7 +902,7 @@ pCommitteeHotKeyOrHashOrFile :: Parser (VerificationKeyOrHashOrFile CommitteeHot pCommitteeHotKeyOrHashOrFile = asum [ VerificationKeyOrFile . VerificationKeyValue <$> pCommitteeHotKey - , VerificationKeyOrFile . VerificationKeyFilePath <$> pCommitteeHotKeyFile + , VerificationKeyOrFile . VerificationKeyFilePath <$> pCommitteeHotVerificationKeyFile "hot-key-file" , VerificationKeyHash <$> pCommitteeHotKeyHash Nothing ] From e83e3bfb3b45142cb05e0f16558159d4de2bbfc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Hurlin?= Date: Mon, 1 Jul 2024 11:17:25 +0200 Subject: [PATCH 4/6] Share code 4 --- .../Cardano/CLI/EraBased/Options/Common.hs | 44 +++++++------------ 1 file changed, 16 insertions(+), 28 deletions(-) diff --git a/cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs b/cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs index 6321a5f6f1..dfecdbd6f0 100644 --- a/cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs +++ b/cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs @@ -714,17 +714,11 @@ pScriptHash longOptionName helpText = pRemoveCommitteeColdVerificationKeyHash :: Parser (Hash CommitteeColdKey) pRemoveCommitteeColdVerificationKeyHash = - Opt.option (Opt.eitherReader deserialiseFromHex) $ mconcat + Opt.option (Opt.eitherReader 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 = @@ -735,17 +729,23 @@ 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 :: String -> Either String (Hash CommitteeColdKey) +deserialiseColdCCKeyHashFromHex = + first (\e -> docToString $ "Invalid Constitutional Committee cold key hash: " <> prettyError e) + . deserialiseFromRawBytesHex (AsHash AsCommitteeColdKey) + . BSC.pack pRemoveCommitteeColdVerificationKeyFile :: Parser (File (VerificationKey keyrole) In) pRemoveCommitteeColdVerificationKeyFile = @@ -774,31 +774,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 (Opt.eitherReader 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 = From 54f027fd3e684583441fd5507938e5ae7f6dcdd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Hurlin?= Date: Tue, 2 Jul 2024 17:21:07 +0200 Subject: [PATCH 5/6] Generalize pHexHash, preparing for reuse --- .../Cardano/CLI/EraBased/Options/Common.hs | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs b/cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs index dfecdbd6f0..a6bd5d90e4 100644 --- a/cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs +++ b/cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs @@ -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 = @@ -966,7 +971,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)." @@ -2384,10 +2389,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 @@ -3260,7 +3265,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)." From daddf0d6fb076de6ddc6deff266531a471a64b65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Hurlin?= Date: Wed, 3 Jul 2024 14:03:01 +0200 Subject: [PATCH 6/6] Reduce duplication by using pHexHash more --- .../Cardano/CLI/EraBased/Options/Common.hs | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs b/cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs index a6bd5d90e4..abc1cce19e 100644 --- a/cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs +++ b/cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs @@ -719,7 +719,7 @@ pScriptHash longOptionName helpText = pRemoveCommitteeColdVerificationKeyHash :: Parser (Hash CommitteeColdKey) pRemoveCommitteeColdVerificationKeyHash = - Opt.option (Opt.eitherReader deserialiseColdCCKeyHashFromHex) $ mconcat + Opt.option deserialiseColdCCKeyHashFromHex $ mconcat [ Opt.long "remove-cc-cold-verification-key-hash" , Opt.metavar "STRING" , Opt.help "Constitutional Committee key hash (hex-encoded)." @@ -746,11 +746,9 @@ deserialiseColdCCKeyFromHex = . deserialiseFromRawBytesHex (AsVerificationKey AsCommitteeColdKey) . BSC.pack -deserialiseColdCCKeyHashFromHex :: String -> Either String (Hash CommitteeColdKey) +deserialiseColdCCKeyHashFromHex :: ReadM (Hash CommitteeColdKey) deserialiseColdCCKeyHashFromHex = - first (\e -> docToString $ "Invalid Constitutional Committee cold key hash: " <> prettyError e) - . deserialiseFromRawBytesHex (AsHash AsCommitteeColdKey) - . BSC.pack + pHexHash AsCommitteeColdKey (Just "Invalid Constitutional Committee cold key hash") pRemoveCommitteeColdVerificationKeyFile :: Parser (File (VerificationKey keyrole) In) pRemoveCommitteeColdVerificationKeyFile = @@ -787,7 +785,7 @@ pCommitteeColdVerificationKey = pCommitteeColdVerificationKeyHash :: Parser (Hash CommitteeColdKey) pCommitteeColdVerificationKeyHash = - Opt.option (Opt.eitherReader deserialiseColdCCKeyHashFromHex) $ mconcat + Opt.option deserialiseColdCCKeyHashFromHex $ mconcat [ Opt.long "cold-verification-key-hash" , Opt.metavar "STRING" , Opt.help "Constitutional Committee key hash (hex-encoded)." @@ -847,7 +845,7 @@ pCommitteeHotVerificationKeyOrFile = pCommitteeHotVerificationKeyHash :: Parser (Hash CommitteeHotKey) pCommitteeHotVerificationKeyHash = - Opt.option (Opt.eitherReader deserialiseHotCCKeyHashFromHex) $ mconcat + Opt.option deserialiseHotCCKeyHashFromHex $ mconcat [ Opt.long "hot-verification-key-hash" , Opt.metavar "STRING" , Opt.help "Constitutional Committee key hash (hex-encoded)." @@ -867,11 +865,9 @@ deserialiseHotCCKeyFromHex = . deserialiseFromRawBytesHex (AsVerificationKey AsCommitteeHotKey) . BSC.pack -deserialiseHotCCKeyHashFromHex :: String -> Either String (Hash CommitteeHotKey) +deserialiseHotCCKeyHashFromHex :: ReadM (Hash CommitteeHotKey) deserialiseHotCCKeyHashFromHex = - first (\e -> docToString $ "Invalid Consitutional Committee hot key hash: " <> prettyError e) - . deserialiseFromRawBytesHex (AsHash AsCommitteeHotKey) - . BSC.pack + pHexHash AsCommitteeHotKey (Just "Invalid Consitutional Committee hot key hash") pCommitteeHotVerificationKeyFile :: String -> Parser (VerificationKeyFile In) pCommitteeHotVerificationKeyFile longFlag = @@ -885,7 +881,7 @@ pCommitteeHotVerificationKeyFile longFlag = -- | The first argument is the optional prefix. pCommitteeHotKeyHash :: Maybe String -> Parser (Hash CommitteeHotKey) pCommitteeHotKeyHash prefix = - Opt.option (Opt.eitherReader deserialiseHotCCKeyHashFromHex) $ mconcat + Opt.option deserialiseHotCCKeyHashFromHex $ mconcat [ Opt.long $ prefixFlag prefix "hot-key-hash" , Opt.metavar "STRING" , Opt.help "Constitutional Committee key hash (hex-encoded)."