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

Scrap hex and rely on bech32 for converting to hex #291

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down Expand Up @@ -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.
</details>


Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion command-line/cardano-addresses-cli.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 8 additions & 15 deletions command-line/lib/Command/Key/Hash.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}

{-# OPTIONS_HADDOCK hide #-}
Expand All @@ -25,8 +24,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
Expand All @@ -37,30 +34,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
Expand Down
27 changes: 7 additions & 20 deletions command-line/lib/Command/Key/Private.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
33 changes: 0 additions & 33 deletions command-line/lib/Options/Applicative/Format.hs

This file was deleted.

Loading