{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# OPTIONS_GHC -Wno-deprecations #-}

module Cardano.Ledger.Serialization
  {-# DEPRECATED "Use `Cardano.Ledger.Binary` from 'cardano-ledger-binary' package instead" #-} (
  EncCBORGroup (..),
  DecCBORGroup (..),
  CBORGroup (..),
  decodeList,
  decodeSeq,
  decodeStrictSeq,
  decodeSet,
  decodeMap,
  decodeMapContents,
  decodeMapTraverse,
  decodeMaybe,
  decodeRecordNamed,
  decodeRecordNamedT,
  decodeRecordSum,
  decodeNullMaybe,
  encodeFoldable,
  encodeFoldableEncoder,
  encodeFoldableMapEncoder,
  encodeNullMaybe,
  encodeMap,
  groupRecord,
  ratioEncCBOR,
  ratioDecCBOR,
  mapEncCBOR,
  mapDecCBOR,
  translateViaCBORAnnotator,
  -- IPv4
  ipv4ToBytes,
  ipv4FromBytes,
  ipv4EncCBOR,
  ipv4DecCBOR,
  -- IPv6
  ipv6ToBytes,
  ipv6FromBytes,
  ipv6EncCBOR,
  ipv6DecCBOR,
  -- Raw
  listLenInt,
  runByteBuilder,
  -- UTC Time
  utcTimeEncCBOR,
  utcTimeDecCBOR,
  -- This abstraction can/should be moved into cardano-binary
  Sized (..),
  mkSized,
  sizedDecoder,
  toSizedL,
)
where

import Cardano.Ledger.Binary (
  CBORGroup (..),
  DecCBOR (..),
  DecCBORGroup (..),
  Decoder,
  DecoderError (..),
  EncCBOR (..),
  EncCBORGroup (..),
  Encoding,
  Sized (..),
  assertTag,
  cborError,
  decodeIPv4,
  decodeIPv6,
  decodeList,
  decodeMap,
  decodeMapContents,
  decodeMapTraverse,
  decodeMaybe,
  decodeNullMaybe,
  decodeSeq,
  decodeSet,
  decodeStrictSeq,
  decodeUTCTime,
  encodeFoldableEncoder,
  encodeFoldableMapEncoder,
  encodeIPv4,
  encodeIPv6,
  encodeMap,
  encodeNullMaybe,
  encodeRatio,
  encodeUTCTime,
  enforceDecoderVersion,
  groupRecord,
  ipv4ToBytes,
  ipv6ToBytes,
  listLenInt,
  mkSized,
  natVersion,
  runByteBuilder,
  sizedDecoder,
  toSizedL,
  translateViaCBORAnnotator,
 )
import Cardano.Ledger.Binary.Coders (
  decodeRecordNamed,
  decodeRecordNamedT,
  decodeRecordSum,
 )
import Control.Monad (when)
import Data.Binary.Get (Get, getWord32le, runGetOrFail)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BSL
import Data.IP (
  IPv4,
  IPv6,
  fromHostAddress,
  fromHostAddress6,
 )
import Data.Map.Strict (Map)
import Data.Ratio (Ratio, (%))
import Data.Time (UTCTime (..))
import Network.Socket (HostAddress6)

mapEncCBOR :: (EncCBOR a, EncCBOR b) => Map a b -> Encoding
mapEncCBOR :: forall a b. (EncCBOR a, EncCBOR b) => Map a b -> Encoding
mapEncCBOR = forall k v.
(k -> Encoding) -> (v -> Encoding) -> Map k v -> Encoding
encodeMap forall a. EncCBOR a => a -> Encoding
encCBOR forall a. EncCBOR a => a -> Encoding
encCBOR

mapDecCBOR :: (Ord a, DecCBOR a, DecCBOR b) => Decoder s (Map a b)
mapDecCBOR :: forall a b s. (Ord a, DecCBOR a, DecCBOR b) => Decoder s (Map a b)
mapDecCBOR = forall k s v.
Ord k =>
Decoder s k -> Decoder s v -> Decoder s (Map k v)
decodeMap forall a s. DecCBOR a => Decoder s a
decCBOR forall a s. DecCBOR a => Decoder s a
decCBOR

ratioEncCBOR :: EncCBOR a => Ratio a -> Encoding
ratioEncCBOR :: forall a. EncCBOR a => Ratio a -> Encoding
ratioEncCBOR = forall t. (t -> Encoding) -> Ratio t -> Encoding
encodeRatio forall a. EncCBOR a => a -> Encoding
encCBOR

ratioDecCBOR :: (Integral a, DecCBOR a) => Decoder s (Ratio a)
ratioDecCBOR :: forall a s. (Integral a, DecCBOR a) => Decoder s (Ratio a)
ratioDecCBOR = forall a s. Integral a => Decoder s a -> Decoder s (Ratio a)
decodeFraction forall a s. DecCBOR a => Decoder s a
decCBOR

decodeFraction :: Integral a => Decoder s a -> Decoder s (Ratio a)
decodeFraction :: forall a s. Integral a => Decoder s a -> Decoder s (Ratio a)
decodeFraction Decoder s a
decoder = do
  forall s. Word -> Decoder s ()
assertTag Word
30
  [a]
values <- forall s a. Decoder s a -> Decoder s [a]
decodeList Decoder s a
decoder
  case [a]
values of
    [a
n, a
d] -> do
      forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (a
d forall a. Eq a => a -> a -> Bool
== a
0) (forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Denominator cannot be 0")
      forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$! a
n forall a. Integral a => a -> a -> Ratio a
% a
d
    [a]
_ -> forall (m :: * -> *) e a. (MonadFail m, Buildable e) => e -> m a
cborError forall a b. (a -> b) -> a -> b
$ Text -> Int -> Int -> DecoderError
DecoderErrorSizeMismatch Text
"Rational" Int
2 (forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
values)

ipv4FromBytes :: BS.ByteString -> Either String IPv4
ipv4FromBytes :: ByteString -> Either String IPv4
ipv4FromBytes ByteString
b =
  case forall a.
Get a
-> ByteString
-> Either
     (ByteString, ByteOffset, String) (ByteString, ByteOffset, a)
runGetOrFail Get Word32
getWord32le (ByteString -> ByteString
BSL.fromStrict ByteString
b) of
    Left (ByteString
_, ByteOffset
_, String
err) -> forall a b. a -> Either a b
Left String
err
    Right (ByteString
_, ByteOffset
_, Word32
ha) -> forall a b. b -> Either a b
Right forall a b. (a -> b) -> a -> b
$ Word32 -> IPv4
fromHostAddress Word32
ha

ipv4EncCBOR :: IPv4 -> Encoding
ipv4EncCBOR :: IPv4 -> Encoding
ipv4EncCBOR = IPv4 -> Encoding
encodeIPv4

ipv4DecCBOR :: Decoder s IPv4
ipv4DecCBOR :: forall s. Decoder s IPv4
ipv4DecCBOR = forall s a. Version -> Decoder s a -> Decoder s a
enforceDecoderVersion (forall (v :: Natural).
(KnownNat v, MinVersion <= v, v <= MaxVersion) =>
Version
natVersion @2) forall s. Decoder s IPv4
decodeIPv4

getHostAddress6 :: Get HostAddress6
getHostAddress6 :: Get HostAddress6
getHostAddress6 = do
  Word32
w1 <- Get Word32
getWord32le
  Word32
w2 <- Get Word32
getWord32le
  Word32
w3 <- Get Word32
getWord32le
  Word32
w4 <- Get Word32
getWord32le
  forall (m :: * -> *) a. Monad m => a -> m a
return (Word32
w1, Word32
w2, Word32
w3, Word32
w4)

ipv6FromBytes :: BS.ByteString -> Either String IPv6
ipv6FromBytes :: ByteString -> Either String IPv6
ipv6FromBytes ByteString
b =
  case forall a.
Get a
-> ByteString
-> Either
     (ByteString, ByteOffset, String) (ByteString, ByteOffset, a)
runGetOrFail Get HostAddress6
getHostAddress6 (ByteString -> ByteString
BSL.fromStrict ByteString
b) of
    Left (ByteString
_, ByteOffset
_, String
err) -> forall a b. a -> Either a b
Left String
err
    Right (ByteString
_, ByteOffset
_, HostAddress6
ha) -> forall a b. b -> Either a b
Right forall a b. (a -> b) -> a -> b
$ HostAddress6 -> IPv6
fromHostAddress6 HostAddress6
ha

ipv6EncCBOR :: IPv6 -> Encoding
ipv6EncCBOR :: IPv6 -> Encoding
ipv6EncCBOR = IPv6 -> Encoding
encodeIPv6

ipv6DecCBOR :: Decoder s IPv6
ipv6DecCBOR :: forall s. Decoder s IPv6
ipv6DecCBOR = forall s a. Version -> Decoder s a -> Decoder s a
enforceDecoderVersion (forall (v :: Natural).
(KnownNat v, MinVersion <= v, v <= MaxVersion) =>
Version
natVersion @2) forall s. Decoder s IPv6
decodeIPv6

utcTimeEncCBOR :: UTCTime -> Encoding
utcTimeEncCBOR :: UTCTime -> Encoding
utcTimeEncCBOR = UTCTime -> Encoding
encodeUTCTime

utcTimeDecCBOR :: Decoder s UTCTime
utcTimeDecCBOR :: forall s. Decoder s UTCTime
utcTimeDecCBOR = forall s. Decoder s UTCTime
decodeUTCTime

encodeFoldable :: (EncCBOR a, Foldable f) => f a -> Encoding
encodeFoldable :: forall a (f :: * -> *). (EncCBOR a, Foldable f) => f a -> Encoding
encodeFoldable = forall (f :: * -> *) a.
Foldable f =>
(a -> Encoding) -> f a -> Encoding
encodeFoldableEncoder forall a. EncCBOR a => a -> Encoding
encCBOR