From 97613349443af764f26571effce56ac0f405896a Mon Sep 17 00:00:00 2001 From: Jordan Millar Date: Fri, 5 Jul 2024 17:38:47 +0200 Subject: [PATCH] Implement deserialisePlutusScript --- .../internal/Cardano/Api/Scripts/New.hs | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/cardano-api/internal/Cardano/Api/Scripts/New.hs b/cardano-api/internal/Cardano/Api/Scripts/New.hs index 6146994817..9d6ae7d9f6 100644 --- a/cardano-api/internal/Cardano/Api/Scripts/New.hs +++ b/cardano-api/internal/Cardano/Api/Scripts/New.hs @@ -142,3 +142,31 @@ deserialiseNativeScript availableEra bs = Just nScript -> Right nScript Nothing -> Left NotASimpleScript Left e -> Left $ NotAScript e + +data PlutusScriptDeserializationError + = NotAPlutusScript + | NotAnyScript DecoderError + +deserialisePlutusScript + :: DecCBOR (Ledger.AlonzoScript (ToConstrainedEra availableera)) + => Era availableera + -> ByteString + -> Either PlutusScriptDeserializationError (Ledger.PlutusScript (ToConstrainedEra availableera)) +deserialisePlutusScript era bs = + case era of + CurrentEraInternal -> deserialise AsMainnetScript bs + UpcomingEraInternal -> deserialise AsUpcomingEraScript bs + where + deserialise + :: SerialiseAsCBOR (Script availableera) + => Ledger.AlonzoEraScript (ToConstrainedEra availableera) + => AsType (Script availableera) + -> ByteString + -> Either PlutusScriptDeserializationError (Ledger.PlutusScript (ToConstrainedEra availableera)) + deserialise as bs' = + case deserialiseFromCBOR as bs' of + Right s -> case Ledger.toPlutusScript $ unScript s of + Just nScript -> Right nScript + Nothing -> Left NotAPlutusScript + Left e -> Left $ NotAnyScript e +