-- Copyright 2026 Biglup Labs
-- SPDX-License-Identifier: Apache-2.0
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
--     https://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}

-- | Immutable certificates, exact envelopes and private CBOR schema workers.
-- No native resources or effects are used by these codecs.
module Cometa.Internal.Certificate
  ( Certificate (..),
    Mir (..),
    Pot (..),
    EncodedCertificate,
    encodeCertificate,
    decodeCertificate,
    fromCertificate,
    toCertificate,
    certificateCbor,
    Certificates (..),
    EncodedCertificates,
    encodeCertificates,
    decodeCertificates,
    fromCertificates,
    toCertificates,
    certificatesCbor,
    certificatesSetEncoding,
    withCertificatesSetEncoding,
  )
where

-- IMPORTS --------------------------------------------------------------------

import qualified Codec.CBOR.Decoding as Decode
import qualified Codec.CBOR.Encoding as Encode
import qualified Codec.CBOR.Read as Read
import qualified Codec.CBOR.Term as Term
import qualified Codec.CBOR.Write as Write
import qualified Cometa.Credential as Credential
import Cometa.Encoding.Set (SetEncoding)
import Cometa.Error (Error (..))
import qualified Cometa.Governance.Anchor as Anchor
import qualified Cometa.Governance.DRep as DRep
import qualified Cometa.Internal.Pool as Pool
import qualified Cometa.Internal.SetEncoding as SetEncoding
import Control.Monad (foldM, replicateM, unless, when)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Lazy as Lazy
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import qualified Data.Text as Text
import Data.Word (Word64)

-- TYPES ----------------------------------------------------------------------

-- | Source accounting pot for instantaneous rewards. Ordering follows CBOR codes.
data Pot
  = -- | The reserve accounting pot.
    Reserves
  | -- | The treasury accounting pot.
    Treasury
  deriving (Pot -> Pot -> Bool
(Pot -> Pot -> Bool) -> (Pot -> Pot -> Bool) -> Eq Pot
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Pot -> Pot -> Bool
== :: Pot -> Pot -> Bool
$c/= :: Pot -> Pot -> Bool
/= :: Pot -> Pot -> Bool
Eq, Eq Pot
Eq Pot =>
(Pot -> Pot -> Ordering)
-> (Pot -> Pot -> Bool)
-> (Pot -> Pot -> Bool)
-> (Pot -> Pot -> Bool)
-> (Pot -> Pot -> Bool)
-> (Pot -> Pot -> Pot)
-> (Pot -> Pot -> Pot)
-> Ord Pot
Pot -> Pot -> Bool
Pot -> Pot -> Ordering
Pot -> Pot -> Pot
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: Pot -> Pot -> Ordering
compare :: Pot -> Pot -> Ordering
$c< :: Pot -> Pot -> Bool
< :: Pot -> Pot -> Bool
$c<= :: Pot -> Pot -> Bool
<= :: Pot -> Pot -> Bool
$c> :: Pot -> Pot -> Bool
> :: Pot -> Pot -> Bool
$c>= :: Pot -> Pot -> Bool
>= :: Pot -> Pot -> Bool
$cmax :: Pot -> Pot -> Pot
max :: Pot -> Pot -> Pot
$cmin :: Pot -> Pot -> Pot
min :: Pot -> Pot -> Pot
Ord, Int -> Pot -> ShowS
[Pot] -> ShowS
Pot -> String
(Int -> Pot -> ShowS)
-> (Pot -> String) -> ([Pot] -> ShowS) -> Show Pot
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Pot -> ShowS
showsPrec :: Int -> Pot -> ShowS
$cshow :: Pot -> String
show :: Pot -> String
$cshowList :: [Pot] -> ShowS
showList :: [Pot] -> ShowS
Show, Pot
Pot -> Pot -> Bounded Pot
forall a. a -> a -> Bounded a
$cminBound :: Pot
minBound :: Pot
$cmaxBound :: Pot
maxBound :: Pot
Bounded, Int -> Pot
Pot -> Int
Pot -> [Pot]
Pot -> Pot
Pot -> Pot -> [Pot]
Pot -> Pot -> Pot -> [Pot]
(Pot -> Pot)
-> (Pot -> Pot)
-> (Int -> Pot)
-> (Pot -> Int)
-> (Pot -> [Pot])
-> (Pot -> Pot -> [Pot])
-> (Pot -> Pot -> [Pot])
-> (Pot -> Pot -> Pot -> [Pot])
-> Enum Pot
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
$csucc :: Pot -> Pot
succ :: Pot -> Pot
$cpred :: Pot -> Pot
pred :: Pot -> Pot
$ctoEnum :: Int -> Pot
toEnum :: Int -> Pot
$cfromEnum :: Pot -> Int
fromEnum :: Pot -> Int
$cenumFrom :: Pot -> [Pot]
enumFrom :: Pot -> [Pot]
$cenumFromThen :: Pot -> Pot -> [Pot]
enumFromThen :: Pot -> Pot -> [Pot]
$cenumFromTo :: Pot -> Pot -> [Pot]
enumFromTo :: Pot -> Pot -> [Pot]
$cenumFromThenTo :: Pot -> Pot -> Pot -> [Pot]
enumFromThenTo :: Pot -> Pot -> Pot -> [Pot]
Enum)

-- | Instantaneous reward transfers. All quantities are
-- lovelace from 0 through 2^64-1; signed reward adjustments are rejected.
data Mir
  = -- | Transfer from the source pot to the other accounting pot.
    ToOtherPot !Pot !Word64
  | -- | Assign rewards from the source pot to unique stake credentials.
    ToStakeCredentials !Pot !(Map (Credential.Credential Credential.Stake) Word64)
  deriving (Mir -> Mir -> Bool
(Mir -> Mir -> Bool) -> (Mir -> Mir -> Bool) -> Eq Mir
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Mir -> Mir -> Bool
== :: Mir -> Mir -> Bool
$c/= :: Mir -> Mir -> Bool
/= :: Mir -> Mir -> Bool
Eq, Eq Mir
Eq Mir =>
(Mir -> Mir -> Ordering)
-> (Mir -> Mir -> Bool)
-> (Mir -> Mir -> Bool)
-> (Mir -> Mir -> Bool)
-> (Mir -> Mir -> Bool)
-> (Mir -> Mir -> Mir)
-> (Mir -> Mir -> Mir)
-> Ord Mir
Mir -> Mir -> Bool
Mir -> Mir -> Ordering
Mir -> Mir -> Mir
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: Mir -> Mir -> Ordering
compare :: Mir -> Mir -> Ordering
$c< :: Mir -> Mir -> Bool
< :: Mir -> Mir -> Bool
$c<= :: Mir -> Mir -> Bool
<= :: Mir -> Mir -> Bool
$c> :: Mir -> Mir -> Bool
> :: Mir -> Mir -> Bool
$c>= :: Mir -> Mir -> Bool
>= :: Mir -> Mir -> Bool
$cmax :: Mir -> Mir -> Mir
max :: Mir -> Mir -> Mir
$cmin :: Mir -> Mir -> Mir
min :: Mir -> Mir -> Mir
Ord, Int -> Mir -> ShowS
[Mir] -> ShowS
Mir -> String
(Int -> Mir -> ShowS)
-> (Mir -> String) -> ([Mir] -> ShowS) -> Show Mir
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Mir -> ShowS
showsPrec :: Int -> Mir -> ShowS
$cshow :: Mir -> String
show :: Mir -> String
$cshowList :: [Mir] -> ShowS
showList :: [Mir] -> ShowS
Show)

