|
| 1 | +{-# LANGUAGE DataKinds #-} |
| 2 | +{-# LANGUAGE FlexibleContexts #-} |
| 3 | +{-# LANGUAGE GADTs #-} |
| 4 | +{-# LANGUAGE RankNTypes #-} |
| 5 | +{-# LANGUAGE ScopedTypeVariables #-} |
| 6 | + |
| 7 | +module Cardano.Api.Experimental.Tx.Internal.BodyContent.Old |
| 8 | + ( extractAllIndexedPlutusScriptWitnesses |
| 9 | + , makeUnsignedTx |
| 10 | + ) |
| 11 | +where |
| 12 | + |
| 13 | +import Cardano.Api.Era |
| 14 | + ( alonzoEraOnwardsConstraints |
| 15 | + , caseShelleyToBabbageOrConwayEraOnwards |
| 16 | + , unFeatured |
| 17 | + ) |
| 18 | +import Cardano.Api.Era.Internal.Eon.ShelleyBasedEra |
| 19 | +import Cardano.Api.Experimental.Era |
| 20 | +import Cardano.Api.Experimental.Plutus |
| 21 | +import Cardano.Api.Experimental.Tx.Internal.TxScriptWitnessRequirements |
| 22 | +import Cardano.Api.Experimental.Tx.Internal.Type |
| 23 | +import Cardano.Api.Ledger.Internal.Reexport (StrictMaybe (..), maybeToStrictMaybe) |
| 24 | +import Cardano.Api.Ledger.Internal.Reexport qualified as L |
| 25 | +import Cardano.Api.Plutus.Internal.Script |
| 26 | +import Cardano.Api.Tx.Internal.Body |
| 27 | + |
| 28 | +import Cardano.Binary qualified as CBOR |
| 29 | +import Cardano.Ledger.Alonzo.Tx qualified as L |
| 30 | +import Cardano.Ledger.Alonzo.TxBody qualified as L |
| 31 | +import Cardano.Ledger.Api qualified as L |
| 32 | +import Cardano.Ledger.Core qualified as Ledger |
| 33 | + |
| 34 | +import GHC.Exts (IsList (..)) |
| 35 | +import Lens.Micro |
| 36 | + |
| 37 | +extractAllIndexedPlutusScriptWitnesses |
| 38 | + :: forall era |
| 39 | + . Era era |
| 40 | + -> TxBodyContent BuildTx era |
| 41 | + -> Either |
| 42 | + CBOR.DecoderError |
| 43 | + [AnyIndexedPlutusScriptWitness (LedgerEra era)] |
| 44 | +extractAllIndexedPlutusScriptWitnesses era b = obtainCommonConstraints era $ do |
| 45 | + let sbe = convert era |
| 46 | + aeon = convert era |
| 47 | + legacyTxInWits = extractWitnessableTxIns aeon $ txIns b |
| 48 | + legacyCertWits = extractWitnessableCertificates aeon $ txCertificates b |
| 49 | + legacyMintWits = extractWitnessableMints aeon $ txMintValue b |
| 50 | + proposalWits |
| 51 | + :: [(Witnessable ProposalItem (ShelleyLedgerEra era), BuildTxWith BuildTx (Witness WitCtxStake era))] = |
| 52 | + caseShelleyToBabbageOrConwayEraOnwards |
| 53 | + (const []) |
| 54 | + (`extractWitnessableProposals` txProposalProcedures b) |
| 55 | + sbe |
| 56 | + legacyWithdrawalWits = extractWitnessableWithdrawals aeon $ txWithdrawals b |
| 57 | + legacyVoteWits |
| 58 | + :: [(Witnessable VoterItem (ShelleyLedgerEra era), BuildTxWith BuildTx (Witness WitCtxStake era))] = |
| 59 | + caseShelleyToBabbageOrConwayEraOnwards |
| 60 | + (const []) |
| 61 | + (`extractWitnessableVotes` txVotingProcedures b) |
| 62 | + sbe |
| 63 | + |
| 64 | + txInWits <- legacyWitnessConversion aeon legacyTxInWits |
| 65 | + let indexedScriptTxInWits = alonzoEraOnwardsConstraints aeon $ createIndexedPlutusScriptWitnesses txInWits |
| 66 | + |
| 67 | + certWits <- legacyWitnessConversion aeon legacyCertWits |
| 68 | + let indexedCertScriptWits = alonzoEraOnwardsConstraints aeon $ createIndexedPlutusScriptWitnesses certWits |
| 69 | + |
| 70 | + mintWits <- legacyWitnessConversion aeon legacyMintWits |
| 71 | + let indexedMintScriptWits = alonzoEraOnwardsConstraints aeon $ createIndexedPlutusScriptWitnesses mintWits |
| 72 | + |
| 73 | + withdrawalWits <- legacyWitnessConversion aeon legacyWithdrawalWits |
| 74 | + let indexedWithdrawalScriptWits = alonzoEraOnwardsConstraints aeon $ createIndexedPlutusScriptWitnesses withdrawalWits |
| 75 | + |
| 76 | + proposalScriptWits <- legacyWitnessConversion aeon proposalWits |
| 77 | + let indexedProposalScriptWits = alonzoEraOnwardsConstraints aeon $ createIndexedPlutusScriptWitnesses proposalScriptWits |
| 78 | + |
| 79 | + voteWits <- legacyWitnessConversion aeon legacyVoteWits |
| 80 | + let indexedVoteScriptWits = alonzoEraOnwardsConstraints aeon $ createIndexedPlutusScriptWitnesses voteWits |
| 81 | + return $ |
| 82 | + mconcat |
| 83 | + [ indexedScriptTxInWits |
| 84 | + , indexedMintScriptWits |
| 85 | + , indexedCertScriptWits |
| 86 | + , indexedWithdrawalScriptWits |
| 87 | + , indexedProposalScriptWits |
| 88 | + , indexedVoteScriptWits |
| 89 | + ] |
| 90 | + |
| 91 | +makeUnsignedTx |
| 92 | + :: Era era |
| 93 | + -> TxBodyContent BuildTx era |
| 94 | + -> Either TxBodyError (UnsignedTx era) |
| 95 | +makeUnsignedTx DijkstraEra _ = error "makeUnsignedTx: Dijkstra era not supported yet" |
| 96 | +makeUnsignedTx era@ConwayEra bc = obtainCommonConstraints era $ do |
| 97 | + let sbe = convert era |
| 98 | + aeon = convert era |
| 99 | + TxScriptWitnessRequirements languages scripts datums redeemers <- |
| 100 | + shelleyBasedEraConstraints sbe $ |
| 101 | + collectTxBodyScriptWitnessRequirements (convert era) bc |
| 102 | + |
| 103 | + -- cardano-api types |
| 104 | + let apiTxOuts = txOuts bc |
| 105 | + apiScriptValidity = txScriptValidity bc |
| 106 | + apiMintValue = txMintValue bc |
| 107 | + apiProtocolParameters = txProtocolParams bc |
| 108 | + apiCollateralTxIns = txInsCollateral bc |
| 109 | + apiReferenceInputs = txInsReference bc |
| 110 | + apiExtraKeyWitnesses = txExtraKeyWits bc |
| 111 | + apiReturnCollateral = txReturnCollateral bc |
| 112 | + apiTotalCollateral = txTotalCollateral bc |
| 113 | + |
| 114 | + -- Ledger types |
| 115 | + txins = convTxIns $ txIns bc |
| 116 | + collTxIns = convCollateralTxIns apiCollateralTxIns |
| 117 | + refTxIns = convReferenceInputs apiReferenceInputs |
| 118 | + outs = convTxOuts sbe apiTxOuts |
| 119 | + fee = convTransactionFee sbe $ txFee bc |
| 120 | + withdrawals = convWithdrawals $ txWithdrawals bc |
| 121 | + returnCollateral = convReturnCollateral sbe apiReturnCollateral |
| 122 | + totalCollateral = convTotalCollateral apiTotalCollateral |
| 123 | + certs = convCertificates sbe $ txCertificates bc |
| 124 | + txAuxData = toAuxiliaryData sbe (txMetadata bc) (txAuxScripts bc) |
| 125 | + scriptIntegrityHash = |
| 126 | + convPParamsToScriptIntegrityHash |
| 127 | + aeon |
| 128 | + apiProtocolParameters |
| 129 | + redeemers |
| 130 | + datums |
| 131 | + languages |
| 132 | + |
| 133 | + let setMint = convMintValue apiMintValue |
| 134 | + setReqSignerHashes = convExtraKeyWitnesses apiExtraKeyWitnesses |
| 135 | + ledgerTxBody = |
| 136 | + L.mkBasicTxBody |
| 137 | + & L.inputsTxBodyL .~ txins |
| 138 | + & L.collateralInputsTxBodyL .~ collTxIns |
| 139 | + & L.referenceInputsTxBodyL .~ refTxIns |
| 140 | + & L.outputsTxBodyL .~ outs |
| 141 | + & L.totalCollateralTxBodyL .~ totalCollateral |
| 142 | + & L.collateralReturnTxBodyL .~ returnCollateral |
| 143 | + & L.feeTxBodyL .~ fee |
| 144 | + & L.vldtTxBodyL . L.invalidBeforeL .~ convValidityLowerBound (txValidityLowerBound bc) |
| 145 | + & L.vldtTxBodyL . L.invalidHereAfterL .~ convValidityUpperBound sbe (txValidityUpperBound bc) |
| 146 | + & L.reqSignerHashesTxBodyL .~ setReqSignerHashes |
| 147 | + & L.scriptIntegrityHashTxBodyL .~ scriptIntegrityHash |
| 148 | + & L.withdrawalsTxBodyL .~ withdrawals |
| 149 | + & L.certsTxBodyL .~ certs |
| 150 | + & L.mintTxBodyL .~ setMint |
| 151 | + & L.auxDataHashTxBodyL .~ maybe SNothing (SJust . Ledger.hashTxAuxData) txAuxData |
| 152 | + |
| 153 | + scriptWitnesses = |
| 154 | + L.mkBasicTxWits |
| 155 | + & L.scriptTxWitsL |
| 156 | + .~ fromList |
| 157 | + [ (L.hashScript sw, sw) |
| 158 | + | sw <- scripts |
| 159 | + ] |
| 160 | + & L.datsTxWitsL .~ datums |
| 161 | + & L.rdmrsTxWitsL .~ redeemers |
| 162 | + |
| 163 | + let eraSpecificTxBody = eraSpecificLedgerTxBody era ledgerTxBody bc |
| 164 | + |
| 165 | + return . UnsignedTx $ |
| 166 | + L.mkBasicTx eraSpecificTxBody |
| 167 | + & L.witsTxL .~ scriptWitnesses |
| 168 | + & L.auxDataTxL .~ maybeToStrictMaybe (toAuxiliaryData sbe (txMetadata bc) (txAuxScripts bc)) |
| 169 | + & L.isValidTxL .~ txScriptValidityToIsValid apiScriptValidity |
| 170 | + |
| 171 | +eraSpecificLedgerTxBody |
| 172 | + :: Era era |
| 173 | + -> Ledger.TxBody (LedgerEra era) |
| 174 | + -> TxBodyContent BuildTx era |
| 175 | + -> Ledger.TxBody (LedgerEra era) |
| 176 | +eraSpecificLedgerTxBody era ledgerbody bc = |
| 177 | + body era |
| 178 | + where |
| 179 | + body e = |
| 180 | + let propProcedures = txProposalProcedures bc |
| 181 | + voteProcedures = txVotingProcedures bc |
| 182 | + treasuryDonation = txTreasuryDonation bc |
| 183 | + currentTresuryValue = txCurrentTreasuryValue bc |
| 184 | + in obtainCommonConstraints e $ |
| 185 | + ledgerbody |
| 186 | + & L.proposalProceduresTxBodyL |
| 187 | + .~ convProposalProcedures (maybe TxProposalProceduresNone unFeatured propProcedures) |
| 188 | + & L.votingProceduresTxBodyL |
| 189 | + .~ convVotingProcedures (maybe TxVotingProceduresNone unFeatured voteProcedures) |
| 190 | + & L.treasuryDonationTxBodyL |
| 191 | + .~ maybe (L.Coin 0) unFeatured treasuryDonation |
| 192 | + & L.currentTreasuryValueTxBodyL |
| 193 | + .~ L.maybeToStrictMaybe (unFeatured =<< currentTresuryValue) |
0 commit comments