{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PatternSynonyms #-}

module Shelley.Spec.Ledger.Credential
  ( Credential (KeyHashObj, ScriptHashObj),
    GenesisCredential (..),
    Ix,
    PaymentCredential,
    Ptr (..),
    StakeCredential,
    StakeReference (..),
  )
where

import Cardano.Binary (FromCBOR (..), ToCBOR (..), encodeListLen)
import qualified Cardano.Ledger.Crypto as CC (Crypto)
import Control.DeepSeq (NFData)
import Data.Aeson (FromJSON (..), FromJSONKey, ToJSON (..), ToJSONKey, (.:), (.=))
import qualified Data.Aeson as Aeson
import Data.Foldable (asum)
import Data.Typeable (Typeable)
import Data.Word (Word8)
import GHC.Generics (Generic)
import NoThunks.Class (NoThunks (..))
import Numeric.Natural (Natural)
import Quiet
import Shelley.Spec.Ledger.BaseTypes (invalidKey)
import Shelley.Spec.Ledger.Keys
  ( HasKeyRole (..),
    KeyHash,
    KeyRole (..),
  )
import Shelley.Spec.Ledger.Orphans ()
import Shelley.Spec.Ledger.Scripts (ScriptHash)
import Shelley.Spec.Ledger.Serialization
  ( CBORGroup (..),
    FromCBORGroup (..),
    ToCBORGroup (..),
    decodeRecordSum,
  )
import Shelley.Spec.Ledger.Slot (SlotNo (..))

-- | Script hash or key hash for a payment or a staking object.
--
-- Note that credentials (unlike raw key hashes) do appear to vary from era to
-- era, since they reference the hash of a script, which can change. This
-- parameter is a phantom, however, so in actuality the instances will remain
-- the same.
data Credential (kr :: KeyRole) crypto
  = ScriptHashObj {-# UNPACK #-} !(ScriptHash crypto)
  | KeyHashObj {-# UNPACK #-} !(KeyHash kr crypto)
  deriving (Int -> Credential kr crypto -> ShowS
[Credential kr crypto] -> ShowS
Credential kr crypto -> String
(Int -> Credential kr crypto -> ShowS)
-> (Credential kr crypto -> String)
-> ([Credential kr crypto] -> ShowS)
-> Show (Credential kr crypto)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall (kr :: KeyRole) crypto. Int -> Credential kr crypto -> ShowS
forall (kr :: KeyRole) crypto. [Credential kr crypto] -> ShowS
forall (kr :: KeyRole) crypto. Credential kr crypto -> String
showList :: [Credential kr crypto] -> ShowS
$cshowList :: forall (kr :: KeyRole) crypto. [Credential kr crypto] -> ShowS
show :: Credential kr crypto -> String
$cshow :: forall (kr :: KeyRole) crypto. Credential kr crypto -> String
showsPrec :: Int -> Credential kr crypto -> ShowS
$cshowsPrec :: forall (kr :: KeyRole) crypto. Int -> Credential kr crypto -> ShowS
Show, Credential kr crypto -> Credential kr crypto -> Bool
(Credential kr crypto -> Credential kr crypto -> Bool)
-> (Credential kr crypto -> Credential kr crypto -> Bool)
-> Eq (Credential kr crypto)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall (kr :: KeyRole) crypto.
Credential kr crypto -> Credential kr crypto -> Bool
/= :: Credential kr crypto -> Credential kr crypto -> Bool
$c/= :: forall (kr :: KeyRole) crypto.
Credential kr crypto -> Credential kr crypto -> Bool
== :: Credential kr crypto -> Credential kr crypto -> Bool
$c== :: forall (kr :: KeyRole) crypto.
Credential kr crypto -> Credential kr crypto -> Bool
Eq, (forall x. Credential kr crypto -> Rep (Credential kr crypto) x)
-> (forall x. Rep (Credential kr crypto) x -> Credential kr crypto)
-> Generic (Credential kr crypto)
forall x. Rep (Credential kr crypto) x -> Credential kr crypto
forall x. Credential kr crypto -> Rep (Credential kr crypto) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (kr :: KeyRole) crypto x.
Rep (Credential kr crypto) x -> Credential kr crypto
forall (kr :: KeyRole) crypto x.
Credential kr crypto -> Rep (Credential kr crypto) x
$cto :: forall (kr :: KeyRole) crypto x.
Rep (Credential kr crypto) x -> Credential kr crypto
$cfrom :: forall (kr :: KeyRole) crypto x.
Credential kr crypto -> Rep (Credential kr crypto) x
Generic, Credential kr crypto -> ()
(Credential kr crypto -> ()) -> NFData (Credential kr crypto)
forall a. (a -> ()) -> NFData a
forall (kr :: KeyRole) crypto. Credential kr crypto -> ()
rnf :: Credential kr crypto -> ()
$crnf :: forall (kr :: KeyRole) crypto. Credential kr crypto -> ()
NFData, Eq (Credential kr crypto)
Eq (Credential kr crypto)
-> (Credential kr crypto -> Credential kr crypto -> Ordering)
-> (Credential kr crypto -> Credential kr crypto -> Bool)
-> (Credential kr crypto -> Credential kr crypto -> Bool)
-> (Credential kr crypto -> Credential kr crypto -> Bool)
-> (Credential kr crypto -> Credential kr crypto -> Bool)
-> (Credential kr crypto
    -> Credential kr crypto -> Credential kr crypto)
-> (Credential kr crypto
    -> Credential kr crypto -> Credential kr crypto)
-> Ord (Credential kr crypto)
Credential kr crypto -> Credential kr crypto -> Bool
Credential kr crypto -> Credential kr crypto -> Ordering
Credential kr crypto
-> Credential kr crypto -> Credential kr crypto
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
forall (kr :: KeyRole) crypto. Eq (Credential kr crypto)
forall (kr :: KeyRole) crypto.
Credential kr crypto -> Credential kr crypto -> Bool
forall (kr :: KeyRole) crypto.
Credential kr crypto -> Credential kr crypto -> Ordering
forall (kr :: KeyRole) crypto.
Credential kr crypto
-> Credential kr crypto -> Credential kr crypto
min :: Credential kr crypto
-> Credential kr crypto -> Credential kr crypto
$cmin :: forall (kr :: KeyRole) crypto.
Credential kr crypto
-> Credential kr crypto -> Credential kr crypto
max :: Credential kr crypto
-> Credential kr crypto -> Credential kr crypto
$cmax :: forall (kr :: KeyRole) crypto.
Credential kr crypto
-> Credential kr crypto -> Credential kr crypto
>= :: Credential kr crypto -> Credential kr crypto -> Bool
$c>= :: forall (kr :: KeyRole) crypto.
Credential kr crypto -> Credential kr crypto -> Bool
> :: Credential kr crypto -> Credential kr crypto -> Bool
$c> :: forall (kr :: KeyRole) crypto.
Credential kr crypto -> Credential kr crypto -> Bool
<= :: Credential kr crypto -> Credential kr crypto -> Bool
$c<= :: forall (kr :: KeyRole) crypto.
Credential kr crypto -> Credential kr crypto -> Bool
< :: Credential kr crypto -> Credential kr crypto -> Bool
$c< :: forall (kr :: KeyRole) crypto.
Credential kr crypto -> Credential kr crypto -> Bool
compare :: Credential kr crypto -> Credential kr crypto -> Ordering
$ccompare :: forall (kr :: KeyRole) crypto.
Credential kr crypto -> Credential kr crypto -> Ordering
$cp1Ord :: forall (kr :: KeyRole) crypto. Eq (Credential kr crypto)
Ord)

instance HasKeyRole Credential where
  coerceKeyRole :: Credential r crypto -> Credential r' crypto
coerceKeyRole (ScriptHashObj ScriptHash crypto
x) = ScriptHash crypto -> Credential r' crypto
forall (kr :: KeyRole) crypto.
ScriptHash crypto -> Credential kr crypto
ScriptHashObj ScriptHash crypto
x
  coerceKeyRole (KeyHashObj KeyHash r crypto
x) = KeyHash r' crypto -> Credential r' crypto
forall (kr :: KeyRole) crypto.
KeyHash kr crypto -> Credential kr crypto
KeyHashObj (KeyHash r' crypto -> Credential r' crypto)
-> KeyHash r' crypto -> Credential r' crypto
forall a b. (a -> b) -> a -> b
$ KeyHash r crypto -> KeyHash r' crypto
forall (a :: KeyRole -> * -> *) (r :: KeyRole) crypto
       (r' :: KeyRole).
HasKeyRole a =>
a r crypto -> a r' crypto
coerceKeyRole KeyHash r crypto
x

instance NoThunks (Credential kr crypto)

instance ToJSON (Credential kr crypto) where
  toJSON :: Credential kr crypto -> Value
toJSON (ScriptHashObj ScriptHash crypto
hash) =
    [Pair] -> Value
Aeson.object
      [ Text
"script hash" Text -> ScriptHash crypto -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= ScriptHash crypto
hash
      ]
  toJSON (KeyHashObj KeyHash kr crypto
hash) =
    [Pair] -> Value
Aeson.object
      [ Text
"key hash" Text -> KeyHash kr crypto -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= KeyHash kr crypto
hash
      ]

instance CC.Crypto crypto => FromJSON (Credential kr crypto) where
  parseJSON :: Value -> Parser (Credential kr crypto)
parseJSON =
    String
-> (Object -> Parser (Credential kr crypto))
-> Value
-> Parser (Credential kr crypto)
forall a. String -> (Object -> Parser a) -> Value -> Parser a
Aeson.withObject String
"Credential" ((Object -> Parser (Credential kr crypto))
 -> Value -> Parser (Credential kr crypto))
-> (Object -> Parser (Credential kr crypto))
-> Value
-> Parser (Credential kr crypto)
forall a b. (a -> b) -> a -> b
$ \Object
obj ->
      [Parser (Credential kr crypto)] -> Parser (Credential kr crypto)
forall (t :: * -> *) (f :: * -> *) a.
(Foldable t, Alternative f) =>
t (f a) -> f a
asum [Object -> Parser (Credential kr crypto)
forall crypto (kr :: KeyRole).
Crypto crypto =>
Object -> Parser (Credential kr crypto)
parser1 Object
obj, Object -> Parser (Credential kr crypto)
forall crypto (kr :: KeyRole).
Crypto crypto =>
Object -> Parser (Credential kr crypto)
parser2 Object
obj]
    where
      parser1 :: Object -> Parser (Credential kr crypto)
parser1 Object
obj = ScriptHash crypto -> Credential kr crypto
forall (kr :: KeyRole) crypto.
ScriptHash crypto -> Credential kr crypto
ScriptHashObj (ScriptHash crypto -> Credential kr crypto)
-> Parser (ScriptHash crypto) -> Parser (Credential kr crypto)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
obj Object -> Text -> Parser (ScriptHash crypto)
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"script hash"
      parser2 :: Object -> Parser (Credential kr crypto)
parser2 Object
obj = KeyHash kr crypto -> Credential kr crypto
forall (kr :: KeyRole) crypto.
KeyHash kr crypto -> Credential kr crypto
KeyHashObj (KeyHash kr crypto -> Credential kr crypto)
-> Parser (KeyHash kr crypto) -> Parser (Credential kr crypto)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
obj Object -> Text -> Parser (KeyHash kr crypto)
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"key hash"

instance ToJSONKey (Credential kr crypto)

instance CC.Crypto crypto => FromJSONKey (Credential kr crypto)

type PaymentCredential crypto = Credential 'Payment crypto

type StakeCredential crypto = Credential 'Staking crypto

data StakeReference crypto
  = StakeRefBase !(StakeCredential crypto)
  | StakeRefPtr !Ptr
  | StakeRefNull
  deriving (Int -> StakeReference crypto -> ShowS
[StakeReference crypto] -> ShowS
StakeReference crypto -> String
(Int -> StakeReference crypto -> ShowS)
-> (StakeReference crypto -> String)
-> ([StakeReference crypto] -> ShowS)
-> Show (StakeReference crypto)
forall crypto. Int -> StakeReference crypto -> ShowS
forall crypto. [StakeReference crypto] -> ShowS
forall crypto. StakeReference crypto -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [StakeReference crypto] -> ShowS
$cshowList :: forall crypto. [StakeReference crypto] -> ShowS
show :: StakeReference crypto -> String
$cshow :: forall crypto. StakeReference crypto -> String
showsPrec :: Int -> StakeReference crypto -> ShowS
$cshowsPrec :: forall crypto. Int -> StakeReference crypto -> ShowS
Show, StakeReference crypto -> StakeReference crypto -> Bool
(StakeReference crypto -> StakeReference crypto -> Bool)
-> (StakeReference crypto -> StakeReference crypto -> Bool)
-> Eq (StakeReference crypto)
forall crypto.
StakeReference crypto -> StakeReference crypto -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: StakeReference crypto -> StakeReference crypto -> Bool
$c/= :: forall crypto.
StakeReference crypto -> StakeReference crypto -> Bool
== :: StakeReference crypto -> StakeReference crypto -> Bool
$c== :: forall crypto.
StakeReference crypto -> StakeReference crypto -> Bool
Eq, (forall x. StakeReference crypto -> Rep (StakeReference crypto) x)
-> (forall x.
    Rep (StakeReference crypto) x -> StakeReference crypto)
-> Generic (StakeReference crypto)
forall x. Rep (StakeReference crypto) x -> StakeReference crypto
forall x. StakeReference crypto -> Rep (StakeReference crypto) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall crypto x.
Rep (StakeReference crypto) x -> StakeReference crypto
forall crypto x.
StakeReference crypto -> Rep (StakeReference crypto) x
$cto :: forall crypto x.
Rep (StakeReference crypto) x -> StakeReference crypto
$cfrom :: forall crypto x.
StakeReference crypto -> Rep (StakeReference crypto) x
Generic, StakeReference crypto -> ()
(StakeReference crypto -> ()) -> NFData (StakeReference crypto)
forall crypto. StakeReference crypto -> ()
forall a. (a -> ()) -> NFData a
rnf :: StakeReference crypto -> ()
$crnf :: forall crypto. StakeReference crypto -> ()
NFData, Eq (StakeReference crypto)
Eq (StakeReference crypto)
-> (StakeReference crypto -> StakeReference crypto -> Ordering)
-> (StakeReference crypto -> StakeReference crypto -> Bool)
-> (StakeReference crypto -> StakeReference crypto -> Bool)
-> (StakeReference crypto -> StakeReference crypto -> Bool)
-> (StakeReference crypto -> StakeReference crypto -> Bool)
-> (StakeReference crypto
    -> StakeReference crypto -> StakeReference crypto)
-> (StakeReference crypto
    -> StakeReference crypto -> StakeReference crypto)
-> Ord (StakeReference crypto)
StakeReference crypto -> StakeReference crypto -> Bool
StakeReference crypto -> StakeReference crypto -> Ordering
StakeReference crypto
-> StakeReference crypto -> StakeReference crypto
forall crypto. Eq (StakeReference crypto)
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
forall crypto.
StakeReference crypto -> StakeReference crypto -> Bool
forall crypto.
StakeReference crypto -> StakeReference crypto -> Ordering
forall crypto.
StakeReference crypto
-> StakeReference crypto -> StakeReference crypto
min :: StakeReference crypto
-> StakeReference crypto -> StakeReference crypto
$cmin :: forall crypto.
StakeReference crypto
-> StakeReference crypto -> StakeReference crypto
max :: StakeReference crypto
-> StakeReference crypto -> StakeReference crypto
$cmax :: forall crypto.
StakeReference crypto
-> StakeReference crypto -> StakeReference crypto
>= :: StakeReference crypto -> StakeReference crypto -> Bool
$c>= :: forall crypto.
StakeReference crypto -> StakeReference crypto -> Bool
> :: StakeReference crypto -> StakeReference crypto -> Bool
$c> :: forall crypto.
StakeReference crypto -> StakeReference crypto -> Bool
<= :: StakeReference crypto -> StakeReference crypto -> Bool
$c<= :: forall crypto.
StakeReference crypto -> StakeReference crypto -> Bool
< :: StakeReference crypto -> StakeReference crypto -> Bool
$c< :: forall crypto.
StakeReference crypto -> StakeReference crypto -> Bool
compare :: StakeReference crypto -> StakeReference crypto -> Ordering
$ccompare :: forall crypto.
StakeReference crypto -> StakeReference crypto -> Ordering
$cp1Ord :: forall crypto. Eq (StakeReference crypto)
Ord)

instance NoThunks (StakeReference crypto)

type Ix = Natural

-- | Pointer to a slot, transaction index and index in certificate list.
data Ptr
  = Ptr !SlotNo !Ix !Ix
  deriving (Int -> Ptr -> ShowS
[Ptr] -> ShowS
Ptr -> String
(Int -> Ptr -> ShowS)
-> (Ptr -> String) -> ([Ptr] -> ShowS) -> Show Ptr
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Ptr] -> ShowS
$cshowList :: [Ptr] -> ShowS
show :: Ptr -> String
$cshow :: Ptr -> String
showsPrec :: Int -> Ptr -> ShowS
$cshowsPrec :: Int -> Ptr -> ShowS
Show, Ptr -> Ptr -> Bool
(Ptr -> Ptr -> Bool) -> (Ptr -> Ptr -> Bool) -> Eq Ptr
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Ptr -> Ptr -> Bool
$c/= :: Ptr -> Ptr -> Bool
== :: Ptr -> Ptr -> Bool
$c== :: Ptr -> Ptr -> Bool
Eq, Eq Ptr
Eq Ptr
-> (Ptr -> Ptr -> Ordering)
-> (Ptr -> Ptr -> Bool)
-> (Ptr -> Ptr -> Bool)
-> (Ptr -> Ptr -> Bool)
-> (Ptr -> Ptr -> Bool)
-> (Ptr -> Ptr -> Ptr)
-> (Ptr -> Ptr -> Ptr)
-> Ord Ptr
Ptr -> Ptr -> Bool
Ptr -> Ptr -> Ordering
Ptr -> Ptr -> Ptr
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
min :: Ptr -> Ptr -> Ptr
$cmin :: Ptr -> Ptr -> Ptr
max :: Ptr -> Ptr -> Ptr
$cmax :: Ptr -> Ptr -> Ptr
>= :: Ptr -> Ptr -> Bool
$c>= :: Ptr -> Ptr -> Bool
> :: Ptr -> Ptr -> Bool
$c> :: Ptr -> Ptr -> Bool
<= :: Ptr -> Ptr -> Bool
$c<= :: Ptr -> Ptr -> Bool
< :: Ptr -> Ptr -> Bool
$c< :: Ptr -> Ptr -> Bool
compare :: Ptr -> Ptr -> Ordering
$ccompare :: Ptr -> Ptr -> Ordering
$cp1Ord :: Eq Ptr
Ord, (forall x. Ptr -> Rep Ptr x)
-> (forall x. Rep Ptr x -> Ptr) -> Generic Ptr
forall x. Rep Ptr x -> Ptr
forall x. Ptr -> Rep Ptr x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Ptr x -> Ptr
$cfrom :: forall x. Ptr -> Rep Ptr x
Generic, Ptr -> ()
(Ptr -> ()) -> NFData Ptr
forall a. (a -> ()) -> NFData a
rnf :: Ptr -> ()
$crnf :: Ptr -> ()
NFData, Context -> Ptr -> IO (Maybe ThunkInfo)
Proxy Ptr -> String
(Context -> Ptr -> IO (Maybe ThunkInfo))
-> (Context -> Ptr -> IO (Maybe ThunkInfo))
-> (Proxy Ptr -> String)
-> NoThunks Ptr
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> String)
-> NoThunks a
showTypeOf :: Proxy Ptr -> String
$cshowTypeOf :: Proxy Ptr -> String
wNoThunks :: Context -> Ptr -> IO (Maybe ThunkInfo)
$cwNoThunks :: Context -> Ptr -> IO (Maybe ThunkInfo)
noThunks :: Context -> Ptr -> IO (Maybe ThunkInfo)
$cnoThunks :: Context -> Ptr -> IO (Maybe ThunkInfo)
NoThunks)
  deriving (Typeable Ptr
Typeable Ptr
-> (Ptr -> Encoding)
-> ((forall t. ToCBOR t => Proxy t -> Size) -> Proxy Ptr -> Size)
-> ((forall t. ToCBOR t => Proxy t -> Size) -> Proxy [Ptr] -> Size)
-> ToCBOR Ptr
Ptr -> Encoding
(forall t. ToCBOR t => Proxy t -> Size) -> Proxy [Ptr] -> Size
(forall t. ToCBOR t => Proxy t -> Size) -> Proxy Ptr -> Size
forall a.
Typeable a
-> (a -> Encoding)
-> ((forall t. ToCBOR t => Proxy t -> Size) -> Proxy a -> Size)
-> ((forall t. ToCBOR t => Proxy t -> Size) -> Proxy [a] -> Size)
-> ToCBOR a
encodedListSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size) -> Proxy [Ptr] -> Size
$cencodedListSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size) -> Proxy [Ptr] -> Size
encodedSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size) -> Proxy Ptr -> Size
$cencodedSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size) -> Proxy Ptr -> Size
toCBOR :: Ptr -> Encoding
$ctoCBOR :: Ptr -> Encoding
$cp1ToCBOR :: Typeable Ptr
ToCBOR, Typeable Ptr
Decoder s Ptr
Typeable Ptr
-> (forall s. Decoder s Ptr) -> (Proxy Ptr -> Text) -> FromCBOR Ptr
Proxy Ptr -> Text
forall s. Decoder s Ptr
forall a.
Typeable a
-> (forall s. Decoder s a) -> (Proxy a -> Text) -> FromCBOR a
label :: Proxy Ptr -> Text
$clabel :: Proxy Ptr -> Text
fromCBOR :: Decoder s Ptr
$cfromCBOR :: forall s. Decoder s Ptr
$cp1FromCBOR :: Typeable Ptr
FromCBOR) via CBORGroup Ptr

