Skip to content

Commit 0f98cab

Browse files
Encode and decode txId
1 parent f7e8fdc commit 0f98cab

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

hydra-node/src/Hydra/Ledger/Cardano.hs

+23-16
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ import Hydra.Prelude
1212
import Hydra.Cardano.Api hiding (initialLedgerState)
1313
import Hydra.Ledger.Cardano.Builder
1414

15-
import Data.Aeson (object, (.:), (.=))
16-
import Data.Aeson qualified as Aeson
1715
import Cardano.Api.UTxO (fromPairs, pairs)
1816
import Cardano.Api.UTxO qualified as UTxO
1917
import Cardano.Crypto.DSIGN qualified as CC
@@ -30,7 +28,11 @@ import Cardano.Ledger.Shelley.UTxO qualified as Ledger
3028
import Codec.CBOR.Decoding qualified as CBOR
3129
import Codec.CBOR.Encoding qualified as CBOR
3230
import Control.Monad (foldM)
31+
import Data.Aeson (object, (.:), (.:?), (.=))
32+
import Data.Aeson qualified as Aeson
33+
import Data.Aeson.Types (withObject)
3334
import Data.ByteString qualified as BS
35+
import Data.ByteString.Base16 qualified as Base16
3436
import Data.Default (def)
3537
import Data.Map.Strict qualified as Map
3638
import Data.Maybe (fromJust)
@@ -52,9 +54,6 @@ import Test.QuickCheck (
5254
suchThat,
5355
vectorOf,
5456
)
55-
import qualified Data.ByteString.Base16 as Base16
56-
import Data.Aeson.Types (withObject)
57-
import Data.Aeson ((.:?))
5857

5958
-- * Ledger
6059

@@ -130,20 +129,28 @@ instance FromCBOR Tx where
130129
(pure . fromLedgerTx)
131130

132131
instance ToJSON Tx where
133-
toJSON tx = object [ "cborHex" .= (Aeson.String $ decodeUtf8 $ Base16.encode $ serialiseToCBOR tx) ]
132+
toJSON tx =
133+
object
134+
[ "cborHex" .= (Aeson.String $ decodeUtf8 $ Base16.encode $ serialiseToCBOR tx)
135+
, "txId" .= (txId tx)
136+
]
134137

135138
instance FromJSON Tx where
136-
parseJSON =
137-
withObject "Tx" $ \o -> do
138-
let TextEnvelopeType envelopeType = textEnvelopeType (proxyToAsType (Proxy @Tx ))
139-
hexText <- o .: "cborHex"
140-
(o .:? "type") >>= \case
139+
parseJSON =
140+
withObject "Tx" $ \o -> do
141+
let TextEnvelopeType envelopeType = textEnvelopeType (proxyToAsType (Proxy @Tx))
142+
hexText <- o .: "cborHex"
143+
(o .:? "type") >>= \case
144+
Nothing -> pure ()
145+
Just x -> guard (envelopeType == x)
146+
bytes <- decodeBase16 hexText
147+
case deserialiseFromCBOR (proxyToAsType (Proxy @Tx)) bytes of
148+
Left e -> fail $ show e
149+
Right tx -> (o .:? "txId") >>= \case
141150
Nothing -> pure ()
142-
Just x -> guard (envelopeType == x)
143-
bytes <- decodeBase16 hexText
144-
case deserialiseFromCBOR (proxyToAsType (Proxy @Tx)) bytes of
145-
Left e -> fail $ show e
146-
Right x -> pure x
151+
Just txid' -> do
152+
guard (txid' == (txId tx))
153+
pure tx
147154

148155
instance Arbitrary Tx where
149156
-- TODO: shrinker!

0 commit comments

Comments
 (0)