4
4
{-# LANGUAGE FlexibleInstances #-}
5
5
{-# LANGUAGE GADTs #-}
6
6
{-# LANGUAGE InstanceSigs #-}
7
+ {-# LANGUAGE LambdaCase #-}
7
8
{-# LANGUAGE MultiParamTypeClasses #-}
8
9
{-# LANGUAGE NamedFieldPuns #-}
9
10
{-# LANGUAGE PatternSynonyms #-}
@@ -47,25 +48,27 @@ module Cardano.Api.Script
47
48
-- * Reference scripts
48
49
, ReferenceScript (.. )
49
50
, refScriptToShelleyScript
50
- , getScriptWitnessReferenceInput
51
51
52
52
-- * Use of a script in an era as a witness
53
53
, WitCtxTxIn
54
54
, WitCtxMint
55
55
, WitCtxStake
56
56
, WitCtx (.. )
57
57
, ScriptWitness (.. )
58
+ , getScriptWitnessReferenceInput
59
+ , getScriptWitnessScript
60
+ , getScriptWitnessReferenceInputOrScript
58
61
, Witness (.. )
59
62
, KeyWitnessInCtx (.. )
60
63
, ScriptWitnessInCtx (.. )
61
64
, IsScriptWitnessInCtx (.. )
62
65
, ScriptDatum (.. )
63
66
, ScriptRedeemer
64
- , scriptWitnessScript
65
67
66
68
-- ** Languages supported in each era
67
69
, ScriptLanguageInEra (.. )
68
70
, scriptLanguageSupportedInEra
71
+ , sbeToSimpleScriptLanguageInEra
69
72
, languageOfScriptLanguageInEra
70
73
, eraOfScriptLanguageInEra
71
74
@@ -228,7 +231,8 @@ instance HasTypeProxy PlutusScriptV3 where
228
231
--
229
232
data ScriptLanguage lang where
230
233
SimpleScriptLanguage :: ScriptLanguage SimpleScript'
231
- PlutusScriptLanguage :: PlutusScriptVersion lang -> ScriptLanguage lang
234
+ PlutusScriptLanguage
235
+ :: IsPlutusScriptLanguage lang => PlutusScriptVersion lang -> ScriptLanguage lang
232
236
233
237
deriving instance (Eq (ScriptLanguage lang ))
234
238
@@ -285,7 +289,8 @@ instance Bounded AnyScriptLanguage where
285
289
286
290
data AnyPlutusScriptVersion where
287
291
AnyPlutusScriptVersion
288
- :: PlutusScriptVersion lang
292
+ :: IsPlutusScriptLanguage lang
293
+ => PlutusScriptVersion lang
289
294
-> AnyPlutusScriptVersion
290
295
291
296
deriving instance (Show AnyPlutusScriptVersion )
@@ -407,7 +412,8 @@ data Script lang where
407
412
:: ! SimpleScript
408
413
-> Script SimpleScript'
409
414
PlutusScript
410
- :: ! (PlutusScriptVersion lang )
415
+ :: IsPlutusScriptLanguage lang
416
+ => ! (PlutusScriptVersion lang )
411
417
-> ! (PlutusScript lang )
412
418
-> Script lang
413
419
@@ -576,18 +582,8 @@ scriptLanguageSupportedInEra
576
582
-> Maybe (ScriptLanguageInEra lang era )
577
583
scriptLanguageSupportedInEra era lang =
578
584
case (era, lang) of
579
- (ShelleyBasedEraShelley , SimpleScriptLanguage ) ->
580
- Just SimpleScriptInShelley
581
- (ShelleyBasedEraAllegra , SimpleScriptLanguage ) ->
582
- Just SimpleScriptInAllegra
583
- (ShelleyBasedEraMary , SimpleScriptLanguage ) ->
584
- Just SimpleScriptInMary
585
- (ShelleyBasedEraAlonzo , SimpleScriptLanguage ) ->
586
- Just SimpleScriptInAlonzo
587
- (ShelleyBasedEraBabbage , SimpleScriptLanguage ) ->
588
- Just SimpleScriptInBabbage
589
- (ShelleyBasedEraConway , SimpleScriptLanguage ) ->
590
- Just SimpleScriptInConway
585
+ (sbe, SimpleScriptLanguage ) ->
586
+ Just $ sbeToSimpleScriptLanguageInEra sbe
591
587
(ShelleyBasedEraAlonzo , PlutusScriptLanguage PlutusScriptV1 ) ->
592
588
Just PlutusScriptV1InAlonzo
593
589
(ShelleyBasedEraBabbage , PlutusScriptLanguage PlutusScriptV1 ) ->
@@ -620,23 +616,33 @@ languageOfScriptLanguageInEra langInEra =
620
616
PlutusScriptV2InConway -> PlutusScriptLanguage PlutusScriptV2
621
617
PlutusScriptV3InConway -> PlutusScriptLanguage PlutusScriptV3
622
618
619
+ sbeToSimpleScriptLanguageInEra
620
+ :: ShelleyBasedEra era
621
+ -> ScriptLanguageInEra SimpleScript' era
622
+ sbeToSimpleScriptLanguageInEra = \ case
623
+ ShelleyBasedEraShelley -> SimpleScriptInShelley
624
+ ShelleyBasedEraAllegra -> SimpleScriptInAllegra
625
+ ShelleyBasedEraMary -> SimpleScriptInMary
626
+ ShelleyBasedEraAlonzo -> SimpleScriptInAlonzo
627
+ ShelleyBasedEraBabbage -> SimpleScriptInBabbage
628
+ ShelleyBasedEraConway -> SimpleScriptInConway
629
+
623
630
eraOfScriptLanguageInEra
624
631
:: ScriptLanguageInEra lang era
625
632
-> ShelleyBasedEra era
626
- eraOfScriptLanguageInEra langInEra =
627
- case langInEra of
628
- SimpleScriptInShelley -> ShelleyBasedEraShelley
629
- SimpleScriptInAllegra -> ShelleyBasedEraAllegra
630
- SimpleScriptInMary -> ShelleyBasedEraMary
631
- SimpleScriptInAlonzo -> ShelleyBasedEraAlonzo
632
- SimpleScriptInBabbage -> ShelleyBasedEraBabbage
633
- SimpleScriptInConway -> ShelleyBasedEraConway
634
- PlutusScriptV1InAlonzo -> ShelleyBasedEraAlonzo
635
- PlutusScriptV1InBabbage -> ShelleyBasedEraBabbage
636
- PlutusScriptV1InConway -> ShelleyBasedEraConway
637
- PlutusScriptV2InBabbage -> ShelleyBasedEraBabbage
638
- PlutusScriptV2InConway -> ShelleyBasedEraConway
639
- PlutusScriptV3InConway -> ShelleyBasedEraConway
633
+ eraOfScriptLanguageInEra = \ case
634
+ SimpleScriptInShelley -> ShelleyBasedEraShelley
635
+ SimpleScriptInAllegra -> ShelleyBasedEraAllegra
636
+ SimpleScriptInMary -> ShelleyBasedEraMary
637
+ SimpleScriptInAlonzo -> ShelleyBasedEraAlonzo
638
+ SimpleScriptInBabbage -> ShelleyBasedEraBabbage
639
+ SimpleScriptInConway -> ShelleyBasedEraConway
640
+ PlutusScriptV1InAlonzo -> ShelleyBasedEraAlonzo
641
+ PlutusScriptV1InBabbage -> ShelleyBasedEraBabbage
642
+ PlutusScriptV1InConway -> ShelleyBasedEraConway
643
+ PlutusScriptV2InBabbage -> ShelleyBasedEraBabbage
644
+ PlutusScriptV2InConway -> ShelleyBasedEraConway
645
+ PlutusScriptV3InConway -> ShelleyBasedEraConway
640
646
641
647
-- | Given a target era and a script in some language, check if the language is
642
648
-- supported in that era, and if so return a 'ScriptInEra'.
@@ -682,27 +688,14 @@ data WitCtx witctx where
682
688
-- or to mint tokens. This datatype encapsulates this concept.
683
689
data PlutusScriptOrReferenceInput lang
684
690
= PScript (PlutusScript lang )
685
- | -- | Needed to construct the redeemer pointer map
686
- -- in the case of minting reference scripts where we don't
687
- -- have direct access to the script
688
- PReferenceScript
689
- TxIn
690
- (Maybe ScriptHash )
691
+ | PReferenceScript TxIn
691
692
deriving (Eq , Show )
692
693
693
694
data SimpleScriptOrReferenceInput lang
694
695
= SScript SimpleScript
695
- | SReferenceScript TxIn ( Maybe ScriptHash )
696
+ | SReferenceScript TxIn
696
697
deriving (Eq , Show )
697
698
698
- getScriptWitnessReferenceInput :: ScriptWitness witctx era -> Maybe TxIn
699
- getScriptWitnessReferenceInput (SimpleScriptWitness _ (SReferenceScript txIn _)) =
700
- Just txIn
701
- getScriptWitnessReferenceInput (PlutusScriptWitness _ _ (PReferenceScript txIn _) _ _ _) =
702
- Just txIn
703
- getScriptWitnessReferenceInput (SimpleScriptWitness _ (SScript _)) = Nothing
704
- getScriptWitnessReferenceInput (PlutusScriptWitness _ _ (PScript _) _ _ _) = Nothing
705
-
706
699
-- | A /use/ of a script within a transaction body to witness that something is
707
700
-- being used in an authorised manner. That can be
708
701
--
@@ -721,7 +714,8 @@ data ScriptWitness witctx era where
721
714
-> SimpleScriptOrReferenceInput SimpleScript'
722
715
-> ScriptWitness witctx era
723
716
PlutusScriptWitness
724
- :: ScriptLanguageInEra lang era
717
+ :: IsPlutusScriptLanguage lang
718
+ => ScriptLanguageInEra lang era
725
719
-> PlutusScriptVersion lang
726
720
-> PlutusScriptOrReferenceInput lang
727
721
-> ScriptDatum witctx
@@ -782,28 +776,26 @@ deriving instance Eq (ScriptDatum witctx)
782
776
783
777
deriving instance Show (ScriptDatum witctx )
784
778
785
- -- We cannot always extract a script from a script witness due to reference scripts.
779
+ getScriptWitnessReferenceInput :: ScriptWitness witctx era -> Maybe TxIn
780
+ getScriptWitnessReferenceInput = either (const Nothing ) Just . getScriptWitnessReferenceInputOrScript
781
+
782
+ getScriptWitnessScript :: ScriptWitness witctx era -> Maybe (ScriptInEra era )
783
+ getScriptWitnessScript = either Just (const Nothing ) . getScriptWitnessReferenceInputOrScript
784
+
785
+ -- | We cannot always extract a script from a script witness due to reference scripts.
786
786
-- Reference scripts exist in the UTxO, so without access to the UTxO we cannot
787
787
-- retrieve the script.
788
- scriptWitnessScript :: ScriptWitness witctx era -> Maybe (ScriptInEra era )
789
- scriptWitnessScript (SimpleScriptWitness SimpleScriptInShelley (SScript script)) =
790
- Just $ ScriptInEra SimpleScriptInShelley (SimpleScript script)
791
- scriptWitnessScript (SimpleScriptWitness SimpleScriptInAllegra (SScript script)) =
792
- Just $ ScriptInEra SimpleScriptInAllegra (SimpleScript script)
793
- scriptWitnessScript (SimpleScriptWitness SimpleScriptInMary (SScript script)) =
794
- Just $ ScriptInEra SimpleScriptInMary (SimpleScript script)
795
- scriptWitnessScript (SimpleScriptWitness SimpleScriptInAlonzo (SScript script)) =
796
- Just $ ScriptInEra SimpleScriptInAlonzo (SimpleScript script)
797
- scriptWitnessScript (SimpleScriptWitness SimpleScriptInBabbage (SScript script)) =
798
- Just $ ScriptInEra SimpleScriptInBabbage (SimpleScript script)
799
- scriptWitnessScript (SimpleScriptWitness SimpleScriptInConway (SScript script)) =
800
- Just $ ScriptInEra SimpleScriptInConway (SimpleScript script)
801
- scriptWitnessScript (PlutusScriptWitness langInEra version (PScript script) _ _ _) =
802
- Just $ ScriptInEra langInEra (PlutusScript version script)
803
- scriptWitnessScript (SimpleScriptWitness _ (SReferenceScript _ _)) =
804
- Nothing
805
- scriptWitnessScript (PlutusScriptWitness _ _ (PReferenceScript _ _) _ _ _) =
806
- Nothing
788
+ -- So in the cases for script reference, the result contains @Right TxIn@.
789
+ getScriptWitnessReferenceInputOrScript :: ScriptWitness witctx era -> Either (ScriptInEra era ) TxIn
790
+ getScriptWitnessReferenceInputOrScript = \ case
791
+ SimpleScriptWitness (s :: (ScriptLanguageInEra SimpleScript' era )) (SScript script) ->
792
+ Left $ ScriptInEra s (SimpleScript script)
793
+ PlutusScriptWitness langInEra version (PScript script) _ _ _ ->
794
+ Left $ ScriptInEra langInEra (PlutusScript version script)
795
+ SimpleScriptWitness _ (SReferenceScript txIn) ->
796
+ Right txIn
797
+ PlutusScriptWitness _ _ (PReferenceScript txIn) _ _ _ ->
798
+ Right txIn
807
799
808
800
-- ----------------------------------------------------------------------------
809
801
-- The kind of witness to use, key (signature) or script
0 commit comments