-- | Certificate semantic data. Deposits, refunds and reward amounts are unsigned
-- 64-bit lovelace. Constructors do not check protocol parameters, era eligibility,
-- registration state, witnesses, or ledger acceptance. Equality and ordering
-- compare constructor then fields; 'Show' is diagnostic.
data Certificate
  = -- | Register a stake credential without an explicit deposit (legacy form).
    StakeRegistration !(Credential.Credential Credential.Stake)
  | -- | Deregister a stake credential without an explicit refund (legacy form).
    StakeDeregistration !(Credential.Credential Credential.Stake)
  | -- | Delegate stake to the pool identified by its operator key hash.
    StakeDelegation !(Credential.Credential Credential.Stake) !(Credential.KeyHash Credential.StakePool)
  | -- | Register or update all pool parameters.
    PoolRegistration !Pool.PoolParameters
  | -- | Retire a pool at the given epoch (unsigned 64-bit epoch number).
    PoolRetirement !(Credential.KeyHash Credential.StakePool) !Word64
  | -- | Delegate a genesis key to a delegate key and VRF key hash (legacy form).
    GenesisKeyDelegation !(Credential.KeyHash Credential.Genesis) !(Credential.KeyHash Credential.GenesisDelegate) !Pool.VrfKeyHash
  | -- | Transfer instantaneous rewards (legacy form).
    MoveInstantaneousRewards !Mir
  | -- | Register a stake credential with its explicit deposit in lovelace.
    Registration !(Credential.Credential Credential.Stake) !Word64
  | -- | Deregister a stake credential with its explicit refund in lovelace.
    Unregistration !(Credential.Credential Credential.Stake) !Word64
  | -- | Delegate voting power to a DRep or a predefined target.
    VoteDelegation !(Credential.Credential Credential.Stake) !DRep.DRep
  | -- | Delegate stake and voting power together.
    StakeVoteDelegation !(Credential.Credential Credential.Stake) !(Credential.KeyHash Credential.StakePool) !DRep.DRep
  | -- | Register and delegate stake; the last argument is the deposit in lovelace.
    StakeRegistrationDelegation !(Credential.Credential Credential.Stake) !(Credential.KeyHash Credential.StakePool) !Word64
  | -- | Register and delegate voting power; the last argument is the deposit in lovelace.
    VoteRegistrationDelegation !(Credential.Credential Credential.Stake) !DRep.DRep !Word64
  | -- | Register and delegate both powers; the last argument is the deposit in lovelace.
    StakeVoteRegistrationDelegation !(Credential.Credential Credential.Stake) !(Credential.KeyHash Credential.StakePool) !DRep.DRep !Word64
  | -- | Authorize a committee hot credential using its cold credential.
    AuthorizeCommitteeHot !(Credential.Credential Credential.CommitteeCold) !(Credential.Credential Credential.CommitteeHot)
  | -- | Resign a committee cold credential with an optional explanation.
    ResignCommitteeCold !(Credential.Credential Credential.CommitteeCold) !(Maybe Anchor.Anchor)
  | -- | Register a DRep with its deposit in lovelace and optional metadata.
    RegisterDRep !(Credential.Credential Credential.DRep) !Word64 !(Maybe Anchor.Anchor)
  | -- | Unregister a DRep with its refund in lovelace.
    UnregisterDRep !(Credential.Credential Credential.DRep) !Word64
  | -- | Update or remove a DRep metadata anchor.
    UpdateDRep !(Credential.Credential Credential.DRep) !(Maybe Anchor.Anchor)
  deriving (Certificate -> Certificate -> Bool
(Certificate -> Certificate -> Bool)
-> (Certificate -> Certificate -> Bool) -> Eq Certificate
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Certificate -> Certificate -> Bool
== :: Certificate -> Certificate -> Bool
$c/= :: Certificate -> Certificate -> Bool
/= :: Certificate -> Certificate -> Bool
Eq, Eq Certificate
Eq Certificate =>
(Certificate -> Certificate -> Ordering)
-> (Certificate -> Certificate -> Bool)
-> (Certificate -> Certificate -> Bool)
-> (Certificate -> Certificate -> Bool)
-> (Certificate -> Certificate -> Bool)
-> (Certificate -> Certificate -> Certificate)
-> (Certificate -> Certificate -> Certificate)
-> Ord Certificate
Certificate -> Certificate -> Bool
Certificate -> Certificate -> Ordering
Certificate -> Certificate -> Certificate
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: Certificate -> Certificate -> Ordering
compare :: Certificate -> Certificate -> Ordering
$c< :: Certificate -> Certificate -> Bool
< :: Certificate -> Certificate -> Bool
$c<= :: Certificate -> Certificate -> Bool
<= :: Certificate -> Certificate -> Bool
$c> :: Certificate -> Certificate -> Bool
> :: Certificate -> Certificate -> Bool
$c>= :: Certificate -> Certificate -> Bool
>= :: Certificate -> Certificate -> Bool
$cmax :: Certificate -> Certificate -> Certificate
max :: Certificate -> Certificate -> Certificate
$cmin :: Certificate -> Certificate -> Certificate
min :: Certificate -> Certificate -> Certificate
Ord, Int -> Certificate -> ShowS
[Certificate] -> ShowS
Certificate -> String
(Int -> Certificate -> ShowS)
-> (Certificate -> String)
-> ([Certificate] -> ShowS)
-> Show Certificate
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Certificate -> ShowS
showsPrec :: Int -> Certificate -> ShowS
$cshow :: Certificate -> String
show :: Certificate -> String
$cshowList :: [Certificate] -> ShowS
showList :: [Certificate] -> ShowS
Show)

-- | Certificate with its exact original CBOR. Equality and ordering include bytes.
data EncodedCertificate = EncodedCertificate !ByteString !Certificate deriving (EncodedCertificate -> EncodedCertificate -> Bool
(EncodedCertificate -> EncodedCertificate -> Bool)
-> (EncodedCertificate -> EncodedCertificate -> Bool)
-> Eq EncodedCertificate
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: EncodedCertificate -> EncodedCertificate -> Bool
== :: EncodedCertificate -> EncodedCertificate -> Bool
$c/= :: EncodedCertificate -> EncodedCertificate -> Bool
/= :: EncodedCertificate -> EncodedCertificate -> Bool
Eq, Eq EncodedCertificate
Eq EncodedCertificate =>
(EncodedCertificate -> EncodedCertificate -> Ordering)
-> (EncodedCertificate -> EncodedCertificate -> Bool)
-> (EncodedCertificate -> EncodedCertificate -> Bool)
-> (EncodedCertificate -> EncodedCertificate -> Bool)
-> (EncodedCertificate -> EncodedCertificate -> Bool)
-> (EncodedCertificate -> EncodedCertificate -> EncodedCertificate)
-> (EncodedCertificate -> EncodedCertificate -> EncodedCertificate)
-> Ord EncodedCertificate
EncodedCertificate -> EncodedCertificate -> Bool
EncodedCertificate -> EncodedCertificate -> Ordering
EncodedCertificate -> EncodedCertificate -> EncodedCertificate
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: EncodedCertificate -> EncodedCertificate -> Ordering
compare :: EncodedCertificate -> EncodedCertificate -> Ordering
$c< :: EncodedCertificate -> EncodedCertificate -> Bool
< :: EncodedCertificate -> EncodedCertificate -> Bool
$c<= :: EncodedCertificate -> EncodedCertificate -> Bool
<= :: EncodedCertificate -> EncodedCertificate -> Bool
$c> :: EncodedCertificate -> EncodedCertificate -> Bool
> :: EncodedCertificate -> EncodedCertificate -> Bool
$c>= :: EncodedCertificate -> EncodedCertificate -> Bool
>= :: EncodedCertificate -> EncodedCertificate -> Bool
$cmax :: EncodedCertificate -> EncodedCertificate -> EncodedCertificate
max :: EncodedCertificate -> EncodedCertificate -> EncodedCertificate
$cmin :: EncodedCertificate -> EncodedCertificate -> EncodedCertificate
min :: EncodedCertificate -> EncodedCertificate -> EncodedCertificate
Ord, Int -> EncodedCertificate -> ShowS
[EncodedCertificate] -> ShowS
EncodedCertificate -> String
(Int -> EncodedCertificate -> ShowS)
-> (EncodedCertificate -> String)
-> ([EncodedCertificate] -> ShowS)
-> Show EncodedCertificate
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> EncodedCertificate -> ShowS
showsPrec :: Int -> EncodedCertificate -> ShowS
$cshow :: EncodedCertificate -> String
show :: EncodedCertificate -> String
$cshowList :: [EncodedCertificate] -> ShowS
showList :: [EncodedCertificate] -> ShowS
Show)

-- | Ordered certificates. Order and repeated certificates are preserved, matching
-- the native certificate collection despite its historical set name.
newtype Certificates = Certificates [Certificate] deriving (Certificates -> Certificates -> Bool
(Certificates -> Certificates -> Bool)
-> (Certificates -> Certificates -> Bool) -> Eq Certificates
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Certificates -> Certificates -> Bool
== :: Certificates -> Certificates -> Bool
$c/= :: Certificates -> Certificates -> Bool
/= :: Certificates -> Certificates -> Bool
Eq, Eq Certificates
Eq Certificates =>
(Certificates -> Certificates -> Ordering)
-> (Certificates -> Certificates -> Bool)
-> (Certificates -> Certificates -> Bool)
-> (Certificates -> Certificates -> Bool)
-> (Certificates -> Certificates -> Bool)
-> (Certificates -> Certificates -> Certificates)
-> (Certificates -> Certificates -> Certificates)
-> Ord Certificates
Certificates -> Certificates -> Bool
Certificates -> Certificates -> Ordering
Certificates -> Certificates -> Certificates
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: Certificates -> Certificates -> Ordering
compare :: Certificates -> Certificates -> Ordering
$c< :: Certificates -> Certificates -> Bool
< :: Certificates -> Certificates -> Bool
$c<= :: Certificates -> Certificates -> Bool
<= :: Certificates -> Certificates -> Bool
$c> :: Certificates -> Certificates -> Bool
> :: Certificates -> Certificates -> Bool
$c>= :: Certificates -> Certificates -> Bool
>= :: Certificates -> Certificates -> Bool
$cmax :: Certificates -> Certificates -> Certificates
max :: Certificates -> Certificates -> Certificates
$cmin :: Certificates -> Certificates -> Certificates
min :: Certificates -> Certificates -> Certificates
Ord, Int -> Certificates -> ShowS
[Certificates] -> ShowS
Certificates -> String
(Int -> Certificates -> ShowS)
-> (Certificates -> String)
-> ([Certificates] -> ShowS)
-> Show Certificates
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Certificates -> ShowS
showsPrec :: Int -> Certificates -> ShowS
$cshow :: Certificates -> String
show :: Certificates -> String
$cshowList :: [Certificates] -> ShowS
showList :: [Certificates] -> ShowS
Show)