instance
  (Typeable kr, CC.Crypto crypto) =>
  ToCBOR (Credential kr crypto)
  where
  toCBOR :: Credential kr crypto -> Encoding
toCBOR = \case
    KeyHashObj KeyHash kr crypto
kh -> Word -> Encoding
encodeListLen Word
2 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word8 -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR (Word8
0 :: Word8) Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> KeyHash kr crypto -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR KeyHash kr crypto
kh
    ScriptHashObj ScriptHash crypto
hs -> Word -> Encoding
encodeListLen Word
2 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word8 -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR (Word8
1 :: Word8) Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ScriptHash crypto -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR ScriptHash crypto
hs

instance
  (Typeable kr, CC.Crypto crypto) =>
  FromCBOR (Credential kr crypto)
  where
  fromCBOR :: Decoder s (Credential kr crypto)
fromCBOR = String
-> (Word -> Decoder s (Int, Credential kr crypto))
-> Decoder s (Credential kr crypto)
forall s a. String -> (Word -> Decoder s (Int, a)) -> Decoder s a
decodeRecordSum String
"Credential" ((Word -> Decoder s (Int, Credential kr crypto))
 -> Decoder s (Credential kr crypto))
-> (Word -> Decoder s (Int, Credential kr crypto))
-> Decoder s (Credential kr crypto)
forall a b. (a -> b) -> a -> b
$
    \case
      Word
