Skip to content

Commit 49bac63

Browse files
authored
Merge pull request #880 from IntersectMBO/serialise-raw-bytes-for-unsigned-tx
Add `SerialiseAsRawBytes` instance to `UnsignedTx ConwayEra`
2 parents e8c3dcc + 4d3c8fe commit 49bac63

File tree

1 file changed

+42
-8
lines changed
  • cardano-api/src/Cardano/Api/Experimental

1 file changed

+42
-8
lines changed

cardano-api/src/Cardano/Api/Experimental/Tx.hs

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
{-# LANGUAGE FlexibleContexts #-}
44
{-# LANGUAGE FlexibleInstances #-}
55
{-# LANGUAGE GADTs #-}
6+
{-# LANGUAGE InstanceSigs #-}
67
{-# LANGUAGE RankNTypes #-}
78
{-# LANGUAGE ScopedTypeVariables #-}
89
{-# LANGUAGE TypeApplications #-}
@@ -149,33 +150,65 @@ import Cardano.Api.Era.Internal.Feature
149150
import Cardano.Api.Experimental.Era
150151
import Cardano.Api.Experimental.Tx.Internal.AnyWitness
151152
import Cardano.Api.Experimental.Tx.Internal.TxScriptWitnessRequirements
153+
import Cardano.Api.HasTypeProxy (HasTypeProxy (..), Proxy, asType)
152154
import Cardano.Api.Ledger.Internal.Reexport (StrictMaybe (..), maybeToStrictMaybe)
153155
import Cardano.Api.Ledger.Internal.Reexport qualified as L
154156
import Cardano.Api.Pretty (docToString, pretty)
157+
import Cardano.Api.Serialise.Raw
158+
( SerialiseAsRawBytes (..)
159+
, SerialiseAsRawBytesError (SerialiseAsRawBytesError)
160+
)
155161
import Cardano.Api.Tx.Internal.Body
156162
import Cardano.Api.Tx.Internal.Sign
157163

158164
import Cardano.Crypto.Hash qualified as Hash
159165
import Cardano.Ledger.Alonzo.TxBody qualified as L
160166
import Cardano.Ledger.Api qualified as L
161-
import Cardano.Ledger.Conway qualified as Ledger
167+
import Cardano.Ledger.Binary qualified as Ledger
162168
import Cardano.Ledger.Conway.TxBody qualified as L
163169
import Cardano.Ledger.Core qualified as Ledger
164170
import Cardano.Ledger.Hashes qualified as L hiding (Hash)
165171

172+
import Control.Exception (displayException)
173+
import Data.Bifunctor (bimap)
174+
import Data.ByteString.Lazy (fromStrict)
166175
import Data.Set qualified as Set
167176
import GHC.Exts (IsList (..))
168177
import GHC.Stack
169178
import Lens.Micro
170179

171180
-- | A transaction that can contain everything
172181
-- except key witnesses.
173-
newtype UnsignedTx era
174-
= UnsignedTx (Ledger.Tx (LedgerEra era))
182+
data UnsignedTx era
183+
= L.EraTx (LedgerEra era) => UnsignedTx (Ledger.Tx (LedgerEra era))
184+
185+
instance HasTypeProxy era => HasTypeProxy (UnsignedTx era) where
186+
data AsType (UnsignedTx era) = AsUnsignedTx (AsType era)
187+
proxyToAsType :: Proxy (UnsignedTx era) -> AsType (UnsignedTx era)
188+
proxyToAsType _ = AsUnsignedTx (asType @era)
175189

176-
instance IsEra era => Show (UnsignedTx era) where
177-
showsPrec p (UnsignedTx tx) = case useEra @era of
178-
ConwayEra -> showsPrec p (tx :: Ledger.Tx Ledger.ConwayEra)
190+
instance
191+
( HasTypeProxy era
192+
, L.EraTx (LedgerEra era)
193+
)
194+
=> SerialiseAsRawBytes (UnsignedTx era)
195+
where
196+
serialiseToRawBytes (UnsignedTx tx) =
197+
Ledger.serialize' (Ledger.eraProtVerHigh @(LedgerEra era)) tx
198+
deserialiseFromRawBytes _ =
199+
bimap wrapError UnsignedTx
200+
. Ledger.decodeFullAnnotator
201+
(Ledger.eraProtVerHigh @(LedgerEra era))
202+
"UnsignedTx"
203+
Ledger.decCBOR
204+
. fromStrict
205+
where
206+
wrapError
207+
:: Ledger.DecoderError -> SerialiseAsRawBytesError
208+
wrapError = SerialiseAsRawBytesError . displayException
209+
210+
instance Show (UnsignedTx era) where
211+
showsPrec p (UnsignedTx tx) = showsPrec p tx
179212

180213
newtype UnsignedTxError
181214
= UnsignedTxError TxBodyError
@@ -275,7 +308,8 @@ eraSpecificLedgerTxBody ConwayEra ledgerbody bc =
275308
.~ convProposalProcedures (maybe TxProposalProceduresNone unFeatured propProcedures)
276309
& L.votingProceduresTxBodyL
277310
.~ convVotingProcedures (maybe TxVotingProceduresNone unFeatured voteProcedures)
278-
& L.treasuryDonationTxBodyL .~ maybe (L.Coin 0) unFeatured treasuryDonation
311+
& L.treasuryDonationTxBodyL
312+
.~ maybe (L.Coin 0) unFeatured treasuryDonation
279313
& L.currentTreasuryValueTxBodyL
280314
.~ L.maybeToStrictMaybe (unFeatured =<< currentTresuryValue)
281315

@@ -328,5 +362,5 @@ convertTxBodyToUnsignedTx sbe txbody =
328362
(error $ "convertTxBodyToUnsignedTx: Error - unsupported era " <> docToString (pretty sbe))
329363
( \w -> do
330364
let ShelleyTx _ unsignedLedgerTx = makeSignedTransaction [] txbody
331-
UnsignedTx $ obtainCommonConstraints w unsignedLedgerTx
365+
obtainCommonConstraints w $ UnsignedTx unsignedLedgerTx
332366
)

0 commit comments

Comments
 (0)