~zinigor/cardano-node/trunk

« back to all changes in this revision

Viewing changes to cardano-cli/src/Cardano/CLI/Mary/TxOutParser.hs

  • Committer: Igor Zinovyev
  • Date: 2021-08-13 19:12:27 UTC
  • Revision ID: zinigor@gmail.com-20210813191227-stlnsj3mc5ypwn0c
Tags: upstream-1.27.0
ImportĀ upstreamĀ versionĀ 1.27.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
module Cardano.CLI.Mary.TxOutParser
 
2
  ( parseTxOutAnyEra
 
3
  ) where
 
4
 
 
5
import           Prelude
 
6
 
 
7
import           Data.Char (isAsciiLower, isAsciiUpper, isDigit)
 
8
import           Data.Text (Text)
 
9
import qualified Data.Text as Text
 
10
 
 
11
import           Control.Applicative (some)
 
12
import           Text.Parsec (option, satisfy, (<?>))
 
13
import           Text.Parsec.Char (char, spaces)
 
14
import           Text.Parsec.String (Parser)
 
15
 
 
16
import           Cardano.Api (AddressAny (..), AsType (..), deserialiseAddress)
 
17
import           Cardano.CLI.Mary.ValueParser (parseValue)
 
18
import           Cardano.CLI.Types (TxOutAnyEra (..))
 
19
 
 
20
 
 
21
parseTxOutAnyEra :: Parser TxOutAnyEra
 
22
parseTxOutAnyEra = do
 
23
    addr <- parseAddressAny
 
24
    spaces
 
25
    -- Accept the old style of separating the address and value in a
 
26
    -- transaction output:
 
27
    option () (char '+' >> spaces)
 
28
    TxOutAnyEra addr <$> parseValue
 
29
 
 
30
parseAddressAny :: Parser AddressAny
 
31
parseAddressAny = do
 
32
  str <- plausibleAddressString <?> "address"
 
33
  case deserialiseAddress AsAddressAny str of
 
34
    Nothing -> fail "expecting valid address"
 
35
    Just addr -> pure addr
 
36
 
 
37
plausibleAddressString :: Parser Text
 
38
plausibleAddressString =
 
39
    Text.pack <$> some (satisfy isPlausibleAddressChar)
 
40
  where
 
41
    -- Covers both base58 and bech32 (with constrained prefixes)
 
42
    isPlausibleAddressChar c =
 
43
      isAsciiLower c
 
44
        || isAsciiUpper c
 
45
        || isDigit c
 
46
        || c == '_'