Skip to content

Commit 99c3936

Browse files
committed
WIP
1 parent 4cdb67f commit 99c3936

File tree

14 files changed

+1126
-224
lines changed

14 files changed

+1126
-224
lines changed

cardano-api/cardano-api.cabal

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,15 +227,18 @@ library
227227
Cardano.Api.Experimental.Plutus.Internal.Script
228228
Cardano.Api.Experimental.Plutus.Internal.ScriptWitness
229229
Cardano.Api.Experimental.Plutus.Internal.Shim.LegacyScripts
230+
Cardano.Api.Experimental.Script
230231
Cardano.Api.Experimental.Serialise.TextEnvelope.Internal
231232
Cardano.Api.Experimental.Tx.Internal.AnyWitness
232-
Cardano.Api.Experimental.Tx.Internal.Body
233+
Cardano.Api.Experimental.Tx.Internal.BodyContent.New
234+
Cardano.Api.Experimental.Tx.Internal.BodyContent.Old
233235
Cardano.Api.Experimental.Tx.Internal.Certificate
234236
Cardano.Api.Experimental.Tx.Internal.Certificate.Compatible
235237
Cardano.Api.Experimental.Tx.Internal.Certificate.Type
236238
Cardano.Api.Experimental.Tx.Internal.Compatible
237239
Cardano.Api.Experimental.Tx.Internal.Fee
238240
Cardano.Api.Experimental.Tx.Internal.TxScriptWitnessRequirements
241+
Cardano.Api.Experimental.Tx.Internal.Type
239242
Cardano.Api.Genesis.Internal
240243
Cardano.Api.Genesis.Internal.Parameters
241244
Cardano.Api.Governance.Internal.Action.ProposalProcedure

cardano-api/src/Cardano/Api/Experimental/Plutus/Internal/ScriptWitness.hs

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ data PlutusScriptPurpose
9292
| -- | Witnesses a vote
9393
VotingScript
9494

95-
data NoScriptDatum = NoScriptDatumAllowed deriving Show
95+
data NoScriptDatum = NoScriptDatumAllowed deriving (Eq, Show)
9696

9797
-- | The PlutusScriptDatum type family is used to determine if a script datum is allowed
9898
-- for a given plutus script purpose and version. This change was proposed in CIP-69
@@ -130,6 +130,55 @@ data PlutusScriptDatum (lang :: L.Language) (purpose :: PlutusScriptPurpose) whe
130130
NoScriptDatum
131131
:: PlutusScriptDatum lang purpose
132132