-- | Ordered certificates retaining their complete imported CBOR envelope.
data EncodedCertificates = EncodedCertificates !ByteString !Certificates deriving (EncodedCertificates -> EncodedCertificates -> Bool
(EncodedCertificates -> EncodedCertificates -> Bool)
-> (EncodedCertificates -> EncodedCertificates -> Bool)
-> Eq EncodedCertificates
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: EncodedCertificates -> EncodedCertificates -> Bool
== :: EncodedCertificates -> EncodedCertificates -> Bool
$c/= :: EncodedCertificates -> EncodedCertificates -> Bool
/= :: EncodedCertificates -> EncodedCertificates -> Bool
Eq, Eq EncodedCertificates
Eq EncodedCertificates =>
(EncodedCertificates -> EncodedCertificates -> Ordering)
-> (EncodedCertificates -> EncodedCertificates -> Bool)
-> (EncodedCertificates -> EncodedCertificates -> Bool)
-> (EncodedCertificates -> EncodedCertificates -> Bool)
-> (EncodedCertificates -> EncodedCertificates -> Bool)
-> (EncodedCertificates
    -> EncodedCertificates -> EncodedCertificates)
-> (EncodedCertificates
    -> EncodedCertificates -> EncodedCertificates)
-> Ord EncodedCertificates
EncodedCertificates -> EncodedCertificates -> Bool
EncodedCertificates -> EncodedCertificates -> Ordering
EncodedCertificates -> EncodedCertificates -> EncodedCertificates
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: EncodedCertificates -> EncodedCertificates -> Ordering
compare :: EncodedCertificates -> EncodedCertificates -> Ordering
$c< :: EncodedCertificates -> EncodedCertificates -> Bool
< :: EncodedCertificates -> EncodedCertificates -> Bool
$c<= :: EncodedCertificates -> EncodedCertificates -> Bool
<= :: EncodedCertificates -> EncodedCertificates -> Bool
$c> :: EncodedCertificates -> EncodedCertificates -> Bool
> :: EncodedCertificates -> EncodedCertificates -> Bool
$c>= :: EncodedCertificates -> EncodedCertificates -> Bool
>= :: EncodedCertificates -> EncodedCertificates -> Bool
$cmax :: EncodedCertificates -> EncodedCertificates -> EncodedCertificates
max :: EncodedCertificates -> EncodedCertificates -> EncodedCertificates
$cmin :: EncodedCertificates -> EncodedCertificates -> EncodedCertificates
min :: EncodedCertificates -> EncodedCertificates -> EncodedCertificates
Ord, Int -> EncodedCertificates -> ShowS
[EncodedCertificates] -> ShowS
EncodedCertificates -> String
(Int -> EncodedCertificates -> ShowS)
-> (EncodedCertificates -> String)
-> ([EncodedCertificates] -> ShowS)
-> Show EncodedCertificates
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> EncodedCertificates -> ShowS
showsPrec :: Int -> EncodedCertificates -> ShowS
$cshow :: EncodedCertificates -> String
show :: EncodedCertificates -> String
$cshowList :: [EncodedCertificates] -> ShowS
showList :: [EncodedCertificates] -> ShowS
Show)

-- DEFINITIONS ----------------------------------------------------------------

-- | Fresh definite certificate encoding, with canonical unsigned integers.
encodeCertificate :: Certificate -> ByteString
encodeCertificate :: Certificate -> ByteString
encodeCertificate = Encoding -> ByteString
Write.toStrictByteString (Encoding -> ByteString)
-> (Certificate -> Encoding) -> Certificate -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Certificate -> Encoding
certificateEncoding

-- | Validate a complete certificate and retain exact bytes; schema failures return InvalidCertificateCbor.
decodeCertificate :: ByteString -> Either Error EncodedCertificate
decodeCertificate :: ByteString -> Either Error EncodedCertificate
decodeCertificate ByteString
bytes = ByteString -> Certificate -> EncodedCertificate
EncodedCertificate ByteString
bytes (Certificate -> EncodedCertificate)
-> Either Error Certificate -> Either Error EncodedCertificate
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (forall s. Decoder s Certificate)
-> ByteString -> Either Error Certificate
forall a. (forall s. Decoder s a) -> ByteString -> Either Error a
decodeComplete Decoder s Certificate
forall s. Decoder s Certificate
certificateDecoder ByteString
bytes

-- | Create an exact envelope using fresh certificate encoding.
fromCertificate :: Certificate -> EncodedCertificate
fromCertificate :: Certificate -> EncodedCertificate
fromCertificate Certificate
value = ByteString -> Certificate -> EncodedCertificate
EncodedCertificate (Certificate -> ByteString
encodeCertificate Certificate
value) Certificate
value

-- | Inspect semantic certificate fields without changing the exact envelope.
toCertificate :: EncodedCertificate -> Certificate
toCertificate :: EncodedCertificate -> Certificate
toCertificate (EncodedCertificate ByteString
_ Certificate
value) = Certificate
value

-- | Return original bytes for an imported certificate envelope.
certificateCbor :: EncodedCertificate -> ByteString
certificateCbor :: EncodedCertificate -> ByteString
certificateCbor (EncodedCertificate ByteString
bytes Certificate
_) = ByteString
bytes

-- | Encode ordered entries with set tag 258, retaining repeated certificates.
encodeCertificates :: Certificates -> ByteString
encodeCertificates :: Certificates -> ByteString
encodeCertificates (Certificates [Certificate]
values) = Encoding -> ByteString
Write.toStrictByteString (Word -> Encoding
Encode.encodeTag Word
258 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
Encode.encodeListLen (Int -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral ([Certificate] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Certificate]
values)) Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> (Certificate -> Encoding) -> [Certificate] -> Encoding
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap Certificate -> Encoding
certificateEncoding [Certificate]
values)

-- | Validate an optionally tagged collection and retain its complete bytes.
decodeCertificates :: ByteString -> Either Error EncodedCertificates
decodeCertificates :: ByteString -> Either Error EncodedCertificates
decodeCertificates ByteString
bytes = ByteString -> Certificates -> EncodedCertificates
EncodedCertificates ByteString
bytes (Certificates -> EncodedCertificates)
-> Either Error Certificates -> Either Error EncodedCertificates
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (forall s. Decoder s Certificates)
-> ByteString -> Either Error Certificates
forall a. (forall s. Decoder s a) -> ByteString -> Either Error a
decodeComplete Decoder s Certificates
forall s. Decoder s Certificates
certificatesDecoder ByteString
bytes

-- | Wrap an ordered collection using its fresh definite encoding.
fromCertificates :: Certificates -> EncodedCertificates
fromCertificates :: Certificates -> EncodedCertificates
fromCertificates Certificates
value = ByteString -> Certificates -> EncodedCertificates
EncodedCertificates (Certificates -> ByteString
encodeCertificates Certificates
value) Certificates
value

-- | Inspect the ordered semantic collection.
toCertificates :: EncodedCertificates -> Certificates
toCertificates :: EncodedCertificates -> Certificates
toCertificates (EncodedCertificates ByteString
_ Certificates
value) = Certificates
value

-- | Return the exact imported collection bytes.
certificatesCbor :: EncodedCertificates -> ByteString
certificatesCbor :: EncodedCertificates -> ByteString
certificatesCbor (EncodedCertificates ByteString
bytes Certificates
_) = ByteString
bytes

-- | Observe the outer collection tag without changing its exact envelope.
certificatesSetEncoding :: EncodedCertificates -> SetEncoding
certificatesSetEncoding :: EncodedCertificates -> SetEncoding
certificatesSetEncoding = ByteString -> SetEncoding
SetEncoding.encoding (ByteString -> SetEncoding)
-> (EncodedCertificates -> ByteString)
-> EncodedCertificates
-> SetEncoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. EncodedCertificates -> ByteString
certificatesCbor

-- | Change only the outer tag; retain every array and certificate byte.
withCertificatesSetEncoding :: SetEncoding -> EncodedCertificates -> EncodedCertificates
withCertificatesSetEncoding :: SetEncoding -> EncodedCertificates -> EncodedCertificates
withCertificatesSetEncoding SetEncoding
requested (EncodedCertificates ByteString
bytes Certificates
values) =
  ByteString -> Certificates -> EncodedCertificates
EncodedCertificates (SetEncoding -> ByteString -> ByteString
SetEncoding.withEncoding SetEncoding
requested ByteString
bytes) Certificates
values

certificateEncoding :: Certificate -> Encode.Encoding
certificateEncoding :: Certificate -> Encoding
certificateEncoding Certificate
certificate = case Certificate
certificate of
  StakeRegistration Credential Stake
