Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose EraTxWits, TxDats, and functions for converting datums and scripts to JSON #689

Merged
merged 5 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion cardano-api/internal/Cardano/Api/ReexposeLedger.hs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ module Cardano.Api.ReexposeLedger
, AsIx (..)
, CoinPerWord (..)
, Data (..)
, EraTxWits (..)
, ExUnits (..)
, Prices (..)
, CostModels
Expand All @@ -127,6 +128,7 @@ module Cardano.Api.ReexposeLedger
, EraGov
, EraTx (witsTxL, bodyTxL)
, Tx
, TxDats (..)
, ppPricesL
, unData
, unRedeemers
Expand Down Expand Up @@ -168,10 +170,11 @@ 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 (..))
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,
Expand Down
42 changes: 41 additions & 1 deletion cardano-api/internal/Cardano/Api/ScriptData.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE LambdaCase #-}
Expand All @@ -15,6 +16,8 @@ module Cardano.Api.ScriptData
, getScriptData
, unsafeHashableScriptData
, ScriptData (..)
, friendlyScript
, friendlyDatum

-- * Validating metadata
, validateScriptData
Expand Down Expand Up @@ -44,6 +47,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
Expand All @@ -58,8 +63,15 @@ 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 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 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
Expand All @@ -85,6 +97,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
Expand Down Expand Up @@ -246,6 +259,33 @@ 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
palas marked this conversation as resolved.
Show resolved Hide resolved
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
--
Expand Down
2 changes: 2 additions & 0 deletions cardano-api/src/Cardano/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,8 @@ module Cardano.Api
, ScriptWitnessIndex (..)
, renderScriptWitnessIndex
, collectTxBodyScriptWitnesses
, friendlyDatum
, friendlyScript

-- ** Languages supported in each era
, ScriptLanguageInEra (..)
Expand Down
Loading