133+
instance Eq (PlutusScriptDatum L.PlutusV1 SpendingScript) where
134+
(==) (SpendingScriptDatum d1) (SpendingScriptDatum d2) = d1 == d2
135+
(==) InlineDatum InlineDatum = True
136+
(==) NoScriptDatum NoScriptDatum = True
137+
(==) _ _ = False
138+
139+
instance Eq (PlutusScriptDatum L.PlutusV2 SpendingScript) where
140+
(==) (SpendingScriptDatum d1) (SpendingScriptDatum d2) = d1 == d2
141+
(==) InlineDatum InlineDatum = True
142+
(==) NoScriptDatum NoScriptDatum = True
143+
(==) _ _ = False
144+
145+
instance Eq (PlutusScriptDatum L.PlutusV3 SpendingScript) where
146+
(==) (SpendingScriptDatum d1) (SpendingScriptDatum d2) = d1 == d2
147+
(==) InlineDatum InlineDatum = True
148+
(==) NoScriptDatum NoScriptDatum = True
149+
(==) _ _ = False
150+
151+
instance Eq (PlutusScriptDatum L.PlutusV4 SpendingScript) where
152+
(==) (SpendingScriptDatum d1) (SpendingScriptDatum d2) = d1 == d2
153+
(==) InlineDatum InlineDatum = True
154+
(==) NoScriptDatum NoScriptDatum = True
155+
(==) _ _ = False
156+
157+
instance Eq (PlutusScriptDatum lang MintingScript) where
158+
(==) InlineDatum InlineDatum = True
159+
(==) NoScriptDatum NoScriptDatum = True
160+
(==) _ _ = False
161+
162+
instance Eq (PlutusScriptDatum lang WithdrawingScript) where
163+
(==) InlineDatum InlineDatum = True
164+
(==) NoScriptDatum NoScriptDatum = True
165+
(==) _ _ = False
166+
167+
instance Eq (PlutusScriptDatum lang CertifyingScript) where
168+
(==) InlineDatum InlineDatum = True
169+
(==) NoScriptDatum NoScriptDatum = True
170+
(==) _ _ = False
171+
172+
instance Eq (PlutusScriptDatum lang ProposingScript) where
173+
(==) InlineDatum InlineDatum = True
174+
(==) NoScriptDatum NoScriptDatum = True
175+
(==) _ _ = False
176+
177+
instance Eq (PlutusScriptDatum lang VotingScript) where
178+
(==) InlineDatum InlineDatum = True
179+
(==) NoScriptDatum NoScriptDatum = True
180+
(==) _ _ = False
181+
133182
instance Show (PlutusScriptDatum lang purpose) where
134183
show = \case
135184
SpendingScriptDatum _d -> "Datum"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{-# LANGUAGE GADTs #-}
2+
3+
module Cardano.Api.Experimental.Script
4+
( AnyScript (..)
5+
)
6+
where
7+
8+
import Cardano.Api.Experimental.Plutus.Internal.Script
9+
import Cardano.Api.Experimental.Simple.Script
10+
11+
data AnyScript era where
12+
SimpleScript :: SimpleScriptOrReferenceInput era -> AnyScript era
13+
PlutusScript :: PlutusScriptOrReferenceInput lang era -> AnyScript era

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

Lines changed: 11 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -146,27 +146,28 @@ module Cardano.Api.Experimental.Tx
146146
)
147147
where
148148

149-
import Cardano.Api.Era.Internal.Core (ToCardanoEra (toCardanoEra), forEraInEon)
150-
import Cardano.Api.Era.Internal.Eon.Convert
149+
import Cardano.Api.Era.Internal.Core qualified as Api
151150
import Cardano.Api.Era.Internal.Eon.ShelleyBasedEra
152-
import Cardano.Api.Era.Internal.Feature
153151
import Cardano.Api.Experimental.Era
154152
import Cardano.Api.Experimental.Tx.Internal.AnyWitness
155-
import Cardano.Api.Experimental.Tx.Internal.Body
153+
import Cardano.Api.Experimental.Tx.Internal.BodyContent.Old
154+
( extractAllIndexedPlutusScriptWitnesses
155+
, makeUnsignedTx
156+
)
156157
import Cardano.Api.Experimental.Tx.Internal.TxScriptWitnessRequirements
158+
import Cardano.Api.Experimental.Tx.Internal.Type
157159
import Cardano.Api.HasTypeProxy (HasTypeProxy (..), Proxy, asType)
158-
import Cardano.Api.Ledger.Internal.Reexport (StrictMaybe (..), maybeToStrictMaybe)
159160
import Cardano.Api.Ledger.Internal.Reexport qualified as L
160161
import Cardano.Api.Pretty (docToString, pretty)
162+
import Cardano.Api.ProtocolParameters
161163
import Cardano.Api.Serialise.Raw
162164
( SerialiseAsRawBytes (..)
163165
, SerialiseAsRawBytesError (SerialiseAsRawBytesError)
164166
)
165-
import Cardano.Api.Tx.Internal.Body
166167
import Cardano.Api.Tx.Internal.Sign
167168

168169
import Cardano.Crypto.Hash qualified as Hash
169-
import Cardano.Ledger.Alonzo.TxBody qualified as L
170+
import Cardano.Ledger.Alonzo.Tx qualified as L
170171
import Cardano.Ledger.Api qualified as L
171172
import Cardano.Ledger.Binary qualified as Ledger
172173
import Cardano.Ledger.Core qualified as Ledger
@@ -176,150 +177,11 @@ import Control.Exception (displayException)
176177
import Data.Bifunctor (bimap)
177178
import Data.ByteString.Lazy (fromStrict)
178179
import Data.Set qualified as Set
179-
import GHC.Exts (IsList (..))
180180
import GHC.Stack
181181
import Lens.Micro
182182

