Skip to content

Commit

Permalink
Do not guard on type when decoding Tx envelopes
Browse files Browse the repository at this point in the history
This makes our JSON representation also backward compatible the way the
CBOR format is backward compatible.
  • Loading branch information
ch1bo committed Sep 9, 2024
1 parent 0809e4e commit 9e98ef6
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions hydra-tx/src/Hydra/Tx/IsTx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -117,17 +117,16 @@ instance FromJSON Tx where
parseJSON =
withObject "Tx" $ \o -> do
hexText <- o .: "cborHex"
ty <- o .: "type"
-- NOTE: We deliberately ingore the "type" to be backwards compatible
bytes <- decodeBase16 hexText
case deserialiseFromCBOR (proxyToAsType (Proxy @Tx)) bytes of
Left e -> fail $ show e
Right tx ->
Right tx -> do
-- NOTE: Check txId equivalence only if present.
(o .:? "txId") >>= \case
Nothing -> pure tx
Just txid' -> do
guard (txType tx == ty)
guard (txid' == txId tx)
pure tx
Just txid'
| txid' /= txId tx -> fail "txId not matching"
_ -> pure tx

-- XXX: Double CBOR encoding?
instance IsShelleyBasedEra era => ToCBOR (Api.Tx era) where
Expand All @@ -150,11 +149,6 @@ instance FromCBOR UTxO where
fromCBOR = fromLedgerUTxO <$> fromCBOR
label _ = label (Proxy @(Ledger.UTxO LedgerEra))

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

instance IsTx Tx where
type TxIdType Tx = TxId
type TxOutType Tx = TxOut CtxUTxO
Expand Down

0 comments on commit 9e98ef6

Please sign in to comment.