Skip to content

Commit 7269f13

Browse files
authored
Merge pull request #763 from IntersectMBO/jordan/new-scripts-api
Introduce new witness api
2 parents dc5eacc + c86846f commit 7269f13

20 files changed

Lines changed: 1318 additions & 93 deletions

File tree

cardano-api/cardano-api.cabal

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,6 @@ library
7777
Cardano.Api.Internal.Eon.ShelleyBasedEra
7878
Cardano.Api.Internal.Eras
7979
Cardano.Api.Internal.Error
80-
Cardano.Api.Internal.Experimental.Eras
81-
Cardano.Api.Internal.Experimental.Tx
8280
Cardano.Api.Internal.Fees
8381
Cardano.Api.Internal.Genesis
8482
Cardano.Api.Internal.GenesisParameters
@@ -206,6 +204,15 @@ library
206204
Cardano.Api.Internal.Eon.ShelleyToMaryEra
207205
Cardano.Api.Internal.Eras.Case
208206
Cardano.Api.Internal.Eras.Core
207+
Cardano.Api.Internal.Experimental.Eras
208+
Cardano.Api.Internal.Experimental.Plutus.IndexedPlutusScriptWitness
209+
Cardano.Api.Internal.Experimental.Plutus.Script
210+
Cardano.Api.Internal.Experimental.Plutus.ScriptWitness
211+
Cardano.Api.Internal.Experimental.Plutus.Shim.LegacyScripts
212+
Cardano.Api.Internal.Experimental.Simple.Script
213+
Cardano.Api.Internal.Experimental.Tx
214+
Cardano.Api.Internal.Experimental.Witness.AnyWitness
215+
Cardano.Api.Internal.Experimental.Witness.TxScriptWitnessRequirements
209216
Cardano.Api.Internal.Feature
210217
Cardano.Api.Internal.Governance.Actions.ProposalProcedure
211218
Cardano.Api.Internal.Governance.Actions.VotingProcedure
@@ -255,6 +262,7 @@ library
255262
Cardano.Api.Internal.SerialiseUsing
256263
Cardano.Api.Internal.SpecialByron
257264
Cardano.Api.Internal.StakePoolMetadata
265+
Cardano.Api.Internal.Tx.BuildTxWith
258266
Cardano.Api.Internal.Tx.UTxO
259267
Cardano.Api.Internal.TxIn
260268
Cardano.Api.Internal.TxMetadata

cardano-api/gen/Test/Gen/Cardano/Api/Typed.hs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ import qualified Data.ByteString.Base16 as Base16
169169
import Data.Ratio (Ratio, (%))
170170
import Data.String
171171
import Test.Gen.Cardano.Api.Hardcoded
172+
import Data.Typeable
172173
import Data.Word (Word16, Word32, Word64)
173174
import GHC.Exts (IsList (..))
174175
import GHC.Stack
@@ -708,7 +709,7 @@ genTxWithdrawals =
708709
]
709710
)
710711

711-
genTxCertificates :: CardanoEra era -> Gen (TxCertificates BuildTx era)
712+
genTxCertificates :: Typeable era => CardanoEra era -> Gen (TxCertificates BuildTx era)
712713
genTxCertificates =
713714
inEonForEra
714715
(pure TxCertificatesNone)
@@ -721,7 +722,7 @@ genTxCertificates =
721722
]
722723
)
723724

724-
genCertificate :: forall era. ShelleyBasedEra era -> Gen (Certificate era)
725+
genCertificate :: forall era. Typeable era => ShelleyBasedEra era -> Gen (Certificate era)
725726
genCertificate sbe =
726727
Gen.choice
727728
$ catMaybes
@@ -878,7 +879,7 @@ genPolicyAssets = do
878879
(,) <$> genAssetName <*> genPositiveQuantity
879880
pure $ fromList assetQuantities
880881

881-
genTxBodyContent :: ShelleyBasedEra era -> Gen (TxBodyContent BuildTx era)
882+
genTxBodyContent :: Typeable era => ShelleyBasedEra era -> Gen (TxBodyContent BuildTx era)
882883
genTxBodyContent sbe = do
883884
let era = toCardanoEra sbe
884885
txIns <-
@@ -999,7 +1000,8 @@ genWitnessesByron = Gen.list (Range.constant 1 10) genByronKeyWitness
9991000

10001001
-- | This generator validates generated 'TxBodyContent' and backtracks when the generated body
10011002
-- fails the validation. That also means that it is quite slow.
1002-
genValidTxBody :: ShelleyBasedEra era
1003+
genValidTxBody :: Typeable era
1004+
=> ShelleyBasedEra era
10031005
-> Gen (TxBody era, TxBodyContent BuildTx era) -- ^ validated 'TxBody' and 'TxBodyContent'
10041006
genValidTxBody sbe =
10051007
Gen.mapMaybe
@@ -1010,7 +1012,7 @@ genValidTxBody sbe =
10101012
(genTxBodyContent sbe)
10111013