stake -> Word -> Encoding
Encode.encodeListLen Word
2 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
Encode.encodeWord Word
0 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Credential Stake -> Encoding
forall purpose. Credential purpose -> Encoding
credentialEncoding Credential Stake
stake
  StakeDeregistration Credential Stake
stake -> Word -> Encoding
Encode.encodeListLen Word
2 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
Encode.encodeWord Word
1 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Credential Stake -> Encoding
forall purpose. Credential purpose -> Encoding
credentialEncoding Credential Stake
stake
  StakeDelegation Credential Stake
stake KeyHash StakePool
pool -> Word -> Encoding
Encode.encodeListLen Word
3 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
Encode.encodeWord Word
2 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Credential Stake -> Encoding
forall purpose. Credential purpose -> Encoding
credentialEncoding Credential Stake
stake Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> KeyHash StakePool -> Encoding
forall purpose. KeyHash purpose -> Encoding
keyEncoding KeyHash StakePool
pool
  PoolRegistration PoolParameters
parameters -> Word -> Encoding
Encode.encodeListLen (Word
1 Word -> Word -> Word
forall a. Num a => a -> a -> a
+ PoolParameters -> Word
Pool.parametersFieldCount PoolParameters
parameters) Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
Encode.encodeWord Word
3 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> PoolParameters -> Encoding
Pool.parametersEncoding PoolParameters
parameters
  PoolRetirement KeyHash StakePool
pool Word64
epoch -> Word -> Encoding
Encode.encodeListLen Word
3 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
Encode.encodeWord Word
4 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> KeyHash StakePool -> Encoding
forall purpose. KeyHash purpose -> Encoding
keyEncoding KeyHash StakePool
pool Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word64 -> Encoding
Encode.encodeWord64 Word64
epoch
  GenesisKeyDelegation KeyHash Genesis
genesis KeyHash GenesisDelegate
delegate VrfKeyHash
vrf -> Word -> Encoding
Encode.encodeListLen Word
4 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
Encode.encodeWord Word
5 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> KeyHash Genesis -> Encoding
forall purpose. KeyHash purpose -> Encoding
keyEncoding KeyHash Genesis
genesis Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> KeyHash GenesisDelegate -> Encoding
forall purpose. KeyHash purpose -> Encoding
keyEncoding KeyHash GenesisDelegate
delegate Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> (ByteString -> Encoding
Encode.encodeBytes (ByteString -> Encoding)
-> (VrfKeyHash -> ByteString) -> VrfKeyHash -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. VrfKeyHash -> ByteString
Pool.vrfKeyHashBytes) VrfKeyHash
vrf
  MoveInstantaneousRewards Mir
rewards -> Word -> Encoding
Encode.encodeListLen Word
2 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
Encode.encodeWord Word
6 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Mir -> Encoding
mirEncoding Mir
rewards
  Registration Credential Stake
stake Word64
deposit -> Word -> Encoding
Encode.encodeListLen Word
3 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
Encode.encodeWord Word
7 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Credential Stake -> Encoding
forall purpose. Credential purpose -> Encoding
credentialEncoding Credential Stake
stake Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word64 -> Encoding
Encode.encodeWord64 Word64
deposit
  Unregistration Credential Stake
stake Word64
refund -> Word -> Encoding
Encode.encodeListLen Word
3 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
Encode.encodeWord Word
8 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Credential Stake -> Encoding
forall purpose. Credential purpose -> Encoding
credentialEncoding Credential Stake
stake Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word64 -> Encoding
Encode.encodeWord64 Word64
refund
  VoteDelegation Credential Stake
stake DRep
drep -> Word -> Encoding
Encode.encodeListLen Word
3 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
Encode.encodeWord Word
9 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Credential Stake -> Encoding
forall purpose. Credential purpose -> Encoding
credentialEncoding Credential Stake
stake Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> (ByteString -> Encoding
Encode.encodePreEncoded (ByteString -> Encoding)
-> (DRep -> ByteString) -> DRep -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DRep -> ByteString
DRep.encodeCbor) DRep
drep
  StakeVoteDelegation Credential Stake
stake KeyHash StakePool
pool DRep
drep -> Word -> Encoding
Encode.encodeListLen Word
4 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
Encode.encodeWord Word
10 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Credential Stake -> Encoding
forall purpose. Credential purpose -> Encoding
credentialEncoding Credential Stake
stake Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> KeyHash StakePool -> Encoding
forall purpose. KeyHash purpose -> Encoding
keyEncoding KeyHash StakePool
pool Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> (ByteString -> Encoding
Encode.encodePreEncoded (ByteString -> Encoding)
-> (DRep -> ByteString) -> DRep -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DRep -> ByteString
DRep.encodeCbor) DRep
drep
  StakeRegistrationDelegation Credential Stake
stake KeyHash StakePool
pool Word64
deposit -> Word -> Encoding
Encode.encodeListLen Word
4 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
Encode.encodeWord Word
11 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Credential Stake -> Encoding
forall purpose. Credential purpose -> Encoding
credentialEncoding Credential Stake
stake Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> KeyHash StakePool -> Encoding
forall purpose. KeyHash purpose -> Encoding
keyEncoding KeyHash StakePool
pool Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word64 -> Encoding
Encode.encodeWord64 Word64
deposit
  VoteRegistrationDelegation Credential Stake
stake DRep
drep Word64
deposit -> Word -> Encoding
Encode.encodeListLen Word
4 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
Encode.encodeWord Word
12 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Credential Stake -> Encoding
forall purpose. Credential purpose -> Encoding
credentialEncoding Credential Stake
stake Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> (ByteString -> Encoding
Encode.encodePreEncoded (ByteString -> Encoding)
-> (DRep -> ByteString) -> DRep -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DRep -> ByteString
DRep.encodeCbor) DRep
drep Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word64 -> Encoding
Encode.encodeWord64 Word64
deposit
  StakeVoteRegistrationDelegation Credential Stake
stake KeyHash StakePool
pool DRep
drep Word64
deposit -> Word -> Encoding
Encode.encodeListLen Word
5 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
Encode.encodeWord Word
13 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Credential Stake -> Encoding
forall purpose. Credential purpose -> Encoding
credentialEncoding Credential Stake
stake Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> KeyHash StakePool -> Encoding
forall purpose. KeyHash purpose -> Encoding
keyEncoding KeyHash StakePool
pool Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> (ByteString -> Encoding
Encode.encodePreEncoded (ByteString -> Encoding)
-> (DRep -> ByteString) -> DRep -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DRep -> ByteString
DRep.encodeCbor) DRep
drep Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word64 -> Encoding
Encode.encodeWord64 Word64
deposit
  AuthorizeCommitteeHot Credential CommitteeCold
cold Credential CommitteeHot
hot -> Word -> Encoding
Encode.encodeListLen Word
3 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
Encode.encodeWord Word
14 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Credential CommitteeCold -> Encoding
forall purpose. Credential purpose -> Encoding
credentialEncoding Credential CommitteeCold
cold Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Credential CommitteeHot -> Encoding
forall purpose. Credential purpose -> Encoding
credentialEncoding Credential CommitteeHot
hot
  ResignCommitteeCold Credential CommitteeCold
cold Maybe Anchor
anchor -> Word -> Encoding
Encode.encodeListLen Word
3 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
Encode.encodeWord Word
15 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Credential CommitteeCold -> Encoding
forall purpose. Credential purpose -> Encoding
credentialEncoding Credential CommitteeCold
cold Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Maybe Anchor -> Encoding
anchorEncoding Maybe Anchor
anchor
  RegisterDRep Credential DRep
drep Word64
deposit Maybe Anchor
anchor -> Word -> Encoding
Encode.encodeListLen Word
4 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
Encode.encodeWord Word
16 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Credential DRep -> Encoding
forall purpose. Credential purpose -> Encoding
credentialEncoding Credential DRep
drep Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word64 -> Encoding
Encode.encodeWord64 Word64
deposit Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Maybe Anchor -> Encoding
anchorEncoding Maybe Anchor
anchor
  UnregisterDRep Credential DRep
drep Word64
refund -> Word -> Encoding
Encode.encodeListLen Word
3 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
Encode.encodeWord Word
17 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Credential DRep -> Encoding
forall purpose. Credential purpose -> Encoding
credentialEncoding Credential DRep
drep Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word64 -> Encoding
Encode.encodeWord64 Word64
refund
  UpdateDRep Credential DRep
drep Maybe Anchor
anchor -> Word -> Encoding
Encode.encodeListLen Word
3 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
Encode.encodeWord Word
18 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Credential DRep -> Encoding
forall purpose. Credential purpose -> Encoding
credentialEncoding Credential DRep
drep Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Maybe Anchor -> Encoding
anchorEncoding Maybe Anchor
anchor

credentialEncoding :: Credential.Credential purpose -> Encode.Encoding
credentialEncoding :: forall purpose. Credential purpose -> Encoding
credentialEncoding Credential purpose
credential =
  Word -> Encoding
Encode.encodeListLen Word
2 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> case Credential purpose
credential of
    Credential.KeyCredential KeyHash purpose
