|
3 | 3 | {-# LANGUAGE FlexibleContexts #-} |
4 | 4 | {-# LANGUAGE FlexibleInstances #-} |
5 | 5 | {-# LANGUAGE GADTs #-} |
| 6 | +{-# LANGUAGE InstanceSigs #-} |
6 | 7 | {-# LANGUAGE RankNTypes #-} |
7 | 8 | {-# LANGUAGE ScopedTypeVariables #-} |
8 | 9 | {-# LANGUAGE TypeApplications #-} |
@@ -149,33 +150,65 @@ import Cardano.Api.Era.Internal.Feature |
149 | 150 | import Cardano.Api.Experimental.Era |
150 | 151 | import Cardano.Api.Experimental.Tx.Internal.AnyWitness |
151 | 152 | import Cardano.Api.Experimental.Tx.Internal.TxScriptWitnessRequirements |
| 153 | +import Cardano.Api.HasTypeProxy (HasTypeProxy (..), Proxy, asType) |
152 | 154 | import Cardano.Api.Ledger.Internal.Reexport (StrictMaybe (..), maybeToStrictMaybe) |
153 | 155 | import Cardano.Api.Ledger.Internal.Reexport qualified as L |
154 | 156 | import Cardano.Api.Pretty (docToString, pretty) |
| 157 | +import Cardano.Api.Serialise.Raw |
| 158 | + ( SerialiseAsRawBytes (..) |
| 159 | + , SerialiseAsRawBytesError (SerialiseAsRawBytesError) |
| 160 | + ) |
155 | 161 | import Cardano.Api.Tx.Internal.Body |
156 | 162 | import Cardano.Api.Tx.Internal.Sign |
157 | 163 |
|
158 | 164 | import Cardano.Crypto.Hash qualified as Hash |
159 | 165 | import Cardano.Ledger.Alonzo.TxBody qualified as L |
160 | 166 | import Cardano.Ledger.Api qualified as L |
161 | | -import Cardano.Ledger.Conway qualified as Ledger |
| 167 | +import Cardano.Ledger.Binary qualified as Ledger |
162 | 168 | import Cardano.Ledger.Conway.TxBody qualified as L |
163 | 169 | import Cardano.Ledger.Core qualified as Ledger |
164 | 170 | import Cardano.Ledger.Hashes qualified as L hiding (Hash) |
165 | 171 |
|
| 172 | +import Control.Exception (displayException) |
| 173 | +import Data.Bifunctor (bimap) |
| 174 | +import Data.ByteString.Lazy (fromStrict) |
166 | 175 | import Data.Set qualified as Set |
167 | 176 | import GHC.Exts (IsList (..)) |
168 | 177 | import GHC.Stack |
169 | 178 | import Lens.Micro |
170 | 179 |
|
171 | 180 | -- | A transaction that can contain everything |
172 | 181 | -- 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) |
175 | 189 |
|
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 |
179 | 212 |
|
180 | 213 | newtype UnsignedTxError |
181 | 214 | = UnsignedTxError TxBodyError |
@@ -275,7 +308,8 @@ eraSpecificLedgerTxBody ConwayEra ledgerbody bc = |
275 | 308 | .~ convProposalProcedures (maybe TxProposalProceduresNone unFeatured propProcedures) |
276 | 309 | & L.votingProceduresTxBodyL |
277 | 310 | .~ convVotingProcedures (maybe TxVotingProceduresNone unFeatured voteProcedures) |
278 | | - & L.treasuryDonationTxBodyL .~ maybe (L.Coin 0) unFeatured treasuryDonation |
| 311 | + & L.treasuryDonationTxBodyL |
| 312 | + .~ maybe (L.Coin 0) unFeatured treasuryDonation |
279 | 313 | & L.currentTreasuryValueTxBodyL |
280 | 314 | .~ L.maybeToStrictMaybe (unFeatured =<< currentTresuryValue) |
281 | 315 |
|
@@ -328,5 +362,5 @@ convertTxBodyToUnsignedTx sbe txbody = |
328 | 362 | (error $ "convertTxBodyToUnsignedTx: Error - unsupported era " <> docToString (pretty sbe)) |
329 | 363 | ( \w -> do |
330 | 364 | let ShelleyTx _ unsignedLedgerTx = makeSignedTransaction [] txbody |
331 | | - UnsignedTx $ obtainCommonConstraints w unsignedLedgerTx |
| 365 | + obtainCommonConstraints w $ UnsignedTx unsignedLedgerTx |
332 | 366 | ) |
0 commit comments