10121014
-- | Partial! This function will throw an error when the generated transaction is invalid.
1013-
genTxBody :: HasCallStack => ShelleyBasedEra era -> Gen (TxBody era)
1015+
genTxBody :: (HasCallStack, Typeable era) => ShelleyBasedEra era -> Gen (TxBody era)
10141016
genTxBody era = do
10151017
res <- Api.createTransactionBody era <$> genTxBodyContent era
10161018
case res of
@@ -1049,15 +1051,15 @@ genScriptValidity :: Gen ScriptValidity
10491051
genScriptValidity = Gen.element [ScriptInvalid, ScriptValid]
10501052

10511053
genTx
1052-
:: ()
1054+
:: Typeable era
10531055
=> ShelleyBasedEra era
10541056
-> Gen (Tx era)
10551057
genTx era =
10561058
makeSignedTransaction
10571059
<$> genWitnesses era
10581060
<*> (fst <$> genValidTxBody era)
10591061

1060-
genWitnesses :: ShelleyBasedEra era -> Gen [KeyWitness era]
1062+
genWitnesses :: Typeable era => ShelleyBasedEra era -> Gen [KeyWitness era]
10611063
genWitnesses sbe = do
10621064
bsWits <- Gen.list (Range.constant 0 10) (genShelleyBootstrapWitness sbe)
10631065
keyWits <- Gen.list (Range.constant 0 10) (genShelleyKeyWitness sbe)
@@ -1102,7 +1104,7 @@ genWitnessNetworkIdOrByronAddress =
11021104
]
11031105

11041106
genShelleyBootstrapWitness
1105-
:: ()
1107+
:: Typeable era
11061108
=> ShelleyBasedEra era
11071109
-> Gen (KeyWitness era)
11081110
genShelleyBootstrapWitness sbe =
@@ -1111,8 +1113,10 @@ genShelleyBootstrapWitness sbe =
11111113
<*> (fst <$> genValidTxBody sbe)
11121114
<*> genSigningKey AsByronKey
11131115

1116+
11141117
genShelleyKeyWitness
11151118
:: ()
1119+
=> Typeable era
11161120
=> ShelleyBasedEra era
11171121
-> Gen (KeyWitness era)
11181122
genShelleyKeyWitness sbe =
@@ -1121,7 +1125,7 @@ genShelleyKeyWitness sbe =
11211125
<*> genShelleyWitnessSigningKey
11221126

11231127
genShelleyWitness
1124-
:: ()
1128+
:: Typeable era
11251129
=> ShelleyBasedEra era
11261130
-> Gen (KeyWitness era)
11271131
genShelleyWitness sbe =
@@ -1142,7 +1146,7 @@ genShelleyWitnessSigningKey =
11421146
]
11431147

