From 5b945e10c7a0b1afab4b35476b9a8d9c5bd26b9a Mon Sep 17 00:00:00 2001 From: Pawel Jakubas Date: Mon, 13 Jan 2025 15:26:05 +0100 Subject: [PATCH 1/4] remove dependency on formatOpt in Key.Hash --- command-line/lib/Command/Key/Hash.hs | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/command-line/lib/Command/Key/Hash.hs b/command-line/lib/Command/Key/Hash.hs index 10cfd31fd..0ce1a6f9d 100644 --- a/command-line/lib/Command/Key/Hash.hs +++ b/command-line/lib/Command/Key/Hash.hs @@ -25,8 +25,6 @@ import Data.Text ( Text ) import Options.Applicative ( CommandFields, Mod, command, footerDoc, helper, info, progDesc ) -import Options.Applicative.Format - ( FormatType (..), formatOpt ) import Options.Applicative.Help.Pretty ( pretty ) import System.IO @@ -37,30 +35,26 @@ import System.IO.Extra import qualified Cardano.Codec.Bech32.Prefixes as CIP5 import qualified Data.ByteString as BS -newtype Cmd = Hash - { outputFormat :: FormatType - } deriving (Show) +data Cmd = Hash + deriving (Show) mod :: (Cmd -> parent) -> Mod CommandFields parent mod liftCmd = command "hash" $ info (helper <*> fmap liftCmd parser) $ mempty <> progDesc "Get the hash of a public key" <> footerDoc (Just $ pretty $ mconcat - [ "The public key is read from stdin." :: Text - , "To get hex-encoded output pass '--hex'." - , "Otherwise bech32-encoded hash is returned." + [ "The public key is read from stdin and" :: Text + , "bech32-encoded hash is returned." + , "To get hex-encoded output pass it to stdin of `bech32`." ]) where - parser = Hash - <$> formatOpt + parser = pure Hash run :: Cmd -> IO () -run Hash{outputFormat} = do +run Hash = do (hrp, bytes) <- hGetBech32 stdin allowedPrefixes guardBytes hrp bytes - let encoding = case outputFormat of - Hex -> EBase16 - Bech32 -> EBech32 $ prefixFor hrp + let encoding = EBech32 $ prefixFor hrp hPutBytes stdout (hashCredential $ BS.take 32 bytes) encoding where -- Mapping of input HRP to output HRP From 51b9deb9f75d6a9c37161473b855d7344b783d2e Mon Sep 17 00:00:00 2001 From: Pawel Jakubas Date: Mon, 13 Jan 2025 16:20:20 +0100 Subject: [PATCH 2/4] remove dependency on formatOpt in Key.Private --- README.md | 6 +++--- command-line/lib/Command/Key/Private.hs | 27 +++++++------------------ 2 files changed, 10 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 97a80ca83..1a26e956f 100644 --- a/README.md +++ b/README.md @@ -231,7 +231,7 @@ $ cardano-address key hash < policy.xvk policy_vkh1qpc9xly4lc7yt98gcf59kdcqcss6dda4u9g72e775yxpxeypamc $ cardano-address key hash < policy.vk policy_vkh1qpc9xly4lc7yt98gcf59kdcqcss6dda4u9g72e775yxpxeypamc -$ cardano-address key hash --hex < policy.vk +$ cardano-address key hash < policy.vk | bech32 0070537c95fe3c4594e8c2685b3700c421a6b7b5e151e567dea10c13 ``` @@ -281,11 +281,11 @@ $ cardano-address key child 1852H/1815H/0H/0/0 < root.xsk | cardano-address key addr_xvk1grvg8qzmkmw2n0dm4pd0h3j4dv6yglyammyp733eyj629dc3z28v6wk22nfmru6xz0vl2s3y5xndyd57fu70hrt84c6zkvlwx6fdl7ct9j7yc $ cardano-address key hash < addr.xvk addr_vkh12j28hnmtwcp3n08vy58vyf0arnnrhtavu3lrfdztw0j0jng3d6v -$ cardano-address key hash --hex < addr.xvk +$ cardano-address key hash < addr.xvk | bech32 54947bcf6b760319bcec250ec225fd1ce63baface47e34b44b73e4f9 ``` -> :information_source: The hashing is available for both stake and payment verification keys. Additional flag '--hex' can be used. +> :information_source: The hashing is available for both stake and payment verification keys. Hex encoding can be achieved by redirecting to `bech32` tool. diff --git a/command-line/lib/Command/Key/Private.hs b/command-line/lib/Command/Key/Private.hs index 652946589..af4dc6389 100644 --- a/command-line/lib/Command/Key/Private.hs +++ b/command-line/lib/Command/Key/Private.hs @@ -22,17 +22,7 @@ import Data.Maybe import Data.Text ( Text ) import Options.Applicative - ( CommandFields - , Mod - , command - , footerDoc - , helper - , info - , optional - , progDesc - ) -import Options.Applicative.Format - ( FormatType (..), formatOpt ) + ( CommandFields, Mod, command, footerDoc, helper, info, progDesc ) import Options.Applicative.Help.Pretty ( pretty ) import Options.Applicative.Private @@ -44,9 +34,8 @@ import System.IO.Extra import qualified Cardano.Codec.Bech32.Prefixes as CIP5 -data Cmd = Private +newtype Cmd = Private { keyPart :: PrivateType - , outputFormat :: Maybe FormatType } deriving (Show) mod :: (Cmd -> parent) -> Mod CommandFields parent @@ -57,16 +46,15 @@ mod liftCmd = command "private" $ [ "The private key is read from stdin." :: Text , "To get a signing key pass '--signing-key'." , "To get a chain code pass '--chain-code'." - , "In order to have the signing key hex encoded pass '--hex'." - , "When omitted bech32 encoding will be used." + , "Bech32 encoding will be used." + , "In order to have the signing key hex encoded pass the result to the stdin of bech32 tool." ]) where parser = Private <$> privateOpt - <*> optional formatOpt run :: Cmd -> IO () -run Private{keyPart,outputFormat} = do +run Private{keyPart} = do (hrp, xprv) <- hGetXPrv stdin allowedPrefixes let bytes = case keyPart of ChainCode -> xprvChainCode xprv @@ -89,6 +77,5 @@ run Private{keyPart,outputFormat} = do ] allowedPrefixes = map fst prefixes getFormat ChainCode _ = EBase16 - getFormat SigningKey hrp = case outputFormat of - Just Hex -> EBase16 - _ -> EBech32 $ fromJust $ lookup hrp prefixes + getFormat SigningKey hrp = + EBech32 $ fromJust $ lookup hrp prefixes From 2aed29d6055f49fdd4b71a06c87926afe1f6f33e Mon Sep 17 00:00:00 2001 From: Pawel Jakubas Date: Mon, 13 Jan 2025 16:25:34 +0100 Subject: [PATCH 3/4] remove Applicative.Format --- README.md | 2 +- command-line/cardano-addresses-cli.cabal | 1 - .../lib/Options/Applicative/Format.hs | 33 ------------------- 3 files changed, 1 insertion(+), 35 deletions(-) delete mode 100644 command-line/lib/Options/Applicative/Format.hs diff --git a/README.md b/README.md index 1a26e956f..cc38d594d 100644 --- a/README.md +++ b/README.md @@ -644,7 +644,7 @@ drep_xsk1vpdsm49smzmdwhd4kjmm2mdyljjysm746rafjr7r8kgfanj849psw8pfm305g59wng0akw3 $ cardano-address key private --signing-key < drep.xsk drep_sk1vpdsm49smzmdwhd4kjmm2mdyljjysm746rafjr7r8kgfanj849psw8pfm305g59wng0akw3qzppmfh6k5z7gx66h2vppu022m4eqajg5xmwma -$ cardano-address key private --signing-key --hex < drep.xsk +$ cardano-address key private --signing-key < drep.xsk | bech32 605b0dd4b0d8b6d75db5b4b7b56da4fca4486fd5d0fa990fc33d909ece47a943071c29dc5f4450ae9a1fdb3a201043b4df56a0bc836b5753021e3d4add720ec9 $ cardano-address key private --chain-code < drep.xsk diff --git a/command-line/cardano-addresses-cli.cabal b/command-line/cardano-addresses-cli.cabal index 996bb74c1..b0ea15867 100644 --- a/command-line/cardano-addresses-cli.cabal +++ b/command-line/cardano-addresses-cli.cabal @@ -52,7 +52,6 @@ library Options.Applicative.Credential Options.Applicative.Derivation Options.Applicative.Discrimination - Options.Applicative.Format Options.Applicative.MnemonicSize Options.Applicative.Public Options.Applicative.Script diff --git a/command-line/lib/Options/Applicative/Format.hs b/command-line/lib/Options/Applicative/Format.hs deleted file mode 100644 index 6151ed531..000000000 --- a/command-line/lib/Options/Applicative/Format.hs +++ /dev/null @@ -1,33 +0,0 @@ -{-# LANGUAGE FlexibleContexts #-} - -{-# OPTIONS_HADDOCK hide #-} - -module Options.Applicative.Format - ( - -- * Types - FormatType (..) - - -- * Applicative Parser - , formatOpt - ) where - -import Prelude - -import Control.Applicative - ( (<|>) ) -import Options.Applicative - ( Parser, flag', long ) - - -data FormatType = Hex | Bech32 - deriving (Eq, Show) - --- --- Applicative Parser --- - --- | Parse an 'FormatType' from the command-line, if there is proper flag then hex is set. -formatOpt :: Parser FormatType -formatOpt = hex <|> pure Bech32 - where - hex = flag' Hex (long "hex") From 1f8890842284c34e2b9b7ea4fc14c9524608e7e9 Mon Sep 17 00:00:00 2001 From: Pawel Jakubas Date: Mon, 13 Jan 2025 16:32:20 +0100 Subject: [PATCH 4/4] coding standards --- command-line/lib/Command/Key/Hash.hs | 1 - 1 file changed, 1 deletion(-) diff --git a/command-line/lib/Command/Key/Hash.hs b/command-line/lib/Command/Key/Hash.hs index 0ce1a6f9d..fcfa925b9 100644 --- a/command-line/lib/Command/Key/Hash.hs +++ b/command-line/lib/Command/Key/Hash.hs @@ -1,5 +1,4 @@ {-# LANGUAGE FlexibleContexts #-} -{-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE OverloadedStrings #-} {-# OPTIONS_HADDOCK hide #-}