Skip to content

Commit

Permalink
Everything builds! Need to sorrt out getScriptIntegrityHash
Browse files Browse the repository at this point in the history
  • Loading branch information
Jimbo4350 committed Feb 27, 2025
1 parent 50702e6 commit 53e997c
Show file tree
Hide file tree
Showing 16 changed files with 1,213 additions and 66 deletions.
9 changes: 7 additions & 2 deletions cardano-api/cardano-api.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ library
Cardano.Api.Internal.Eon.ShelleyBasedEra
Cardano.Api.Internal.Eras
Cardano.Api.Internal.Error
Cardano.Api.Internal.Experimental.Eras
Cardano.Api.Internal.Experimental.Tx
Cardano.Api.Internal.Fees
Cardano.Api.Internal.Genesis
Cardano.Api.Internal.GenesisParameters
Expand Down Expand Up @@ -200,6 +198,12 @@ library
Cardano.Api.Internal.Eon.ShelleyToMaryEra
Cardano.Api.Internal.Eras.Case
Cardano.Api.Internal.Eras.Core
Cardano.Api.Internal.Experimental.Eras
Cardano.Api.Internal.Experimental.Plutus.Shim.LegacyScripts
Cardano.Api.Internal.Experimental.Plutus.Script
Cardano.Api.Internal.Experimental.Plutus.ScriptWitness
Cardano.Api.Internal.Experimental.Simple.Script
Cardano.Api.Internal.Experimental.Tx
Cardano.Api.Internal.Feature
Cardano.Api.Internal.Governance.Actions.ProposalProcedure
Cardano.Api.Internal.Governance.Actions.VotingProcedure
Expand Down Expand Up @@ -248,6 +252,7 @@ library
Cardano.Api.Internal.SerialiseUsing
Cardano.Api.Internal.SpecialByron
Cardano.Api.Internal.StakePoolMetadata
Cardano.Api.Internal.Tx.BuildTxWith
Cardano.Api.Internal.Tx.UTxO
Cardano.Api.Internal.TxIn
Cardano.Api.Internal.TxMetadata
Expand Down
24 changes: 14 additions & 10 deletions cardano-api/gen/Test/Gen/Cardano/Api/Typed.hs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ import qualified Data.ByteString.Base16 as Base16
import Data.Ratio (Ratio, (%))
import Data.String
import Test.Gen.Cardano.Api.Hardcoded
import Data.Typeable
import Data.Word (Word16, Word32, Word64)
import GHC.Exts (IsList (..))
import GHC.Stack
Expand Down Expand Up @@ -707,7 +708,7 @@ genTxWithdrawals =
]
)

genTxCertificates :: CardanoEra era -> Gen (TxCertificates BuildTx era)
genTxCertificates :: Typeable era => CardanoEra era -> Gen (TxCertificates BuildTx era)
genTxCertificates =
inEonForEra
(pure TxCertificatesNone)
Expand All @@ -720,7 +721,7 @@ genTxCertificates =
]
)

genCertificate :: forall era. ShelleyBasedEra era -> Gen (Certificate era)
genCertificate :: forall era. Typeable era => ShelleyBasedEra era -> Gen (Certificate era)
genCertificate sbe =
Gen.choice
$ catMaybes
Expand Down Expand Up @@ -871,7 +872,7 @@ genTxMintValue =
, pure $ TxMintValue w (fromList assets)
]

genTxBodyContent :: ShelleyBasedEra era -> Gen (TxBodyContent BuildTx era)
genTxBodyContent :: Typeable era => ShelleyBasedEra era -> Gen (TxBodyContent BuildTx era)
genTxBodyContent sbe = do
let era = toCardanoEra sbe
txIns <-
Expand Down Expand Up @@ -992,7 +993,8 @@ genWitnessesByron = Gen.list (Range.constant 1 10) genByronKeyWitness

-- | This generator validates generated 'TxBodyContent' and backtracks when the generated body
-- fails the validation. That also means that it is quite slow.
genValidTxBody :: ShelleyBasedEra era
genValidTxBody :: Typeable era
=> ShelleyBasedEra era
-> Gen (TxBody era, TxBodyContent BuildTx era) -- ^ validated 'TxBody' and 'TxBodyContent'
genValidTxBody sbe =
Gen.mapMaybe
Expand All @@ -1003,7 +1005,7 @@ genValidTxBody sbe =
(genTxBodyContent sbe)

