From ba0fef57c416e2475d399e67a271cf69e6088f06 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 ab6fb0c665..080de6f0e5 100644 --- a/cardano-api/internal/Cardano/Api/Tx/Body.hs +++ b/cardano-api/internal/Cardano/Api/Tx/Body.hs @@ -214,6 +214,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 @@ -266,6 +268,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_) @@ -288,6 +291,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 (..)) @@ -372,6 +376,7 @@ txOutToJsonValue era (TxOut addr val dat refScript) = , datHashJsonVal dat , "datum" .= datJsonVal dat , "inlineDatum" .= inlineDatumJsonVal dat + , "inlineDatumRaw" .= inlineDatumRawJsonCbor dat , "referenceScript" .= refScriptJsonVal refScript ] ConwayEra -> @@ -381,6 +386,7 @@ txOutToJsonValue era (TxOut addr val dat refScript) = , datHashJsonVal dat , "datum" .= datJsonVal dat , "inlineDatum" .= inlineDatumJsonVal dat + , "inlineDatumRaw" .= inlineDatumRawJsonCbor dat , "referenceScript" .= refScriptJsonVal refScript ] where @@ -412,6 +418,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