never executed always true always false
1 {-# LANGUAGE FlexibleContexts #-}
2 {-# LANGUAGE NamedFieldPuns #-}
3 {-# LANGUAGE OverloadedStrings #-}
4 {-# LANGUAGE ScopedTypeVariables #-}
5 {-# LANGUAGE TypeFamilies #-}
6
7 {-# OPTIONS_HADDOCK hide #-}
8 {-# OPTIONS_GHC -fno-warn-orphans #-}
9 {-# OPTIONS_GHC -fno-warn-deprecations #-}
10
11 module Command.Address.Inspect
12 ( Cmd (..)
13 , mod
14 , run
15 ) where
16
17 import Prelude hiding
18 ( mod )
19
20 import Cardano.Address
21 ( Address, unsafeMkAddress )
22 import Cardano.Address.Derivation
23 ( XPub )
24 import Control.Applicative
25 ( optional )
26 import Control.Exception
27 ( displayException )
28 import Fmt
29 ( format )
30 import Options.Applicative
31 ( CommandFields, Mod, command, footerDoc, helper, info, progDesc )
32 import Options.Applicative.Derivation
33 ( xpubOpt )
34 import Options.Applicative.Help.Pretty
35 ( bold, indent, string, vsep )
36 import System.Exit
37 ( die )
38 import System.IO
39 ( stdin, stdout )
40 import System.IO.Extra
41 ( hGetBytes, progName )
42
43 import qualified Cardano.Address.Style.Shelley as Shelley
44 import qualified Cardano.Codec.Bech32.Prefixes as CIP5
45 import qualified Data.Aeson.Encode.Pretty as Json
46 import qualified Data.ByteString.Lazy.Char8 as BL8
47
48 newtype Cmd = Inspect
49 { rootPublicKey :: Maybe XPub
50 } deriving (Show)
51
52 mod :: (Cmd -> parent) -> Mod CommandFields parent
53 mod liftCmd = command "inspect" $
54 info (helper <*> fmap liftCmd parser) $ mempty
55 <> progDesc "Show information about an address"
56 <> footerDoc (Just $ vsep
57 [ string "The address is read from stdin."
58 , string ""
59 , string "Example:"
60 , indent 2 $ bold $ string "$ cat addr.prv \\"
61 , indent 4 $ bold $ string $ "| "<>progName<>" key public --with-chain-code \\"
62 , indent 4 $ bold $ string $ "| "<>progName<>" address payment --network-tag testnet \\"
63 , indent 4 $ bold $ string $ "| "<>progName<>" address delegation $(cat stake.prv | "<>progName<>" key public --with-chain-code) \\"
64 , indent 4 $ bold $ string $ "| "<>progName<>" address inspect"
65 , indent 2 $ string "{"
66 , indent 2 $ string " \"address_style\": \"Shelley\","
67 , indent 2 $ string " \"stake_reference\": \"by value\","
68 , indent 2 $ string " \"stake_key_hash\": \"6b542d6da35e6c95d95a33c6f66ec482d3f4caf3ad35e2ede09cf827\","
69 , indent 2 $ string " \"spending_key_hash\": \"44bc4f524f49a78a9c8a45b882d8710cb9254b3da6a978d50dc9b870\","
70 , indent 2 $ string " \"network_tag\": 0,"
71 , indent 2 $ string " \"address_type\": 0"
72 , indent 2 $ string "}"
73 ])
74 where
75 parser = Inspect
76 <$> optional (xpubOpt [CIP5.root_xvk] "root" helpDoc)
77 helpDoc =
78 "A root public key. If specified, tries to decrypt the derivation path \
79 \of Byron addresses."
80
81 run :: Cmd -> IO ()
82 run Inspect{rootPublicKey} = do
83 bytes <- hGetBytes stdin
84 case inspect (unsafeMkAddress bytes) of
85 Right json -> BL8.hPutStrLn stdout (Json.encodePretty json)
86 Left e -> die $ format "Error: {}" (displayException e)
87 where
88 inspect :: Address -> Either Shelley.ErrInspectAddress Shelley.InspectAddress
89 inspect = Shelley.eitherInspectAddress rootPublicKey