11441148
genCardanoKeyWitness
1145-
:: ()
1149+
:: Typeable era
11461150
=> ShelleyBasedEra era
11471151
-> Gen (KeyWitness era)
11481152
genCardanoKeyWitness = genShelleyWitness

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

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
{-# LANGUAGE StandaloneDeriving #-}
1111
{-# LANGUAGE TypeApplications #-}
1212
{-# LANGUAGE TypeFamilies #-}
13+
{-# LANGUAGE TypeOperators #-}
1314

1415
-- | Certificates embedded in transactions
1516
module Cardano.Api.Internal.Certificate
@@ -71,6 +72,7 @@ module Cardano.Api.Internal.Certificate
7172
, AsType (..)
7273

7374
-- * Internal functions
75+
, certificateToTxCert
7476
, filterUnRegCreds
7577
, filterUnRegDRepCreds
7678
, isDRepRegOrUpdateCert
@@ -99,6 +101,7 @@ import Cardano.Api.Internal.StakePoolMetadata
99101
import Cardano.Api.Internal.Utils (noInlineMaybeToStrictMaybe)
100102
import Cardano.Api.Internal.Value
101103

104+
import Cardano.Ledger.Api qualified as L
102105
import Cardano.Ledger.BaseTypes (strictMaybe)
103106
import Cardano.Ledger.Coin qualified as L
104107
import Cardano.Ledger.Keys qualified as Ledger
@@ -111,6 +114,7 @@ import Data.Maybe
111114
import Data.Text (Text)
112115
import Data.Text qualified as Text
113116
import Data.Text.Encoding qualified as Text
117+
import Data.Type.Equality (TestEquality (..))
114118
import Data.Typeable
115119
import GHC.Exts (IsList (..), fromString)
116120
import Network.Socket (PortNumber)
@@ -129,13 +133,15 @@ data Certificate era where
129133
-- 6. Genesis delegation
130134
-- 7. MIR certificates
131135
ShelleyRelatedCertificate
132-
:: ShelleyToBabbageEra era
136+
:: Typeable era
137+
=> ShelleyToBabbageEra era
133138
-> Ledger.ShelleyTxCert (ShelleyLedgerEra era)
134139
-> Certificate era
135140
-- Conway onwards
136141
-- TODO: Add comments about the new types of certificates
137142
ConwayCertificate
138-
:: ConwayEraOnwards era
143+
:: Typeable era
144+
=> ConwayEraOnwards era
139145
-> Ledger.ConwayTxCert (ShelleyLedgerEra era)
140146
-> Certificate era
141147
deriving anyclass SerialiseAsCBOR
@@ -146,6 +152,28 @@ deriving instance Ord (Certificate era)
146152

147153
deriving instance Show (Certificate era)
148154

155+
instance TestEquality Certificate where
156+
testEquality (ShelleyRelatedCertificate _ c) (ShelleyRelatedCertificate _ c') =
157+
shelleyCertTypeEquality c c'
158+
testEquality (ConwayCertificate _ c) (ConwayCertificate _ c') =
159+
conwayCertTypeEquality c c'
160+
testEquality ShelleyRelatedCertificate{} ConwayCertificate{} = Nothing
161+
testEquality ConwayCertificate{} ShelleyRelatedCertificate{} = Nothing
162+
163+
conwayCertTypeEquality
164+
:: (Typeable eraA, Typeable eraB)
165+
=> Ledger.ConwayTxCert (ShelleyLedgerEra eraA)
166+
-> Ledger.ConwayTxCert (ShelleyLedgerEra eraB)
167+
-> Maybe (eraA :~: eraB)
168+
conwayCertTypeEquality _ _ = eqT
169+
170+
shelleyCertTypeEquality
171+
:: (Typeable eraA, Typeable eraB)
172+
=> Ledger.ShelleyTxCert (ShelleyLedgerEra eraA)
173+
-> Ledger.ShelleyTxCert (ShelleyLedgerEra eraB)
174+
-> Maybe (eraA :~: eraB)
175+
shelleyCertTypeEquality _ _ = eqT
176+
149177
instance Typeable era => HasTypeProxy (Certificate era) where
150178
data AsType (Certificate era) = AsCertificate
151179
proxyToAsType _ = AsCertificate
@@ -198,6 +226,20 @@ instance
198226
ConwayCertificate _ (Ledger.ConwayTxCertPool Ledger.RegPool{}) -> "Pool registration"
199227
ConwayCertificate _ (Ledger.ConwayTxCertPool Ledger.RetirePool{}) -> "Pool retirement"
200228

229+
certificateToTxCert :: Certificate era -> L.TxCert (ShelleyLedgerEra era)
230+
certificateToTxCert c =
231+
case c of
232+
ShelleyRelatedCertificate eon cert ->
233+
case eon of
234+
ShelleyToBabbageEraShelley -> cert
235+
ShelleyToBabbageEraAllegra -> cert
236+
ShelleyToBabbageEraMary -> cert
237+
ShelleyToBabbageEraAlonzo -> cert
238+
ShelleyToBabbageEraBabbage -> cert
239+
ConwayCertificate eon cert ->
240+
case eon of
241+
ConwayEraOnwardsConway -> cert
242+
201243
-- ----------------------------------------------------------------------------
202244
-- Stake pool parameters
203245
--
@@ -373,7 +415,8 @@ data GenesisKeyDelegationRequirements era where
373415
-> Hash VrfKey
374416
-> GenesisKeyDelegationRequirements era
375417

376-
makeGenesisKeyDelegationCertificate :: GenesisKeyDelegationRequirements era -> Certificate era
418+
makeGenesisKeyDelegationCertificate
419+
:: Typeable era => GenesisKeyDelegationRequirements era -> Certificate era
377420
makeGenesisKeyDelegationCertificate
378421
( GenesisKeyDelegationRequirements
379422
atMostEra
@@ -394,7 +437,7 @@ data MirCertificateRequirements era where
394437
-> MirCertificateRequirements era
395438

396439
makeMIRCertificate
397-
:: ()
440+
:: Typeable era
398441
=> MirCertificateRequirements era
399442
-> Certificate era
400443
makeMIRCertificate (MirCertificateRequirements atMostEra mirPot mirTarget) =
@@ -410,7 +453,7 @@ data DRepRegistrationRequirements era where
410453
-> DRepRegistrationRequirements era
411454

412455
makeDrepRegistrationCertificate
413-
:: ()
456+
:: Typeable era
414457
=> DRepRegistrationRequirements era
415458
-> Maybe (Ledger.Anchor (EraCrypto (ShelleyLedgerEra era)))
416459
-> Certificate era
@@ -427,7 +470,7 @@ data CommitteeHotKeyAuthorizationRequirements era where
427470
-> CommitteeHotKeyAuthorizationRequirements era
428471

429472
makeCommitteeHotKeyAuthorizationCertificate
430-
:: ()
473+
:: Typeable era
431474
=> CommitteeHotKeyAuthorizationRequirements era
432475
-> Certificate era
433476
makeCommitteeHotKeyAuthorizationCertificate (CommitteeHotKeyAuthorizationRequirements cOnwards coldKeyCredential hotKeyCredential) =
@@ -443,7 +486,7 @@ data CommitteeColdkeyResignationRequirements era where
443486
-> CommitteeColdkeyResignationRequirements era
444487

445488
makeCommitteeColdkeyResignationCertificate
446-
:: ()
489+
:: Typeable era
447490
=> CommitteeColdkeyResignationRequirements era
448491
-> Certificate era
449492
makeCommitteeColdkeyResignationCertificate (CommitteeColdkeyResignationRequirements cOnwards coldKeyCred anchor) =
@@ -461,7 +504,7 @@ data DRepUnregistrationRequirements era where
461504
-> DRepUnregistrationRequirements era
462505

463506
makeDrepUnregistrationCertificate
464-
:: ()
507+
:: Typeable era
465508
=> DRepUnregistrationRequirements era
466509
-> Certificate era
467510
makeDrepUnregistrationCertificate (DRepUnregistrationRequirements conwayOnwards vcred deposit) =
@@ -488,7 +531,8 @@ data DRepUpdateRequirements era where
488531
-> DRepUpdateRequirements era
489532

490533
makeDrepUpdateCertificate
491-
:: DRepUpdateRequirements era
534+
:: Typeable era
535+
=> DRepUpdateRequirements era
492536
-> Maybe (Ledger.Anchor (EraCrypto (ShelleyLedgerEra era)))
493537
-> Certificate era
494538
makeDrepUpdateCertificate (DRepUpdateRequirements conwayOnwards vcred) mAnchor =

cardano-api/src/Cardano/Api/Internal/Eon/AlonzoEraOnwards.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import Cardano.Binary
2929
import Cardano.Crypto.Hash.Blake2b qualified as Blake2b
3030
import Cardano.Crypto.Hash.Class qualified as C
3131
import Cardano.Crypto.VRF qualified as C
32+
import Cardano.Ledger.Allegra.Scripts qualified as L
3233
import Cardano.Ledger.Alonzo.Plutus.Context qualified as Plutus
3334
import Cardano.Ledger.Alonzo.Scripts qualified as L
3435
import Cardano.Ledger.Alonzo.Tx qualified as L
@@ -103,6 +104,7 @@ type AlonzoEraOnwardsConstraints era =
103104
, L.EraUTxO (ShelleyLedgerEra era)
104105
, L.HashAnnotated (L.TxBody (ShelleyLedgerEra era)) L.EraIndependentTxBody L.StandardCrypto
105106
, L.MaryEraTxBody (ShelleyLedgerEra era)
107+
, L.NativeScript (ShelleyLedgerEra era) ~ L.Timelock (ShelleyLedgerEra era)
106108
, Plutus.EraPlutusContext (ShelleyLedgerEra era)
107109
, L.Script (ShelleyLedgerEra era) ~ L.AlonzoScript (ShelleyLedgerEra era)
108110
, L.ScriptsNeeded (ShelleyLedgerEra era) ~ L.AlonzoScriptsNeeded (ShelleyLedgerEra era)

cardano-api/src/Cardano/Api/Internal/Experimental/Eras.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ module Cardano.Api.Internal.Experimental.Eras
3232
)
3333
where
3434

35+
import Cardano.Api.Internal.Eon.AlonzoEraOnwards
3536
import Cardano.Api.Internal.Eon.BabbageEraOnwards
3637
import Cardano.Api.Internal.Eon.Convert
3738
import Cardano.Api.Internal.Eon.ShelleyBasedEra (ShelleyBasedEra (..), ShelleyLedgerEra)
@@ -219,6 +220,11 @@ instance Convert Era ShelleyBasedEra where
219220
BabbageEra -> ShelleyBasedEraBabbage
220221
ConwayEra -> ShelleyBasedEraConway
221222

223+
instance Convert Era AlonzoEraOnwards where
224+
convert = \case
225+
BabbageEra -> AlonzoEraOnwardsBabbage
226+
ConwayEra -> AlonzoEraOnwardsConway
227+
222228
instance Convert Era BabbageEraOnwards where
223229
convert = \case
224230
BabbageEra -> BabbageEraOnwardsBabbage

0 commit comments

Comments
 (0)