Skip to content

Commit

Permalink
Merge #1426
Browse files Browse the repository at this point in the history
1426: JSONify output for Shelley address info command r=intricate a=intricate



Co-authored-by: Luke Nadur <19835357+intricate@users.noreply.github.com>
  • Loading branch information
iohk-bors[bot] and intricate committed Jul 9, 2020
2 parents 4c50f80 + 20ae9f0 commit c51d08d
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 22 deletions.
2 changes: 1 addition & 1 deletion cardano-cli/src/Cardano/CLI/Shelley/Commands.hs
Expand Up @@ -80,7 +80,7 @@ data AddressCmd
| AddressKeyHash VerificationKeyFile (Maybe OutputFile)
| AddressBuild VerificationKeyFile (Maybe VerificationKeyFile) NetworkId (Maybe OutputFile)
| AddressBuildMultiSig --TODO
| AddressInfo Text
| AddressInfo Text (Maybe OutputFile)
| AddressConvertKey SigningKeyFile SigningKeyFile
deriving (Eq, Show)

Expand Down
2 changes: 1 addition & 1 deletion cardano-cli/src/Cardano/CLI/Shelley/Parsers.hs
Expand Up @@ -157,7 +157,7 @@ pAddressCmd =
pAddressBuildMultiSig = pure AddressBuildMultiSig

pAddressInfo :: Parser AddressCmd
pAddressInfo = AddressInfo <$> pAddress
pAddressInfo = AddressInfo <$> pAddress <*> pMaybeOutputFile

pAddressConvert :: Parser AddressCmd
pAddressConvert = AddressConvertKey <$> pByronKeyFile Input
Expand Down
2 changes: 1 addition & 1 deletion cardano-cli/src/Cardano/CLI/Shelley/Run/Address.hs
Expand Up @@ -49,7 +49,7 @@ runAddressCmd cmd =
AddressKeyHash vkf mOFp -> runAddressKeyHash vkf mOFp
AddressBuild payVk stkVk nw mOutFp -> runAddressBuild payVk stkVk nw mOutFp
AddressBuildMultiSig {} -> runAddressBuildMultiSig
AddressInfo txt -> firstExceptT ShelleyAddressCmdAddressInfoError $ runAddressInfo txt
AddressInfo txt mOFp -> firstExceptT ShelleyAddressCmdAddressInfoError $ runAddressInfo txt mOFp
AddressConvertKey skfOld skfNew -> runAddressConvertKey skfOld skfNew


Expand Down
73 changes: 54 additions & 19 deletions cardano-cli/src/Cardano/CLI/Shelley/Run/Address/Info.hs
Expand Up @@ -4,14 +4,19 @@ module Cardano.CLI.Shelley.Run.Address.Info
, ShelleyAddressInfoError(..)
) where

import Cardano.Prelude hiding (putStrLn)
import Prelude (putStrLn)
import Cardano.Prelude

import Data.Aeson (ToJSON (..), (.=), object)
import Data.Aeson.Encode.Pretty (encodePretty)
import qualified Data.ByteString.Lazy.Char8 as LBS

import Control.Monad.Trans.Except (ExceptT)
import Control.Monad.Trans.Except.Extra (left)

import Cardano.Api.Typed

import Cardano.CLI.Shelley.Parsers (OutputFile (..))


data ShelleyAddressInfoError = ShelleyAddressInvalid Text
deriving Show
Expand All @@ -20,25 +25,55 @@ instance Error ShelleyAddressInfoError where
displayError (ShelleyAddressInvalid addrTxt) =
"Invalid address: " <> show addrTxt

runAddressInfo :: Text -> ExceptT ShelleyAddressInfoError IO ()
runAddressInfo addrTxt =
case (Left <$> deserialiseAddress AsShelleyAddress addrTxt)
<|> (Right <$> deserialiseAddress AsStakeAddress addrTxt) of
data AddressInfo = AddressInfo
{ aiType :: !Text
, aiEra :: !Text
, aiEncoding :: !Text
, aiAddress :: !Text
}

instance ToJSON AddressInfo where
toJSON addrInfo =
object
[ "type" .= aiType addrInfo
, "era" .= aiEra addrInfo
, "encoding" .= aiEncoding addrInfo
, "address" .= aiAddress addrInfo
]

runAddressInfo :: Text -> Maybe OutputFile -> ExceptT ShelleyAddressInfoError IO ()
runAddressInfo addrTxt mOutputFp = do
addrInfo <- case (Left <$> deserialiseAddress AsShelleyAddress addrTxt)
<|> (Right <$> deserialiseAddress AsStakeAddress addrTxt) of

Nothing ->
left $ ShelleyAddressInvalid addrTxt

Just (Left payaddr) -> liftIO $ do
putStrLn "Type: Payment address"
Just (Left payaddr) ->
case payaddr of
ByronAddress{} -> do
putStrLn "Era: Byron"
putStrLn "Encoding: Base58"
ShelleyAddress{} -> do
putStrLn "Era: Shelley"
putStrLn "Encoding: Bech32"

Just (Right _stakeaddr) -> liftIO $ do
putStrLn "Type: Stake address"
putStrLn "Era: Shelley"
putStrLn "Encoding: Bech32"
ByronAddress{} ->
pure $ AddressInfo
{ aiType = "payment"
, aiEra = "byron"
, aiEncoding = "base58"
, aiAddress = addrTxt
}
ShelleyAddress{} ->
pure $ AddressInfo
{ aiType = "payment"
, aiEra = "shelley"
, aiEncoding = "bech32"
, aiAddress = addrTxt
}

Just (Right _stakeaddr) ->
pure $ AddressInfo
{ aiType = "stake"
, aiEra = "shelley"
, aiEncoding = "bech32"
, aiAddress = addrTxt
}

case mOutputFp of
Just (OutputFile fpath) -> liftIO $ LBS.writeFile fpath $ encodePretty addrInfo
Nothing -> liftIO $ LBS.putStrLn $ encodePretty addrInfo

0 comments on commit c51d08d

Please sign in to comment.