@@ -46,6 +46,7 @@ import Cardano.Api.Internal.SerialiseTextEnvelope
46
46
, TextEnvelopeType (TextEnvelopeType )
47
47
, deserialiseFromTextEnvelope
48
48
, legacyComparison
49
+ , serialiseTextEnvelope
49
50
, serialiseToTextEnvelope
50
51
)
51
52
import Cardano.Api.Internal.Tx.Sign
@@ -57,13 +58,11 @@ import Cardano.Ledger.Binary qualified as CBOR
57
58
58
59
import Control.Monad.Trans.Except.Extra
59
60
( firstExceptT
60
- , handleIOExceptT
61
61
, hoistEither
62
62
, newExceptT
63
63
, runExceptT
64
64
)
65
65
import Data.Aeson qualified as Aeson
66
- import Data.Aeson.Encode.Pretty (Config (.. ), defConfig , encodePretty' , keyOrder )
67
66
import Data.Bifunctor (first )
68
67
import Data.ByteString (ByteString )
69
68
import Data.ByteString.Lazy qualified as LBS
@@ -136,11 +135,10 @@ writeByronTxFileTextEnvelopeCddl
136
135
:: File content Out
137
136
-> Byron. ATxAux ByteString
138
137
-> IO (Either (FileError () ) () )
139
- writeByronTxFileTextEnvelopeCddl path w =
140
- runExceptT $ do
141
- handleIOExceptT (FileIOError (unFile path)) $ LBS. writeFile (unFile path) txJson
142
- where
143
- txJson = encodePretty' textEnvelopeCddlJSONConfig (serializeByronTx w) <> " \n "
138
+ writeByronTxFileTextEnvelopeCddl path =
139
+ writeLazyByteStringFile path
140
+ . serialiseTextEnvelope
141
+ . serializeByronTx
144
142
145
143
serializeByronTx :: Byron. ATxAux ByteString -> TextEnvelope
146
144
serializeByronTx tx =
@@ -217,63 +215,51 @@ writeTxFileTextEnvelopeCddl
217
215
-> File content Out
218
216
-> Tx era
219
217
-> IO (Either (FileError () ) () )
220
- writeTxFileTextEnvelopeCddl sbe = writeTxFileTextEnvelopeCddl' sbe False
218
+ writeTxFileTextEnvelopeCddl sbe path =
219
+ writeLazyByteStringFile path
220
+ . serialiseTextEnvelope
221
+ . serialiseTxToTextEnvelope sbe
221
222
222
- -- | Write transaction in the text envelope format, the CBOR will be in canonical format according
223
- -- to CIP-21 https://github.com/cardano-foundation/CIPs/blob/master/CIP-0021/README.md#canonical-cbor-serialization-format
223
+ -- | Write transaction in the text envelope format. The CBOR will be in canonical format according
224
+ -- to RFC 7049. It is also a requirement of CIP-21, which is not fully implemented.
225
+ --
226
+ -- 1. RFC 7049: https://datatracker.ietf.org/doc/html/rfc7049#section-3.9
227
+ -- 2. CIP-21: https://github.com/cardano-foundation/CIPs/blob/master/CIP-0021/README.md#canonical-cbor-serialization-format
224
228
writeTxFileTextEnvelopeCanonicalCddl
225
229
:: ShelleyBasedEra era
226
230
-> File content Out
227
231
-> Tx era
228
232
-> IO (Either (FileError () ) () )
229
- writeTxFileTextEnvelopeCanonicalCddl sbe = writeTxFileTextEnvelopeCddl' sbe True
230
-
231
- writeTxFileTextEnvelopeCddl'
232
- :: ()
233
- => ShelleyBasedEra era
234
- -> Bool
235
- -- ^ True to produce canonical CBOR
236
- -> File content Out
237
- -> Tx era
238
- -> IO (Either (FileError () ) () )
239
- writeTxFileTextEnvelopeCddl' era isCanonicalCbor path tx =
240
- runExceptT $ do
241
- handleIOExceptT (FileIOError (unFile path)) $ LBS. writeFile (unFile path) txJson
233
+ writeTxFileTextEnvelopeCanonicalCddl sbe path =
234
+ writeLazyByteStringFile path
235
+ . serialiseTextEnvelope
236
+ . canonicaliseTextEnvelopeCbor
237
+ . serialiseTxToTextEnvelope sbe
242
238
where
243
- txJson = encodePretty' textEnvelopeCddlJSONConfig (serialiseTxLedgerCddl era tx) <> " \n "
244
-
245
- serialiseTxLedgerCddl :: ShelleyBasedEra era -> Tx era -> TextEnvelope
246
- serialiseTxLedgerCddl era' tx' =
247
- shelleyBasedEraConstraints era' $ do
248
- let te = serialiseToTextEnvelope (Just (TextEnvelopeDescr " Ledger Cddl Format" )) tx'
249
- if isCanonicalCbor
250
- then do
251
- let canonicalisedTxBs =
252
- either
253
- (\ err -> error $ " Impossible - deserialisation of just serialised bytes failed " <> show err)
254
- id
255
- . canonicaliseCborBs
256
- $ teRawCBOR te
257
- te{teRawCBOR = canonicalisedTxBs}
258
- else te
239
+ canonicaliseTextEnvelopeCbor :: TextEnvelope -> TextEnvelope
240
+ canonicaliseTextEnvelopeCbor te = do
241
+ let canonicalisedTxBs =
242
+ either
243
+ (\ err -> error $ " Impossible - deserialisation of just serialised bytes failed " <> show err)
244
+ id
245
+ . canonicaliseCborBs
246
+ $ teRawCBOR te
247
+ te{teRawCBOR = canonicalisedTxBs}
248
+
249
+ serialiseTxToTextEnvelope :: ShelleyBasedEra era -> Tx era -> TextEnvelope
250
+ serialiseTxToTextEnvelope era' tx' =
251
+ shelleyBasedEraConstraints era' $ do
252
+ serialiseToTextEnvelope (Just " Ledger Cddl Format" ) tx'
259
253
260
254
writeTxWitnessFileTextEnvelopeCddl
261
255
:: ShelleyBasedEra era
262
256
-> File () Out
263
257
-> KeyWitness era
264
258
-> IO (Either (FileError () ) () )
265
- writeTxWitnessFileTextEnvelopeCddl sbe path w =
266
- runExceptT $ do
267
- handleIOExceptT (FileIOError (unFile path)) $ LBS. writeFile (unFile path) txJson
268
- where
269
- txJson = encodePretty' textEnvelopeCddlJSONConfig (serialiseWitnessLedgerCddl sbe w) <> " \n "
270
-
271
- textEnvelopeCddlJSONConfig :: Config
272
- textEnvelopeCddlJSONConfig =
273
- defConfig{confCompare = textEnvelopeCddlJSONKeyOrder}
274
-
275
- textEnvelopeCddlJSONKeyOrder :: Text -> Text -> Ordering
276
- textEnvelopeCddlJSONKeyOrder = keyOrder [" type" , " description" , " cborHex" ]
259
+ writeTxWitnessFileTextEnvelopeCddl sbe path =
260
+ writeLazyByteStringFile path
261
+ . serialiseTextEnvelope
262
+ . serialiseWitnessLedgerCddl sbe
277
263
278
264
-- | This GADT allows us to deserialise a tx or key witness without
279
265
-- having to provide the era.
0 commit comments