Skip to content

Commit

Permalink
Fix the tx type
Browse files Browse the repository at this point in the history
  • Loading branch information
ffakenz committed May 9, 2024
1 parent 95c0ff0 commit b8751f4
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions hydra-node/src/Hydra/Ledger/Cardano.hs
Original file line number Diff line number Diff line change
Expand Up @@ -130,31 +130,33 @@ instance FromCBOR Tx where
(fail . toString . toLazyText . build)
(pure . fromLedgerTx)

txType :: Tx -> Text
txType tx' = case getTxWitnesses tx' of
[] -> "Unwitnessed Tx BabbageEra"
_ -> "Witnessed Tx BabbageEra"

instance ToJSON Tx where
toJSON tx =
let TextEnvelopeType envelopeType = textEnvelopeType (proxyToAsType (Proxy @Tx))
in object
[ "cborHex" .= Aeson.String (decodeUtf8 $ Base16.encode $ serialiseToCBOR tx)
, "txId" .= txId tx
, "type" .= envelopeType
, "description" .= Aeson.String mempty
]
object
[ "cborHex" .= Aeson.String (decodeUtf8 $ Base16.encode $ serialiseToCBOR tx)
, "txId" .= txId tx
, "type" .= txType tx
, "description" .= Aeson.String mempty
]

instance FromJSON Tx where
parseJSON =
withObject "Tx" $ \o -> do
let TextEnvelopeType envelopeType = textEnvelopeType (proxyToAsType (Proxy @Tx))
hexText <- o .: "cborHex"
ty <- o .: "type"
guard (envelopeType == ty)
bytes <- decodeBase16 hexText

case deserialiseFromCBOR (proxyToAsType (Proxy @Tx)) bytes of
Left e -> fail $ show e
Right tx ->
(o .:? "txId") >>= \case
Nothing -> pure tx
Just txid' -> do
guard (txType tx == ty)
guard (txid' == txId tx)
pure tx

Expand Down

0 comments on commit b8751f4

Please sign in to comment.