183-
-- | A transaction that can contain everything
184-
-- except key witnesses.
185-
data UnsignedTx era
186-
= L.EraTx (LedgerEra era) => UnsignedTx (Ledger.Tx (LedgerEra era))
187-
188-
instance HasTypeProxy era => HasTypeProxy (UnsignedTx era) where
189-
data AsType (UnsignedTx era) = AsUnsignedTx (AsType era)
190-
proxyToAsType :: Proxy (UnsignedTx era) -> AsType (UnsignedTx era)
191-
proxyToAsType _ = AsUnsignedTx (asType @era)
192-
193-
instance
194-
( HasTypeProxy era
195-
, L.EraTx (LedgerEra era)
196-
)
197-
=> SerialiseAsRawBytes (UnsignedTx era)
198-
where
199-
serialiseToRawBytes (UnsignedTx tx) =
200-
Ledger.serialize' (Ledger.eraProtVerHigh @(LedgerEra era)) tx
201-
deserialiseFromRawBytes _ =
202-
bimap wrapError UnsignedTx
203-
. Ledger.decodeFullAnnotator
204-
(Ledger.eraProtVerHigh @(LedgerEra era))
205-
"UnsignedTx"
206-
Ledger.decCBOR
207-
. fromStrict
208-
where
209-
wrapError
210-
:: Ledger.DecoderError -> SerialiseAsRawBytesError
211-
wrapError = SerialiseAsRawBytesError . displayException
212-
213-
deriving instance Eq (UnsignedTx era)
214-
215-
deriving instance Show (UnsignedTx era)
216-
217183
newtype UnsignedTxError
218-
= UnsignedTxError TxBodyError
219-
220-
makeUnsignedTx
221-
:: Era era
222-
-> TxBodyContent BuildTx era
223-
-> Either TxBodyError (UnsignedTx era)
224-
makeUnsignedTx DijkstraEra _ = error "makeUnsignedTx: Dijkstra era not supported yet"
225-
makeUnsignedTx era@ConwayEra bc = obtainCommonConstraints era $ do
226-
let sbe = convert era
227-
aeon = convert era
228-
TxScriptWitnessRequirements languages scripts datums redeemers <-
229-
shelleyBasedEraConstraints sbe $
230-
collectTxBodyScriptWitnessRequirements (convert era) bc
231-
232-
-- cardano-api types
233-
let apiTxOuts = txOuts bc
234-
apiScriptValidity = txScriptValidity bc
235-
apiMintValue = txMintValue bc
236-
apiProtocolParameters = txProtocolParams bc
237-
apiCollateralTxIns = txInsCollateral bc
238-
apiReferenceInputs = txInsReference bc
239-
apiExtraKeyWitnesses = txExtraKeyWits bc
240-
apiReturnCollateral = txReturnCollateral bc
241-
apiTotalCollateral = txTotalCollateral bc
242-
243-
-- Ledger types
244-
txins = convTxIns $ txIns bc
245-
collTxIns = convCollateralTxIns apiCollateralTxIns
246-
refTxIns = convReferenceInputs apiReferenceInputs
247-
outs = convTxOuts sbe apiTxOuts
248-
fee = convTransactionFee sbe $ txFee bc
249-
withdrawals = convWithdrawals $ txWithdrawals bc
250-
returnCollateral = convReturnCollateral sbe apiReturnCollateral
251-
totalCollateral = convTotalCollateral apiTotalCollateral
252-
certs = convCertificates sbe $ txCertificates bc
253-
txAuxData = toAuxiliaryData sbe (txMetadata bc) (txAuxScripts bc)
254-
scriptIntegrityHash =
255-
convPParamsToScriptIntegrityHash
256-
aeon
257-
apiProtocolParameters
258-
redeemers
259-
datums
260-
languages
261-
262-
let setMint = convMintValue apiMintValue
263-
setReqSignerHashes = convExtraKeyWitnesses apiExtraKeyWitnesses
264-
ledgerTxBody =
265-
L.mkBasicTxBody
266-
& L.inputsTxBodyL .~ txins
267-
& L.collateralInputsTxBodyL .~ collTxIns
268-
& L.referenceInputsTxBodyL .~ refTxIns
269-
& L.outputsTxBodyL .~ outs
270-
& L.totalCollateralTxBodyL .~ totalCollateral
271-
& L.collateralReturnTxBodyL .~ returnCollateral
272-
& L.feeTxBodyL .~ fee
273-
& L.vldtTxBodyL . L.invalidBeforeL .~ convValidityLowerBound (txValidityLowerBound bc)
274-
& L.vldtTxBodyL . L.invalidHereAfterL .~ convValidityUpperBound sbe (txValidityUpperBound bc)
275-
& L.reqSignerHashesTxBodyL .~ setReqSignerHashes
276-
& L.scriptIntegrityHashTxBodyL .~ scriptIntegrityHash
277-
& L.withdrawalsTxBodyL .~ withdrawals
278-
& L.certsTxBodyL .~ certs
279-
& L.mintTxBodyL .~ setMint
280-
& L.auxDataHashTxBodyL .~ maybe SNothing (SJust . Ledger.hashTxAuxData) txAuxData
281-
282-
scriptWitnesses =
283-
L.mkBasicTxWits
284-
& L.scriptTxWitsL
285-
.~ fromList
286-
[ (L.hashScript sw, sw)
287-
| sw <- scripts
288-
]
289-
& L.datsTxWitsL .~ datums
290-
& L.rdmrsTxWitsL .~ redeemers
291-
292-
let eraSpecificTxBody = eraSpecificLedgerTxBody era ledgerTxBody bc
293-
294-
return . UnsignedTx $
295-
L.mkBasicTx eraSpecificTxBody
296-
& L.witsTxL .~ scriptWitnesses
297-
& L.auxDataTxL .~ maybeToStrictMaybe (toAuxiliaryData sbe (txMetadata bc) (txAuxScripts bc))
298-
& L.isValidTxL .~ txScriptValidityToIsValid apiScriptValidity
299-
300-
eraSpecificLedgerTxBody
301-
:: Era era
302-
-> Ledger.TxBody (LedgerEra era)
303-
-> TxBodyContent BuildTx era
304-
-> Ledger.TxBody (LedgerEra era)
305-
eraSpecificLedgerTxBody era ledgerbody bc =
306-
body era
307-
where
308-
body e =
309-
let propProcedures = txProposalProcedures bc
310-
voteProcedures = txVotingProcedures bc
311-
treasuryDonation = txTreasuryDonation bc
312-
currentTresuryValue = txCurrentTreasuryValue bc
313-
in obtainCommonConstraints e $
314-
ledgerbody
315-
& L.proposalProceduresTxBodyL
316-
.~ convProposalProcedures (maybe TxProposalProceduresNone unFeatured propProcedures)
317-
& L.votingProceduresTxBodyL
318-
.~ convVotingProcedures (maybe TxVotingProceduresNone unFeatured voteProcedures)
319-
& L.treasuryDonationTxBodyL
320-
.~ maybe (L.Coin 0) unFeatured treasuryDonation
321-
& L.currentTreasuryValueTxBodyL
322-
.~ L.maybeToStrictMaybe (unFeatured =<< currentTresuryValue)
184+
= UnsignedTxError String
323185

