{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilyDependencies #-}
module Cardano.Ledger.Era
( Era (..),
getTxOutBootstrapAddress,
PreviousEra,
TranslationContext,
TranslateEra (..),
translateEra',
translateEraMaybe,
WellFormed,
ValidateScript (..),
SupportsSegWit (..),
)
where
import qualified Cardano.Crypto.Hash as Hash
import Cardano.Ledger.Address (Addr (..), BootstrapAddress)
import Cardano.Ledger.AuxiliaryData (AuxiliaryDataHash)
import Cardano.Ledger.Coin (Coin)
import Cardano.Ledger.CompactAddress (CompactAddr, compactAddr, decompactAddr, isBootstrapCompactAddr)
import Cardano.Ledger.Compactible (Compactible)
import qualified Cardano.Ledger.Core as Core
import qualified Cardano.Ledger.Crypto as CryptoClass
import Cardano.Ledger.Hashes
( EraIndependentAuxiliaryData,
EraIndependentBlockBody,
EraIndependentTxBody,
ScriptHash (..),
)
import Cardano.Ledger.SafeHash
( HashAnnotated (..),
SafeToHash (..),
)
import Cardano.Ledger.TxIn (TxIn (..))
import Cardano.Ledger.Val (Val)
import Control.Monad.Except (Except, runExcept)
import qualified Data.ByteString as BS
import Data.Coerce (Coercible, coerce)
import Data.Kind (Type)
import Data.Map (Map)
import Data.Maybe.Strict (StrictMaybe)
import Data.Sequence.Strict (StrictSeq)
import Data.Set (Set)
import Data.Typeable (Typeable)
import Data.Void (Void, absurd)
import Data.Word (Word64)
import GHC.Records (HasField (..))
class
( CryptoClass.Crypto (Crypto e),
Typeable e,
WellFormed e
) =>
Era e
where
type Crypto e :: Type
getTxOutEitherAddr ::
Core.TxOut e ->
Either (Addr (Crypto e)) (CompactAddr (Crypto e))
getTxOutAddr :: Core.TxOut e -> Addr (Crypto e)
getTxOutAddr TxOut e
t =
case TxOut e -> Either (Addr (Crypto e)) (CompactAddr (Crypto e))
forall e.
Era e =>
TxOut e -> Either (Addr (Crypto e)) (CompactAddr (Crypto e))
getTxOutEitherAddr TxOut e
t of
Left Addr (Crypto e)
a -> Addr (Crypto e)
a
Right CompactAddr (Crypto e)
ca -> CompactAddr (Crypto e) -> Addr (Crypto e)
forall crypto. Crypto crypto => CompactAddr crypto -> Addr crypto
decompactAddr CompactAddr (Crypto e)
ca
getTxOutCompactAddr :: Core.TxOut e -> CompactAddr (Crypto e)
getTxOutCompactAddr TxOut e
t =
case TxOut e -> Either (Addr (Crypto e)) (CompactAddr (Crypto e))
forall e.
Era e =>
TxOut e -> Either (Addr (Crypto e)) (CompactAddr (Crypto e))
getTxOutEitherAddr TxOut e
t of
Left Addr (Crypto e)
a -> Addr (Crypto e) -> CompactAddr (Crypto e)
forall crypto. Addr crypto -> CompactAddr crypto
compactAddr Addr (Crypto e)
a
Right CompactAddr (Crypto e)
ca -> CompactAddr (Crypto e)
ca
getAllTxInputs :: Core.TxBody e -> Set (TxIn (Crypto e))
getTxOutBootstrapAddress ::
forall era.
Era era =>
Core.TxOut era ->
Maybe (BootstrapAddress (Crypto era))
getTxOutBootstrapAddress :: TxOut era -> Maybe (BootstrapAddress (Crypto era))
getTxOutBootstrapAddress TxOut era
txOut =
case TxOut era -> Either (Addr (Crypto era)) (CompactAddr (Crypto era))
forall e.
Era e =>
TxOut e -> Either (Addr (Crypto e)) (CompactAddr (Crypto e))
getTxOutEitherAddr TxOut era
txOut of
Left (AddrBootstrap BootstrapAddress (Crypto era)
bootstrapAddr) -> BootstrapAddress (Crypto era)
-> Maybe (BootstrapAddress (Crypto era))
forall a. a -> Maybe a
Just BootstrapAddress (Crypto era)
bootstrapAddr
Right CompactAddr (Crypto era)
cAddr
| CompactAddr (Crypto era) -> Bool
forall crypto. CompactAddr crypto -> Bool
isBootstrapCompactAddr CompactAddr (Crypto era)
cAddr -> do
AddrBootstrap BootstrapAddress (Crypto era)
bootstrapAddr <- Addr (Crypto era) -> Maybe (Addr (Crypto era))
forall a. a -> Maybe a
Just (CompactAddr (Crypto era) -> Addr (Crypto era)
forall crypto. Crypto crypto => CompactAddr crypto -> Addr crypto
decompactAddr CompactAddr (Crypto era)
cAddr)
BootstrapAddress (Crypto era)
-> Maybe (BootstrapAddress (Crypto era))
forall a. a -> Maybe a
Just BootstrapAddress (Crypto era)
bootstrapAddr
Either (Addr (Crypto era)) (CompactAddr (Crypto era))
_ -> Maybe (BootstrapAddress (Crypto era))
forall a. Maybe a
Nothing
class
( Era era,
SafeToHash (Core.Script era),
HasField "body" (Core.Tx era) (Core.TxBody era)
) =>
ValidateScript era
where
scriptPrefixTag :: Core.Script era -> BS.ByteString
validateScript :: Core.Script era -> Core.Tx era -> Bool
hashScript :: Core.Script era -> ScriptHash (Crypto era)
hashScript =
Hash (ADDRHASH (Crypto era)) EraIndependentScript
-> ScriptHash (Crypto era)
forall crypto.
Hash (ADDRHASH crypto) EraIndependentScript -> ScriptHash crypto
ScriptHash (Hash (ADDRHASH (Crypto era)) EraIndependentScript
-> ScriptHash (Crypto era))
-> (Script era
-> Hash (ADDRHASH (Crypto era)) EraIndependentScript)
-> Script era
-> ScriptHash (Crypto era)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Hash (ADDRHASH (Crypto era)) (Script era)
-> Hash (ADDRHASH (Crypto era)) EraIndependentScript
forall h a b. Hash h a -> Hash h b
Hash.castHash
(Hash (ADDRHASH (Crypto era)) (Script era)
-> Hash (ADDRHASH (Crypto era)) EraIndependentScript)
-> (Script era -> Hash (ADDRHASH (Crypto era)) (Script era))
-> Script era
-> Hash (ADDRHASH (Crypto era)) EraIndependentScript
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Script era -> ByteString)
-> Script era -> Hash (ADDRHASH (Crypto era)) (Script era)
forall h a. HashAlgorithm h => (a -> ByteString) -> a -> Hash h a
Hash.hashWith
(\Script era
x -> Script era -> ByteString
forall era. ValidateScript era => Script era -> ByteString
scriptPrefixTag @era Script era
x ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Script era -> ByteString
forall t. SafeToHash t => t -> ByteString
originalBytes Script era
x)
isNativeScript :: Core.Script era -> Bool
isNativeScript Script era
_ = Bool
True
class SupportsSegWit era where
type TxSeq era = (r :: Type) | r -> era
fromTxSeq :: TxSeq era -> StrictSeq (Core.Tx era)
toTxSeq :: StrictSeq (Core.Tx era) -> TxSeq era
hashTxSeq ::
TxSeq era ->
Hash.Hash (CryptoClass.HASH (Crypto era)) EraIndependentBlockBody
numSegComponents :: Word64
type family PreviousEra era :: Type
type family TranslationContext era :: Type
class (Era era, Era (PreviousEra era)) => TranslateEra era f where
type TranslationError era f :: Type
type TranslationError era f = Void
translateEra :: TranslationContext era -> f (PreviousEra era) -> Except (TranslationError era f) (f era)
default translateEra ::
Coercible (f (PreviousEra era)) (f era) =>
TranslationContext era ->
f (PreviousEra era) ->
Except (TranslationError era f) (f era)
translateEra TranslationContext era
_ = f era -> Except (TranslationError era f) (f era)
forall (m :: * -> *) a. Monad m => a -> m a
return (f era -> Except (TranslationError era f) (f era))
-> (f (PreviousEra era) -> f era)
-> f (PreviousEra era)
-> Except (TranslationError era f) (f era)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f (PreviousEra era) -> f era
coerce
translateEra' ::
(TranslateEra era f, TranslationError era f ~ Void) =>
TranslationContext era ->
f (PreviousEra era) ->
f era
translateEra' :: TranslationContext era -> f (PreviousEra era) -> f era
translateEra' TranslationContext era
ctxt = (Void -> f era) -> (f era -> f era) -> Either Void (f era) -> f era
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either Void -> f era
forall a. Void -> a
absurd f era -> f era
forall a. a -> a
id (Either Void (f era) -> f era)
-> (f (PreviousEra era) -> Either Void (f era))
-> f (PreviousEra era)
-> f era
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Except Void (f era) -> Either Void (f era)
forall e a. Except e a -> Either e a
runExcept (Except Void (f era) -> Either Void (f era))
-> (f (PreviousEra era) -> Except Void (f era))
-> f (PreviousEra era)
-> Either Void (f era)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TranslationContext era
-> f (PreviousEra era) -> Except (TranslationError era f) (f era)
forall era (f :: * -> *).
TranslateEra era f =>
TranslationContext era
-> f (PreviousEra era) -> Except (TranslationError era f) (f era)
translateEra TranslationContext era
ctxt
translateEraMaybe ::
(TranslateEra era f, TranslationError era f ~ ()) =>
TranslationContext era ->
f (PreviousEra era) ->
Maybe (f era)
translateEraMaybe :: TranslationContext era -> f (PreviousEra era) -> Maybe (f era)
translateEraMaybe TranslationContext era
ctxt =
(() -> Maybe (f era))
-> (f era -> Maybe (f era)) -> Either () (f era) -> Maybe (f era)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Maybe (f era) -> () -> Maybe (f era)
forall a b. a -> b -> a
const Maybe (f era)
forall a. Maybe a
Nothing) f era -> Maybe (f era)
forall a. a -> Maybe a
Just (Either () (f era) -> Maybe (f era))
-> (f (PreviousEra era) -> Either () (f era))
-> f (PreviousEra era)
-> Maybe (f era)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Except () (f era) -> Either () (f era)
forall e a. Except e a -> Either e a
runExcept (Except () (f era) -> Either () (f era))
-> (f (PreviousEra era) -> Except () (f era))
-> f (PreviousEra era)
-> Either () (f era)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TranslationContext era
-> f (PreviousEra era) -> Except (TranslationError era f) (f era)
forall era (f :: * -> *).
TranslateEra era f =>
TranslationContext era
-> f (PreviousEra era) -> Except (TranslationError era f) (f era)
translateEra TranslationContext era
ctxt
type WellFormed era =
(
HasField "outputs" (Core.TxBody era) (StrictSeq (Core.TxOut era)),
HasField "txfee" (Core.TxBody era) Coin,
HasField "minted" (Core.TxBody era) (Set (ScriptHash (Crypto era))),
HasField "adHash" (Core.TxBody era) (StrictMaybe (AuxiliaryDataHash (Crypto era))),
HasField "body" (Core.Tx era) (Core.TxBody era),
HasField "wits" (Core.Tx era) (Core.Witnesses era),
HasField "auxiliaryData" (Core.Tx era) (StrictMaybe (Core.AuxiliaryData era)),
HasField "txsize" (Core.Tx era) Integer,
HasField "scriptWits" (Core.Tx era) (Map (ScriptHash (Crypto era)) (Core.Script era)),
HasField "value" (Core.TxOut era) (Core.Value era),
HashAnnotated (Core.AuxiliaryData era) EraIndependentAuxiliaryData (Crypto era),
HashAnnotated (Core.TxBody era) EraIndependentTxBody (Crypto era),
SupportsSegWit era,
Val (Core.Value era),
Compactible (Core.Value era)
)