module Cardano.Ledger.Api.Tx.Address (
  -- * Address
  Addr (..),
  getNetwork,
  BootstrapAddress (..),
  serialiseAddr,

  -- ** Strict decoders

  -- | Decoders below will only decode addresses that are allowed to be placed on chain
  -- today. Historically there were a few bugs in the decoder which allowed a few
  -- malformed addressed to be placed on chain. If you need backwards compatibility, reach
  -- out for `decodeAddrLenient`.
  decodeAddr,
  decodeAddrEither,
  decodeAddrShort,
  decodeAddrShortEither,

  -- ** Lenient decoders

  -- | These lenient decoders do not fail for addresses with known bugs
  DecAddr (..),
  decodeAddrLenient,
  decodeAddrLenientEither,

  -- * Reward Account
  RewardAccount (..),
  serialiseRewardAccount,
  deserialiseRewardAccount,
) where

import Cardano.Ledger.Address
import Control.Applicative ((<|>))
import Control.Monad.Trans.Fail (runFail, runFailLast)
import Control.Monad.Trans.State.Strict (evalStateT, get)
import qualified Data.ByteString as BS
import Data.ByteString.Short (ShortByteString)

-- | Same as `decodeAddrShort`, but produces an `Either` result
decodeAddrShortEither ::
  ShortByteString ->
  Either String Addr