324186
hashTxBody
325187
:: L.HashAnnotated (Ledger.TxBody era) L.EraIndependentTxBody
@@ -398,8 +260,8 @@ signTx era bootstrapWits shelleyKeyWits (UnsignedTx unsigned) =
398260
convertTxBodyToUnsignedTx
399261
:: HasCallStack => ShelleyBasedEra era -> TxBody era -> UnsignedTx era
400262
convertTxBodyToUnsignedTx sbe txbody =
401-
forEraInEon
402-
(toCardanoEra sbe)
263+
Api.forEraInEon
264+
(Api.toCardanoEra sbe)
403265
(error $ "convertTxBodyToUnsignedTx: Error - unsupported era " <> docToString (pretty sbe))
404266
( \w -> do
405267
let ShelleyTx _ unsignedLedgerTx = makeSignedTransaction [] txbody

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1+
{-# LANGUAGE DataKinds #-}
12
{-# LANGUAGE GADTs #-}
3+
{-# LANGUAGE KindSignatures #-}
4+
{-# LANGUAGE ScopedTypeVariables #-}
25
{-# LANGUAGE StandaloneDeriving #-}
36

47
module Cardano.Api.Experimental.Tx.Internal.AnyWitness
58
( -- * Any witness (key, simple script, plutus script).
69
AnyWitness (..)
710
, getAnyWitnessScript
11+
, getAnyWitnessSimpleScript
812
, getAnyWitnessPlutusLanguage
913
, getAnyWitnessScriptData
14+
, getPlutusDatum
1015
)
1116
where
1217

@@ -22,6 +27,7 @@ import Cardano.Api.Experimental.Simple.Script
2227
( SimpleScript (SimpleScript)
2328
, SimpleScriptOrReferenceInput (..)
2429
)
30+
import Cardano.Api.Internal.Orphans.Misc ()
2531
import Cardano.Api.Ledger qualified as L
2632
import Cardano.Api.Plutus.Internal.ScriptData
2733

@@ -51,6 +57,34 @@ data AnyWitness era where
5157

5258
deriving instance Show (AnyWitness era)
5359

60+
instance Eq (AnyWitness era) where
61+
AnyKeyWitnessPlaceholder == AnyKeyWitnessPlaceholder = True
62+
(AnySimpleScriptWitness s1) == (AnySimpleScriptWitness s2) = s1 == s2
63+
(AnyPlutusScriptWitness (PlutusScriptWitness l1 s1 d1 r1 e1)) == (AnyPlutusScriptWitness (PlutusScriptWitness l2 s2 d2 r2 e2)) =
64+
case (l1, l2) of
65+
(L.SPlutusV1, L.SPlutusV1) -> case (d1, d2) of
66+
(InlineDatum, InlineDatum) -> s1 == s2 && r1 == r2 && e1 == e2
67+
(NoScriptDatum, NoScriptDatum) -> s1 == s2 && r1 == r2 && e1 == e2
68+
(SpendingScriptDatum d1', SpendingScriptDatum d2') -> s1 == s2 && r1 == r2 && e1 == e2 && d1' == d2'
69+
(_, _) -> False
70+
(L.SPlutusV2, L.SPlutusV2) -> case (d1, d2) of
71+
(InlineDatum, InlineDatum) -> s1 == s2 && r1 == r2 && e1 == e2
72+
(NoScriptDatum, NoScriptDatum) -> s1 == s2 && r1 == r2 && e1 == e2
73+
(SpendingScriptDatum d1', SpendingScriptDatum d2') -> s1 == s2 && r1 == r2 && e1 == e2 && d1' == d2'
74+
(_, _) -> False
75+
(L.SPlutusV3, L.SPlutusV3) -> case (d1, d2) of
76+
(InlineDatum, InlineDatum) -> s1 == s2 && r1 == r2 && e1 == e2
77+
(NoScriptDatum, NoScriptDatum) -> s1 == s2 && r1 == r2 && e1 == e2
78+
(SpendingScriptDatum d1', SpendingScriptDatum d2') -> s1 == s2 && r1 == r2 && e1 == e2 && d1' == d2'
79+
(_, _) -> False
80+
(L.SPlutusV4, L.SPlutusV4) -> case (d1, d2) of
81+
(InlineDatum, InlineDatum) -> s1 == s2 && r1 == r2 && e1 == e2
82+
(NoScriptDatum, NoScriptDatum) -> s1 == s2 && r1 == r2 && e1 == e2
83+
(SpendingScriptDatum d1', SpendingScriptDatum d2') -> s1 == s2 && r1 == r2 && e1 == e2 && d1' == d2'
84+
(_, _) -> False
85+
(_, _) -> False
86+
_ == _ = False
87+
5488
getAnyWitnessPlutusLanguage :: AnyWitness era -> Maybe L.Language
5589
getAnyWitnessPlutusLanguage AnyKeyWitnessPlaceholder = Nothing
5690
getAnyWitnessPlutusLanguage (AnySimpleScriptWitness _) = Nothing

0 commit comments

Comments
 (0)