hash -> Word -> Encoding
Encode.encodeWord Word
0 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> KeyHash purpose -> Encoding
forall purpose. KeyHash purpose -> Encoding
keyEncoding KeyHash purpose
hash
    Credential.ScriptCredential ScriptHash
hash -> Word -> Encoding
Encode.encodeWord Word
1 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ByteString -> Encoding
Encode.encodeBytes (ScriptHash -> ByteString
Credential.scriptHashBytes ScriptHash
hash)

keyEncoding :: Credential.KeyHash purpose -> Encode.Encoding
keyEncoding :: forall purpose. KeyHash purpose -> Encoding
keyEncoding = ByteString -> Encoding
Encode.encodeBytes (ByteString -> Encoding)
-> (KeyHash purpose -> ByteString) -> KeyHash purpose -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. KeyHash purpose -> ByteString
forall purpose. KeyHash purpose -> ByteString
Credential.keyHashBytes

anchorEncoding :: Maybe Anchor.Anchor -> Encode.Encoding
anchorEncoding :: Maybe Anchor -> Encoding
anchorEncoding = Encoding -> (Anchor -> Encoding) -> Maybe Anchor -> Encoding
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Encoding
Encode.encodeNull (ByteString -> Encoding
Encode.encodePreEncoded (ByteString -> Encoding)
-> (Anchor -> ByteString) -> Anchor -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Anchor -> ByteString
Anchor.encodeCbor)

mirEncoding :: Mir -> Encode.Encoding
mirEncoding :: Mir -> Encoding
mirEncoding Mir
rewards =
  Word -> Encoding
Encode.encodeListLen Word
2 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> case Mir
rewards of
    ToOtherPot Pot
source Word64
amount -> Pot -> Encoding
potEncoding Pot
source Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word64 -> Encoding
Encode.encodeWord64 Word64
amount
    ToStakeCredentials Pot
source Map (Credential Stake) Word64
amounts -> Pot -> Encoding
potEncoding Pot
source Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
Encode.encodeMapLen (Int -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Map (Credential Stake) Word64 -> Int
forall k a. Map k a -> Int
Map.size Map (Credential Stake) Word64
amounts)) Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ((Credential Stake, Word64) -> Encoding)
-> [(Credential Stake, Word64)] -> Encoding
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (\(Credential Stake
credential, Word64
amount) -> Credential Stake -> Encoding
forall purpose. Credential purpose -> Encoding
credentialEncoding Credential Stake
credential Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word64 -> Encoding
Encode.encodeWord64 Word64
amount) (Map (Credential Stake) Word64 -> [(Credential Stake, Word64)]
forall k a. Map k a -> [(k, a)]
Map.toAscList Map (Credential Stake) Word64
amounts)

potEncoding :: Pot -> Encode.Encoding
potEncoding :: Pot -> Encoding
potEncoding Pot
Reserves = Word -> Encoding
Encode.encodeWord Word
0
potEncoding Pot
Treasury = Word -> Encoding
Encode.encodeWord Word
1

certificateDecoder :: Decode.Decoder s Certificate
certificateDecoder :: forall s. Decoder s Certificate
certificateDecoder = do
  Maybe Int
size <- Decoder s (Maybe Int)
forall s. Decoder s (Maybe Int)
Decode.decodeListLenOrIndef
  Word
kind <- Decoder s Word
forall s. Decoder s Word
Decode.decodeWord
  Certificate
value <- case Word
kind of
    Word
0 -> Int -> Maybe Int -> Decoder s ()
forall s. Int -> Maybe Int -> Decoder s ()
checkSize Int
2 Maybe Int
size Decoder s () -> Decoder s Certificate -> Decoder s Certificate
forall a b. Decoder s a -> Decoder s b -> Decoder s b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (Credential Stake -> Certificate
StakeRegistration (Credential Stake -> Certificate)
-> Decoder s (Credential Stake) -> Decoder s Certificate
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s (Credential Stake)
forall s purpose. Decoder s (Credential purpose)
credentialDecoder)
    Word
1 -> Int -> Maybe Int -> Decoder s ()
forall s. Int -> Maybe Int -> Decoder s ()
checkSize Int
2 Maybe Int
size Decoder s () -> Decoder s Certificate -> Decoder s Certificate
forall a b. Decoder s a -> Decoder s b -> Decoder s b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (Credential Stake -> Certificate
StakeDeregistration (Credential Stake -> Certificate)
-> Decoder s (Credential Stake) -> Decoder s Certificate
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s (Credential Stake)
forall s purpose. Decoder s (Credential purpose)
credentialDecoder)
    Word
2 -> Int -> Maybe Int -> Decoder s ()
forall s. Int -> Maybe Int -> Decoder s ()
checkSize Int
3 Maybe Int
size Decoder s () -> Decoder s Certificate -> Decoder s Certificate
forall a b. Decoder s a -> Decoder s b -> Decoder s b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (Credential Stake -> KeyHash StakePool -> Certificate
StakeDelegation (Credential Stake -> KeyHash StakePool -> Certificate)
-> Decoder s (Credential Stake)
-> Decoder s (KeyHash StakePool -> Certificate)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s (Credential Stake)
forall s purpose. Decoder s (Credential purpose)
credentialDecoder Decoder s (KeyHash StakePool -> Certificate)
-> Decoder s (KeyHash StakePool) -> Decoder s Certificate
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s (KeyHash StakePool)
forall s purpose. Decoder s (KeyHash purpose)
keyDecoder)
    Word
3 -> do
      (Word
fields, PoolParameters
parameters) <- Decoder s (Word, PoolParameters)
forall s. Decoder s (Word, PoolParameters)
Pool.parametersDecoderWithFieldCount
      Int -> Maybe Int -> Decoder s ()
forall s. Int -> Maybe Int -> Decoder s ()
checkSize (Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Word -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word
fields) Maybe Int
size
      Certificate -> Decoder s Certificate
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PoolParameters -> Certificate
PoolRegistration PoolParameters
parameters)
    Word
4 -> Int -> Maybe Int -> Decoder s ()
forall s. Int -> Maybe Int -> Decoder s ()
checkSize Int
3 Maybe Int
size Decoder s () -> Decoder s Certificate -> Decoder s Certificate
forall a b. Decoder s a -> Decoder s b -> Decoder s b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (KeyHash StakePool -> Word64 -> Certificate
PoolRetirement (KeyHash StakePool -> Word64 -> Certificate)
-> Decoder s (KeyHash StakePool)
-> Decoder s (Word64 -> Certificate)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s (KeyHash StakePool)
forall s purpose. Decoder s (KeyHash purpose)
keyDecoder Decoder s (Word64 -> Certificate)
-> Decoder s Word64 -> Decoder s Certificate
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s Word64
forall s. Decoder s Word64
Decode.decodeWord64)
    Word
5 -> Int -> Maybe Int -> Decoder s ()
forall s. Int -> Maybe Int -> Decoder s ()
checkSize Int
4 Maybe Int
size Decoder s () -> Decoder s Certificate -> Decoder s Certificate
forall a b. Decoder s a -> Decoder s b -> Decoder s b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (KeyHash Genesis
-> KeyHash GenesisDelegate -> VrfKeyHash -> Certificate
GenesisKeyDelegation (KeyHash Genesis
 -> KeyHash GenesisDelegate -> VrfKeyHash -> Certificate)
-> Decoder s (KeyHash Genesis)
-> Decoder s (KeyHash GenesisDelegate -> VrfKeyHash -> Certificate)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s (KeyHash Genesis)
forall s purpose. Decoder s (KeyHash purpose)
keyDecoder Decoder s (KeyHash GenesisDelegate -> VrfKeyHash -> Certificate)
-> Decoder s (KeyHash GenesisDelegate)
-> Decoder s (VrfKeyHash -> Certificate)
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s (KeyHash GenesisDelegate)
forall s purpose. Decoder s (KeyHash purpose)
keyDecoder Decoder s (VrfKeyHash -> Certificate)
-> Decoder s VrfKeyHash -> Decoder s Certificate
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ((ByteString -> Either Error VrfKeyHash)
-> ByteString -> Decoder s VrfKeyHash
forall a b s. (a -> Either Error b) -> a -> Decoder s b
validated ByteString -> Either Error VrfKeyHash
Pool.vrfKeyHashFromBytes (ByteString -> Decoder s VrfKeyHash)
-> Decoder s ByteString -> Decoder s VrfKeyHash
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Decoder s ByteString
forall s. Decoder s ByteString
Decode.decodeBytes))
    Word
6 -> Int -> Maybe Int -> Decoder s ()
forall s. Int -> Maybe Int -> Decoder s ()
checkSize Int
2 Maybe Int
size Decoder s () -> Decoder s Certificate -> Decoder s Certificate
forall a b. Decoder s a -> Decoder s b -> Decoder s b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (Mir -> Certificate
MoveInstantaneousRewards (Mir -> Certificate) -> Decoder s Mir -> Decoder s Certificate
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s Mir
forall s. Decoder s Mir
mirDecoder)
    Word
