From 415bea8b7bb139b27918ba4d1cd45ce16ab05c7e Mon Sep 17 00:00:00 2001 From: Pablo Lamela Date: Fri, 22 Nov 2024 12:50:09 +0100 Subject: [PATCH 1/5] Expose EraTxWits from Alonzo --- cardano-api/internal/Cardano/Api/ReexposeLedger.hs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cardano-api/internal/Cardano/Api/ReexposeLedger.hs b/cardano-api/internal/Cardano/Api/ReexposeLedger.hs index fcf067551..01e243ccd 100644 --- a/cardano-api/internal/Cardano/Api/ReexposeLedger.hs +++ b/cardano-api/internal/Cardano/Api/ReexposeLedger.hs @@ -119,6 +119,7 @@ module Cardano.Api.ReexposeLedger , AsIx (..) , CoinPerWord (..) , Data (..) + , EraTxWits (..) , ExUnits (..) , Prices (..) , CostModels @@ -168,7 +169,7 @@ where import Cardano.Crypto.Hash.Class (hashFromBytes, hashToBytes) import Cardano.Ledger.Alonzo.Core (AlonzoEraScript (..), AlonzoEraTxBody (..), AlonzoEraTxWits (..), AsIx (..), AsIxItem (AsIxItem), CoinPerWord (..), EraGov, - EraTx (bodyTxL, witsTxL), PParamsUpdate (..), Tx, ppPricesL) + EraTx (bodyTxL, witsTxL), EraTxWits (..), PParamsUpdate (..), Tx, ppPricesL) import Cardano.Ledger.Alonzo.Genesis (AlonzoGenesis) import Cardano.Ledger.Alonzo.Scripts (AlonzoPlutusPurpose (..), CostModels, ExUnits (..), Prices (..)) From 955aefdcbdedb2959f176e03da80e82ce27fc879 Mon Sep 17 00:00:00 2001 From: Pablo Lamela Date: Fri, 22 Nov 2024 16:36:52 +0100 Subject: [PATCH 2/5] Add function that encodes `Script` as JSON `friendlyScript` --- .../internal/Cardano/Api/ScriptData.hs | 35 ++++++++++++++++++- cardano-api/src/Cardano/Api.hs | 1 + 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/cardano-api/internal/Cardano/Api/ScriptData.hs b/cardano-api/internal/Cardano/Api/ScriptData.hs index 4951db6c2..fd48a8bef 100644 --- a/cardano-api/internal/Cardano/Api/ScriptData.hs +++ b/cardano-api/internal/Cardano/Api/ScriptData.hs @@ -1,5 +1,6 @@ {-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE DerivingVia #-} +{-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE InstanceSigs #-} {-# LANGUAGE LambdaCase #-} @@ -15,6 +16,7 @@ module Cardano.Api.ScriptData , getScriptData , unsafeHashableScriptData , ScriptData (..) + , friendlyScript -- * Validating metadata , validateScriptData @@ -44,6 +46,8 @@ 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 @@ -58,8 +62,14 @@ import Cardano.Api.TxMetadata (pBytes, pSigned, parseAll) import qualified Cardano.Binary as CBOR import qualified Cardano.Crypto.Hash.Class as Crypto -import Cardano.Ledger.Core (Era) +import Cardano.Ledger.Allegra.Scripts (showTimelock) +import Cardano.Ledger.Alonzo.Core (AlonzoEraScript (..)) +import Cardano.Ledger.Alonzo.Scripts (plutusScriptLanguage) +import Cardano.Ledger.Binary.Plain (serializeAsHexText) +import Cardano.Ledger.Core (Era, EraScript (..), Script) +import Cardano.Ledger.Plutus (Language) 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 @@ -85,6 +95,7 @@ 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 @@ -246,6 +257,28 @@ fromPlutusData (PlutusAPI.List xs) = fromPlutusData (PlutusAPI.I n) = ScriptDataNumber n fromPlutusData (PlutusAPI.B bs) = ScriptDataBytes bs +-- | Friendly script JSON +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) + ] + -- ---------------------------------------------------------------------------- -- Validate script data -- diff --git a/cardano-api/src/Cardano/Api.hs b/cardano-api/src/Cardano/Api.hs index f8a0238c4..3d4a80167 100644 --- a/cardano-api/src/Cardano/Api.hs +++ b/cardano-api/src/Cardano/Api.hs @@ -548,6 +548,7 @@ module Cardano.Api , ScriptWitnessIndex (..) , renderScriptWitnessIndex , collectTxBodyScriptWitnesses + , friendlyScript -- ** Languages supported in each era , ScriptLanguageInEra (..) From 9b3bdc0e95409ac3a72b17a5bb224cb9c19abba6 Mon Sep 17 00:00:00 2001 From: Pablo Lamela Date: Mon, 25 Nov 2024 15:21:18 +0100 Subject: [PATCH 3/5] Add function that encodes `Data` as JSON `friendlyDatum` --- cardano-api/internal/Cardano/Api/ReexposeLedger.hs | 2 ++ cardano-api/internal/Cardano/Api/ScriptData.hs | 7 +++++++ cardano-api/src/Cardano/Api.hs | 1 + 3 files changed, 10 insertions(+) diff --git a/cardano-api/internal/Cardano/Api/ReexposeLedger.hs b/cardano-api/internal/Cardano/Api/ReexposeLedger.hs index 01e243ccd..92a008cdf 100644 --- a/cardano-api/internal/Cardano/Api/ReexposeLedger.hs +++ b/cardano-api/internal/Cardano/Api/ReexposeLedger.hs @@ -128,6 +128,7 @@ module Cardano.Api.ReexposeLedger , EraGov , EraTx (witsTxL, bodyTxL) , Tx + , TxDats (..) , ppPricesL , unData , unRedeemers @@ -173,6 +174,7 @@ import Cardano.Ledger.Alonzo.Core (AlonzoEraScript (..), AlonzoEraTxBo import Cardano.Ledger.Alonzo.Genesis (AlonzoGenesis) import Cardano.Ledger.Alonzo.Scripts (AlonzoPlutusPurpose (..), CostModels, ExUnits (..), Prices (..)) +import Cardano.Ledger.Alonzo.TxWits (TxDats (..)) import Cardano.Ledger.Api (Constitution (..), GovAction (..), unRedeemers) import Cardano.Ledger.Api.Tx.Cert (pattern AuthCommitteeHotKeyTxCert, pattern DelegStakeTxCert, pattern DelegTxCert, pattern GenesisDelegTxCert, diff --git a/cardano-api/internal/Cardano/Api/ScriptData.hs b/cardano-api/internal/Cardano/Api/ScriptData.hs index fd48a8bef..df8ed04b3 100644 --- a/cardano-api/internal/Cardano/Api/ScriptData.hs +++ b/cardano-api/internal/Cardano/Api/ScriptData.hs @@ -17,6 +17,7 @@ module Cardano.Api.ScriptData , unsafeHashableScriptData , ScriptData (..) , friendlyScript + , friendlyDatum -- * Validating metadata , validateScriptData @@ -65,6 +66,7 @@ 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) @@ -279,6 +281,11 @@ friendlyScript script = Aeson.Object $ , ("script", Aeson.String $ serializeAsHexText $ plutusBinary plutusScript) ] +-- | Friendly dats JSON +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 3d4a80167..ac527d887 100644 --- a/cardano-api/src/Cardano/Api.hs +++ b/cardano-api/src/Cardano/Api.hs @@ -548,6 +548,7 @@ module Cardano.Api , ScriptWitnessIndex (..) , renderScriptWitnessIndex , collectTxBodyScriptWitnesses + , friendlyDatum , friendlyScript -- ** Languages supported in each era From 07cfd58f4ffabc96391bba53d2f464b859be0a6a Mon Sep 17 00:00:00 2001 From: Pablo Lamela Date: Mon, 25 Nov 2024 16:29:14 +0100 Subject: [PATCH 4/5] Improve comments --- cardano-api/internal/Cardano/Api/ScriptData.hs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cardano-api/internal/Cardano/Api/ScriptData.hs b/cardano-api/internal/Cardano/Api/ScriptData.hs index df8ed04b3..8b8c46bc2 100644 --- a/cardano-api/internal/Cardano/Api/ScriptData.hs +++ b/cardano-api/internal/Cardano/Api/ScriptData.hs @@ -259,7 +259,7 @@ fromPlutusData (PlutusAPI.List xs) = fromPlutusData (PlutusAPI.I n) = ScriptDataNumber n fromPlutusData (PlutusAPI.B bs) = ScriptDataBytes bs --- | Friendly script JSON +-- | Create a friendly JSON out of a script friendlyScript :: AlonzoEraOnwardsConstraints era => Script (ShelleyLedgerEra era) -> Aeson.Value friendlyScript script = Aeson.Object $ KeyMap.fromList $ @@ -281,7 +281,7 @@ friendlyScript script = Aeson.Object $ , ("script", Aeson.String $ serializeAsHexText $ plutusBinary plutusScript) ] --- | Friendly dats JSON +-- | 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) From 72a82e2806465d3ba07b6f23f5b7d21686c59669 Mon Sep 17 00:00:00 2001 From: Pablo Lamela Date: Wed, 27 Nov 2024 19:01:05 +0100 Subject: [PATCH 5/5] 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..f83906d5a 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 @@ -168,12 +177,13 @@ module Cardano.Api.ReexposeLedger where import Cardano.Crypto.Hash.Class (hashFromBytes, hashToBytes) +import Cardano.Ledger.Allegra.Scripts (showTimelock) import Cardano.Ledger.Alonzo.Core (AlonzoEraScript (..), AlonzoEraTxBody (..), AlonzoEraTxWits (..), AsIx (..), AsIxItem (AsIxItem), CoinPerWord (..), EraGov, 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 +192,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 +200,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 (..), @@ -212,6 +222,7 @@ import Cardano.Ledger.DRep (DRep (..), drepAnchorL, drepDepositL, drep import Cardano.Ledger.Keys (HasKeyRole, KeyHash (..), KeyRole (..), VKey (..), hashWithSerialiser) import Cardano.Ledger.Plutus.Data (Data (..), unData) +import Cardano.Ledger.Plutus.Language (Language, Plutus, languageToText, plutusBinary) import Cardano.Ledger.PoolParams (PoolMetadata (..), PoolParams (..), StakePoolRelay (..)) import Cardano.Ledger.SafeHash (SafeHash, castSafeHash, extractHash, unsafeMakeSafeHash) import Cardano.Ledger.Shelley.API (AccountState (..), GenDelegPair (..), 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 (..)