module Cardano.Ledger.Api.Tx.Address (
Addr (..),
getNetwork,
BootstrapAddress (..),
serialiseAddr,
decodeAddr,
decodeAddrEither,
decodeAddrShort,
decodeAddrShortEither,
DecAddr (..),
decodeAddrLenient,
decodeAddrLenientEither,
RewardAccount (..),
serialiseRewardAccount,
deserialiseRewardAccount,
)
where
import Cardano.Ledger.Address
import Cardano.Ledger.Crypto
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)
decodeAddrShortEither ::
Crypto c =>
ShortByteString ->
Either String (Addr c)
decodeAddrShortEither :: forall c. Crypto c => ShortByteString -> Either String (Addr c)
decodeAddrShortEither ShortByteString
sbs = forall e a. (IsString e, Semigroup e) => Fail e a -> Either e a
runFail forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) s a. Monad m => StateT s m a -> s -> m a
evalStateT (forall c (m :: * -> *) b.
(Crypto c, MonadFail m, AddressBuffer b) =>
b -> StateT Int m (Addr c)
decodeAddrStateT ShortByteString
sbs) Int
0
{-# INLINE decodeAddrShortEither #-}
decodeAddrShort ::
(Crypto c, MonadFail m) =>
ShortByteString ->
m (Addr c)
decodeAddrShort :: forall c (m :: * -> *).
(Crypto c, MonadFail m) =>
ShortByteString -> m (Addr c)
decodeAddrShort ShortByteString
sbs = forall (m :: * -> *) s a. Monad m => StateT s m a -> s -> m a
evalStateT (forall c (m :: * -> *) b.
(Crypto c, MonadFail m, AddressBuffer b) =>
b -> StateT Int m (Addr c)
decodeAddrStateT ShortByteString
sbs) Int
0
{-# INLINE decodeAddrShort #-}
data DecAddr c
=
DecAddr (Addr c)
|
DecAddrBadPtr (Addr c)
|
DecAddrUnconsumed
(Addr c)
BS.ByteString
deriving (DecAddr c -> DecAddr c -> Bool
forall c. DecAddr c -> DecAddr c -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: DecAddr c -> DecAddr c -> Bool
$c/= :: forall c. DecAddr c -> DecAddr c -> Bool
== :: DecAddr c -> DecAddr c -> Bool
$c== :: forall c. DecAddr c -> DecAddr c -> Bool
Eq, Int -> DecAddr c -> ShowS
forall c. Int -> DecAddr c -> ShowS
forall c. [DecAddr c] -> ShowS
forall c. DecAddr c -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [DecAddr c] -> ShowS
$cshowList :: forall c. [DecAddr c] -> ShowS
show :: DecAddr c -> String
$cshow :: forall c. DecAddr c -> String
showsPrec :: Int -> DecAddr c -> ShowS
$cshowsPrec :: forall c. Int -> DecAddr c -> ShowS
Show)
decodeAddrLenient ::
(Crypto c, MonadFail m) =>
BS.ByteString ->
m (Addr c)
decodeAddrLenient :: forall c (m :: * -> *).
(Crypto c, MonadFail m) =>
ByteString -> m (Addr c)
decodeAddrLenient ByteString
bs = forall (m :: * -> *) s a. Monad m => StateT s m a -> s -> m a
evalStateT (forall c (m :: * -> *) b.
(Crypto c, MonadFail m, AddressBuffer b) =>
Bool -> Bool -> b -> StateT Int m (Addr c)
decodeAddrStateLenientT Bool
True Bool
True ByteString
bs) Int
0
decodeAddrLenientEither ::
Crypto c =>
BS.ByteString ->
Either String (DecAddr c)
decodeAddrLenientEither :: forall c. Crypto c => ByteString -> Either String (DecAddr c)
decodeAddrLenientEither ByteString
bs =
forall e a. IsString e => Fail e a -> Either e a
runFailLast forall a b. (a -> b) -> a -> b
$
(forall c. Addr c -> DecAddr c
DecAddr forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall c (m :: * -> *).
(Crypto c, MonadFail m) =>
ByteString -> m (Addr c)
decodeAddr ByteString
bs)
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (forall c. Addr c -> DecAddr c
DecAddrBadPtr forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) s a. Monad m => StateT s m a -> s -> m a
evalStateT (forall c (m :: * -> *) b.
(Crypto c, MonadFail m, AddressBuffer b) =>
Bool -> Bool -> b -> StateT Int m (Addr c)
decodeAddrStateLenientT Bool
True Bool
False ByteString
bs) Int
0)
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> FailT String Identity (DecAddr c)
decodeWithUnconsumed
where
decodeWithUnconsumed :: FailT String Identity (DecAddr c)
decodeWithUnconsumed = forall a b c. (a -> b -> c) -> b -> a -> c
flip forall (m :: * -> *) s a. Monad m => StateT s m a -> s -> m a
evalStateT Int
0 forall a b. (a -> b) -> a -> b
$ do
Addr c
addr <- forall c (m :: * -> *) b.
(Crypto c, MonadFail m, AddressBuffer b) =>
Bool -> Bool -> b -> StateT Int m (Addr c)
decodeAddrStateLenientT Bool
False Bool
True ByteString
bs
Int
bytesConsumed <- forall (m :: * -> *) s. Monad m => StateT s m s
get
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall c. Addr c -> ByteString -> DecAddr c
DecAddrUnconsumed Addr c
addr (Int -> ByteString -> ByteString
BS.drop Int
bytesConsumed ByteString
bs)