7 -> Int -> Maybe Int -> Decoder s ()
forall s. Int -> Maybe Int -> Decoder s ()
checkSize Int
3 Maybe Int
size Decoder s () -> Decoder s Certificate -> Decoder s Certificate
forall a b. Decoder s a -> Decoder s b -> Decoder s b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (Credential Stake -> Word64 -> Certificate
Registration (Credential Stake -> Word64 -> Certificate)
-> Decoder s (Credential Stake)
-> Decoder s (Word64 -> Certificate)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s (Credential Stake)
forall s purpose. Decoder s (Credential purpose)
credentialDecoder Decoder s (Word64 -> Certificate)
-> Decoder s Word64 -> Decoder s Certificate
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s Word64
forall s. Decoder s Word64
Decode.decodeWord64)
    Word
8 -> Int -> Maybe Int -> Decoder s ()
forall s. Int -> Maybe Int -> Decoder s ()
checkSize Int
3 Maybe Int
size Decoder s () -> Decoder s Certificate -> Decoder s Certificate
forall a b. Decoder s a -> Decoder s b -> Decoder s b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (Credential Stake -> Word64 -> Certificate
Unregistration (Credential Stake -> Word64 -> Certificate)
-> Decoder s (Credential Stake)
-> Decoder s (Word64 -> Certificate)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s (Credential Stake)
forall s purpose. Decoder s (Credential purpose)
credentialDecoder Decoder s (Word64 -> Certificate)
-> Decoder s Word64 -> Decoder s Certificate
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s Word64
forall s. Decoder s Word64
Decode.decodeWord64)
    Word
9 -> Int -> Maybe Int -> Decoder s ()
forall s. Int -> Maybe Int -> Decoder s ()
checkSize Int
3 Maybe Int
size Decoder s () -> Decoder s Certificate -> Decoder s Certificate
forall a b. Decoder s a -> Decoder s b -> Decoder s b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (Credential Stake -> DRep -> Certificate
VoteDelegation (Credential Stake -> DRep -> Certificate)
-> Decoder s (Credential Stake) -> Decoder s (DRep -> Certificate)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s (Credential Stake)
forall s purpose. Decoder s (Credential purpose)
credentialDecoder Decoder s (DRep -> Certificate)
-> Decoder s DRep -> Decoder s Certificate
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (ByteString -> Either Error DRep) -> Decoder s DRep
forall a s. (ByteString -> Either Error a) -> Decoder s a
embedded ByteString -> Either Error DRep
DRep.decodeCbor)
    Word
10 -> Int -> Maybe Int -> Decoder s ()
forall s. Int -> Maybe Int -> Decoder s ()
checkSize Int
4 Maybe Int
size Decoder s () -> Decoder s Certificate -> Decoder s Certificate
forall a b. Decoder s a -> Decoder s b -> Decoder s b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (Credential Stake -> KeyHash StakePool -> DRep -> Certificate
StakeVoteDelegation (Credential Stake -> KeyHash StakePool -> DRep -> Certificate)
-> Decoder s (Credential Stake)
-> Decoder s (KeyHash StakePool -> DRep -> Certificate)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s (Credential Stake)
forall s purpose. Decoder s (Credential purpose)
credentialDecoder Decoder s (KeyHash StakePool -> DRep -> Certificate)
-> Decoder s (KeyHash StakePool) -> Decoder s (DRep -> Certificate)
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s (KeyHash StakePool)
forall s purpose. Decoder s (KeyHash purpose)
keyDecoder Decoder s (DRep -> Certificate)
-> Decoder s DRep -> Decoder s Certificate
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (ByteString -> Either Error DRep) -> Decoder s DRep
forall a s. (ByteString -> Either Error a) -> Decoder s a
embedded ByteString -> Either Error DRep
DRep.decodeCbor)
    Word
11 -> Int -> Maybe Int -> Decoder s ()
forall s. Int -> Maybe Int -> Decoder s ()
checkSize Int
4 Maybe Int
size Decoder s () -> Decoder s Certificate -> Decoder s Certificate
forall a b. Decoder s a -> Decoder s b -> Decoder s b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (Credential Stake -> KeyHash StakePool -> Word64 -> Certificate
StakeRegistrationDelegation (Credential Stake -> KeyHash StakePool -> Word64 -> Certificate)
-> Decoder s (Credential Stake)
-> Decoder s (KeyHash StakePool -> Word64 -> Certificate)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s (Credential Stake)
forall s purpose. Decoder s (Credential purpose)
credentialDecoder Decoder s (KeyHash StakePool -> Word64 -> Certificate)
-> Decoder s (KeyHash StakePool)
-> Decoder s (Word64 -> Certificate)
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s (KeyHash StakePool)
forall s purpose. Decoder s (KeyHash purpose)
keyDecoder Decoder s (Word64 -> Certificate)
-> Decoder s Word64 -> Decoder s Certificate
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s Word64
forall s. Decoder s Word64
Decode.decodeWord64)
    Word
12 -> Int -> Maybe Int -> Decoder s ()
forall s. Int -> Maybe Int -> Decoder s ()
checkSize Int
4 Maybe Int
size Decoder s () -> Decoder s Certificate -> Decoder s Certificate
forall a b. Decoder s a -> Decoder s b -> Decoder s b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (Credential Stake -> DRep -> Word64 -> Certificate
VoteRegistrationDelegation (Credential Stake -> DRep -> Word64 -> Certificate)
-> Decoder s (Credential Stake)
-> Decoder s (DRep -> Word64 -> Certificate)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s (Credential Stake)
forall s purpose. Decoder s (Credential purpose)
credentialDecoder Decoder s (DRep -> Word64 -> Certificate)
-> Decoder s DRep -> Decoder s (Word64 -> Certificate)
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (ByteString -> Either Error DRep) -> Decoder s DRep
forall a s. (ByteString -> Either Error a) -> Decoder s a
embedded ByteString -> Either Error DRep
DRep.decodeCbor Decoder s (Word64 -> Certificate)
-> Decoder s Word64 -> Decoder s Certificate
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s Word64
forall s. Decoder s Word64
Decode.decodeWord64)
    Word
13 -> Int -> Maybe Int -> Decoder s ()
forall s. Int -> Maybe Int -> Decoder s ()
checkSize Int
5 Maybe Int
size Decoder s () -> Decoder s Certificate -> Decoder s Certificate
forall a b. Decoder s a -> Decoder s b -> Decoder s b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (Credential Stake
-> KeyHash StakePool -> DRep -> Word64 -> Certificate
StakeVoteRegistrationDelegation (Credential Stake
 -> KeyHash StakePool -> DRep -> Word64 -> Certificate)
-> Decoder s (Credential Stake)
-> Decoder s (KeyHash StakePool -> DRep -> Word64 -> Certificate)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s (Credential Stake)
forall s purpose. Decoder s (Credential purpose)
credentialDecoder Decoder s (KeyHash StakePool -> DRep -> Word64 -> Certificate)
-> Decoder s (KeyHash StakePool)
-> Decoder s (DRep -> Word64 -> Certificate)
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s (KeyHash StakePool)
forall s purpose. Decoder s (KeyHash purpose)
keyDecoder Decoder s (DRep -> Word64 -> Certificate)
-> Decoder s DRep -> Decoder s (Word64 -> Certificate)
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (ByteString -> Either Error DRep) -> Decoder s DRep
forall a s. (ByteString -> Either Error a) -> Decoder s a
embedded ByteString -> Either Error DRep
DRep.decodeCbor Decoder s (Word64 -> Certificate)
-> Decoder s Word64 -> Decoder s Certificate
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s Word64
forall s. Decoder s Word64
Decode.decodeWord64)
    Word
14 -> Int -> Maybe Int -> Decoder s ()
forall s. Int -> Maybe Int -> Decoder s ()
checkSize Int
3 Maybe Int
size Decoder s () -> Decoder s Certificate -> Decoder s Certificate
forall a b. Decoder s a -> Decoder s b -> Decoder s b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (Credential CommitteeCold -> Credential CommitteeHot -> Certificate
AuthorizeCommitteeHot (Credential CommitteeCold
 -> Credential CommitteeHot -> Certificate)
-> Decoder s (Credential CommitteeCold)
-> Decoder s (Credential CommitteeHot -> Certificate)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s (Credential CommitteeCold)
forall s purpose. Decoder s (Credential purpose)
credentialDecoder Decoder s (Credential CommitteeHot -> Certificate)
-> Decoder s (Credential CommitteeHot) -> Decoder s Certificate
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s (Credential CommitteeHot)
forall s purpose. Decoder s (Credential purpose)
credentialDecoder)
    Word
