From f2a06d4a809eeb117b98438bad9d2015606308b2 Mon Sep 17 00:00:00 2001 From: Pablo Lamela Date: Fri, 22 Nov 2024 17:24:19 +0100 Subject: [PATCH 1/4] Add script info to `friendlyTxImpl` --- cardano-cli/src/Cardano/CLI/Json/Friendly.hs | 23 +++++++++++++++---- .../golden/alonzo/signed-transaction-view.out | 1 + .../files/golden/alonzo/transaction-view.out | 1 + ...ansaction-view-metadata-detailedschema.out | 1 + .../transaction-view-metadata-noschema.out | 1 + .../babbage/transaction-view-redeemer.out | 6 +++++ .../files/golden/conway/tx-proposal.out.json | 1 + .../conway/tx-three-votes-view.out.json | 1 + 8 files changed, 30 insertions(+), 5 deletions(-) diff --git a/cardano-cli/src/Cardano/CLI/Json/Friendly.hs b/cardano-cli/src/Cardano/CLI/Json/Friendly.hs index 874a65caf4..f7dfba0f2c 100644 --- a/cardano-cli/src/Cardano/CLI/Json/Friendly.hs +++ b/cardano-cli/src/Cardano/CLI/Json/Friendly.hs @@ -72,7 +72,6 @@ import Data.Maybe import Data.Ratio (numerator) import qualified Data.Text as T import qualified Data.Text as Text -import qualified Data.Vector as Vector import Data.Yaml (array) import Data.Yaml.Pretty (setConfCompare) import qualified Data.Yaml.Pretty as Yaml @@ -80,6 +79,8 @@ import GHC.Exts (IsList (..)) import GHC.Real (denominator) import GHC.Unicode (isAlphaNum) import Lens.Micro ((^.)) +import qualified Data.Aeson.KeyMap as KeyMap +import qualified Data.Vector as Vector data FriendlyFormat = FriendlyJson | FriendlyYaml @@ -249,7 +250,7 @@ friendlyTxBodyImpl ] ++ ( monoidForEraInEon @AlonzoEraOnwards era - (`getRedeemerDetails` tb) + (`getAlonzoSpecificDetails` tb) ) ++ ( monoidForEraInEon @ConwayEraOnwards era @@ -304,11 +305,13 @@ data EraIndependentPlutusScriptPurpose | Voting | Proposing -getRedeemerDetails +getAlonzoSpecificDetails :: forall era. AlonzoEraOnwards era -> TxBody era -> [Aeson.Pair] -getRedeemerDetails aeo tb = +getAlonzoSpecificDetails aeo tb = let ShelleyTx _ ledgerTx = makeSignedTransaction [] tb - in ["redeemers" .= friendlyRedeemers ledgerTx] + in [ "redeemers" .= friendlyRedeemers ledgerTx + , "scripts" .= friendlyScriptData ledgerTx + ] where friendlyRedeemers :: Ledger.Tx (ShelleyLedgerEra era) @@ -385,6 +388,16 @@ getRedeemerDetails aeo tb = addLabelToPurpose Voting vp = Aeson.object ["voting using script protected voter credentials" .= vp] addLabelToPurpose Proposing pp = Aeson.object ["submitting a proposal following proposal policy" .= pp] + friendlyScriptData :: Ledger.Tx (ShelleyLedgerEra era) -> Aeson.Value + friendlyScriptData tx = + alonzoEraOnwardsConstraints aeo $ do + Aeson.Array $ Vector.fromList $ + [ Aeson.Object $ KeyMap.fromList [ + "script hash" .= scriptHash, + "script data" .= Api.friendlyScript scriptData + ] + | (scriptHash, scriptData) <- Map.toList $ tx ^. Ledger.witsTxL . Ledger.scriptTxWitsL] + friendlyTotalCollateral :: TxTotalCollateral era -> Aeson.Value friendlyTotalCollateral TxTotalCollateralNone = Aeson.Null friendlyTotalCollateral (TxTotalCollateral _ coll) = toJSON coll diff --git a/cardano-cli/test/cardano-cli-golden/files/golden/alonzo/signed-transaction-view.out b/cardano-cli/test/cardano-cli-golden/files/golden/alonzo/signed-transaction-view.out index 62a6d1fd59..70fb432671 100644 --- a/cardano-cli/test/cardano-cli-golden/files/golden/alonzo/signed-transaction-view.out +++ b/cardano-cli/test/cardano-cli-golden/files/golden/alonzo/signed-transaction-view.out @@ -15,6 +15,7 @@ required signers (payment key hashes needed for scripts): - 98717eaba8105a50a2a71831267552e337dfdc893bef5e40b8676d27 - fafaaac8681b5050a8987f95bce4a7f99362f189879258fdbf733fa4 return collateral: null +scripts: [] total collateral: null update proposal: null validity range: diff --git a/cardano-cli/test/cardano-cli-golden/files/golden/alonzo/transaction-view.out b/cardano-cli/test/cardano-cli-golden/files/golden/alonzo/transaction-view.out index 1ef8d0560f..827e0b9c2b 100644 --- a/cardano-cli/test/cardano-cli-golden/files/golden/alonzo/transaction-view.out +++ b/cardano-cli/test/cardano-cli-golden/files/golden/alonzo/transaction-view.out @@ -15,6 +15,7 @@ required signers (payment key hashes needed for scripts): - 98717eaba8105a50a2a71831267552e337dfdc893bef5e40b8676d27 - fafaaac8681b5050a8987f95bce4a7f99362f189879258fdbf733fa4 return collateral: null +scripts: [] total collateral: null update proposal: epoch: 190 diff --git a/cardano-cli/test/cardano-cli-golden/files/golden/babbage/transaction-view-metadata-detailedschema.out b/cardano-cli/test/cardano-cli-golden/files/golden/babbage/transaction-view-metadata-detailedschema.out index 427cf0a5bb..1ef0554b79 100644 --- a/cardano-cli/test/cardano-cli-golden/files/golden/babbage/transaction-view-metadata-detailedschema.out +++ b/cardano-cli/test/cardano-cli-golden/files/golden/babbage/transaction-view-metadata-detailedschema.out @@ -33,6 +33,7 @@ redeemers: [] reference inputs: [] required signers (payment key hashes needed for scripts): null return collateral: null +scripts: [] total collateral: null update proposal: null validity range: diff --git a/cardano-cli/test/cardano-cli-golden/files/golden/babbage/transaction-view-metadata-noschema.out b/cardano-cli/test/cardano-cli-golden/files/golden/babbage/transaction-view-metadata-noschema.out index 63cb905c45..99f59c01e4 100644 --- a/cardano-cli/test/cardano-cli-golden/files/golden/babbage/transaction-view-metadata-noschema.out +++ b/cardano-cli/test/cardano-cli-golden/files/golden/babbage/transaction-view-metadata-noschema.out @@ -66,6 +66,7 @@ redeemers: [] reference inputs: [] required signers (payment key hashes needed for scripts): null return collateral: null +scripts: [] total collateral: null update proposal: null validity range: diff --git a/cardano-cli/test/cardano-cli-golden/files/golden/babbage/transaction-view-redeemer.out b/cardano-cli/test/cardano-cli-golden/files/golden/babbage/transaction-view-redeemer.out index 7242ec0211..2da234d28e 100644 --- a/cardano-cli/test/cardano-cli-golden/files/golden/babbage/transaction-view-redeemer.out +++ b/cardano-cli/test/cardano-cli-golden/files/golden/babbage/transaction-view-redeemer.out @@ -20,6 +20,12 @@ redeemers: reference inputs: [] required signers (payment key hashes needed for scripts): null return collateral: null +scripts: +- script data: + plutus version: PlutusV1 + script: 4e4d01000033222220051200120011 + type: plutus + script hash: 67f33146617a5e61936081db3b2117cbf59bd2123748f58ac9678656 total collateral: null update proposal: null validity range: diff --git a/cardano-cli/test/cardano-cli-golden/files/golden/conway/tx-proposal.out.json b/cardano-cli/test/cardano-cli-golden/files/golden/conway/tx-proposal.out.json index b8e34d66ee..e2759c686a 100644 --- a/cardano-cli/test/cardano-cli-golden/files/golden/conway/tx-proposal.out.json +++ b/cardano-cli/test/cardano-cli-golden/files/golden/conway/tx-proposal.out.json @@ -54,6 +54,7 @@ "reference inputs": [], "required signers (payment key hashes needed for scripts)": null, "return collateral": null, + "scripts": [], "total collateral": null, "treasuryDonation": 0, "update proposal": null, diff --git a/cardano-cli/test/cardano-cli-golden/files/golden/conway/tx-three-votes-view.out.json b/cardano-cli/test/cardano-cli-golden/files/golden/conway/tx-three-votes-view.out.json index f2c5bb9920..0b86ef0a94 100644 --- a/cardano-cli/test/cardano-cli-golden/files/golden/conway/tx-three-votes-view.out.json +++ b/cardano-cli/test/cardano-cli-golden/files/golden/conway/tx-three-votes-view.out.json @@ -30,6 +30,7 @@ "reference inputs": [], "required signers (payment key hashes needed for scripts)": null, "return collateral": null, + "scripts": [], "total collateral": null, "treasuryDonation": 0, "update proposal": null, From 501a05d1378cc5d821bba4ac8eb28681f24e4ab7 Mon Sep 17 00:00:00 2001 From: Pablo Lamela Date: Mon, 25 Nov 2024 15:59:35 +0100 Subject: [PATCH 2/4] Add datum info to `friendlyTxImpl` --- cardano-cli/src/Cardano/CLI/Json/Friendly.hs | 13 +++++++++++++ .../files/golden/alonzo/signed-transaction-view.out | 1 + .../files/golden/alonzo/transaction-view.out | 1 + .../transaction-view-metadata-detailedschema.out | 1 + .../babbage/transaction-view-metadata-noschema.out | 1 + .../golden/babbage/transaction-view-redeemer.out | 3 +++ .../files/golden/conway/tx-proposal.out.json | 1 + .../golden/conway/tx-three-votes-view.out.json | 1 + 8 files changed, 22 insertions(+) diff --git a/cardano-cli/src/Cardano/CLI/Json/Friendly.hs b/cardano-cli/src/Cardano/CLI/Json/Friendly.hs index f7dfba0f2c..10fe937881 100644 --- a/cardano-cli/src/Cardano/CLI/Json/Friendly.hs +++ b/cardano-cli/src/Cardano/CLI/Json/Friendly.hs @@ -311,6 +311,7 @@ getAlonzoSpecificDetails aeo tb = let ShelleyTx _ ledgerTx = makeSignedTransaction [] tb in [ "redeemers" .= friendlyRedeemers ledgerTx , "scripts" .= friendlyScriptData ledgerTx + , "datums" .= friendlyDats ledgerTx ] where friendlyRedeemers @@ -398,6 +399,18 @@ getAlonzoSpecificDetails aeo tb = ] | (scriptHash, scriptData) <- Map.toList $ tx ^. Ledger.witsTxL . Ledger.scriptTxWitsL] + friendlyDats :: Ledger.Tx (ShelleyLedgerEra era) -> Aeson.Value + friendlyDats tx = + alonzoEraOnwardsConstraints aeo $ + let Ledger.TxDats dats = tx ^. Ledger.witsTxL . Ledger.datsTxWitsL in + Aeson.Array $ Vector.fromList $ + [ Aeson.Object $ KeyMap.fromList [ + "datum hash" .= datHash, + "datum" .= Api.friendlyDatum dat + ] + | (datHash, dat) <- Map.toList dats + ] + friendlyTotalCollateral :: TxTotalCollateral era -> Aeson.Value friendlyTotalCollateral TxTotalCollateralNone = Aeson.Null friendlyTotalCollateral (TxTotalCollateral _ coll) = toJSON coll diff --git a/cardano-cli/test/cardano-cli-golden/files/golden/alonzo/signed-transaction-view.out b/cardano-cli/test/cardano-cli-golden/files/golden/alonzo/signed-transaction-view.out index 70fb432671..e6f1a37059 100644 --- a/cardano-cli/test/cardano-cli-golden/files/golden/alonzo/signed-transaction-view.out +++ b/cardano-cli/test/cardano-cli-golden/files/golden/alonzo/signed-transaction-view.out @@ -2,6 +2,7 @@ auxiliary scripts: null certificates: null collateral inputs: - c9765d7d0e3955be8920e6d7a38e1f3f2032eac48c7c59b0b9193caa87727e7e#256 +datums: [] era: Alonzo fee: 213 Lovelace inputs: diff --git a/cardano-cli/test/cardano-cli-golden/files/golden/alonzo/transaction-view.out b/cardano-cli/test/cardano-cli-golden/files/golden/alonzo/transaction-view.out index 827e0b9c2b..8c812c11fc 100644 --- a/cardano-cli/test/cardano-cli-golden/files/golden/alonzo/transaction-view.out +++ b/cardano-cli/test/cardano-cli-golden/files/golden/alonzo/transaction-view.out @@ -2,6 +2,7 @@ auxiliary scripts: null certificates: null collateral inputs: - c9765d7d0e3955be8920e6d7a38e1f3f2032eac48c7c59b0b9193caa87727e7e#256 +datums: [] era: Alonzo fee: 213 Lovelace inputs: diff --git a/cardano-cli/test/cardano-cli-golden/files/golden/babbage/transaction-view-metadata-detailedschema.out b/cardano-cli/test/cardano-cli-golden/files/golden/babbage/transaction-view-metadata-detailedschema.out index 1ef0554b79..be78001b74 100644 --- a/cardano-cli/test/cardano-cli-golden/files/golden/babbage/transaction-view-metadata-detailedschema.out +++ b/cardano-cli/test/cardano-cli-golden/files/golden/babbage/transaction-view-metadata-detailedschema.out @@ -1,6 +1,7 @@ auxiliary scripts: null certificates: null collateral inputs: [] +datums: [] era: Babbage fee: 21300 Lovelace inputs: diff --git a/cardano-cli/test/cardano-cli-golden/files/golden/babbage/transaction-view-metadata-noschema.out b/cardano-cli/test/cardano-cli-golden/files/golden/babbage/transaction-view-metadata-noschema.out index 99f59c01e4..cf8e9d0264 100644 --- a/cardano-cli/test/cardano-cli-golden/files/golden/babbage/transaction-view-metadata-noschema.out +++ b/cardano-cli/test/cardano-cli-golden/files/golden/babbage/transaction-view-metadata-noschema.out @@ -1,6 +1,7 @@ auxiliary scripts: null certificates: null collateral inputs: [] +datums: [] era: Babbage fee: 21300 Lovelace inputs: diff --git a/cardano-cli/test/cardano-cli-golden/files/golden/babbage/transaction-view-redeemer.out b/cardano-cli/test/cardano-cli-golden/files/golden/babbage/transaction-view-redeemer.out index 2da234d28e..90b60cb81b 100644 --- a/cardano-cli/test/cardano-cli-golden/files/golden/babbage/transaction-view-redeemer.out +++ b/cardano-cli/test/cardano-cli-golden/files/golden/babbage/transaction-view-redeemer.out @@ -2,6 +2,9 @@ auxiliary scripts: null certificates: null collateral inputs: - c9765d7d0e3955be8920e6d7a38e1f3f2032eac48c7c59b0b9193caa87727e7e#256 +datums: +- datum: I 6666 + datum hash: 9e478573ab81ea7a8e31891ce0648b81229f408d596a3483e6f4f9b92d3cf710 era: Babbage fee: 213 Lovelace inputs: diff --git a/cardano-cli/test/cardano-cli-golden/files/golden/conway/tx-proposal.out.json b/cardano-cli/test/cardano-cli-golden/files/golden/conway/tx-proposal.out.json index e2759c686a..b5655243b9 100644 --- a/cardano-cli/test/cardano-cli-golden/files/golden/conway/tx-proposal.out.json +++ b/cardano-cli/test/cardano-cli-golden/files/golden/conway/tx-proposal.out.json @@ -3,6 +3,7 @@ "certificates": null, "collateral inputs": [], "currentTreasuryValue": null, + "datums": [], "era": "Conway", "fee": "181517 Lovelace", "governance actions": [ diff --git a/cardano-cli/test/cardano-cli-golden/files/golden/conway/tx-three-votes-view.out.json b/cardano-cli/test/cardano-cli-golden/files/golden/conway/tx-three-votes-view.out.json index 0b86ef0a94..72c1f0ac7d 100644 --- a/cardano-cli/test/cardano-cli-golden/files/golden/conway/tx-three-votes-view.out.json +++ b/cardano-cli/test/cardano-cli-golden/files/golden/conway/tx-three-votes-view.out.json @@ -3,6 +3,7 @@ "certificates": null, "collateral inputs": [], "currentTreasuryValue": null, + "datums": [], "era": "Conway", "fee": "185433 Lovelace", "governance actions": [], From 2f2d5b65bf0387fe0c8f01ce5d2aed7e3c422e4f Mon Sep 17 00:00:00 2001 From: Pablo Lamela Date: Wed, 27 Nov 2024 18:07:19 +0100 Subject: [PATCH 3/4] Rename `getAlonzoSpecificDetails` to `getScriptWitnessDetails` --- cardano-cli/src/Cardano/CLI/Json/Friendly.hs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cardano-cli/src/Cardano/CLI/Json/Friendly.hs b/cardano-cli/src/Cardano/CLI/Json/Friendly.hs index 10fe937881..65ef344114 100644 --- a/cardano-cli/src/Cardano/CLI/Json/Friendly.hs +++ b/cardano-cli/src/Cardano/CLI/Json/Friendly.hs @@ -250,7 +250,7 @@ friendlyTxBodyImpl ] ++ ( monoidForEraInEon @AlonzoEraOnwards era - (`getAlonzoSpecificDetails` tb) + (`getScriptWitnessDetails` tb) ) ++ ( monoidForEraInEon @ConwayEraOnwards era @@ -305,9 +305,9 @@ data EraIndependentPlutusScriptPurpose | Voting | Proposing -getAlonzoSpecificDetails +getScriptWitnessDetails :: forall era. AlonzoEraOnwards era -> TxBody era -> [Aeson.Pair] -getAlonzoSpecificDetails aeo tb = +getScriptWitnessDetails aeo tb = let ShelleyTx _ ledgerTx = makeSignedTransaction [] tb in [ "redeemers" .= friendlyRedeemers ledgerTx , "scripts" .= friendlyScriptData ledgerTx From ba67090f0ffc8c82302d6969faab8886f9add15c Mon Sep 17 00:00:00 2001 From: Pablo Lamela Date: Wed, 27 Nov 2024 19:04:17 +0100 Subject: [PATCH 4/4] Move `friendlyScript` and `friendlyDatum` from `cardano-api` --- cardano-cli/src/Cardano/CLI/Json/Friendly.hs | 69 +++++++++++++++----- 1 file changed, 53 insertions(+), 16 deletions(-) diff --git a/cardano-cli/src/Cardano/CLI/Json/Friendly.hs b/cardano-cli/src/Cardano/CLI/Json/Friendly.hs index 65ef344114..5585aff263 100644 --- a/cardano-cli/src/Cardano/CLI/Json/Friendly.hs +++ b/cardano-cli/src/Cardano/CLI/Json/Friendly.hs @@ -43,6 +43,7 @@ where import Cardano.Api as Api import Cardano.Api.Byron (KeyWitness (ByronKeyWitness)) import Cardano.Api.Ledger (ExUnits (..), extractHash, strictMaybeToMaybe) +import qualified Cardano.Api.Ledger as Alonzo import qualified Cardano.Api.Ledger as L import qualified Cardano.Api.Ledger as Ledger import Cardano.Api.Shelley (Hash (..), @@ -60,6 +61,7 @@ import Data.Aeson (Value (..), object, toJSON, (.=)) import qualified Data.Aeson as Aeson import qualified Data.Aeson.Encode.Pretty as Aeson import qualified Data.Aeson.Key as Aeson +import qualified Data.Aeson.KeyMap as KeyMap import qualified Data.Aeson.Types as Aeson import qualified Data.ByteString as BS import qualified Data.ByteString.Char8 as BSC @@ -72,6 +74,7 @@ import Data.Maybe import Data.Ratio (numerator) import qualified Data.Text as T import qualified Data.Text as Text +import qualified Data.Vector as Vector import Data.Yaml (array) import Data.Yaml.Pretty (setConfCompare) import qualified Data.Yaml.Pretty as Yaml @@ -79,8 +82,6 @@ import GHC.Exts (IsList (..)) import GHC.Real (denominator) import GHC.Unicode (isAlphaNum) import Lens.Micro ((^.)) -import qualified Data.Aeson.KeyMap as KeyMap -import qualified Data.Vector as Vector data FriendlyFormat = FriendlyJson | FriendlyYaml @@ -392,24 +393,60 @@ getScriptWitnessDetails aeo tb = friendlyScriptData :: Ledger.Tx (ShelleyLedgerEra era) -> Aeson.Value friendlyScriptData tx = alonzoEraOnwardsConstraints aeo $ do - Aeson.Array $ Vector.fromList $ - [ Aeson.Object $ KeyMap.fromList [ - "script hash" .= scriptHash, - "script data" .= Api.friendlyScript scriptData - ] - | (scriptHash, scriptData) <- Map.toList $ tx ^. Ledger.witsTxL . Ledger.scriptTxWitsL] + Aeson.Array $ + Vector.fromList $ + [ Aeson.Object $ + KeyMap.fromList + [ "script hash" .= scriptHash + , "script data" .= friendlyScript scriptData + ] + | (scriptHash, scriptData) <- Map.toList $ tx ^. Ledger.witsTxL . Ledger.scriptTxWitsL + ] friendlyDats :: Ledger.Tx (ShelleyLedgerEra era) -> Aeson.Value friendlyDats tx = alonzoEraOnwardsConstraints aeo $ - let Ledger.TxDats dats = tx ^. Ledger.witsTxL . Ledger.datsTxWitsL in - Aeson.Array $ Vector.fromList $ - [ Aeson.Object $ KeyMap.fromList [ - "datum hash" .= datHash, - "datum" .= Api.friendlyDatum dat - ] - | (datHash, dat) <- Map.toList dats - ] + let Ledger.TxDats dats = tx ^. Ledger.witsTxL . Ledger.datsTxWitsL + in Aeson.Array $ + Vector.fromList $ + [ Aeson.Object $ + KeyMap.fromList + [ "datum hash" .= datHash + , "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