@@ -65,7 +65,7 @@ module Cardano.Ledger.Alonzo.Scripts (
6565import Cardano.Ledger.Allegra.Scripts
6666import Cardano.Ledger.Alonzo.Era (AlonzoEra )
6767import Cardano.Ledger.Alonzo.TxCert ()
68- import Cardano.Ledger.BaseTypes (ProtVer (.. ), kindObject )
68+ import Cardano.Ledger.BaseTypes (ProtVer (.. ), kindObjectValue )
6969import Cardano.Ledger.Binary (
7070 Annotator ,
7171 CBORGroup (.. ),
@@ -88,7 +88,6 @@ import Cardano.Ledger.Binary.Coders (
8888 (!>) ,
8989 (<*!) ,
9090 )
91- import Cardano.Ledger.Binary.Plain (serializeAsHexText )
9291import Cardano.Ledger.Core
9392import Cardano.Ledger.Mary.Value (PolicyID )
9493import Cardano.Ledger.MemoBytes (EqRaw (.. ))
@@ -102,6 +101,7 @@ import Cardano.Ledger.Plutus.Language (
102101 SLanguage (.. ),
103102 asSLanguage ,
104103 isValidPlutus ,
104+ languageFromText ,
105105 plutusLanguage ,
106106 plutusSLanguage ,
107107 withSLanguage ,
@@ -110,12 +110,28 @@ import Cardano.Ledger.Shelley.Scripts (ShelleyEraScript (..), nativeMultiSigTag)
110110import Cardano.Ledger.TxIn (TxIn )
111111import Control.DeepSeq (NFData (.. ), deepseq , rwhnf )
112112import Control.Monad (guard , (>=>) )
113- import Data.Aeson (ToJSON (.. ), Value (String ), object , (.=) )
113+ import Data.Aeson (FromJSON (parseJSON ), ToJSON (toJSON ), object , withObject , (.:) , (.=) )
114+ import qualified Data.Aeson as Aeson
115+ import Data.Aeson.Types (Parser )
114116import qualified Data.ByteString as BS
117+ import qualified Data.ByteString.Base16 as BS16
118+ import qualified Data.ByteString.Base64 as BS64
119+ import qualified Data.ByteString.Short as SBS
115120import Data.Kind (Type )
116121import qualified Data.Map.Strict as Map
117122import Data.Maybe (fromJust , isJust )
118- import Data.MemPack
123+ import Data.MemPack (
124+ MemPack ,
125+ packM ,
126+ packTagM ,
127+ packedByteCount ,
128+ packedTagByteCount ,
129+ unknownTagM ,
130+ unpackM ,
131+ unpackTagM ,
132+ )
133+ import Data.Text (Text )
134+ import Data.Text.Encoding (decodeLatin1 , encodeUtf8 )
119135import Data.Typeable
120136import Data.Word (Word32 )
121137import GHC.Generics (Generic )
@@ -296,6 +312,11 @@ instance (ToJSON ix, ToJSON it) => ToJSON (AsIxItem ix it) where
296312 , " item" .= toJSON it
297313 ]
298314
315+ instance FromJSON ix => FromJSON (AsIx ix it ) where
316+ parseJSON =
317+ withObject " AsIx" $ \ o ->
318+ AsIx <$> o .: " index"
319+
299320toAsPurpose :: f ix it -> AsPurpose ix it
300321toAsPurpose = const AsPurpose
301322
@@ -406,7 +427,25 @@ instance
406427 AlonzoCertifying n -> kindObjectWithValue " AlonzoCertifying" n
407428 AlonzoRewarding n -> kindObjectWithValue " AlonzoRewarding" n
408429 where
409- kindObjectWithValue name n = kindObject name [" value" .= n]
430+ kindObjectWithValue name n = kindObjectValue name [" value" .= n]
431+
432+ instance
433+ ( FromJSON (AsIx Word32 TxIn )
434+ , FromJSON (AsIx Word32 PolicyID )
435+ , FromJSON (AsIx Word32 (TxCert era ))
436+ , FromJSON (AsIx Word32 AccountAddress )
437+ ) =>
438+ FromJSON (AlonzoPlutusPurpose AsIx era )
439+ where
440+ parseJSON =
441+ withObject " AlonzoPlutusPurpose" $ \ o -> do
442+ kind <- o .: " kind" :: Parser Text
443+ case kind of
444+ " AlonzoSpending" -> AlonzoSpending <$> o .: " value"
445+ " AlonzoMinting" -> AlonzoMinting <$> o .: " value"
446+ " AlonzoCertifying" -> AlonzoCertifying <$> o .: " value"
447+ " AlonzoRewarding" -> AlonzoRewarding <$> o .: " value"
448+ _ -> fail $ " Unknown AlonzoPlutusPurpose kind: " <> show kind
410449
411450pattern SpendingPurpose ::
412451 AlonzoEraScript era => f Word32 TxIn -> PlutusPurpose f era
@@ -579,8 +618,41 @@ instance
579618 where
580619 eqRaw = eqAlonzoScriptRaw
581620
582- instance AlonzoEraScript era => ToJSON (AlonzoScript era ) where
583- toJSON = String . serializeAsHexText
621+ instance (AlonzoEraScript era , Script era ~ AlonzoScript era ) => ToJSON (AlonzoScript era ) where
622+ toJSON script =
623+ object $
624+ [ " scriptHash" .= hashScript script
625+ , " scriptBase16Tag" .= toBase16Text (scriptPrefixTag script)
626+ , " scriptBase64Bytes" .= toBase64Text (originalBytes script)
627+ , " scriptType"
628+ .= case script of
629+ NativeScript _ -> Aeson. String " NativeScript"
630+ PlutusScript plutusScript -> toJSON (withPlutusScript plutusScript plutusLanguage)
631+ ]
632+ ++ case script of
633+ NativeScript nativeScript -> [" nativeScriptContents" .= toJSON nativeScript]
634+ PlutusScript _ -> []
635+ where
636+ toBase64Text = decodeLatin1 . BS64. encode
637+ toBase16Text = (" 0x" <> ) . decodeLatin1 . BS16. encode
638+
639+ instance AlonzoEraScript era => FromJSON (AlonzoScript era ) where
640+ parseJSON = withObject typeName $ \ o -> do
641+ scriptType <- o .: " scriptType" :: Parser Text
642+ case scriptType of
643+ " NativeScript" -> NativeScript <$> o .: " nativeScriptContents"
644+ _ -> do
645+ lang <- languageFromText scriptType
646+ b64 <- o .: " scriptBase64Bytes" :: Parser Text
647+ bytes <-
648+ either
649+ (fail . (" AlonzoScript: invalid base64: " <> ))
650+ pure
651+ (BS64. decode (encodeUtf8 b64))
652+ let pb = PlutusBinary (SBS. toShort bytes)
653+ PlutusScript <$> mkBinaryPlutusScript lang pb
654+ where
655+ typeName = show $ typeRep (Proxy @ (AlonzoScript era ))
584656
585657-- | It might seem that this instance unnecessarily utilizes a zero Tag, but it is needed for
586658-- forward compatibility with plutus scripts from future eras.
@@ -620,12 +692,13 @@ instance AlonzoEraScript era => ToCBOR (AlonzoScript era) where
620692encodeScript :: AlonzoEraScript era => AlonzoScript era -> Encode Open (AlonzoScript era )
621693encodeScript = \ case
622694 NativeScript i -> Sum NativeScript 0 !> To i
623- PlutusScript plutusScript -> withPlutusScript plutusScript $ \ plutus@ (Plutus pb) ->
624- case plutusSLanguage plutus of
625- SPlutusV1 -> Sum (PlutusScript . fromJust . mkPlutusScript . Plutus @ 'PlutusV1) 1 !> To pb
626- SPlutusV2 -> Sum (PlutusScript . fromJust . mkPlutusScript . Plutus @ 'PlutusV2) 2 !> To pb
627- SPlutusV3 -> Sum (PlutusScript . fromJust . mkPlutusScript . Plutus @ 'PlutusV3) 3 !> To pb
628- SPlutusV4 -> Sum (PlutusScript . fromJust . mkPlutusScript . Plutus @ 'PlutusV4) 4 !> To pb
695+ PlutusScript plutusScript ->
696+ withPlutusScript plutusScript $ \ plutus@ (Plutus pb) ->
697+ case plutusSLanguage plutus of
698+ SPlutusV1 -> Sum (PlutusScript . fromJust . mkPlutusScript . Plutus @ 'PlutusV1) 1 !> To pb
699+ SPlutusV2 -> Sum (PlutusScript . fromJust . mkPlutusScript . Plutus @ 'PlutusV2) 2 !> To pb
700+ SPlutusV3 -> Sum (PlutusScript . fromJust . mkPlutusScript . Plutus @ 'PlutusV3) 3 !> To pb
701+ SPlutusV4 -> Sum (PlutusScript . fromJust . mkPlutusScript . Plutus @ 'PlutusV4) 4 !> To pb
629702
630703instance AlonzoEraScript era => DecCBOR (Annotator (AlonzoScript era )) where
631704 decCBOR = decode (Summands " AlonzoScript" decodeScript)
0 commit comments