15 -> Int -> Maybe Int -> Decoder s ()
forall s. Int -> Maybe Int -> Decoder s ()
checkSize Int
3 Maybe Int
size Decoder s () -> Decoder s Certificate -> Decoder s Certificate
forall a b. Decoder s a -> Decoder s b -> Decoder s b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (Credential CommitteeCold -> Maybe Anchor -> Certificate
ResignCommitteeCold (Credential CommitteeCold -> Maybe Anchor -> Certificate)
-> Decoder s (Credential CommitteeCold)
-> Decoder s (Maybe Anchor -> Certificate)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s (Credential CommitteeCold)
forall s purpose. Decoder s (Credential purpose)
credentialDecoder Decoder s (Maybe Anchor -> Certificate)
-> Decoder s (Maybe Anchor) -> Decoder s Certificate
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s (Maybe Anchor)
forall s. Decoder s (Maybe Anchor)
anchorDecoder)
    Word
16 -> Int -> Maybe Int -> Decoder s ()
forall s. Int -> Maybe Int -> Decoder s ()
checkSize Int
4 Maybe Int
size Decoder s () -> Decoder s Certificate -> Decoder s Certificate
forall a b. Decoder s a -> Decoder s b -> Decoder s b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (Credential DRep -> Word64 -> Maybe Anchor -> Certificate
RegisterDRep (Credential DRep -> Word64 -> Maybe Anchor -> Certificate)
-> Decoder s (Credential DRep)
-> Decoder s (Word64 -> Maybe Anchor -> Certificate)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s (Credential DRep)
forall s purpose. Decoder s (Credential purpose)
credentialDecoder Decoder s (Word64 -> Maybe Anchor -> Certificate)
-> Decoder s Word64 -> Decoder s (Maybe Anchor -> Certificate)
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s Word64
forall s. Decoder s Word64
Decode.decodeWord64 Decoder s (Maybe Anchor -> Certificate)
-> Decoder s (Maybe Anchor) -> Decoder s Certificate
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s (Maybe Anchor)
forall s. Decoder s (Maybe Anchor)
anchorDecoder)
    Word
17 -> Int -> Maybe Int -> Decoder s ()
forall s. Int -> Maybe Int -> Decoder s ()
checkSize Int
3 Maybe Int
size Decoder s () -> Decoder s Certificate -> Decoder s Certificate
forall a b. Decoder s a -> Decoder s b -> Decoder s b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (Credential DRep -> Word64 -> Certificate
UnregisterDRep (Credential DRep -> Word64 -> Certificate)
-> Decoder s (Credential DRep) -> Decoder s (Word64 -> Certificate)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s (Credential DRep)
forall s purpose. Decoder s (Credential purpose)
credentialDecoder Decoder s (Word64 -> Certificate)
-> Decoder s Word64 -> Decoder s Certificate
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s Word64
forall s. Decoder s Word64
Decode.decodeWord64)
    Word
18 -> Int -> Maybe Int -> Decoder s ()
forall s. Int -> Maybe Int -> Decoder s ()
checkSize Int
3 Maybe Int
size Decoder s () -> Decoder s Certificate -> Decoder s Certificate
forall a b. Decoder s a -> Decoder s b -> Decoder s b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (Credential DRep -> Maybe Anchor -> Certificate
UpdateDRep (Credential DRep -> Maybe Anchor -> Certificate)
-> Decoder s (Credential DRep)
-> Decoder s (Maybe Anchor -> Certificate)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s (Credential DRep)
forall s purpose. Decoder s (Credential purpose)
credentialDecoder Decoder s (Maybe Anchor -> Certificate)
-> Decoder s (Maybe Anchor) -> Decoder s Certificate
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s (Maybe Anchor)
forall s. Decoder s (Maybe Anchor)
anchorDecoder)
    Word
_ -> String -> Decoder s Certificate
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Unknown certificate kind"
  Maybe Int -> Decoder s ()
forall s. Maybe Int -> Decoder s ()
endArray Maybe Int
size
  Certificate -> Decoder s Certificate
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Certificate
value

credentialDecoder :: Decode.Decoder s (Credential.Credential purpose)
credentialDecoder :: forall s purpose. Decoder s (Credential purpose)
credentialDecoder = Int
-> Decoder s (Credential purpose) -> Decoder s (Credential purpose)
forall s a. Int -> Decoder s a -> Decoder s a
arrayOf Int
2 (Decoder s (Credential purpose) -> Decoder s (Credential purpose))
-> Decoder s (Credential purpose) -> Decoder s (Credential purpose)
forall a b. (a -> b) -> a -> b
$ do
  Word
kind <- Decoder s Word
forall s. Decoder s Word
Decode.decodeWord
  case Word
kind of
    Word
0 -> KeyHash purpose -> Credential purpose
forall purpose. KeyHash purpose -> Credential purpose
Credential.KeyCredential (KeyHash purpose -> Credential purpose)
-> Decoder s (KeyHash purpose) -> Decoder s (Credential purpose)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s (KeyHash purpose)
forall s purpose. Decoder s (KeyHash purpose)
keyDecoder
    Word
1 -> ScriptHash -> Credential purpose
forall purpose. ScriptHash -> Credential purpose
Credential.ScriptCredential (ScriptHash -> Credential purpose)
-> Decoder s ScriptHash -> Decoder s (Credential purpose)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((ByteString -> Either Error ScriptHash)
-> ByteString -> Decoder s ScriptHash
forall a b s. (a -> Either Error b) -> a -> Decoder s b
validated ByteString -> Either Error ScriptHash
Credential.scriptHashFromBytes (ByteString -> Decoder s ScriptHash)
-> Decoder s ByteString -> Decoder s ScriptHash
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Decoder s ByteString
forall s. Decoder s ByteString
Decode.decodeBytes)
    Word
_ -> String -> Decoder s (Credential purpose)
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Unknown credential kind"

keyDecoder :: Decode.Decoder s (Credential.KeyHash purpose)
keyDecoder :: forall s purpose. Decoder s (KeyHash purpose)
keyDecoder = (ByteString -> Either Error (KeyHash purpose))
-> ByteString -> Decoder s (KeyHash purpose)
forall a b s. (a -> Either Error b) -> a -> Decoder s b
validated ByteString -> Either Error (KeyHash purpose)
forall purpose. ByteString -> Either Error (KeyHash purpose)
Credential.keyHashFromBytes (ByteString -> Decoder s (KeyHash purpose))
-> Decoder s ByteString -> Decoder s (KeyHash purpose)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Decoder s ByteString
forall s. Decoder s ByteString
Decode.decodeBytes

anchorDecoder :: Decode.Decoder s (Maybe Anchor.Anchor)
anchorDecoder :: forall s. Decoder s (Maybe Anchor)
anchorDecoder = do
  TokenType
token <- Decoder s TokenType
forall s. Decoder s TokenType
Decode.peekTokenType
  if TokenType
token TokenType -> TokenType -> Bool
forall a. Eq a => a -> a -> Bool
== TokenType
Decode.TypeNull then Decoder s ()
forall s. Decoder s ()
Decode.decodeNull Decoder s ()
-> Decoder s (Maybe Anchor) -> Decoder s (Maybe Anchor)
forall a b. Decoder s a -> Decoder s b -> Decoder s b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Maybe Anchor -> Decoder s (Maybe Anchor)
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe Anchor
forall a. Maybe a
Nothing else Anchor -> Maybe Anchor
forall a. a -> Maybe a
Just (Anchor -> Maybe Anchor)
-> Decoder s Anchor -> Decoder s (Maybe Anchor)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (ByteString -> Either Error Anchor) -> Decoder s Anchor
forall a s. (ByteString -> Either Error a) -> Decoder s a
embedded ByteString -> Either Error Anchor
Anchor.decodeCbor

mirDecoder :: Decode.Decoder s Mir
mirDecoder :: forall s. Decoder s Mir
mirDecoder = Int -> Decoder s Mir -> Decoder s Mir
forall s a. Int -> Decoder s a -> Decoder s a
arrayOf Int
2 (Decoder s Mir -> Decoder s Mir) -> Decoder s Mir -> Decoder s Mir
forall a b. (a -> b) -> a -> b
$ do
  Word
code <- Decoder s Word
forall s. Decoder s Word
Decode.decodeWord
  Pot
source <- case Word
code of
    Word
0 -> Pot -> Decoder s Pot
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Pot
Reserves
    Word
1 -> Pot -> Decoder s Pot
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Pot
Treasury
    Word
_ -> String -> Decoder s Pot
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Unknown rewards pot"
  TokenType
token <- Decoder s TokenType
forall s. Decoder s TokenType
Decode.peekTokenType
  if TokenType
token TokenType -> TokenType -> Bool
forall a. Eq a => a -> a -> Bool
== TokenType
Decode.TypeMapLen Bool -> Bool -> Bool
|| TokenType
token TokenType -> TokenType -> Bool
forall a. Eq a => a -> a -> Bool
== TokenType
Decode.TypeMapLenIndef
    then Pot -> Map (Credential Stake) Word64 -> Mir
ToStakeCredentials Pot
source (Map (Credential Stake) Word64 -> Mir)
-> Decoder s (Map (Credential Stake) Word64) -> Decoder s Mir
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s (Map (Credential Stake) Word64)
forall s. Decoder s (Map (Credential Stake) Word64)
rewardsDecoder
    else Pot -> Word64 -> Mir
