/
TxBody.hs
2369 lines (2003 loc) · 88.1 KB
/
TxBody.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE DisambiguateRecordFields #-}
{-# LANGUAGE EmptyCase #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE ViewPatterns #-}
-- | Transaction bodies
--
module Cardano.Api.TxBody (
-- * Transaction bodies
TxBody(.., TxBody),
makeTransactionBody,
TxBodyContent(..),
TxBodyError(..),
TxBodyScriptData(..),
-- * Transaction Ids
TxId(..),
getTxId,
-- * Transaction inputs
TxIn(..),
TxIx(..),
genesisUTxOPseudoTxIn,
-- * Transaction outputs
TxOut(..),
TxOutValue(..),
serialiseAddressForTxOut,
TxOutDatumHash(..),
-- * Other transaction body types
TxInsCollateral(..),
TxFee(..),
TxValidityLowerBound(..),
TxValidityUpperBound(..),
TxMetadataInEra(..),
TxAuxScripts(..),
TxAuxScriptData(..),
TxExtraKeyWitnesses(..),
TxWithdrawals(..),
TxCertificates(..),
TxUpdateProposal(..),
TxMintValue(..),
-- ** Building vs viewing transactions
BuildTxWith(..),
BuildTx,
ViewTx,
-- * Era-dependent transaction body features
CollateralSupportedInEra(..),
MultiAssetSupportedInEra(..),
OnlyAdaSupportedInEra(..),
TxFeesExplicitInEra(..),
TxFeesImplicitInEra(..),
ValidityUpperBoundSupportedInEra(..),
ValidityNoUpperBoundSupportedInEra(..),
ValidityLowerBoundSupportedInEra(..),
TxMetadataSupportedInEra(..),
AuxScriptsSupportedInEra(..),
TxExtraKeyWitnessesSupportedInEra(..),
ScriptDataSupportedInEra(..),
WithdrawalsSupportedInEra(..),
CertificatesSupportedInEra(..),
UpdateProposalSupportedInEra(..),
-- ** Feature availability functions
collateralSupportedInEra,
multiAssetSupportedInEra,
txFeesExplicitInEra,
validityUpperBoundSupportedInEra,
validityNoUpperBoundSupportedInEra,
validityLowerBoundSupportedInEra,
txMetadataSupportedInEra,
auxScriptsSupportedInEra,
extraKeyWitnessesSupportedInEra,
scriptDataSupportedInEra,
withdrawalsSupportedInEra,
certificatesSupportedInEra,
updateProposalSupportedInEra,
-- * Internal conversion functions & types
toShelleyTxId,
toShelleyTxIn,
toShelleyTxOut,
fromShelleyTxId,
fromShelleyTxIn,
fromShelleyTxOut,
fromTxOut,
-- * Data family instances
AsType(AsTxId, AsTxBody, AsByronTxBody, AsShelleyTxBody, AsMaryTxBody),
-- * Conversion functions
fromByronTxIn,
) where
import Prelude
import Control.Monad (guard)
import Data.Aeson (object, (.=))
import qualified Data.Aeson as Aeson
import Data.Aeson.Types (ToJSONKey (..), toJSONKeyText)
import Data.Bifunctor (first)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Lazy as LBS
import Data.Foldable (toList)
import Data.List (intercalate)
import qualified Data.List.NonEmpty as NonEmpty
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import Data.Maybe (fromMaybe, maybeToList)
import qualified Data.Sequence.Strict as Seq
import Data.Set (Set)
import qualified Data.Set as Set
import Data.String (IsString)
import Data.Text (Text)
import qualified Data.Text as Text
import Data.Word (Word64)
import GHC.Generics
import Cardano.Binary (Annotated (..), reAnnotate, recoverBytes)
import qualified Cardano.Binary as CBOR
import qualified Cardano.Crypto.Hash.Class as Crypto
import qualified Cardano.Ledger.Serialization as CBOR (decodeNullMaybe, encodeNullMaybe)
import Cardano.Slotting.Slot (SlotNo (..))
import qualified Cardano.Chain.Common as Byron
import qualified Cardano.Chain.UTxO as Byron
import qualified Cardano.Crypto.Hashing as Byron
import qualified Cardano.Ledger.AuxiliaryData as Ledger (hashAuxiliaryData)
import qualified Cardano.Ledger.Core as Core
import qualified Cardano.Ledger.Core as Ledger
import qualified Cardano.Ledger.Era as Ledger
import qualified Cardano.Ledger.SafeHash as SafeHash
import qualified Cardano.Ledger.Shelley.Constraints as Ledger
import Cardano.Ledger.BaseTypes (StrictMaybe (..), maybeToStrictMaybe)
import qualified Cardano.Ledger.Keys as Shelley
import qualified Shelley.Spec.Ledger.Address as Shelley
import qualified Shelley.Spec.Ledger.Credential as Shelley
import qualified Shelley.Spec.Ledger.Genesis as Shelley
import qualified Shelley.Spec.Ledger.Metadata as Shelley
import qualified Shelley.Spec.Ledger.Tx as Shelley
import qualified Shelley.Spec.Ledger.TxBody as Shelley
import qualified Shelley.Spec.Ledger.UTxO as Shelley
import qualified Cardano.Ledger.ShelleyMA.AuxiliaryData as Allegra
import qualified Cardano.Ledger.ShelleyMA.AuxiliaryData as Mary
import qualified Cardano.Ledger.ShelleyMA.TxBody as Allegra
import qualified Cardano.Ledger.ShelleyMA.TxBody as Mary
import Cardano.Ledger.Val (isZero)
import qualified Cardano.Ledger.Alonzo as Alonzo
import qualified Cardano.Ledger.Alonzo.Data as Alonzo
import qualified Cardano.Ledger.Alonzo.Language as Alonzo
import qualified Cardano.Ledger.Alonzo.Scripts as Alonzo
import qualified Cardano.Ledger.Alonzo.Tx as Alonzo
import qualified Cardano.Ledger.Alonzo.TxBody as Alonzo
import qualified Cardano.Ledger.Alonzo.TxWitness as Alonzo
import Ouroboros.Consensus.Shelley.Eras (StandardAllegra, StandardAlonzo, StandardMary,
StandardShelley)
import Cardano.Api.Address
import Cardano.Api.Certificate
import Cardano.Api.Eras
import Cardano.Api.Error
import Cardano.Api.HasTypeProxy
import Cardano.Api.Hash
import Cardano.Api.KeysByron
import Cardano.Api.KeysShelley
import Cardano.Api.NetworkId
import Cardano.Api.ProtocolParameters
import Cardano.Api.Script
import Cardano.Api.SerialiseBech32
import Cardano.Api.SerialiseCBOR
import Cardano.Api.SerialiseJSON
import Cardano.Api.SerialiseRaw
import Cardano.Api.SerialiseTextEnvelope
import Cardano.Api.SerialiseUsing
import Cardano.Api.TxMetadata
import Cardano.Api.Utils
import Cardano.Api.Value
import Cardano.Ledger.Crypto (StandardCrypto)
{- HLINT ignore "Redundant flip" -}
{- HLINT ignore "Use section" -}
-- ----------------------------------------------------------------------------
-- Transaction Ids
--
newtype TxId = TxId (Shelley.Hash StandardCrypto Shelley.EraIndependentTxBody)
-- We use the Shelley representation and convert to/from the Byron one
deriving stock (Eq, Ord)
deriving (Show, IsString) via UsingRawBytesHex TxId
deriving (ToJSON, FromJSON) via UsingRawBytesHex TxId
deriving (ToJSONKey, FromJSONKey) via UsingRawBytesHex TxId
instance HasTypeProxy TxId where
data AsType TxId = AsTxId
proxyToAsType _ = AsTxId
instance SerialiseAsRawBytes TxId where
serialiseToRawBytes (TxId h) = Crypto.hashToBytes h
deserialiseFromRawBytes AsTxId bs = TxId <$> Crypto.hashFromBytes bs
toByronTxId :: TxId -> Byron.TxId
toByronTxId (TxId h) =
Byron.unsafeHashFromBytes (Crypto.hashToBytes h)
toShelleyTxId :: TxId -> Shelley.TxId StandardCrypto
toShelleyTxId (TxId h) =
Shelley.TxId (SafeHash.unsafeMakeSafeHash (Crypto.castHash h))
fromShelleyTxId :: Shelley.TxId StandardCrypto -> TxId
fromShelleyTxId (Shelley.TxId h) =
TxId (Crypto.castHash (SafeHash.extractHash h))
-- | Calculate the transaction identifier for a 'TxBody'.
--
getTxId :: forall era. TxBody era -> TxId
getTxId (ByronTxBody tx) =
TxId
. fromMaybe impossible
. Crypto.hashFromBytesShort
. Byron.abstractHashToShort
. Byron.hashDecoded
$ tx
where
impossible =
error "getTxId: byron and shelley hash sizes do not match"
getTxId (ShelleyTxBody era tx _ _ _) =
case era of
ShelleyBasedEraShelley -> getTxIdShelley tx
ShelleyBasedEraAllegra -> getTxIdShelley tx
ShelleyBasedEraMary -> getTxIdShelley tx
ShelleyBasedEraAlonzo -> getTxIdShelley tx
where
getTxIdShelley :: Ledger.Crypto (ShelleyLedgerEra era) ~ StandardCrypto
=> Ledger.UsesTxBody (ShelleyLedgerEra era)
=> Ledger.TxBody (ShelleyLedgerEra era) -> TxId
getTxIdShelley =
TxId
. Crypto.castHash
. (\(Shelley.TxId txhash) -> SafeHash.extractHash txhash)
. (Shelley.txid @(ShelleyLedgerEra era))
-- ----------------------------------------------------------------------------
-- Transaction inputs
--
data TxIn = TxIn TxId TxIx
deriving (Eq, Ord, Show)
instance ToJSON TxIn where
toJSON txIn = Aeson.String $ renderTxIn txIn
instance ToJSONKey TxIn where
toJSONKey = toJSONKeyText renderTxIn
renderTxIn :: TxIn -> Text
renderTxIn (TxIn txId (TxIx ix)) =
serialiseToRawBytesHexText txId <> "#" <> Text.pack (show ix)
newtype TxIx = TxIx Word
deriving stock (Eq, Ord, Show)
deriving newtype (Enum)
deriving newtype (ToJSON, FromJSON)
fromByronTxIn :: Byron.TxIn -> TxIn
fromByronTxIn (Byron.TxInUtxo txId index) =
let shortBs = Byron.abstractHashToShort txId
mApiHash = Crypto.hashFromBytesShort shortBs
in case mApiHash of
Just apiHash -> TxIn (TxId apiHash) (TxIx . fromIntegral $ toInteger index)
Nothing -> error $ "Error converting Byron era TxId: " <> show txId
toByronTxIn :: TxIn -> Byron.TxIn
toByronTxIn (TxIn txid (TxIx txix)) =
Byron.TxInUtxo (toByronTxId txid) (fromIntegral txix)
toShelleyTxIn :: TxIn -> Shelley.TxIn StandardCrypto
toShelleyTxIn (TxIn txid (TxIx txix)) =
Shelley.TxIn (toShelleyTxId txid) (fromIntegral txix)
fromShelleyTxIn :: Shelley.TxIn StandardCrypto -> TxIn
fromShelleyTxIn (Shelley.TxIn txid txix) =
TxIn (fromShelleyTxId txid) (TxIx (fromIntegral txix))
-- ----------------------------------------------------------------------------
-- Transaction outputs
--
data TxOut era = TxOut (AddressInEra era)
(TxOutValue era)
(TxOutDatumHash era)
deriving Generic
deriving instance Eq (TxOut era)
deriving instance Show (TxOut era)
instance IsCardanoEra era => ToJSON (TxOut era) where
toJSON (TxOut addr val TxOutDatumHashNone) =
object [ "address" .= serialiseAddressForTxOut addr
, "value" .= toJSON val
]
toJSON (TxOut addr val (TxOutDatumHash _ d)) =
object [ "address" .= serialiseAddressForTxOut addr
, "value" .= toJSON val
, "data" .= toJSON d
]
serialiseAddressForTxOut :: AddressInEra era -> Text
serialiseAddressForTxOut (AddressInEra addrType addr) =
case addrType of
ByronAddressInAnyEra -> serialiseToRawBytesHexText addr
ShelleyAddressInEra _ -> serialiseToBech32 addr
fromByronTxOut :: Byron.TxOut -> TxOut ByronEra
fromByronTxOut (Byron.TxOut addr value) =
TxOut
(AddressInEra ByronAddressInAnyEra (ByronAddress addr))
(TxOutAdaOnly AdaOnlyInByronEra (fromByronLovelace value))
TxOutDatumHashNone
toByronTxOut :: TxOut ByronEra -> Maybe Byron.TxOut
toByronTxOut (TxOut (AddressInEra ByronAddressInAnyEra (ByronAddress addr))
(TxOutAdaOnly AdaOnlyInByronEra value) _) =
Byron.TxOut addr <$> toByronLovelace value
toByronTxOut (TxOut (AddressInEra ByronAddressInAnyEra (ByronAddress _))
(TxOutValue era _) _) = case era of {}
toByronTxOut (TxOut (AddressInEra (ShelleyAddressInEra era) ShelleyAddress{})
_ _) = case era of {}
toShelleyTxOut :: forall era ledgerera.
(ShelleyLedgerEra era ~ ledgerera,
IsShelleyBasedEra era, Ledger.ShelleyBased ledgerera)
=> TxOut era -> Ledger.TxOut ledgerera
toShelleyTxOut (TxOut _ (TxOutAdaOnly AdaOnlyInByronEra _) _) =
case shelleyBasedEra :: ShelleyBasedEra era of {}
toShelleyTxOut (TxOut addr (TxOutAdaOnly AdaOnlyInShelleyEra value) _) =
Shelley.TxOut (toShelleyAddr addr) (toShelleyLovelace value)
toShelleyTxOut (TxOut addr (TxOutAdaOnly AdaOnlyInAllegraEra value) _) =
Shelley.TxOut (toShelleyAddr addr) (toShelleyLovelace value)
toShelleyTxOut (TxOut addr (TxOutValue MultiAssetInMaryEra value) _) =
Shelley.TxOut (toShelleyAddr addr) (toMaryValue value)
toShelleyTxOut (TxOut addr (TxOutValue MultiAssetInAlonzoEra value) txoutdata) =
Alonzo.TxOut (toShelleyAddr addr) (toMaryValue value)
(toAlonzoTxOutDataHash txoutdata)
fromShelleyTxOut :: Shelley.TxOut StandardShelley -> TxOut ShelleyEra
fromShelleyTxOut = fromTxOut ShelleyBasedEraShelley
fromTxOut
:: ShelleyLedgerEra era ~ ledgerera
=> ShelleyBasedEra era
-> Core.TxOut ledgerera
-> TxOut era
fromTxOut shelleyBasedEra' ledgerTxOut =
case shelleyBasedEra' of
ShelleyBasedEraShelley ->
TxOut (fromShelleyAddr addr)
(TxOutAdaOnly AdaOnlyInShelleyEra
(fromShelleyLovelace value))
TxOutDatumHashNone
where
Shelley.TxOut addr value = ledgerTxOut
ShelleyBasedEraAllegra ->
TxOut (fromShelleyAddr addr)
(TxOutAdaOnly AdaOnlyInAllegraEra
(fromShelleyLovelace value))
TxOutDatumHashNone
where
Shelley.TxOut addr value = ledgerTxOut
ShelleyBasedEraMary ->
TxOut (fromShelleyAddr addr)
(TxOutValue MultiAssetInMaryEra
(fromMaryValue value))
TxOutDatumHashNone
where
Shelley.TxOut addr value = ledgerTxOut
ShelleyBasedEraAlonzo ->
TxOut (fromShelleyAddr addr)
(TxOutValue MultiAssetInAlonzoEra
(fromMaryValue value))
(fromAlonzoTxOutDataHash ScriptDataInAlonzoEra datahash)
where
Alonzo.TxOut addr value datahash = ledgerTxOut
toAlonzoTxOutDataHash :: TxOutDatumHash era
-> StrictMaybe (Alonzo.DataHash StandardCrypto)
toAlonzoTxOutDataHash TxOutDatumHashNone = SNothing
toAlonzoTxOutDataHash (TxOutDatumHash _ (ScriptDataHash dh)) = SJust dh
fromAlonzoTxOutDataHash :: ScriptDataSupportedInEra era
-> StrictMaybe (Alonzo.DataHash StandardCrypto)
-> TxOutDatumHash era
fromAlonzoTxOutDataHash _ SNothing = TxOutDatumHashNone
fromAlonzoTxOutDataHash era (SJust dh) = TxOutDatumHash era (ScriptDataHash dh)
-- ----------------------------------------------------------------------------
-- Era-dependent transaction body features
--
-- | A representation of whether the era supports transactions with inputs used
-- only for collateral for script fees.
--
-- The Alonzo and subsequent eras support collateral inputs.
--
data CollateralSupportedInEra era where
CollateralInAlonzoEra :: CollateralSupportedInEra AlonzoEra
deriving instance Eq (CollateralSupportedInEra era)
deriving instance Show (CollateralSupportedInEra era)
collateralSupportedInEra :: CardanoEra era
-> Maybe (CollateralSupportedInEra era)
collateralSupportedInEra ByronEra = Nothing
collateralSupportedInEra ShelleyEra = Nothing
collateralSupportedInEra AllegraEra = Nothing
collateralSupportedInEra MaryEra = Nothing
collateralSupportedInEra AlonzoEra = Just CollateralInAlonzoEra
-- | A representation of whether the era supports multi-asset transactions.
--
-- The Mary and subsequent eras support multi-asset transactions.
--
-- The negation of this is 'OnlyAdaSupportedInEra'.
--
data MultiAssetSupportedInEra era where
-- | Multi-asset transactions are supported in the 'Mary' era.
MultiAssetInMaryEra :: MultiAssetSupportedInEra MaryEra
-- | Multi-asset transactions are supported in the 'Alonzo' era.
MultiAssetInAlonzoEra :: MultiAssetSupportedInEra AlonzoEra
deriving instance Eq (MultiAssetSupportedInEra era)
deriving instance Show (MultiAssetSupportedInEra era)
instance ToJSON (MultiAssetSupportedInEra era) where
toJSON = Aeson.String . Text.pack . show
-- | A representation of whether the era supports only ada transactions.
--
-- Prior to the Mary era only ada transactions are supported. Multi-assets are
-- supported from the Mary era onwards.
--
-- This is the negation of 'MultiAssetSupportedInEra'. It exists since we need
-- evidence to be positive.
--
data OnlyAdaSupportedInEra era where
AdaOnlyInByronEra :: OnlyAdaSupportedInEra ByronEra
AdaOnlyInShelleyEra :: OnlyAdaSupportedInEra ShelleyEra
AdaOnlyInAllegraEra :: OnlyAdaSupportedInEra AllegraEra
deriving instance Eq (OnlyAdaSupportedInEra era)
deriving instance Show (OnlyAdaSupportedInEra era)
multiAssetSupportedInEra :: CardanoEra era
-> Either (OnlyAdaSupportedInEra era)
(MultiAssetSupportedInEra era)
multiAssetSupportedInEra ByronEra = Left AdaOnlyInByronEra
multiAssetSupportedInEra ShelleyEra = Left AdaOnlyInShelleyEra
multiAssetSupportedInEra AllegraEra = Left AdaOnlyInAllegraEra
multiAssetSupportedInEra MaryEra = Right MultiAssetInMaryEra
multiAssetSupportedInEra AlonzoEra = Right MultiAssetInAlonzoEra
-- | A representation of whether the era requires explicitly specified fees in
-- transactions.
--
-- The Byron era tx fees are implicit (as the difference bettween the sum of
-- outputs and sum of inputs), but all later eras the fees are specified in the
-- transaction explicitly.
--
data TxFeesExplicitInEra era where
TxFeesExplicitInShelleyEra :: TxFeesExplicitInEra ShelleyEra
TxFeesExplicitInAllegraEra :: TxFeesExplicitInEra AllegraEra
TxFeesExplicitInMaryEra :: TxFeesExplicitInEra MaryEra
TxFeesExplicitInAlonzoEra :: TxFeesExplicitInEra AlonzoEra
deriving instance Eq (TxFeesExplicitInEra era)
deriving instance Show (TxFeesExplicitInEra era)
-- | A representation of whether the era requires implicitly specified fees in
-- transactions.
--
-- This is the negation of 'TxFeesExplicitInEra'.
--
data TxFeesImplicitInEra era where
TxFeesImplicitInByronEra :: TxFeesImplicitInEra ByronEra
deriving instance Eq (TxFeesImplicitInEra era)
deriving instance Show (TxFeesImplicitInEra era)
txFeesExplicitInEra :: CardanoEra era
-> Either (TxFeesImplicitInEra era)
(TxFeesExplicitInEra era)
txFeesExplicitInEra ByronEra = Left TxFeesImplicitInByronEra
txFeesExplicitInEra ShelleyEra = Right TxFeesExplicitInShelleyEra
txFeesExplicitInEra AllegraEra = Right TxFeesExplicitInAllegraEra
txFeesExplicitInEra MaryEra = Right TxFeesExplicitInMaryEra
txFeesExplicitInEra AlonzoEra = Right TxFeesExplicitInAlonzoEra
-- | A representation of whether the era supports transactions with an upper
-- bound on the range of slots in which they are valid.
--
-- The Shelley and subsequent eras support an upper bound on the validity
-- range. In the Shelley era specifically it is actually required. It is
-- optional in later eras.
--
data ValidityUpperBoundSupportedInEra era where
ValidityUpperBoundInShelleyEra :: ValidityUpperBoundSupportedInEra ShelleyEra
ValidityUpperBoundInAllegraEra :: ValidityUpperBoundSupportedInEra AllegraEra
ValidityUpperBoundInMaryEra :: ValidityUpperBoundSupportedInEra MaryEra
ValidityUpperBoundInAlonzoEra :: ValidityUpperBoundSupportedInEra AlonzoEra
deriving instance Eq (ValidityUpperBoundSupportedInEra era)
deriving instance Show (ValidityUpperBoundSupportedInEra era)
validityUpperBoundSupportedInEra :: CardanoEra era
-> Maybe (ValidityUpperBoundSupportedInEra era)
validityUpperBoundSupportedInEra ByronEra = Nothing
validityUpperBoundSupportedInEra ShelleyEra = Just ValidityUpperBoundInShelleyEra
validityUpperBoundSupportedInEra AllegraEra = Just ValidityUpperBoundInAllegraEra
validityUpperBoundSupportedInEra MaryEra = Just ValidityUpperBoundInMaryEra
validityUpperBoundSupportedInEra AlonzoEra = Just ValidityUpperBoundInAlonzoEra
-- | A representation of whether the era supports transactions having /no/
-- upper bound on the range of slots in which they are valid.
--
-- Note that the 'ShelleyEra' /does not support/ omitting a validity upper
-- bound. It was introduced as a /required/ field in Shelley and then made
-- optional in Allegra and subsequent eras.
--
-- The Byron era supports this by virtue of the fact that it does not support
-- validity ranges at all.
--
data ValidityNoUpperBoundSupportedInEra era where
ValidityNoUpperBoundInByronEra :: ValidityNoUpperBoundSupportedInEra ByronEra
ValidityNoUpperBoundInAllegraEra :: ValidityNoUpperBoundSupportedInEra AllegraEra
ValidityNoUpperBoundInMaryEra :: ValidityNoUpperBoundSupportedInEra MaryEra
ValidityNoUpperBoundInAlonzoEra :: ValidityNoUpperBoundSupportedInEra AlonzoEra
deriving instance Eq (ValidityNoUpperBoundSupportedInEra era)
deriving instance Show (ValidityNoUpperBoundSupportedInEra era)
validityNoUpperBoundSupportedInEra :: CardanoEra era
-> Maybe (ValidityNoUpperBoundSupportedInEra era)
validityNoUpperBoundSupportedInEra ByronEra = Just ValidityNoUpperBoundInByronEra
validityNoUpperBoundSupportedInEra ShelleyEra = Nothing
validityNoUpperBoundSupportedInEra AllegraEra = Just ValidityNoUpperBoundInAllegraEra
validityNoUpperBoundSupportedInEra MaryEra = Just ValidityNoUpperBoundInMaryEra
validityNoUpperBoundSupportedInEra AlonzoEra = Just ValidityNoUpperBoundInAlonzoEra
-- | A representation of whether the era supports transactions with a lower
-- bound on the range of slots in which they are valid.
--
-- The Allegra and subsequent eras support an optional lower bound on the
-- validity range. No equivalent of 'ValidityNoUpperBoundSupportedInEra' is
-- needed since all eras support having no lower bound.
--
data ValidityLowerBoundSupportedInEra era where
ValidityLowerBoundInAllegraEra :: ValidityLowerBoundSupportedInEra AllegraEra
ValidityLowerBoundInMaryEra :: ValidityLowerBoundSupportedInEra MaryEra
ValidityLowerBoundInAlonzoEra :: ValidityLowerBoundSupportedInEra AlonzoEra
deriving instance Eq (ValidityLowerBoundSupportedInEra era)
deriving instance Show (ValidityLowerBoundSupportedInEra era)
validityLowerBoundSupportedInEra :: CardanoEra era
-> Maybe (ValidityLowerBoundSupportedInEra era)
validityLowerBoundSupportedInEra ByronEra = Nothing
validityLowerBoundSupportedInEra ShelleyEra = Nothing
validityLowerBoundSupportedInEra AllegraEra = Just ValidityLowerBoundInAllegraEra
validityLowerBoundSupportedInEra MaryEra = Just ValidityLowerBoundInMaryEra
validityLowerBoundSupportedInEra AlonzoEra = Just ValidityLowerBoundInAlonzoEra
-- | A representation of whether the era supports transaction metadata.
--
-- Transaction metadata is supported from the Shelley era onwards.
--
data TxMetadataSupportedInEra era where
TxMetadataInShelleyEra :: TxMetadataSupportedInEra ShelleyEra
TxMetadataInAllegraEra :: TxMetadataSupportedInEra AllegraEra
TxMetadataInMaryEra :: TxMetadataSupportedInEra MaryEra
TxMetadataInAlonzoEra :: TxMetadataSupportedInEra AlonzoEra
deriving instance Eq (TxMetadataSupportedInEra era)
deriving instance Show (TxMetadataSupportedInEra era)
txMetadataSupportedInEra :: CardanoEra era
-> Maybe (TxMetadataSupportedInEra era)
txMetadataSupportedInEra ByronEra = Nothing
txMetadataSupportedInEra ShelleyEra = Just TxMetadataInShelleyEra
txMetadataSupportedInEra AllegraEra = Just TxMetadataInAllegraEra
txMetadataSupportedInEra MaryEra = Just TxMetadataInMaryEra
txMetadataSupportedInEra AlonzoEra = Just TxMetadataInAlonzoEra
-- | A representation of whether the era supports auxiliary scripts in
-- transactions.
--
-- Auxiliary scripts are supported from the Allegra era onwards.
--
data AuxScriptsSupportedInEra era where
AuxScriptsInAllegraEra :: AuxScriptsSupportedInEra AllegraEra
AuxScriptsInMaryEra :: AuxScriptsSupportedInEra MaryEra
AuxScriptsInAlonzoEra :: AuxScriptsSupportedInEra AlonzoEra
deriving instance Eq (AuxScriptsSupportedInEra era)
deriving instance Show (AuxScriptsSupportedInEra era)
auxScriptsSupportedInEra :: CardanoEra era
-> Maybe (AuxScriptsSupportedInEra era)
auxScriptsSupportedInEra ByronEra = Nothing
auxScriptsSupportedInEra ShelleyEra = Nothing
auxScriptsSupportedInEra AllegraEra = Just AuxScriptsInAllegraEra
auxScriptsSupportedInEra MaryEra = Just AuxScriptsInMaryEra
auxScriptsSupportedInEra AlonzoEra = Just AuxScriptsInAlonzoEra
-- | A representation of whether the era supports transactions that specify
-- in the body that they need extra key witnesses, and where this fact is
-- visible to scripts.
--
-- Extra key witnesses visible to scripts are supported from the Alonzo era
-- onwards.
--
data TxExtraKeyWitnessesSupportedInEra era where
ExtraKeyWitnessesInAlonzoEra :: TxExtraKeyWitnessesSupportedInEra AlonzoEra
deriving instance Eq (TxExtraKeyWitnessesSupportedInEra era)
deriving instance Show (TxExtraKeyWitnessesSupportedInEra era)
extraKeyWitnessesSupportedInEra :: CardanoEra era
-> Maybe (TxExtraKeyWitnessesSupportedInEra era)
extraKeyWitnessesSupportedInEra ByronEra = Nothing
extraKeyWitnessesSupportedInEra ShelleyEra = Nothing
extraKeyWitnessesSupportedInEra AllegraEra = Nothing
extraKeyWitnessesSupportedInEra MaryEra = Nothing
extraKeyWitnessesSupportedInEra AlonzoEra = Just ExtraKeyWitnessesInAlonzoEra
-- | A representation of whether the era supports multi-asset transactions.
--
-- The Mary and subsequent eras support multi-asset transactions.
--
-- The negation of this is 'OnlyAdaSupportedInEra'.
--
data ScriptDataSupportedInEra era where
-- | Script data is supported in transactions in the 'Alonzo' era.
ScriptDataInAlonzoEra :: ScriptDataSupportedInEra AlonzoEra
deriving instance Eq (ScriptDataSupportedInEra era)
deriving instance Show (ScriptDataSupportedInEra era)
scriptDataSupportedInEra :: CardanoEra era
-> Maybe (ScriptDataSupportedInEra era)
scriptDataSupportedInEra ByronEra = Nothing
scriptDataSupportedInEra ShelleyEra = Nothing
scriptDataSupportedInEra AllegraEra = Nothing
scriptDataSupportedInEra MaryEra = Nothing
scriptDataSupportedInEra AlonzoEra = Just ScriptDataInAlonzoEra
-- | A representation of whether the era supports withdrawals from reward
-- accounts.
--
-- The Shelley and subsequent eras support stake addresses, their associated
-- reward accounts and support for withdrawals from them.
--
data WithdrawalsSupportedInEra era where
WithdrawalsInShelleyEra :: WithdrawalsSupportedInEra ShelleyEra
WithdrawalsInAllegraEra :: WithdrawalsSupportedInEra AllegraEra
WithdrawalsInMaryEra :: WithdrawalsSupportedInEra MaryEra
WithdrawalsInAlonzoEra :: WithdrawalsSupportedInEra AlonzoEra
deriving instance Eq (WithdrawalsSupportedInEra era)
deriving instance Show (WithdrawalsSupportedInEra era)
withdrawalsSupportedInEra :: CardanoEra era
-> Maybe (WithdrawalsSupportedInEra era)
withdrawalsSupportedInEra ByronEra = Nothing
withdrawalsSupportedInEra ShelleyEra = Just WithdrawalsInShelleyEra
withdrawalsSupportedInEra AllegraEra = Just WithdrawalsInAllegraEra
withdrawalsSupportedInEra MaryEra = Just WithdrawalsInMaryEra
withdrawalsSupportedInEra AlonzoEra = Just WithdrawalsInAlonzoEra
-- | A representation of whether the era supports 'Certificate's embedded in
-- transactions.
--
-- The Shelley and subsequent eras support such certificates.
--
data CertificatesSupportedInEra era where
CertificatesInShelleyEra :: CertificatesSupportedInEra ShelleyEra
CertificatesInAllegraEra :: CertificatesSupportedInEra AllegraEra
CertificatesInMaryEra :: CertificatesSupportedInEra MaryEra
CertificatesInAlonzoEra :: CertificatesSupportedInEra AlonzoEra
deriving instance Eq (CertificatesSupportedInEra era)
deriving instance Show (CertificatesSupportedInEra era)
certificatesSupportedInEra :: CardanoEra era
-> Maybe (CertificatesSupportedInEra era)
certificatesSupportedInEra ByronEra = Nothing
certificatesSupportedInEra ShelleyEra = Just CertificatesInShelleyEra
certificatesSupportedInEra AllegraEra = Just CertificatesInAllegraEra
certificatesSupportedInEra MaryEra = Just CertificatesInMaryEra
certificatesSupportedInEra AlonzoEra = Just CertificatesInAlonzoEra
-- | A representation of whether the era supports 'UpdateProposal's embedded in
-- transactions.
--
-- The Shelley and subsequent eras support such update proposals. They Byron
-- era has a notion of an update proposal, but it is a standalone chain object
-- and not embedded in a transaction.
--
data UpdateProposalSupportedInEra era where
UpdateProposalInShelleyEra :: UpdateProposalSupportedInEra ShelleyEra
UpdateProposalInAllegraEra :: UpdateProposalSupportedInEra AllegraEra
UpdateProposalInMaryEra :: UpdateProposalSupportedInEra MaryEra
UpdateProposalInAlonzoEra :: UpdateProposalSupportedInEra AlonzoEra
deriving instance Eq (UpdateProposalSupportedInEra era)
deriving instance Show (UpdateProposalSupportedInEra era)
updateProposalSupportedInEra :: CardanoEra era
-> Maybe (UpdateProposalSupportedInEra era)
updateProposalSupportedInEra ByronEra = Nothing
updateProposalSupportedInEra ShelleyEra = Just UpdateProposalInShelleyEra
updateProposalSupportedInEra AllegraEra = Just UpdateProposalInAllegraEra
updateProposalSupportedInEra MaryEra = Just UpdateProposalInMaryEra
updateProposalSupportedInEra AlonzoEra = Just UpdateProposalInAlonzoEra
-- ----------------------------------------------------------------------------
-- Building vs viewing transactions
--
data BuildTx
data ViewTx
data BuildTxWith build a where
ViewTx :: BuildTxWith ViewTx a
BuildTxWith :: a -> BuildTxWith BuildTx a
deriving instance Eq a => Eq (BuildTxWith build a)
deriving instance Show a => Show (BuildTxWith build a)
-- ----------------------------------------------------------------------------
-- Transaction input values (era-dependent)
--
type TxIns build era = [(TxIn, BuildTxWith build (Witness WitCtxTxIn era))]
data TxInsCollateral era where
TxInsCollateralNone :: TxInsCollateral era
TxInsCollateral :: CollateralSupportedInEra era
-> [TxIn] -- Only key witnesses, no scripts.
-> TxInsCollateral era
deriving instance Eq (TxInsCollateral era)
deriving instance Show (TxInsCollateral era)
-- ----------------------------------------------------------------------------
-- Transaction output values (era-dependent)
--
data TxOutValue era where
TxOutAdaOnly :: OnlyAdaSupportedInEra era -> Lovelace -> TxOutValue era
TxOutValue :: MultiAssetSupportedInEra era -> Value -> TxOutValue era
deriving instance Eq (TxOutValue era)
deriving instance Show (TxOutValue era)
deriving instance Generic (TxOutValue era)
instance ToJSON (TxOutValue era) where
toJSON (TxOutAdaOnly _ ll) = toJSON ll
toJSON (TxOutValue _ val) = toJSON val
-- ----------------------------------------------------------------------------
-- Transaction output datum (era-dependent)
--
data TxOutDatumHash era where
TxOutDatumHashNone :: TxOutDatumHash era
TxOutDatumHash :: ScriptDataSupportedInEra era
-> Hash ScriptData
-> TxOutDatumHash era
deriving instance Eq (TxOutDatumHash era)
deriving instance Show (TxOutDatumHash era)
deriving instance Generic (TxOutDatumHash era)
-- ----------------------------------------------------------------------------
-- Transaction fees
--
data TxFee era where
TxFeeImplicit :: TxFeesImplicitInEra era -> TxFee era
TxFeeExplicit :: TxFeesExplicitInEra era -> Lovelace -> TxFee era
deriving instance Eq (TxFee era)
deriving instance Show (TxFee era)
-- ----------------------------------------------------------------------------
-- Transaction validity range
--
-- | This was formerly known as the TTL.
--
data TxValidityUpperBound era where
TxValidityNoUpperBound :: ValidityNoUpperBoundSupportedInEra era
-> TxValidityUpperBound era
TxValidityUpperBound :: ValidityUpperBoundSupportedInEra era
-> SlotNo
-> TxValidityUpperBound era
deriving instance Eq (TxValidityUpperBound era)
deriving instance Show (TxValidityUpperBound era)
data TxValidityLowerBound era where
TxValidityNoLowerBound :: TxValidityLowerBound era
TxValidityLowerBound :: ValidityLowerBoundSupportedInEra era
-> SlotNo
-> TxValidityLowerBound era
deriving instance Eq (TxValidityLowerBound era)
deriving instance Show (TxValidityLowerBound era)
-- ----------------------------------------------------------------------------
-- Transaction metadata (era-dependent)
--
data TxMetadataInEra era where
TxMetadataNone :: TxMetadataInEra era
TxMetadataInEra :: TxMetadataSupportedInEra era
-> TxMetadata
-> TxMetadataInEra era
deriving instance Eq (TxMetadataInEra era)
deriving instance Show (TxMetadataInEra era)
-- ----------------------------------------------------------------------------
-- Auxiliary scripts (era-dependent)
--
data TxAuxScripts era where
TxAuxScriptsNone :: TxAuxScripts era
TxAuxScripts :: AuxScriptsSupportedInEra era
-> [ScriptInEra era]
-> TxAuxScripts era
deriving instance Eq (TxAuxScripts era)
deriving instance Show (TxAuxScripts era)
-- ----------------------------------------------------------------------------
-- Optionally required signatures (era-dependent)
--
data TxExtraKeyWitnesses era where
TxExtraKeyWitnessesNone :: TxExtraKeyWitnesses era
TxExtraKeyWitnesses :: TxExtraKeyWitnessesSupportedInEra era
-> [Hash PaymentKey]
-> TxExtraKeyWitnesses era
deriving instance Eq (TxExtraKeyWitnesses era)
deriving instance Show (TxExtraKeyWitnesses era)
-- ----------------------------------------------------------------------------
-- Auxiliary script data (era-dependent)
--
data TxAuxScriptData era where
TxAuxScriptDataNone :: TxAuxScriptData era
TxAuxScriptData :: ScriptDataSupportedInEra era
-> [ScriptData]
-> TxAuxScriptData era
deriving instance Eq (TxAuxScriptData era)
deriving instance Show (TxAuxScriptData era)
-- ----------------------------------------------------------------------------
-- Withdrawals within transactions (era-dependent)
--
data TxWithdrawals build era where
TxWithdrawalsNone :: TxWithdrawals build era
TxWithdrawals :: WithdrawalsSupportedInEra era
-> [(StakeAddress, Lovelace,
BuildTxWith build (Witness WitCtxStake era))]
-> TxWithdrawals build era
deriving instance Eq (TxWithdrawals build era)
deriving instance Show (TxWithdrawals build era)
-- ----------------------------------------------------------------------------
-- Certificates within transactions (era-dependent)
--
data TxCertificates build era where
TxCertificatesNone :: TxCertificates build era
TxCertificates :: CertificatesSupportedInEra era
-> [Certificate]
-> BuildTxWith build
(Map StakeCredential (Witness WitCtxStake era))
-> TxCertificates build era