/
Info.hs
72 lines (60 loc) · 2.59 KB
/
Info.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
{-# LANGUAGE MultiWayIf #-}
module Cardano.CLI.Shelley.Run.Address.Info
( ShelleyAddressInfoError
, renderShelleyAddressInfoError
, runAddressInfo
) where
import Cardano.Prelude hiding (putStrLn)
import Prelude (putStrLn)
import qualified Data.ByteString.Char8 as BS
import qualified Data.ByteString.Base58 as Base58
import qualified Data.ByteString.Base16 as Base16
import Data.Char (isHexDigit)
import qualified Data.Text as Text
import qualified Data.Text.Encoding as Text
import qualified Data.Text.IO as Text
import Control.Monad.Trans.Except (ExceptT)
import Control.Monad.Trans.Except.Extra (left)
import Cardano.Api
data ShelleyAddressInfoError
= ShelleyAddressDescribeError !Text
deriving Show
renderShelleyAddressInfoError :: ShelleyAddressInfoError -> Text
renderShelleyAddressInfoError err =
case err of
ShelleyAddressDescribeError descErr ->
"Error occurred while describing address: " <> descErr
runAddressInfo :: Text -> ExceptT ShelleyAddressInfoError IO ()
runAddressInfo addrTxt = do
liftIO $ Text.putStrLn $ "Address: " <> addrTxt
if
| Text.all isHexDigit addrTxt -> do
liftIO $ putStrLn "Encoding: Hex"
runAddressInfoHex addrTxt
| Text.all isBase58Char addrTxt -> do
liftIO $ putStrLn "Encoding: Base58"
runAddressInfoBase58 addrTxt
-- TODO: Add Bech32 support.
| otherwise -> left $ ShelleyAddressDescribeError ("Unknown address type: " <> addrTxt)
where
isBase58Char :: Char -> Bool
isBase58Char c = c `elem` BS.unpack (Base58.unAlphabet Base58.bitcoinAlphabet)
-- -------------------------------------------------------------------------------------------------
runAddressInfoHex :: Text -> ExceptT ShelleyAddressInfoError IO ()
runAddressInfoHex addrTxt = do
case addressFromHex addrTxt of
Right addr -> describeAddr addr
Left err -> left . ShelleyAddressDescribeError $ textShow err
runAddressInfoBase58 :: Text -> ExceptT ShelleyAddressInfoError IO ()
runAddressInfoBase58 addrTxt = do
case Base16.encode <$> Base58.decodeBase58 Base58.bitcoinAlphabet (Text.encodeUtf8 addrTxt) of
Just hex -> do
runAddressInfoHex $ Text.decodeUtf8 hex
liftIO $ BS.putStrLn ("Hex: " <> hex)
Nothing -> left $ ShelleyAddressDescribeError "Failed Base58 decode. Impossible!"
describeAddr :: Address -> ExceptT ShelleyAddressInfoError IO ()
describeAddr addr =
case addr of
AddressByron {} -> liftIO $ putStrLn "Era: Byron"
AddressShelley {} -> liftIO $ putStrLn "Era: Shelley"
AddressShelleyReward {} -> liftIO $ putStrLn "Era: Shelley"