@@ -22,6 +22,7 @@ import Cardano.Ledger.Binary (Annotator, DecCBOR (decCBOR), EncCBOR, Version, de
22
22
import Cardano.Wasm.Internal.ExceptionHandling (justOrError , rightOrError )
23
23
24
24
import Codec.CBOR.Write qualified as CBOR
25
+ import Control.Monad.Catch (Exception (displayException ), MonadThrow )
25
26
import Data.Aeson (ToJSON (toJSON ), (.=) )
26
27
import Data.Aeson qualified as Aeson
27
28
import Data.Aeson.Types qualified as Aeson
@@ -64,15 +65,17 @@ instance FromJSON UnsignedTxObject where
64
65
parseJSON :: HasCallStack => Aeson. Value -> Aeson. Parser UnsignedTxObject
65
66
parseJSON = Aeson. withObject " UnsignedTxObject" $ \ o -> do
66
67
Exp. Some era <- o Aeson. .: " era"
67
- let decode :: forall a . DecCBOR (Annotator a ) => Text. Text -> Text. Text -> a
68
+ let decode :: forall m a . ( MonadThrow m , DecCBOR (Annotator a )) => Text. Text -> Text. Text -> m a
68
69
decode desc cbor = do
69
- let cddlBS = rightOrError $ Base16. decode $ Text. encodeUtf8 cbor
70
+ cddlBS <- rightOrError $ Base16. decode $ Text. encodeUtf8 cbor
70
71
rightOrError $ decodeFullAnnotator (eraToVersion era) desc decCBOR (fromStrict cddlBS)
71
72
keyWitnesses :: [Text. Text ] <- o Aeson. .: " keyWitnesess"
72
73
tx :: Text. Text <- o Aeson. .: " tx"
73
- obtainCommonConstraints era $
74
+ obtainCommonConstraints era $ do
75
+ decodedWitnesses <- mapM (toMonadFail . decode " KeyWitness" ) keyWitnesses
76
+ decodedTx <- toMonadFail $ decode " Tx" tx
74
77
return $
75
- UnsignedTxObject era ( map (decode " KeyWitness " ) keyWitnesses) ( Exp. UnsignedTx (decode " Tx " tx) )
78
+ UnsignedTxObject era decodedWitnesses ( Exp. UnsignedTx decodedTx )
76
79
77
80
-- | Create a new unsigned transaction object for making a Conway era transaction.
78
81
newConwayTxImpl :: UnsignedTxObject
@@ -88,11 +91,12 @@ addTxInputImpl (UnsignedTxObject era keyWitnesess (Exp.UnsignedTx tx)) txId txIx
88
91
89
92
-- | Add a simple transaction output to an unsigned transaction object.
90
93
-- It takes a destination address and an amount in lovelace.
91
- addSimpleTxOutImpl :: HasCallStack => UnsignedTxObject -> String -> Ledger. Coin -> UnsignedTxObject
94
+ addSimpleTxOutImpl
95
+ :: (HasCallStack , MonadThrow m ) => UnsignedTxObject -> String -> Ledger. Coin -> m UnsignedTxObject
92
96
addSimpleTxOutImpl (UnsignedTxObject era keyWitnesess (Exp. UnsignedTx tx)) destAddr lovelaceAmount =
93
- obtainCommonConstraints era $
94
- let destAddress = deserialiseAddress era destAddr
95
- sbe = Api. convert era
97
+ obtainCommonConstraints era $ do
98
+ destAddress <- deserialiseAddress era destAddr
99
+ let sbe = Api. convert era
96
100
txOut =
97
101
Api. TxOut
98
102
destAddress
@@ -101,12 +105,11 @@ addSimpleTxOutImpl (UnsignedTxObject era keyWitnesess (Exp.UnsignedTx tx)) destA
101
105
Shelley. ReferenceScriptNone
102
106
shelleyTxOut = TxBody. toShelleyTxOutAny sbe txOut
103
107
tx' = tx & Ledger. bodyTxL . Ledger. outputsTxBodyL %~ (<> StrictSeq. fromList [shelleyTxOut])
104
- in UnsignedTxObject era keyWitnesess $ Exp. UnsignedTx tx'
108
+ return $ UnsignedTxObject era keyWitnesess $ Exp. UnsignedTx tx'
105
109
where
106
110
deserialiseAddress
107
- :: HasCallStack
108
- => Exp. EraCommonConstraints era
109
- => Exp. Era era -> String -> Api. AddressInEra era
111
+ :: (HasCallStack , MonadThrow m , Exp. EraCommonConstraints era )
112
+ => Exp. Era era -> String -> m (Api. AddressInEra era )
110
113
deserialiseAddress _eon destAddrStr =
111
114
justOrError
112
115
" Couldn't deserialise destination address"
@@ -155,14 +158,21 @@ instance FromJSON SignedTxObject where
155
158
parseJSON :: HasCallStack => Aeson. Value -> Aeson. Parser SignedTxObject
156
159
parseJSON = Aeson. withObject " SignedTxObject" $ \ o -> do
157
160
Exp. Some era <- o Aeson. .: " era"
158
- let decode :: forall a . DecCBOR (Annotator a ) => Text. Text -> Text. Text -> a
161
+ let decode :: forall m a . ( MonadThrow m , DecCBOR (Annotator a )) => Text. Text -> Text. Text -> m a
159
162
decode desc cbor = do
160
- let cddlBS = rightOrError $ Base16. decode $ Text. encodeUtf8 cbor
163
+ cddlBS <- rightOrError $ Base16. decode $ Text. encodeUtf8 cbor
161
164
rightOrError $ decodeFullAnnotator (eraToVersion era) desc decCBOR (fromStrict cddlBS)
162
165
tx :: Text. Text <- o Aeson. .: " tx"
163
- obtainCommonConstraints era $
166
+ obtainCommonConstraints era $ do
167
+ decodedTx <- toMonadFail $ decode " Tx" tx
164
168
return $
165
- SignedTxObject era (decode " Tx" tx)
169
+ SignedTxObject era decodedTx
170
+
171
+ -- | Convert an 'Either' value to a 'MonadFail' monad. This can be useful for converting
172
+ -- MonadThrow monads into Aeson Parser monads, but it loses the stack trace information.
173
+ toMonadFail :: (Exception e , MonadFail m ) => Either e a -> m a
174
+ toMonadFail (Left e) = fail $ displayException e
175
+ toMonadFail (Right a) = return a
166
176
167
177
-- | Convert a signed transaction object to a base16 encoded string of its CBOR representation.
168
178
toCborImpl :: SignedTxObject -> String
0 commit comments