Skip to content

Commit 89fd078

Browse files
committed
Add some instance for data Certificate era
- SerialiseAsCBOR - HasTypeProxy - HasTextEnvelope
1 parent 20b9e9d commit 89fd078

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

cardano-api/src/Cardano/Api/Era/Internal/Eon/ShelleyBasedEra.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ type ShelleyBasedEraConstraints era =
226226
, L.HashAnnotated (L.TxBody (ShelleyLedgerEra era)) L.EraIndependentTxBody
227227
, L.ShelleyEraTxCert (ShelleyLedgerEra era)
228228
, FromCBOR (Consensus.ChainDepState (ConsensusProtocol era))
229+
, FromCBOR (L.TxCert (ShelleyLedgerEra era))
229230
, HasTypeProxy era
230231
, IsCardanoEra era
231232
, IsShelleyBasedEra era

cardano-api/src/Cardano/Api/Experimental/Tx/Internal/Certificate.hs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
{-# LANGUAGE EmptyCase #-}
22
{-# LANGUAGE FlexibleContexts #-}
3+
{-# LANGUAGE FlexibleInstances #-}
34
{-# LANGUAGE GADTs #-}
45
{-# LANGUAGE RankNTypes #-}
6+
{-# LANGUAGE ScopedTypeVariables #-}
57
{-# LANGUAGE StandaloneDeriving #-}
68
{-# LANGUAGE TupleSections #-}
9+
{-# LANGUAGE TypeApplications #-}
10+
{-# LANGUAGE TypeFamilies #-}
11+
{-# LANGUAGE TypeOperators #-}
12+
{-# LANGUAGE UndecidableInstances #-}
713

814
module Cardano.Api.Experimental.Tx.Internal.Certificate
915
( Certificate (..)
@@ -17,22 +23,28 @@ import Cardano.Api.Address qualified as Api
1723
import Cardano.Api.Certificate.Internal qualified as Api
1824
import Cardano.Api.Era.Internal.Eon.Convert
1925
import Cardano.Api.Era.Internal.Eon.ConwayEraOnwards
26+
import Cardano.Api.Era.Internal.Eon.ShelleyBasedEra
2027
import Cardano.Api.Era.Internal.Eon.ShelleyToBabbageEra qualified as Api
2128
import Cardano.Api.Experimental.Era
2229
import Cardano.Api.Experimental.Plutus.Internal.Script qualified as Exp
2330
import Cardano.Api.Experimental.Plutus.Internal.ScriptWitness qualified as Exp
2431
import Cardano.Api.Experimental.Simple.Script qualified as Exp
2532
import Cardano.Api.Experimental.Tx.Internal.AnyWitness
33+
import Cardano.Api.HasTypeProxy
2634
import Cardano.Api.Ledger qualified as L
2735
import Cardano.Api.Plutus.Internal.Script
2836
import Cardano.Api.Plutus.Internal.Script qualified as Api
37+
import Cardano.Api.Serialise.Cbor
38+
import Cardano.Api.Serialise.TextEnvelope.Internal
2939
import Cardano.Api.Tx.Internal.Body (TxCertificates (..))
3040
import Cardano.Api.Tx.Internal.Body qualified as Api
3141

42+
import Cardano.Binary qualified as CBOR
3243
import Cardano.Ledger.Allegra.Scripts qualified as L
3344
import Cardano.Ledger.Plutus.Language qualified as L
3445
import Cardano.Ledger.Plutus.Language qualified as Plutus
3546

47+
import Data.Typeable
3648
import GHC.IsList
3749

3850
data Certificate era where
@@ -44,6 +56,31 @@ deriving instance Eq (Certificate era)
4456

4557
deriving instance Ord (Certificate era)
4658

59+
instance
60+
( Typeable ledgerera
61+
, IsShelleyBasedEra era
62+
, ShelleyLedgerEra era ~ ledgerera
63+
)
64+
=> HasTextEnvelope (Certificate ledgerera)
65+
where
66+
textEnvelopeType _ = "Certificate"
67+
68+
instance Typeable era => HasTypeProxy (Certificate era) where
69+
data AsType (Certificate era) = AsCertificate
70+
proxyToAsType _ = AsCertificate
71+
72+
instance
73+
( Typeable ledgerera
74+
, IsShelleyBasedEra era
75+
, ShelleyLedgerEra era ~ ledgerera
76+
)
77+
=> SerialiseAsCBOR (Certificate ledgerera)
78+
where
79+
serialiseToCBOR (Certificate cert) =
80+
CBOR.serialize' cert
81+
deserialiseFromCBOR _ bs =
82+
shelleyBasedEraConstraints (shelleyBasedEra @era) $ Certificate <$> CBOR.decodeFull' bs
83+
4784
convertToOldApiCertificate :: Era era -> Certificate (LedgerEra era) -> Api.Certificate era
4885
convertToOldApiCertificate ConwayEra (Certificate cert) =
4986
Api.ConwayCertificate ConwayEraOnwardsConway cert

0 commit comments

Comments
 (0)