-- | Partial! This function will throw an error when the generated transaction is invalid.
genTxBody :: HasCallStack => ShelleyBasedEra era -> Gen (TxBody era)
genTxBody :: (HasCallStack, Typeable era) => ShelleyBasedEra era -> Gen (TxBody era)
genTxBody era = do
res <- Api.createTransactionBody era <$> genTxBodyContent era
case res of
Expand Down Expand Up @@ -1042,15 +1044,15 @@ genScriptValidity :: Gen ScriptValidity
genScriptValidity = Gen.element [ScriptInvalid, ScriptValid]

genTx
:: ()
:: Typeable era
=> ShelleyBasedEra era
-> Gen (Tx era)
genTx era =
makeSignedTransaction
<$> genWitnesses era
<*> (fst <$> genValidTxBody era)

genWitnesses :: ShelleyBasedEra era -> Gen [KeyWitness era]
genWitnesses :: Typeable era => ShelleyBasedEra era -> Gen [KeyWitness era]
genWitnesses sbe = do
bsWits <- Gen.list (Range.constant 0 10) (genShelleyBootstrapWitness sbe)
keyWits <- Gen.list (Range.constant 0 10) (genShelleyKeyWitness sbe)
Expand Down Expand Up @@ -1095,7 +1097,7 @@ genWitnessNetworkIdOrByronAddress =
]

genShelleyBootstrapWitness
:: ()
:: Typeable era
=> ShelleyBasedEra era
-> Gen (KeyWitness era)
genShelleyBootstrapWitness sbe =
Expand All @@ -1104,8 +1106,10 @@ genShelleyBootstrapWitness sbe =
<*> (fst <$> genValidTxBody sbe)
<*> genSigningKey AsByronKey


genShelleyKeyWitness
:: ()
=> Typeable era
=> ShelleyBasedEra era
-> Gen (KeyWitness era)
genShelleyKeyWitness sbe =
Expand All @@ -1114,7 +1118,7 @@ genShelleyKeyWitness sbe =
<*> genShelleyWitnessSigningKey

genShelleyWitness
:: ()
:: Typeable era
=> ShelleyBasedEra era
-> Gen (KeyWitness era)
genShelleyWitness sbe =
Expand All @@ -1135,7 +1139,7 @@ genShelleyWitnessSigningKey =
]

genCardanoKeyWitness
:: ()
:: Typeable era
=> ShelleyBasedEra era
-> Gen (KeyWitness era)
genCardanoKeyWitness = genShelleyWitness
Expand Down
45 changes: 36 additions & 9 deletions cardano-api/src/Cardano/Api/Internal/Certificate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}

-- | Certificates embedded in transactions
module Cardano.Api.Internal.Certificate
Expand Down Expand Up @@ -111,6 +112,7 @@ import Data.Maybe
import Data.Text (Text)
import Data.Text qualified as Text
import Data.Text.Encoding qualified as Text
import Data.Type.Equality (TestEquality (..))
import Data.Typeable
import GHC.Exts (IsList (..), fromString)
import Network.Socket (PortNumber)
Expand All @@ -129,13 +131,15 @@ data Certificate era where
-- 6. Genesis delegation
-- 7. MIR certificates
ShelleyRelatedCertificate
:: ShelleyToBabbageEra era
:: Typeable era
=> ShelleyToBabbageEra era
-> Ledger.ShelleyTxCert (ShelleyLedgerEra era)
-> Certificate era
-- Conway onwards
-- TODO: Add comments about the new types of certificates
ConwayCertificate
:: ConwayEraOnwards era
:: Typeable era
=> ConwayEraOnwards era
-> Ledger.ConwayTxCert (ShelleyLedgerEra era)
-> Certificate era
deriving anyclass SerialiseAsCBOR
Expand All @@ -146,6 +150,27 @@ deriving instance Ord (Certificate era)

deriving instance Show (Certificate era)

