From 57d4bc78c75c5b062e127635d8a1901980ee6102 Mon Sep 17 00:00:00 2001 From: Pablo Lamela Date: Wed, 27 Nov 2024 19:04:17 +0100 Subject: [PATCH] Move `friendlyScript` and `friendlyDatum` from `cardano-api` --- cabal.project | 4 +-- cardano-cli/src/Cardano/CLI/Json/Friendly.hs | 33 ++++++++++++++++++-- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/cabal.project b/cabal.project index 1d413d56ce..b461bc71a4 100644 --- a/cabal.project +++ b/cabal.project @@ -23,8 +23,8 @@ source-repository-package type: git location: https://github.com/IntersectMBO/cardano-api subdir: cardano-api - tag: 07cfd58f4ffabc96391bba53d2f464b859be0a6a - --sha256: sha256-3Hdp3g/F1py8Lnev2Dk+eMBukPmmjsz1bnupkuYMgjA= + tag: 2ddebb6283b7cc7b825ac386ee6b4da3b074c1a8 + --sha256: sha256-/DMzDr9KqaiLrbJWd8X+wOVfwbWyRNJPgx4vGfcZmWI= program-options ghc-options: -Werror diff --git a/cardano-cli/src/Cardano/CLI/Json/Friendly.hs b/cardano-cli/src/Cardano/CLI/Json/Friendly.hs index 939a87a13c..05c6ad4d4d 100644 --- a/cardano-cli/src/Cardano/CLI/Json/Friendly.hs +++ b/cardano-cli/src/Cardano/CLI/Json/Friendly.hs @@ -81,6 +81,7 @@ import GHC.Unicode (isAlphaNum) import Lens.Micro ((^.)) import qualified Data.Aeson.KeyMap as KeyMap import qualified Data.Vector as Vector +import qualified Cardano.Api.Ledger as Alonzo data FriendlyFormat = FriendlyJson | FriendlyYaml @@ -395,7 +396,7 @@ getScriptWitnessDetails aeo tb = Aeson.Array $ Vector.fromList $ [ Aeson.Object $ KeyMap.fromList [ "script hash" .= scriptHash, - "script data" .= Api.friendlyScript scriptData + "script data" .= friendlyScript scriptData ] | (scriptHash, scriptData) <- Map.toList $ tx ^. Ledger.witsTxL . Ledger.scriptTxWitsL] @@ -406,11 +407,39 @@ getScriptWitnessDetails aeo tb = Aeson.Array $ Vector.fromList $ [ Aeson.Object $ KeyMap.fromList [ "datum hash" .= datHash, - "datum" .= Api.friendlyDatum dat + "datum" .= friendlyDatum dat ] | (datHash, dat) <- Map.toList dats ] +-- | Create a friendly JSON out of a script +friendlyScript :: AlonzoEraOnwardsConstraints era => Ledger.Script (ShelleyLedgerEra era) -> Aeson.Value +friendlyScript script = Aeson.Object $ + KeyMap.fromList $ + case Ledger.getNativeScript script of + Just nativeScript -> + [ ("type", "native") + , ("script", Aeson.String $ T.pack $ Ledger.showTimelock nativeScript) + ] + Nothing -> + ( case Ledger.toPlutusScript script of + Just plutusScript -> Ledger.withPlutusScript plutusScript $ friendlyPlutusScript $ Ledger.plutusScriptLanguage plutusScript + Nothing -> [("error", Aeson.String "Unsupported script type")] + ) + where + friendlyPlutusScript :: Ledger.Language -> Ledger.Plutus l -> [(KeyMap.Key, Aeson.Value)] + friendlyPlutusScript language plutusScript = + [ ("type", "plutus") + , ("plutus version", Aeson.String $ Ledger.languageToText language) + , ("script", Aeson.String $ Ledger.serializeAsHexText $ Ledger.plutusBinary plutusScript) + ] + +-- | Create a friendly JSON out of a datum +friendlyDatum + :: AlonzoEraOnwardsConstraints era => Alonzo.Data (ShelleyLedgerEra era) -> Aeson.Value +friendlyDatum (Alonzo.Data datum) = Aeson.String (T.pack $ show datum) + + friendlyTotalCollateral :: TxTotalCollateral era -> Aeson.Value friendlyTotalCollateral TxTotalCollateralNone = Aeson.Null friendlyTotalCollateral (TxTotalCollateral _ coll) = toJSON coll