0 -> do
        KeyHash kr crypto
x <- Decoder s (KeyHash kr crypto)
forall a s. FromCBOR a => Decoder s a
fromCBOR
        (Int, Credential kr crypto)
-> Decoder s (Int, Credential kr crypto)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int
2, KeyHash kr crypto -> Credential kr crypto
forall (kr :: KeyRole) crypto.
KeyHash kr crypto -> Credential kr crypto
KeyHashObj KeyHash kr crypto
x)
      Word
1 -> do
        ScriptHash crypto
x <- Decoder s (ScriptHash crypto)
forall a s. FromCBOR a => Decoder s a
fromCBOR
        (Int, Credential kr crypto)
-> Decoder s (Int, Credential kr crypto)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int
2, ScriptHash crypto -> Credential kr crypto
forall (kr :: KeyRole) crypto.
ScriptHash crypto -> Credential kr crypto
ScriptHashObj ScriptHash crypto
x)
      Word
k -> Word -> Decoder s (Int, Credential kr crypto)
forall s a. Word -> Decoder s a
invalidKey Word
k

instance ToCBORGroup Ptr where
  toCBORGroup :: Ptr -> Encoding
toCBORGroup (Ptr SlotNo
sl Ix
txIx Ix
certIx) =
    SlotNo -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR SlotNo