decodeAddrShortEither :: ShortByteString -> Either String Addr
decodeAddrShortEither ShortByteString
sbs = Fail String Addr -> Either String Addr
forall e a. (IsString e, Semigroup e) => Fail e a -> Either e a
runFail (Fail String Addr -> Either String Addr)
-> Fail String Addr -> Either String Addr
forall a b. (a -> b) -> a -> b
$ StateT Int (FailT String Identity) Addr -> Int -> Fail String Addr
forall (m :: * -> *) s a. Monad m => StateT s m a -> s -> m a
evalStateT (ShortByteString -> StateT Int (FailT String Identity) Addr
forall (m :: * -> *) b.
(MonadFail m, AddressBuffer b) =>
b -> StateT Int m Addr
decodeAddrStateT ShortByteString
sbs) Int
0
{-# INLINE decodeAddrShortEither #-}

-- | Same as `decodeAddr`, but works on `ShortByteString`
decodeAddrShort ::
  MonadFail m =>
  ShortByteString ->
  m Addr
decodeAddrShort :: forall (m :: * -> *). MonadFail m => ShortByteString -> m Addr
decodeAddrShort ShortByteString
sbs = StateT Int m Addr -> Int -> m Addr
forall (m :: * -> *) s a. Monad m => StateT s m a -> s -> m a
evalStateT (ShortByteString -> StateT Int m Addr
forall (m :: * -> *) b.
(MonadFail m, AddressBuffer b) =>
b -> StateT Int m Addr
decodeAddrStateT ShortByteString
sbs) Int
0
{-# INLINE decodeAddrShort #-}

-- | Decoded Address.
data DecAddr
  = -- | Address was decoded with no problems
    DecAddr Addr
  | -- | Address was decoded, but it contains an invalid `Cardano.Ledger.Credential.Ptr`, which
    -- means that address will be decoded with Ptr that has all values clamped to zero.
    DecAddrBadPtr Addr
  | -- | Address was decoded, but not all of input was consumed
    DecAddrUnconsumed
      Addr
      -- | Left over bytes after consuming the input
      BS.ByteString
  deriving (DecAddr -> DecAddr -> Bool
(DecAddr -> DecAddr -> Bool)
-> (DecAddr -> DecAddr -> Bool) -> Eq DecAddr
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: DecAddr -> DecAddr -> Bool
== :: DecAddr -> DecAddr -> Bool
$c/= :: DecAddr -> DecAddr -> Bool
/= :: DecAddr -> DecAddr -> Bool
Eq, Int -> DecAddr -> ShowS
[DecAddr] -> ShowS
DecAddr -> String
(Int -> DecAddr -> ShowS)
-> (DecAddr -> String) -> ([DecAddr] -> ShowS) -> Show DecAddr
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DecAddr -> ShowS
showsPrec :: Int -> DecAddr -> ShowS
$cshow :: DecAddr -> String
show :: DecAddr -> String
$cshowList :: [DecAddr] -> ShowS
showList :: [DecAddr] -> ShowS
Show)

-- | This is a lenient decoder that will disregard known bugs in the address
-- deserialization. This function is intended for clients that need to deal with
-- historical data that has already been placed on chain. If you also require information
-- on what exactly is bad in the address, or you would like to guard only against a
-- specific bug, you should use `decodeAddrLenientEither` instead.
--
-- @since 1.8.0
decodeAddrLenient ::
  MonadFail m =>
  BS.ByteString ->
  m Addr
decodeAddrLenient :: forall (m :: * -> *). MonadFail m => ByteString -> m Addr
decodeAddrLenient ByteString
bs = StateT Int m Addr -> Int -> m Addr
forall (m :: * -> *) s a. Monad m => StateT s m a -> s -> m a
evalStateT (Bool -> Bool -> ByteString -> StateT Int m Addr
forall (m :: * -> *) b.
(MonadFail m, AddressBuffer b) =>
Bool -> Bool -> b -> StateT Int m Addr
decodeAddrStateLenientT Bool
True Bool
True ByteString
bs) Int
0

-- | Decode an address and fail only for addresses that could have never been placed on
-- chain, while decoding addresses with information about potential problems in
-- them. Similar to `decodeAddrLenient`, this function is not intended for addresses that
-- will be placed into a new transaction.
--
-- @since 1.8.0
decodeAddrLenientEither ::
  BS.ByteString ->
  Either String DecAddr
decodeAddrLenientEither :: ByteString -> Either String DecAddr
decodeAddrLenientEither ByteString
bs =
  Fail String DecAddr -> Either String DecAddr
forall e a. IsString e => Fail e a -> Either e a
runFailLast (Fail String DecAddr -> Either String DecAddr)
-> Fail String DecAddr -> Either String DecAddr
forall a b. (a -> b) -> a -> b
$
    (Addr -> DecAddr
DecAddr (Addr -> DecAddr) -> Fail String Addr -> Fail String DecAddr
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Fail String Addr
forall (m :: * -> *). MonadFail m => ByteString -> m Addr
decodeAddr ByteString
bs)
      Fail String DecAddr -> Fail String DecAddr -> Fail String DecAddr
forall a.
FailT String Identity a
-> FailT String Identity a -> FailT String Identity a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (Addr -> DecAddr
DecAddrBadPtr (Addr -> DecAddr) -> Fail String Addr -> Fail String DecAddr
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> StateT Int (FailT String Identity) Addr -> Int -> Fail String Addr
forall (m :: * -> *) s a. Monad m => StateT s m a -> s -> m a
evalStateT (Bool
-> Bool -> ByteString -> StateT Int (FailT String Identity) Addr
forall (m :: * -> *) b.
(MonadFail m, AddressBuffer b) =>
Bool -> Bool -> b -> StateT Int m Addr
decodeAddrStateLenientT Bool
True Bool
False ByteString
bs) Int
0)
      Fail String DecAddr -> Fail String DecAddr -> Fail String DecAddr
forall a.
FailT String Identity a
-> FailT String Identity a -> FailT String Identity a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Fail String DecAddr
decodeWithUnconsumed
  where
    decodeWithUnconsumed :: Fail String DecAddr
decodeWithUnconsumed = (StateT Int (FailT String Identity) DecAddr
 -> Int -> Fail String DecAddr)
-> Int
-> StateT Int (FailT String Identity) DecAddr
-> Fail String DecAddr
forall a b c. (a -> b -> c) -> b -> a -> c
flip StateT Int (FailT String Identity) DecAddr
-> Int -> Fail String DecAddr
forall (m :: * -> *) s a. Monad m => StateT s m a -> s -> m a
evalStateT Int
0 (StateT Int (FailT String Identity) DecAddr -> Fail String DecAddr)
-> StateT Int (FailT String Identity) DecAddr
-> Fail String DecAddr
forall a b. (a -> b) -> a -> b
$ do
      Addr
addr <- Bool
-> Bool -> ByteString -> StateT Int (FailT String Identity) Addr
forall (m :: * -> *) b.
(MonadFail m, AddressBuffer b) =>
Bool -> Bool -> b -> StateT Int m Addr
decodeAddrStateLenientT Bool
False Bool
True ByteString
bs
      Int
bytesConsumed <- StateT Int (FailT String Identity) Int
forall (m :: * -> *) s. Monad m => StateT s m s
get
      DecAddr -> StateT Int (FailT String Identity) DecAddr
forall a. a -> StateT Int (FailT String Identity) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (DecAddr -> StateT Int (FailT String Identity) DecAddr)
-> DecAddr -> StateT Int (FailT String Identity) DecAddr
forall a b. (a -> b) -> a -> b
$ Addr -> ByteString -> DecAddr
DecAddrUnconsumed Addr
addr (Int -> ByteString -> ByteString
BS.drop Int
bytesConsumed ByteString
bs)