Skip to content

Commit

Permalink
Script ToJSON instances serialises the full script
Browse files Browse the repository at this point in the history
There used to be no orphan instance for ledger Script so we (Hydra)
provided our own. This newly introduced instance overlaps with our own
which would not be a problem, as we are fine using upstream instances,
but for the fact the serialisation is lossy as cardano-api only
serialised the hash of the script.
  • Loading branch information
abailly authored and ch1bo committed Jul 25, 2022
1 parent 753d91b commit 8794da7
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions cardano-api/src/Cardano/Api/Orphans.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE UndecidableInstances #-}

{-# OPTIONS_GHC -Wno-orphans #-}
Expand Down Expand Up @@ -40,6 +39,7 @@ import Cardano.Ledger.UnifiedMap (UnifiedMap)
import Cardano.Slotting.Slot (SlotNo (..))
import Cardano.Slotting.Time (SystemStart (..))

import qualified Cardano.Binary as CBOR
import qualified Cardano.Crypto.Hash.Class as Crypto
import qualified Cardano.Ledger.Alonzo.Data as Alonzo
import qualified Cardano.Ledger.Alonzo.Scripts as Alonzo
Expand All @@ -61,7 +61,6 @@ import qualified Cardano.Ledger.Shelley.Rewards as Shelley
import qualified Ouroboros.Consensus.Shelley.Eras as Consensus

import Cardano.Api.Script
import Cardano.Api.SerialiseRaw (serialiseToRawBytesHexText)

-- Orphan instances involved in the JSON output of the API queries.
-- We will remove/replace these as we provide more API wrapper types
Expand Down Expand Up @@ -241,11 +240,10 @@ instance ( Ledger.Era era
SNothing -> Aeson.Null
SJust dH -> toJSON $ ScriptDataHash dH

instance ToJSON (Alonzo.Script (Babbage.BabbageEra Consensus.StandardCrypto)) where
toJSON s = Aeson.String . serialiseToRawBytesHexText
$ ScriptHash $ Ledger.hashScript @(Babbage.BabbageEra Consensus.StandardCrypto) s


instance ToJSON (Alonzo.Script (Babbage.BabbageEra Consensus.StandardCrypto)) where
toJSON = Aeson.String . Text.decodeUtf8 . B16.encode . CBOR.serialize'

instance Crypto.Crypto crypto => ToJSON (Shelley.DPState crypto) where
toJSON dpState = object [ "dstate" .= Shelley.dpsDState dpState
Expand Down

0 comments on commit 8794da7

Please sign in to comment.