sl
      Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR (Integer -> Word
forall a. Num a => Integer -> a
fromInteger (Ix -> Integer
forall a. Integral a => a -> Integer
toInteger Ix
txIx) :: Word)
      Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR (Integer -> Word
forall a. Num a => Integer -> a
fromInteger (Ix -> Integer
forall a. Integral a => a -> Integer
toInteger Ix
certIx) :: Word)
  encodedGroupSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size) -> Proxy Ptr -> Size
encodedGroupSizeExpr forall t. ToCBOR t => Proxy t -> Size
size_ Proxy Ptr
proxy =
    (forall t. ToCBOR t => Proxy t -> Size) -> Proxy SlotNo -> Size
forall a.
ToCBOR a =>
(forall t. ToCBOR t => Proxy t -> Size) -> Proxy a -> Size
encodedSizeExpr forall t. ToCBOR t => Proxy t -> Size
size_ (Ptr -> SlotNo
getSlotNo (Ptr -> SlotNo) -> Proxy Ptr -> Proxy SlotNo
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Proxy Ptr
proxy)
      Size -> Size -> Size
forall a. Num a => a -> a -> a
+ (forall t. ToCBOR t => Proxy t -> Size) -> Proxy Ix -> Size
forall a.
ToCBOR a =>
(forall t. ToCBOR t => Proxy t -> Size) -> Proxy a -> Size
encodedSizeExpr forall t. ToCBOR t => Proxy t -> Size
size_ (Ptr -> Ix
getIx1 (Ptr -> Ix) -> Proxy Ptr -> Proxy Ix
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Proxy Ptr
proxy)
      Size -> Size -> Size
