Skip to content

Commit

Permalink
Move friendlyScript and friendlyDatum to cardano-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
palas committed Nov 27, 2024
1 parent 07cfd58 commit 2ddebb6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 45 deletions.
17 changes: 14 additions & 3 deletions cardano-api/internal/Cardano/Api/ReexposeLedger.hs
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,26 @@ module Cardano.Api.ReexposeLedger
, Data (..)
, EraTxWits (..)
, ExUnits (..)
, Language
, Plutus
, Prices (..)
, Script
, CostModels
, AlonzoGenesis
, AsIxItem (..)
, EraGov
, EraTx (witsTxL, bodyTxL)
, Tx
, TxDats (..)
, getNativeScript
, languageToText
, plutusBinary
, plutusScriptLanguage
, ppPricesL
, unData
, unRedeemers
, serializeAsHexText
, showTimelock
-- Base
, boundRational
, unboundRational
Expand Down Expand Up @@ -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,
Expand All @@ -182,15 +191,15 @@ 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,
portToWord16, strictMaybeToMaybe, textToDns, textToUrl, txIxToInt,
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 (..),
Expand Down Expand Up @@ -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)
41 changes: 1 addition & 40 deletions cardano-api/internal/Cardano/Api/ScriptData.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ module Cardano.Api.ScriptData
, getScriptData
, unsafeHashableScriptData
, ScriptData (..)
, friendlyScript
, friendlyDatum

-- * Validating metadata
, validateScriptData
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
--
Expand Down
3 changes: 1 addition & 2 deletions cardano-api/src/Cardano/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ module Cardano.Api
, eraOfScriptInEra
, HasScriptLanguageInEra (..)
, ToAlonzoScript (..)
, AlonzoEraOnwardsConstraints

-- ** Use of a script in an era as a witness
, WitCtxTxIn
Expand All @@ -548,8 +549,6 @@ module Cardano.Api
, ScriptWitnessIndex (..)
, renderScriptWitnessIndex
, collectTxBodyScriptWitnesses
, friendlyDatum
, friendlyScript

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

0 comments on commit 2ddebb6

Please sign in to comment.