Skip to content

Commit

Permalink
Implement data PlutusScriptInEra era lang
Browse files Browse the repository at this point in the history
When decoding plutus script bytes to this type the validity of those
bytes are checked and the decoder will fail if they are invalid
This type is used in testing to double check we can still decode double
encoded plutus script bytes and that the resulting bytes are valid
  • Loading branch information
Jimbo4350 committed Jan 16, 2025
1 parent 86ac719 commit a780bc2
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions cardano-api/internal/Cardano/Api/Script.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1329,6 +1329,44 @@ fromAllegraTimelock = go
go (Shelley.RequireAnyOf s) = RequireAnyOf (map go (toList s))
go (Shelley.RequireMOf i s) = RequireMOf i (map go (toList s))

type family ToLedgerPlutusLanguage lang where
ToLedgerPlutusLanguage PlutusScriptV1 = Plutus.PlutusV1
ToLedgerPlutusLanguage PlutusScriptV2 = Plutus.PlutusV2
ToLedgerPlutusLanguage PlutusScriptV3 = Plutus.PlutusV3

data PlutusScriptInEra era lang where
PlutusScriptInEra :: PlutusScript lang -> PlutusScriptInEra era lang

deriving instance Eq (PlutusScriptInEra era lang)

deriving instance Show (PlutusScriptInEra era lang)

instance (HasTypeProxy era, HasTypeProxy lang) => HasTypeProxy (PlutusScriptInEra era lang) where
data AsType (PlutusScriptInEra era lang) = AsPlutusScriptInEra (AsType lang)
proxyToAsType _ = AsPlutusScriptInEra (proxyToAsType (Proxy :: Proxy lang))

instance
( Ledger.Era (ShelleyLedgerEra era)
, HasTypeProxy (PlutusScriptInEra era lang)
, Plutus.PlutusLanguage (ToLedgerPlutusLanguage lang)
)
=> SerialiseAsCBOR (PlutusScriptInEra era lang)
where
serialiseToCBOR (PlutusScriptInEra (PlutusScriptSerialised s)) =
SBS.fromShort s
deserialiseFromCBOR _ bs = do
let v = Ledger.eraProtVerLow @(ShelleyLedgerEra era)
scriptShortBs = SBS.toShort $ removePlutusScriptDoubleEncoding $ LBS.fromStrict bs
let plutusScript :: Plutus.Plutus (ToLedgerPlutusLanguage lang)
plutusScript = PlutusScriptBinary scriptShortBs
plutusScriptInEra = PlutusScriptInEra $ PlutusScriptSerialised scriptShortBs

case Plutus.decodePlutusRunnable v plutusScript of
Left e ->
Left $
CBOR.DecoderErrorCustom "PlutusLedgerApi.Common.ScriptDecodeError" (Text.pack . show $ pretty e)
Right{} -> Right plutusScriptInEra

-- ----------------------------------------------------------------------------
-- JSON serialisation
--
Expand Down

0 comments on commit a780bc2

Please sign in to comment.