forall a. Num a => a -> a -> a
+ (forall t. ToCBOR t => Proxy t -> Size) -> Proxy Ix -> Size
forall a.
ToCBOR a =>
(forall t. ToCBOR t => Proxy t -> Size) -> Proxy a -> Size
encodedSizeExpr forall t. ToCBOR t => Proxy t -> Size
size_ (Ptr -> Ix
getIx2 (Ptr -> Ix) -> Proxy Ptr -> Proxy Ix
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Proxy Ptr
proxy)
    where
      getSlotNo :: Ptr -> SlotNo
      getSlotNo :: Ptr -> SlotNo
getSlotNo (Ptr SlotNo
a Ix
_ Ix
_) = SlotNo
a
      getIx1, getIx2 :: Ptr -> Ix
      getIx1 :: Ptr -> Ix
getIx1 (Ptr SlotNo
_ Ix
x Ix
_) = Ix
x
      getIx2 :: Ptr -> Ix
getIx2 (Ptr SlotNo
_ Ix
_ Ix
x) = Ix
x

  listLen :: Ptr -> Word
listLen Ptr
_ = Word
3
  listLenBound :: Proxy Ptr -> Word
listLenBound Proxy Ptr
_ = Word
3

instance FromCBORGroup Ptr where
  fromCBORGroup :: Decoder s Ptr
