From 2ddebb6283b7cc7b825ac386ee6b4da3b074c1a8 Mon Sep 17 00:00:00 2001 From: Pablo Lamela Date: Wed, 27 Nov 2024 19:01:05 +0100 Subject: [PATCH] Move `friendlyScript` and `friendlyDatum` to `cardano-cli` --- .../internal/Cardano/Api/ReexposeLedger.hs | 17 ++++++-- .../internal/Cardano/Api/ScriptData.hs | 41 +------------------ cardano-api/src/Cardano/Api.hs | 3 +- 3 files changed, 16 insertions(+), 45 deletions(-) diff --git a/cardano-api/internal/Cardano/Api/ReexposeLedger.hs b/cardano-api/internal/Cardano/Api/ReexposeLedger.hs index 92a008cdf..1b5f952a0 100644 --- a/cardano-api/internal/Cardano/Api/ReexposeLedger.hs +++ b/cardano-api/internal/Cardano/Api/ReexposeLedger.hs @@ -121,7 +121,10 @@ module Cardano.Api.ReexposeLedger , Data (..) , EraTxWits (..) , ExUnits (..) + , Language + , Plutus , Prices (..) + , Script , CostModels , AlonzoGenesis , AsIxItem (..) @@ -129,9 +132,15 @@ module Cardano.Api.ReexposeLedger , EraTx (witsTxL, bodyTxL) , Tx , TxDats (..) + , getNativeScript + , languageToText + , plutusBinary + , plutusScriptLanguage , ppPricesL , unData , unRedeemers + , serializeAsHexText + , showTimelock -- Base , boundRational , unboundRational @@ -173,7 +182,7 @@ import Cardano.Ledger.Alonzo.Core (AlonzoEraScript (..), AlonzoEraTxBo EraTx (bodyTxL, witsTxL), EraTxWits (..), PParamsUpdate (..), Tx, ppPricesL) import Cardano.Ledger.Alonzo.Genesis (AlonzoGenesis) import Cardano.Ledger.Alonzo.Scripts (AlonzoPlutusPurpose (..), CostModels, ExUnits (..), - Prices (..)) + Prices (..), Script, plutusScriptLanguage) import Cardano.Ledger.Alonzo.TxWits (TxDats (..)) import Cardano.Ledger.Api (Constitution (..), GovAction (..), unRedeemers) import Cardano.Ledger.Api.Tx.Cert (pattern AuthCommitteeHotKeyTxCert, @@ -182,7 +191,7 @@ import Cardano.Ledger.Api.Tx.Cert (pattern AuthCommitteeHotKeyTxCert, pattern RegDepositTxCert, pattern RegPoolTxCert, pattern RegTxCert, pattern ResignCommitteeColdTxCert, pattern RetirePoolTxCert, pattern UnRegDRepTxCert, pattern UnRegDepositTxCert, pattern UnRegTxCert) -import Cardano.Ledger.Babbage.Core (CoinPerByte (..)) +import Cardano.Ledger.Babbage.Core (CoinPerByte (..), getNativeScript) import Cardano.Ledger.BaseTypes (AnchorData (..), DnsName, EpochInterval (..), Network (..), NonNegativeInterval, ProtVer (..), StrictMaybe (..), UnitInterval, Url, boundRational, dnsToText, hashAnchorData, maybeToStrictMaybe, mkVersion, @@ -190,7 +199,7 @@ import Cardano.Ledger.BaseTypes (AnchorData (..), DnsName, EpochInterv unboundRational, urlToText) import Cardano.Ledger.Binary (Annotated (..), ByteSpan (..), byronProtVer, fromCBOR, serialize', slice, toCBOR, toPlainDecoder) -import Cardano.Ledger.Binary.Plain (Decoder) +import Cardano.Ledger.Binary.Plain (Decoder, serializeAsHexText) import Cardano.Ledger.CertState (DRepState (..), csCommitteeCredsL) import Cardano.Ledger.Coin (Coin (..), addDeltaCoin, toDeltaCoin) import Cardano.Ledger.Conway.Core (DRepVotingThresholds (..), PoolVotingThresholds (..), @@ -224,3 +233,5 @@ import Cardano.Ledger.Shelley.TxCert (EraTxCert (..), GenesisDelegCert ShelleyTxCert (..)) import Cardano.Ledger.TxIn (TxId (..), TxIn (..)) import Cardano.Slotting.Slot (EpochNo (..)) +import Cardano.Ledger.Plutus.Language ( Language, Plutus, languageToText, plutusBinary ) +import Cardano.Ledger.Allegra.Scripts (showTimelock) diff --git a/cardano-api/internal/Cardano/Api/ScriptData.hs b/cardano-api/internal/Cardano/Api/ScriptData.hs index 8b8c46bc2..759f8a743 100644 --- a/cardano-api/internal/Cardano/Api/ScriptData.hs +++ b/cardano-api/internal/Cardano/Api/ScriptData.hs @@ -16,8 +16,6 @@ module Cardano.Api.ScriptData , getScriptData , unsafeHashableScriptData , ScriptData (..) - , friendlyScript - , friendlyDatum -- * Validating metadata , validateScriptData @@ -47,8 +45,6 @@ module Cardano.Api.ScriptData ) where -import Cardano.Api.Eon.AlonzoEraOnwards (AlonzoEraOnwardsConstraints) -import Cardano.Api.Eon.ShelleyBasedEra (ShelleyLedgerEra) import Cardano.Api.Eras import Cardano.Api.Error import Cardano.Api.Hash @@ -63,15 +59,8 @@ import Cardano.Api.TxMetadata (pBytes, pSigned, parseAll) import qualified Cardano.Binary as CBOR import qualified Cardano.Crypto.Hash.Class as Crypto -import Cardano.Ledger.Allegra.Scripts (showTimelock) -import Cardano.Ledger.Alonzo.Core (AlonzoEraScript (..)) -import Cardano.Ledger.Alonzo.Scripts (plutusScriptLanguage) -import qualified Cardano.Ledger.Api as Alonzo -import Cardano.Ledger.Binary.Plain (serializeAsHexText) -import Cardano.Ledger.Core (Era, EraScript (..), Script) -import Cardano.Ledger.Plutus (Language) +import Cardano.Ledger.Core (Era) import qualified Cardano.Ledger.Plutus.Data as Plutus -import Cardano.Ledger.Plutus.Language (Plutus (plutusBinary), languageToText) import qualified Cardano.Ledger.SafeHash as Ledger import Ouroboros.Consensus.Shelley.Eras (StandardAlonzo, StandardCrypto) import qualified PlutusLedgerApi.V1 as PlutusAPI @@ -97,7 +86,6 @@ import Data.Maybe (fromMaybe) import qualified Data.Scientific as Scientific import Data.String (IsString) import Data.Text (Text) -import qualified Data.Text as T import qualified Data.Text as Text import qualified Data.Text.Encoding as Text import qualified Data.Text.Lazy as Text.Lazy @@ -259,33 +247,6 @@ fromPlutusData (PlutusAPI.List xs) = fromPlutusData (PlutusAPI.I n) = ScriptDataNumber n fromPlutusData (PlutusAPI.B bs) = ScriptDataBytes bs --- | Create a friendly JSON out of a script -friendlyScript :: AlonzoEraOnwardsConstraints era => Script (ShelleyLedgerEra era) -> Aeson.Value -friendlyScript script = Aeson.Object $ - KeyMap.fromList $ - case getNativeScript script of - Just nativeScript -> - [ ("type", "native") - , ("script", Aeson.String $ T.pack $ showTimelock nativeScript) - ] - Nothing -> - ( case toPlutusScript script of - Just plutusScript -> withPlutusScript plutusScript $ friendlyPlutusScript $ plutusScriptLanguage plutusScript - Nothing -> [("error", Aeson.String "Unsupported script type")] - ) - where - friendlyPlutusScript :: Language -> Plutus l -> [(KeyMap.Key, Aeson.Value)] - friendlyPlutusScript language plutusScript = - [ ("type", "plutus") - , ("plutus version", Aeson.String $ languageToText language) - , ("script", Aeson.String $ serializeAsHexText $ 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) - -- ---------------------------------------------------------------------------- -- Validate script data -- diff --git a/cardano-api/src/Cardano/Api.hs b/cardano-api/src/Cardano/Api.hs index ac527d887..785bb0ab5 100644 --- a/cardano-api/src/Cardano/Api.hs +++ b/cardano-api/src/Cardano/Api.hs @@ -528,6 +528,7 @@ module Cardano.Api , eraOfScriptInEra , HasScriptLanguageInEra (..) , ToAlonzoScript (..) + , AlonzoEraOnwardsConstraints -- ** Use of a script in an era as a witness , WitCtxTxIn @@ -548,8 +549,6 @@ module Cardano.Api , ScriptWitnessIndex (..) , renderScriptWitnessIndex , collectTxBodyScriptWitnesses - , friendlyDatum - , friendlyScript -- ** Languages supported in each era , ScriptLanguageInEra (..)