ToOtherPot Pot
source (Word64 -> Mir) -> Decoder s Word64 -> Decoder s Mir
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s Word64
forall s. Decoder s Word64
Decode.decodeWord64

rewardsDecoder :: Decode.Decoder s (Map (Credential.Credential Credential.Stake) Word64)
rewardsDecoder :: forall s. Decoder s (Map (Credential Stake) Word64)
rewardsDecoder = do
  Maybe Int
size <- Decoder s (Maybe Int)
forall s. Decoder s (Maybe Int)
Decode.decodeMapLenOrIndef
  [(Credential Stake, Word64)]
entries <- Maybe Int
-> Decoder s (Credential Stake, Word64)
-> Decoder s [(Credential Stake, Word64)]
forall s a. Maybe Int -> Decoder s a -> Decoder s [a]
sequenceEntries Maybe Int
size ((,) (Credential Stake -> Word64 -> (Credential Stake, Word64))
-> Decoder s (Credential Stake)
-> Decoder s (Word64 -> (Credential Stake, Word64))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s (Credential Stake)
forall s purpose. Decoder s (Credential purpose)
credentialDecoder Decoder s (Word64 -> (Credential Stake, Word64))
-> Decoder s Word64 -> Decoder s (Credential Stake, Word64)
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s Word64
forall s. Decoder s Word64
Decode.decodeWord64)
  (Map (Credential Stake) Word64
 -> (Credential Stake, Word64)
 -> Decoder s (Map (Credential Stake) Word64))
-> Map (Credential Stake) Word64
-> [(Credential Stake, Word64)]
-> Decoder s (Map (Credential Stake) Word64)
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM Map (Credential Stake) Word64
-> (Credential Stake, Word64)
-> Decoder s (Map (Credential Stake) Word64)
forall {k} {m :: * -> *} {a}.
(Ord k, MonadFail m) =>
Map k a -> (k, a) -> m (Map k a)
insertReward Map (Credential Stake) Word64
forall k a. Map k a
Map.empty [(Credential Stake, Word64)]
entries
  where
    insertReward :: Map k a -> (k, a) -> m (Map k a)
insertReward Map k a
amounts (k
credential, a
amount)
      | k -> Map k a -> Bool
forall k a. Ord k => k -> Map k a -> Bool
Map.member k
credential Map k a
amounts = String -> m (Map k a)
forall a. String -> m a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Duplicate rewards credential"
      | Bool
otherwise = Map k a -> m (Map k a)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (k -> a -> Map k a -> Map k a
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert k
credential a
amount Map k a
amounts)

certificatesDecoder :: Decode.Decoder s Certificates
certificatesDecoder :: forall s. Decoder s Certificates
certificatesDecoder = do
  TokenType
token <- Decoder s TokenType
forall s. Decoder s TokenType
Decode.peekTokenType
  Bool -> Decoder s () -> Decoder s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (TokenType
token TokenType -> TokenType -> Bool
forall a. Eq a => a -> a -> Bool
== TokenType
Decode.TypeTag Bool -> Bool -> Bool
|| TokenType
token TokenType -> TokenType -> Bool
forall a. Eq a => a -> a -> Bool
== TokenType
Decode.TypeTag64) (Decoder s () -> Decoder s ()) -> Decoder s () -> Decoder s ()
forall a b. (a -> b) -> a -> b
$ do
    Word64
tag <- Decoder s Word64
forall s. Decoder s Word64
Decode.decodeTag64
    Bool -> Decoder s () -> Decoder s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Word64
tag Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64
258) (String -> Decoder s ()
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Expected certificate set tag 258")
  Maybe Int
size <- Decoder s (Maybe Int)
forall s. Decoder s (Maybe Int)
Decode.decodeListLenOrIndef
  [Certificate] -> Certificates
Certificates ([Certificate] -> Certificates)
-> Decoder s [Certificate] -> Decoder s Certificates
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Int -> Decoder s Certificate -> Decoder s [Certificate]
forall s a. Maybe Int -> Decoder s a -> Decoder s [a]
sequenceEntries Maybe Int
size Decoder s Certificate
forall s. Decoder s Certificate
certificateDecoder

sequenceEntries :: Maybe Int -> Decode.Decoder s a -> Decode.Decoder s [a]
sequenceEntries :: forall s a. Maybe Int -> Decoder s a -> Decoder s [a]
sequenceEntries Maybe Int
size Decoder s a
entry = case Maybe Int
size of
  Just Int
count -> Int -> Decoder s a -> Decoder s [a]
forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM Int
count Decoder s a
entry
  Maybe Int
Nothing -> Decoder s [a]
go
  where
    go :: Decoder s [a]
go = do
      Bool
ended <- Decoder s Bool
forall s. Decoder s Bool
Decode.decodeBreakOr
      if Bool
ended then [a] -> Decoder s [a]
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure [] else (:) (a -> [a] -> [a]) -> Decoder s a -> Decoder s ([a] -> [a])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s a
entry Decoder s ([a] -> [a]) -> Decoder s [a] -> Decoder s [a]
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s [a]
go

embedded :: (ByteString -> Either Error a) -> Decode.Decoder s a
embedded :: forall a s. (ByteString -> Either Error a) -> Decoder s a
embedded ByteString -> Either Error a
decode = do
  Term
term <- Decoder s Term
forall s. Decoder s Term
Term.decodeTerm
  (ByteString -> Either Error a) -> ByteString -> Decoder s a
forall a b s. (a -> Either Error b) -> a -> Decoder s b
validated ByteString -> Either Error a
decode (Encoding -> ByteString
Write.toStrictByteString (Term -> Encoding
Term.encodeTerm Term
term))

arrayOf :: Int -> Decode.Decoder s a -> Decode.Decoder s a
arrayOf :: forall s a. Int -> Decoder s a -> Decoder s a
arrayOf Int
expected Decoder s a
entry = do
  Maybe Int
size <- Decoder s (Maybe Int)
forall s. Decoder s (Maybe Int)
Decode.decodeListLenOrIndef
  Int -> Maybe Int -> Decoder s ()
forall s. Int -> Maybe Int -> Decoder s ()
checkSize Int
expected Maybe Int
size
  a
value <- Decoder s a
entry
  Maybe Int -> Decoder s ()
forall s. Maybe Int -> Decoder s ()
endArray Maybe Int
size
  a -> Decoder s a
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
value

checkSize :: Int -> Maybe Int -> Decode.Decoder s ()
checkSize :: forall s. Int -> Maybe Int -> Decoder s ()
checkSize Int
expected Maybe Int
size = case Maybe Int
size of
  Just Int
actual -> Bool -> Decoder s () -> Decoder s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Int
actual Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
expected) (String -> Decoder s ()
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Incorrect certificate array length")
  Maybe Int
Nothing -> () -> Decoder s ()
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()

endArray :: Maybe Int -> Decode.Decoder s ()
endArray :: forall s. Maybe Int -> Decoder s ()
endArray Maybe Int
size = case Maybe Int
size of
  Just Int
_ -> () -> Decoder s ()
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
  Maybe Int
Nothing -> do
    Bool
ended <- Decoder s Bool
forall s. Decoder s Bool
Decode.decodeBreakOr
    Bool -> Decoder s () -> Decoder s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
ended (String -> Decoder s ()
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Extra certificate array elements")

validated :: (a -> Either Error b) -> a -> Decode.Decoder s b
validated :: forall a b s. (a -> Either Error b) -> a -> Decoder s b
validated a -> Either Error b
validate a
value = (Error -> Decoder s b)
-> (b -> Decoder s b) -> Either Error b -> Decoder s b
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> Decoder s b
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Decoder s b)
-> (Error -> String) -> Error -> Decoder s b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Error -> String
forall a. Show a => a -> String
show) b -> Decoder s b
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> Either Error b
validate a
value)

decodeComplete :: (forall s. Decode.Decoder s a) -> ByteString -> Either Error a
decodeComplete :: forall a. (forall s. Decoder s a) -> ByteString -> Either Error a
decodeComplete forall s. Decoder s a
decoder ByteString
bytes = case (forall s. Decoder s a)
-> ByteString -> Either DeserialiseFailure (ByteString, a)
forall a.
(forall s. Decoder s a)
-> ByteString -> Either DeserialiseFailure (ByteString, a)
Read.deserialiseFromBytes Decoder s a
forall s. Decoder s a
decoder (ByteString -> ByteString
Lazy.fromStrict ByteString
bytes) of
  Left DeserialiseFailure
failure -> Error -> Either Error a
forall a b. a -> Either a b
Left (Text -> Error
InvalidCertificateCbor (String -> Text
Text.pack (DeserialiseFailure -> String
forall a. Show a => a -> String
show DeserialiseFailure
failure)))
  Right (ByteString
remaining, a
value)
    | ByteString -> Bool
Lazy.null ByteString
remaining -> a -> Either Error a
forall a b. b -> Either a b
Right a
value
    | Bool
otherwise -> Error -> Either Error a
forall a b. a -> Either a b
Left (Text -> Error
InvalidCertificateCbor Text
"Trailing bytes after certificate value")