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

Add datums and scripts to friendlyTxImpl #977

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ index-state:
packages:
cardano-cli

source-repository-package
type: git
location: https://github.com/IntersectMBO/cardano-api
subdir: cardano-api
tag: 72a82e2806465d3ba07b6f23f5b7d21686c59669
--sha256: sha256-Win07OoMIDbcvHXagZ8kpyNdarJ8sAfRIljVWuXKYOg=

program-options
ghc-options: -Werror

Expand Down
71 changes: 67 additions & 4 deletions cardano-cli/src/Cardano/CLI/Json/Friendly.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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 (..),
Expand All @@ -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
Expand Down Expand Up @@ -249,7 +251,7 @@ friendlyTxBodyImpl
]
++ ( monoidForEraInEon @AlonzoEraOnwards
era
(`getRedeemerDetails` tb)
(`getScriptWitnessDetails` tb)
)
++ ( monoidForEraInEon @ConwayEraOnwards
era
Expand Down Expand Up @@ -304,11 +306,14 @@ data EraIndependentPlutusScriptPurpose
| Voting
| Proposing

getRedeemerDetails
getScriptWitnessDetails
:: forall era. AlonzoEraOnwards era -> TxBody era -> [Aeson.Pair]
getRedeemerDetails aeo tb =
getScriptWitnessDetails aeo tb =
let ShelleyTx _ ledgerTx = makeSignedTransaction [] tb
in ["redeemers" .= friendlyRedeemers ledgerTx]
in [ "redeemers" .= friendlyRedeemers ledgerTx
, "scripts" .= friendlyScriptData ledgerTx
, "datums" .= friendlyDats ledgerTx
]
where
friendlyRedeemers
:: Ledger.Tx (ShelleyLedgerEra era)
Expand Down Expand Up @@ -385,6 +390,64 @@ 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" .= 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" .= 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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ auxiliary scripts: null
certificates: null
collateral inputs:
- c9765d7d0e3955be8920e6d7a38e1f3f2032eac48c7c59b0b9193caa87727e7e#256
datums: []
era: Alonzo
fee: 213 Lovelace
inputs:
Expand All @@ -15,6 +16,7 @@ required signers (payment key hashes needed for scripts):
- 98717eaba8105a50a2a71831267552e337dfdc893bef5e40b8676d27
- fafaaac8681b5050a8987f95bce4a7f99362f189879258fdbf733fa4
return collateral: null
scripts: []
total collateral: null
update proposal: null
validity range:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ auxiliary scripts: null
certificates: null
collateral inputs:
- c9765d7d0e3955be8920e6d7a38e1f3f2032eac48c7c59b0b9193caa87727e7e#256
datums: []
era: Alonzo
fee: 213 Lovelace
inputs:
Expand All @@ -15,6 +16,7 @@ required signers (payment key hashes needed for scripts):
- 98717eaba8105a50a2a71831267552e337dfdc893bef5e40b8676d27
- fafaaac8681b5050a8987f95bce4a7f99362f189879258fdbf733fa4
return collateral: null
scripts: []
total collateral: null
update proposal:
epoch: 190
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
auxiliary scripts: null
certificates: null
collateral inputs: []
datums: []
era: Babbage
fee: 21300 Lovelace
inputs:
Expand Down Expand Up @@ -33,6 +34,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:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
auxiliary scripts: null
certificates: null
collateral inputs: []
datums: []
era: Babbage
fee: 21300 Lovelace
inputs:
Expand Down Expand Up @@ -66,6 +67,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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ auxiliary scripts: null
certificates: null
collateral inputs:
- c9765d7d0e3955be8920e6d7a38e1f3f2032eac48c7c59b0b9193caa87727e7e#256
datums:
palas marked this conversation as resolved.
Show resolved Hide resolved
- datum: I 6666
datum hash: 9e478573ab81ea7a8e31891ce0648b81229f408d596a3483e6f4f9b92d3cf710
era: Babbage
fee: 213 Lovelace
inputs:
Expand All @@ -20,6 +23,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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"certificates": null,
"collateral inputs": [],
"currentTreasuryValue": null,
"datums": [],
"era": "Conway",
"fee": "181517 Lovelace",
"governance actions": [
Expand Down Expand Up @@ -54,6 +55,7 @@
"reference inputs": [],
"required signers (payment key hashes needed for scripts)": null,
"return collateral": null,
"scripts": [],
"total collateral": null,
"treasuryDonation": 0,
"update proposal": null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"certificates": null,
"collateral inputs": [],
"currentTreasuryValue": null,
"datums": [],
"era": "Conway",
"fee": "185433 Lovelace",
"governance actions": [],
Expand Down Expand Up @@ -30,6 +31,7 @@
"reference inputs": [],
"required signers (payment key hashes needed for scripts)": null,
"return collateral": null,
"scripts": [],
"total collateral": null,
"treasuryDonation": 0,
"update proposal": null,
Expand Down
Loading