From cb548d80f3281a5ddb29eb61c52f53257fd0af37 Mon Sep 17 00:00:00 2001 From: Franco Testagrossa Date: Wed, 11 Sep 2024 02:26:45 +0200 Subject: [PATCH] Add new field 'inlineDatumRaw' to TxOut ToJSON instance It contains the raw CBOR for any inline datum. Reason: > When building applications that need to spend from a script UTxO which has a datum attached (like in a Hydra head), that off-chain code needs access to the raw Datum for evaluating the transaction and calculate fees. --- cardano-api/internal/Cardano/Api/Tx/Body.hs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/cardano-api/internal/Cardano/Api/Tx/Body.hs b/cardano-api/internal/Cardano/Api/Tx/Body.hs index 0e40356f0c..46baa9d217 100644 --- a/cardano-api/internal/Cardano/Api/Tx/Body.hs +++ b/cardano-api/internal/Cardano/Api/Tx/Body.hs @@ -211,6 +211,8 @@ import Cardano.Api.ProtocolParameters import qualified Cardano.Api.ReexposeLedger as Ledger import Cardano.Api.Script import Cardano.Api.ScriptData +import Cardano.Api.ScriptData () +import Cardano.Api.SerialiseCBOR import Cardano.Api.SerialiseJSON import Cardano.Api.SerialiseRaw import Cardano.Api.Tx.Sign @@ -263,6 +265,7 @@ import qualified Data.Aeson.Key as Aeson import qualified Data.Aeson.Types as Aeson import Data.Bifunctor (Bifunctor (..)) import Data.ByteString (ByteString) +import qualified Data.ByteString.Base16 as Base16 import qualified Data.ByteString.Char8 as BSC import qualified Data.DList as DList import Data.Foldable (for_) @@ -285,6 +288,7 @@ import qualified Data.Set as Set import Data.String import Data.Text (Text) import qualified Data.Text as Text +import qualified Data.Text.Encoding as Text import Data.Type.Equality (TestEquality (..), (:~:) (Refl)) import Data.Word (Word16, Word32, Word64) import GHC.Exts (IsList (..)) @@ -369,6 +373,7 @@ txOutToJsonValue era (TxOut addr val dat refScript) = , datHashJsonVal dat , "datum" .= datJsonVal dat , "inlineDatum" .= inlineDatumJsonVal dat + , "inlineDatumRaw" .= inlineDatumRawJsonCbor dat , "referenceScript" .= refScriptJsonVal refScript ] ConwayEra -> @@ -378,6 +383,7 @@ txOutToJsonValue era (TxOut addr val dat refScript) = , datHashJsonVal dat , "datum" .= datJsonVal dat , "inlineDatum" .= inlineDatumJsonVal dat + , "inlineDatumRaw" .= inlineDatumRawJsonCbor dat , "referenceScript" .= refScriptJsonVal refScript ] where @@ -409,6 +415,19 @@ txOutToJsonValue era (TxOut addr val dat refScript) = TxOutDatumInTx'{} -> Aeson.Null TxOutDatumInline _ datum -> scriptDataToJson ScriptDataJsonDetailedSchema datum + inlineDatumRawJsonCbor :: TxOutDatum ctx era -> Aeson.Value + inlineDatumRawJsonCbor d = + case d of + TxOutDatumNone -> Aeson.Null + TxOutDatumHash{} -> Aeson.Null + TxOutDatumInTx'{} -> Aeson.Null + TxOutDatumInline _ datum -> + Aeson.String + . Text.decodeUtf8 + . Base16.encode + . serialiseToCBOR + $ datum + refScriptJsonVal :: ReferenceScript era -> Aeson.Value refScriptJsonVal rScript = case rScript of