fromCBORGroup = SlotNo -> Ix -> Ix -> Ptr
Ptr (SlotNo -> Ix -> Ix -> Ptr)
-> Decoder s SlotNo -> Decoder s (Ix -> Ix -> Ptr)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s SlotNo
forall a s. FromCBOR a => Decoder s a
fromCBOR Decoder s (Ix -> Ix -> Ptr)
-> Decoder s Ix -> Decoder s (Ix -> Ptr)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s Ix
forall a s. FromCBOR a => Decoder s a
fromCBOR Decoder s (Ix -> Ptr) -> Decoder s Ix -> Decoder s Ptr
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s Ix
forall a s. FromCBOR a => Decoder s a
fromCBOR

newtype GenesisCredential crypto = GenesisCredential
  { GenesisCredential crypto -> KeyHash 'Genesis crypto
unGenesisCredential :: KeyHash 'Genesis crypto
  }
  deriving ((forall x.
 GenesisCredential crypto -> Rep (GenesisCredential crypto) x)
-> (forall x.
    Rep (GenesisCredential crypto) x -> GenesisCredential crypto)
-> Generic (GenesisCredential crypto)
forall x.
Rep (GenesisCredential crypto) x -> GenesisCredential crypto
forall x.
GenesisCredential crypto -> Rep (GenesisCredential crypto) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall crypto x.
Rep (GenesisCredential crypto) x -> GenesisCredential crypto
forall crypto x.
GenesisCredential crypto -> Rep (GenesisCredential crypto) x
$cto :: forall crypto x.
Rep (GenesisCredential crypto) x -> GenesisCredential crypto
$cfrom :: forall crypto x.
GenesisCredential crypto -> Rep (GenesisCredential crypto) x
Generic)
  deriving (Int -> GenesisCredential crypto -> ShowS
[GenesisCredential crypto] -> ShowS
GenesisCredential crypto -> String
(Int -> GenesisCredential crypto -> ShowS)
-> (GenesisCredential crypto -> String)
-> ([GenesisCredential crypto] -> ShowS)
-> Show (GenesisCredential crypto)
forall crypto. Int -> GenesisCredential crypto -> ShowS
forall crypto. [GenesisCredential crypto] -> ShowS
forall crypto. GenesisCredential crypto -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [GenesisCredential crypto] -> ShowS
$cshowList :: forall crypto. [GenesisCredential crypto] -> ShowS
show :: GenesisCredential crypto -> String
$cshow :: forall crypto. GenesisCredential crypto -> String
showsPrec :: Int -> GenesisCredential crypto -> ShowS
$cshowsPrec :: forall crypto. Int -> GenesisCredential crypto -> ShowS
Show) via Quiet (GenesisCredential crypto)

