Skip to content

Commit

Permalink
Add new field 'inlineDatumRaw' to TxOut ToJSON instance
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ffakenz committed Sep 19, 2024
1 parent 9b3cbd9 commit e98364c
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions cardano-api/internal/Cardano/Api/Tx/Body.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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_)
Expand All @@ -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 (..))
Expand Down Expand Up @@ -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 ->
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e98364c

Please sign in to comment.