|
| 1 | +{-# LANGUAGE FlexibleContexts #-} |
| 2 | +{-# LANGUAGE GADTs #-} |
| 3 | +{-# LANGUAGE RankNTypes #-} |
| 4 | +{-# LANGUAGE TupleSections #-} |
| 5 | + |
| 6 | +module Cardano.Api.Experimental.Tx.Internal.Compatible |
| 7 | + ( mkTxCertificates |
| 8 | + ) |
| 9 | +where |
| 10 | + |
| 11 | +import Cardano.Api.Address qualified as Api |
| 12 | +import Cardano.Api.Certificate.Internal qualified as Api |
| 13 | +import Cardano.Api.Era.Internal.Eon.Convert |
| 14 | +import Cardano.Api.Experimental.Era |
| 15 | +import Cardano.Api.Experimental.Plutus.Internal.Script qualified as Exp |
| 16 | +import Cardano.Api.Experimental.Plutus.Internal.ScriptWitness qualified as Exp |
| 17 | +import Cardano.Api.Experimental.Simple.Script qualified as Exp |
| 18 | +import Cardano.Api.Experimental.Tx.Internal.AnyWitness |
| 19 | +import Cardano.Api.Experimental.Tx.Internal.Certificate |
| 20 | +import Cardano.Api.Ledger.Internal.Reexport qualified as L |
| 21 | +import Cardano.Api.Plutus.Internal.Script (fromAllegraTimelock, sbeToSimpleScriptLanguageInEra) |
| 22 | +import Cardano.Api.Plutus.Internal.Script qualified as Api |
| 23 | +import Cardano.Api.Tx.Internal.Body (TxCertificates (..)) |
| 24 | +import Cardano.Api.Tx.Internal.Body qualified as Api |
| 25 | + |
| 26 | +import Cardano.Binary |
| 27 | +import Cardano.Ledger.Allegra.Scripts qualified as L |
| 28 | +import Cardano.Ledger.Alonzo.Scripts qualified as L |
| 29 | +import Cardano.Ledger.Plutus.Language qualified as L |
| 30 | +import Cardano.Ledger.Plutus.Language qualified as Plutus |
| 31 | + |
| 32 | +import GHC.Exts (IsList (..)) |
| 33 | + |
| 34 | +mkTxCertificates |
| 35 | + :: forall era |
| 36 | + . IsEra era |
| 37 | + => [(Certificate (LedgerEra era), AnyWitness (LedgerEra era))] |
| 38 | + -> Api.TxCertificates Api.BuildTx era |
| 39 | +mkTxCertificates [] = TxCertificatesNone |
| 40 | +mkTxCertificates certs = |
| 41 | + TxCertificates (convert useEra) $ fromList $ map (getStakeCred useEra) certs |
| 42 | + where |
| 43 | + -- TxCertificate now uses experimental Certificate type therefore getStakeCred |
| 44 | + -- needs to be adjusted! |
| 45 | + |
| 46 | + getStakeCred |
| 47 | + :: Era era |
| 48 | + -> (Certificate (LedgerEra era), AnyWitness (LedgerEra era)) |
| 49 | + -> ( Api.Certificate era |
| 50 | + , Api.BuildTxWith |
| 51 | + Api.BuildTx |
| 52 | + (Maybe (Api.StakeCredential, Api.Witness Api.WitCtxStake era)) |
| 53 | + ) |
| 54 | + getStakeCred era (Certificate cert, witness) = |
| 55 | + case era of |
| 56 | + ConwayEra -> do |
| 57 | + let oldApiCert = Api.ConwayCertificate (convert era) cert |
| 58 | + mStakeCred = Api.selectStakeCredentialWitness oldApiCert |
| 59 | + wit = |
| 60 | + case witness of |
| 61 | + AnyKeyWitnessPlaceholder -> Api.KeyWitness Api.KeyWitnessForStakeAddr |
| 62 | + AnySimpleScriptWitness ss -> |
| 63 | + Api.ScriptWitness Api.ScriptWitnessForStakeAddr $ newToOldSimpleScriptWitness era ss |
| 64 | + AnyPlutusScriptWitness psw -> |
| 65 | + Api.ScriptWitness Api.ScriptWitnessForStakeAddr $ |
| 66 | + newToOldPlutusCertificateScriptWitness ConwayEra psw |
| 67 | + (oldApiCert, pure $ (,wit) <$> mStakeCred) |
| 68 | + |
| 69 | +newToOldSimpleScriptWitness |
| 70 | + :: L.AllegraEraScript (LedgerEra era) |
| 71 | + => Era era -> Exp.SimpleScriptOrReferenceInput (LedgerEra era) -> Api.ScriptWitness Api.WitCtxStake era |
| 72 | +newToOldSimpleScriptWitness era simple = |
| 73 | + case simple of |
| 74 | + Exp.SScript (Exp.SimpleScript script) -> |
| 75 | + Api.SimpleScriptWitness |
| 76 | + (sbeToSimpleScriptLanguageInEra $ convert era) |
| 77 | + (Api.SScript $ fromAllegraTimelock script) |
| 78 | + Exp.SReferenceScript inp -> |
| 79 | + Api.SimpleScriptWitness |
| 80 | + (sbeToSimpleScriptLanguageInEra $ convert era) |
| 81 | + (Api.SReferenceScript inp) |
| 82 | + |
| 83 | +newToOldPlutusCertificateScriptWitness |
| 84 | + :: Era era |
| 85 | + -> Exp.PlutusScriptWitness lang purpose (LedgerEra era) |
| 86 | + -> Api.ScriptWitness Api.WitCtxStake era |
| 87 | +newToOldPlutusCertificateScriptWitness ConwayEra (Exp.PlutusScriptWitness Plutus.SPlutusV1 scriptOrRef _ redeemer execUnits) = |
| 88 | + Api.PlutusScriptWitness |
| 89 | + Api.PlutusScriptV1InConway |
| 90 | + Api.PlutusScriptV1 |
| 91 | + (newToOldPlutusScriptOrReferenceInput ConwayEra scriptOrRef) |
| 92 | + Api.NoScriptDatumForStake |
| 93 | + redeemer |
| 94 | + execUnits |
| 95 | +newToOldPlutusCertificateScriptWitness ConwayEra (Exp.PlutusScriptWitness Plutus.SPlutusV2 scriptOrRef _ redeemer execUnits) = |
| 96 | + Api.PlutusScriptWitness |
| 97 | + Api.PlutusScriptV2InConway |
| 98 | + Api.PlutusScriptV2 |
| 99 | + (newToOldPlutusScriptOrReferenceInput ConwayEra scriptOrRef) |
| 100 | + Api.NoScriptDatumForStake |
| 101 | + redeemer |
| 102 | + execUnits |
| 103 | +newToOldPlutusCertificateScriptWitness ConwayEra (Exp.PlutusScriptWitness Plutus.SPlutusV3 scriptOrRef _ redeemer execUnits) = |
| 104 | + Api.PlutusScriptWitness |
| 105 | + Api.PlutusScriptV3InConway |
| 106 | + Api.PlutusScriptV3 |
| 107 | + (newToOldPlutusScriptOrReferenceInput ConwayEra scriptOrRef) |
| 108 | + Api.NoScriptDatumForStake |
| 109 | + redeemer |
| 110 | + execUnits |
| 111 | + |
| 112 | +newToOldPlutusScriptOrReferenceInput |
| 113 | + :: Era era |
| 114 | + -> Exp.PlutusScriptOrReferenceInput lang (LedgerEra era) |
| 115 | + -> Api.PlutusScriptOrReferenceInput oldlang |
| 116 | +newToOldPlutusScriptOrReferenceInput ConwayEra (Exp.PReferenceScript txin) = Api.PReferenceScript txin |
| 117 | +newToOldPlutusScriptOrReferenceInput ConwayEra (Exp.PScript (Exp.PlutusScriptInEra plutusRunnable)) = |
| 118 | + let oldScript = L.unPlutusBinary . L.plutusBinary $ L.plutusFromRunnable plutusRunnable |
| 119 | + in Api.PScript $ Api.PlutusScriptSerialised oldScript |
0 commit comments