instance TestEquality Certificate where
testEquality (ShelleyRelatedCertificate _ c) (ShelleyRelatedCertificate _ c') =
shelleyCertTypeEquality c c'
testEquality (ConwayCertificate _ c) (ConwayCertificate _ c') =
conwayCertTypeEquality c c'
testEquality _ _ = Nothing

conwayCertTypeEquality
:: (Typeable eraA, Typeable eraB)
=> Ledger.ConwayTxCert (ShelleyLedgerEra eraA)
-> Ledger.ConwayTxCert (ShelleyLedgerEra eraB)
-> Maybe (eraA :~: eraB)
conwayCertTypeEquality _ _ = eqT

shelleyCertTypeEquality
:: (Typeable eraA, Typeable eraB)
=> Ledger.ShelleyTxCert (ShelleyLedgerEra eraA)
-> Ledger.ShelleyTxCert (ShelleyLedgerEra eraB)
-> Maybe (eraA :~: eraB)
shelleyCertTypeEquality _ _ = eqT

instance Typeable era => HasTypeProxy (Certificate era) where
data AsType (Certificate era) = AsCertificate
proxyToAsType _ = AsCertificate
Expand Down Expand Up @@ -373,7 +398,8 @@ data GenesisKeyDelegationRequirements era where
-> Hash VrfKey
-> GenesisKeyDelegationRequirements era

makeGenesisKeyDelegationCertificate :: GenesisKeyDelegationRequirements era -> Certificate era
makeGenesisKeyDelegationCertificate
:: Typeable era => GenesisKeyDelegationRequirements era -> Certificate era
makeGenesisKeyDelegationCertificate
( GenesisKeyDelegationRequirements
atMostEra
Expand All @@ -394,7 +420,7 @@ data MirCertificateRequirements era where
-> MirCertificateRequirements era

makeMIRCertificate
:: ()
:: Typeable era
=> MirCertificateRequirements era
-> Certificate era
makeMIRCertificate (MirCertificateRequirements atMostEra mirPot mirTarget) =
Expand All @@ -410,7 +436,7 @@ data DRepRegistrationRequirements era where
-> DRepRegistrationRequirements era

makeDrepRegistrationCertificate
:: ()
:: Typeable era
=> DRepRegistrationRequirements era
-> Maybe (Ledger.Anchor (EraCrypto (ShelleyLedgerEra era)))
-> Certificate era
Expand All @@ -427,7 +453,7 @@ data CommitteeHotKeyAuthorizationRequirements era where
-> CommitteeHotKeyAuthorizationRequirements era

makeCommitteeHotKeyAuthorizationCertificate
:: ()
:: Typeable era
=> CommitteeHotKeyAuthorizationRequirements era
-> Certificate era
makeCommitteeHotKeyAuthorizationCertificate (CommitteeHotKeyAuthorizationRequirements cOnwards coldKeyCredential hotKeyCredential) =
Expand All @@ -443,7 +469,7 @@ data CommitteeColdkeyResignationRequirements era where
-> CommitteeColdkeyResignationRequirements era

makeCommitteeColdkeyResignationCertificate
:: ()
:: Typeable era
=> CommitteeColdkeyResignationRequirements era
-> Certificate era
makeCommitteeColdkeyResignationCertificate (CommitteeColdkeyResignationRequirements cOnwards coldKeyCred anchor) =
Expand All @@ -461,7 +487,7 @@ data DRepUnregistrationRequirements era where
-> DRepUnregistrationRequirements era

makeDrepUnregistrationCertificate
:: ()
:: Typeable era
=> DRepUnregistrationRequirements era
-> Certificate era
makeDrepUnregistrationCertificate (DRepUnregistrationRequirements conwayOnwards vcred deposit) =
Expand All @@ -488,7 +514,8 @@ data DRepUpdateRequirements era where
-> DRepUpdateRequirements era

makeDrepUpdateCertificate
:: DRepUpdateRequirements era
:: Typeable era
=> DRepUpdateRequirements era
-> Maybe (Ledger.Anchor (EraCrypto (ShelleyLedgerEra era)))
-> Certificate era
makeDrepUpdateCertificate (DRepUpdateRequirements conwayOnwards vcred) mAnchor =
Expand Down
2 changes: 2 additions & 0 deletions cardano-api/src/Cardano/Api/Internal/Eon/AlonzoEraOnwards.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import Cardano.Binary
import Cardano.Crypto.Hash.Blake2b qualified as Blake2b
import Cardano.Crypto.Hash.Class qualified as C
import Cardano.Crypto.VRF qualified as C
import Cardano.Ledger.Allegra.Scripts qualified as L
import Cardano.Ledger.Alonzo.Plutus.Context qualified as Plutus
import Cardano.Ledger.Alonzo.Scripts qualified as L
import Cardano.Ledger.Alonzo.Tx qualified as L
Expand Down Expand Up @@ -103,6 +104,7 @@ type AlonzoEraOnwardsConstraints era =
, L.EraUTxO (ShelleyLedgerEra era)
, L.HashAnnotated (L.TxBody (ShelleyLedgerEra era)) L.EraIndependentTxBody L.StandardCrypto
, L.MaryEraTxBody (ShelleyLedgerEra era)
, L.NativeScript (ShelleyLedgerEra era) ~ L.Timelock (ShelleyLedgerEra era)
, Plutus.EraPlutusContext (ShelleyLedgerEra era)
, L.Script (ShelleyLedgerEra era) ~ L.AlonzoScript (ShelleyLedgerEra era)
, L.ScriptsNeeded (ShelleyLedgerEra era) ~ L.AlonzoScriptsNeeded (ShelleyLedgerEra era)
Expand Down
6 changes: 6 additions & 0 deletions cardano-api/src/Cardano/Api/Internal/Experimental/Eras.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ module Cardano.Api.Internal.Experimental.Eras
)
where

import Cardano.Api.Internal.Eon.AlonzoEraOnwards
import Cardano.Api.Internal.Eon.BabbageEraOnwards
import Cardano.Api.Internal.Eon.Convert
import Cardano.Api.Internal.Eon.ShelleyBasedEra (ShelleyBasedEra (..), ShelleyLedgerEra)
Expand Down Expand Up @@ -206,6 +207,11 @@ instance Convert Era ShelleyBasedEra where
BabbageEra -> ShelleyBasedEraBabbage
ConwayEra -> ShelleyBasedEraConway

instance Convert Era AlonzoEraOnwards where
convert = \case
BabbageEra -> AlonzoEraOnwardsBabbage
ConwayEra -> AlonzoEraOnwardsConway

instance Convert Era BabbageEraOnwards where
convert = \case
BabbageEra -> BabbageEraOnwardsBabbage
Expand Down
48 changes: 48 additions & 0 deletions cardano-api/src/Cardano/Api/Internal/Experimental/Plutus/Script.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE StandaloneDeriving #-}

module Cardano.Api.Internal.Experimental.Plutus.Script
( PlutusScriptInEra (..)
, PlutusScriptOrReferenceInput (..)
)
where

import Cardano.Api.Internal.TxIn (TxIn)

import Cardano.Ledger.Plutus.Language (PlutusRunnable)
import Cardano.Ledger.Plutus.Language qualified as L

-- | A Plutus script in a particular era.
-- Why PlutusRunnable? Mainly for deserialization benefits.
-- The deserialization of this type looks at the
-- major protocol version and the script language to determine if
-- indeed the script is runnable. This is a dramatic improvement over the old api
-- which essentially read a 'ByteString'. Any failures due to malformed/invalid scripts
-- were caught upon transaction submission or running the script when attempting to
-- predict the necessary execution units.
--
-- Where do we get the major protocol version from?
-- In order to access the major protocol version we pass in an 'era` type parameter which
-- can be translated to the major protocol version.
--
-- Where do we get the script language from?
-- The serialized version of 'PlutusRunnable' encodes the script language.
-- See `DecCBOR (PlutusRunnable l)` in cardano-ledger for more details.
data PlutusScriptInEra (lang :: L.Language) era where
PlutusScriptInEra
:: PlutusRunnable lang -> PlutusScriptInEra lang era

deriving instance Show (PlutusScriptInEra lang era)

-- :: IsEra era => PlutusRunnable lang -> PlutusScriptInEra lang era

-- | You can provide the plutus script directly in the transaction
-- or a reference input that points to the script in the UTxO.
-- Using a reference script saves space in your transaction.
data PlutusScriptOrReferenceInput lang era
= PScript (PlutusScriptInEra lang era)
| PReferenceScript TxIn
deriving Show
Loading

0 comments on commit 53e997c

Please sign in to comment.