Skip to content

Commit

Permalink
Move friendlyScript and friendlyDatum from cardano-api
Browse files Browse the repository at this point in the history
  • Loading branch information
palas committed Nov 27, 2024
1 parent 1ed1b69 commit 57d4bc7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
4 changes: 2 additions & 2 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 31 additions & 2 deletions cardano-cli/src/Cardano/CLI/Json/Friendly.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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]

Expand All @@ -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
Expand Down

0 comments on commit 57d4bc7

Please sign in to comment.