instance Ord (GenesisCredential crypto) where
  compare :: GenesisCredential crypto -> GenesisCredential crypto -> Ordering
compare (GenesisCredential KeyHash 'Genesis crypto
gh) (GenesisCredential KeyHash 'Genesis crypto
gh') = KeyHash 'Genesis crypto -> KeyHash 'Genesis crypto -> Ordering
forall a. Ord a => a -> a -> Ordering
compare KeyHash 'Genesis crypto
gh KeyHash 'Genesis crypto
gh'

instance Eq (GenesisCredential crypto) where
  == :: GenesisCredential crypto -> GenesisCredential crypto -> Bool
(==) (GenesisCredential KeyHash 'Genesis crypto
gh) (GenesisCredential KeyHash 'Genesis crypto
gh') = KeyHash 'Genesis crypto
gh KeyHash 'Genesis crypto -> KeyHash 'Genesis crypto -> Bool
forall a. Eq a => a -> a -> Bool
== KeyHash 'Genesis crypto
gh'

instance
  CC.Crypto crypto =>
  ToCBOR (GenesisCredential crypto)
  where
  toCBOR :: GenesisCredential crypto -> Encoding
toCBOR (GenesisCredential KeyHash 'Genesis crypto
kh) =
    KeyHash 'Genesis crypto -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR KeyHash 'Genesis crypto
kh