/
Tx.hs
601 lines (536 loc) · 18.4 KB
/
Tx.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
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE UndecidableSuperClasses #-}
{-# OPTIONS_GHC -Wno-orphans #-}
-- | This module exports implementations of many of the functions outlined in the Alonzo specification.
-- The link to source of the specification
-- https://github.com/input-output-hk/cardano-ledger/tree/master/eras/alonzo/formal-spec
-- The most recent version of the document can be found here:
-- https://github.com/input-output-hk/cardano-ledger/releases/latest/download/alonzo-ledger.pdf
-- The functions can be found in Figures in that document, and sections of this code refer to those figures.
module Cardano.Ledger.Alonzo.Tx (
-- Figure 1
CostModel,
getLanguageView,
-- Figure 2
Data,
DataHash,
IsValid (..),
hashData,
nonNativeLanguages,
hashScriptIntegrity,
getCoin,
EraIndependentScriptIntegrity,
ScriptIntegrity (ScriptIntegrity),
ScriptIntegrityHash,
-- Figure 3
AlonzoTx (AlonzoTx, body, wits, isValid, auxiliaryData),
AlonzoEraTx (..),
mkBasicAlonzoTx,
bodyAlonzoTxL,
witsAlonzoTxL,
auxDataAlonzoTxL,
sizeAlonzoTxF,
isValidAlonzoTxL,
txdats',
txscripts',
txrdmrs,
AlonzoTxBody (..),
-- Figure 4
totExUnits,
isTwoPhaseScriptAddress,
alonzoMinFeeTx,
minfee,
-- Figure 5
Indexable (..), -- indexOf
ScriptPurpose (..),
isTwoPhaseScriptAddressFromMap,
Shelley.txouts,
indexedRdmrs,
rdptr,
-- Figure 6
rdptrInv,
getMapFromValue,
-- Segwit
alonzoSegwitTx,
-- Other
toCBORForSizeComputation,
toCBORForMempoolSubmission,
)
where
import Cardano.Crypto.Hash.Class (HashAlgorithm)
import Cardano.Ledger.Address (Addr (..), RewardAcnt (..))
import Cardano.Ledger.Allegra.Core ()
import Cardano.Ledger.Allegra.Tx (validateTimelock)
import Cardano.Ledger.Alonzo.Core (AlonzoEraPParams, ppPricesL)
import Cardano.Ledger.Alonzo.Era (AlonzoEra)
import Cardano.Ledger.Alonzo.PParams (
LangDepView (..),
encodeLangViews,
getLanguageView,
)
import Cardano.Ledger.Alonzo.Scripts (
CostModel,
ExUnits (..),
Tag (..),
txscriptfee,
)
import Cardano.Ledger.Alonzo.Scripts.Data (Data, hashData)
import Cardano.Ledger.Alonzo.TxBody (
AlonzoEraTxBody (..),
AlonzoTxBody (..),
MaryEraTxBody (..),
ScriptIntegrityHash,
)
import Cardano.Ledger.Alonzo.TxWits (
AlonzoEraTxWits (..),
AlonzoTxWits (..),
RdmrPtr (..),
Redeemers (..),
TxDats (..),
nullDats,
nullRedeemers,
txrdmrs,
unRedeemers,
)
import Cardano.Ledger.Binary (
Annotator (..),
DecCBOR (..),
EncCBOR (encCBOR),
Encoding,
ToCBOR (..),
decodeNullMaybe,
encodeListLen,
encodeNullMaybe,
serialize,
serialize',
)
import Cardano.Ledger.Binary.Coders
import Cardano.Ledger.Coin (Coin (..))
import Cardano.Ledger.Core
import Cardano.Ledger.Crypto (Crypto (HASH), StandardCrypto)
import Cardano.Ledger.Language (nonNativeLanguages)
import Cardano.Ledger.Mary.Value (AssetName, MaryValue (..), MultiAsset (..), PolicyID (..))
import Cardano.Ledger.SafeHash (HashAnnotated, SafeToHash (..), hashAnnotated)
import Cardano.Ledger.Shelley.TxBody (Withdrawals (..), unWithdrawals)
import Cardano.Ledger.TxIn (TxIn (..))
import qualified Cardano.Ledger.UTxO as Shelley
import Cardano.Ledger.Val (Val ((<+>), (<×>)))
import Control.DeepSeq (NFData (..), rwhnf)
import qualified Data.ByteString.Lazy as LBS
import qualified Data.Map.Strict as Map
import Data.Maybe.Strict (
StrictMaybe (..),
maybeToStrictMaybe,
strictMaybeToMaybe,
)
import Data.Sequence.Strict (StrictSeq)
import qualified Data.Sequence.Strict as StrictSeq
import Data.Set (Set)
import qualified Data.Set as Set
import Data.Typeable (Typeable)
import Data.Word (Word64)
import GHC.Generics (Generic)
import Lens.Micro hiding (set)
import NoThunks.Class (NoThunks)
-- ===================================================
-- | Tag indicating whether non-native scripts in this transaction are expected
-- to validate. This is added by the block creator when constructing the block.
newtype IsValid = IsValid Bool
deriving (Eq, Show, Generic)
deriving newtype (NoThunks, NFData, ToCBOR, EncCBOR, DecCBOR)
data AlonzoTx era = AlonzoTx
{ body :: !(TxBody era)
, wits :: !(TxWits era)
, isValid :: !IsValid
, auxiliaryData :: !(StrictMaybe (TxAuxData era))
}
deriving (Generic)
instance Crypto c => EraTx (AlonzoEra c) where
{-# SPECIALIZE instance EraTx (AlonzoEra StandardCrypto) #-}
type Tx (AlonzoEra c) = AlonzoTx (AlonzoEra c)
mkBasicTx = mkBasicAlonzoTx
bodyTxL = bodyAlonzoTxL
{-# INLINE bodyTxL #-}
witsTxL = witsAlonzoTxL
{-# INLINE witsTxL #-}
auxDataTxL = auxDataAlonzoTxL
{-# INLINE auxDataTxL #-}
sizeTxF = sizeAlonzoTxF
{-# INLINE sizeTxF #-}
validateScript (Phase1Script script) = validateTimelock @(AlonzoEra c) script
{-# INLINE validateScript #-}
getMinFeeTx = alonzoMinFeeTx
{-# INLINE getMinFeeTx #-}
class (EraTx era, AlonzoEraTxBody era, AlonzoEraTxWits era) => AlonzoEraTx era where
isValidTxL :: Lens' (Tx era) IsValid
instance Crypto c => AlonzoEraTx (AlonzoEra c) where
{-# SPECIALIZE instance AlonzoEraTx (AlonzoEra StandardCrypto) #-}
isValidTxL = isValidAlonzoTxL
{-# INLINE isValidTxL #-}
mkBasicAlonzoTx :: Monoid (TxWits era) => TxBody era -> AlonzoTx era
mkBasicAlonzoTx txBody = AlonzoTx txBody mempty (IsValid True) SNothing
-- | `TxBody` setter and getter for `AlonzoTx`.
bodyAlonzoTxL :: Lens' (AlonzoTx era) (TxBody era)
bodyAlonzoTxL = lens body (\tx txBody -> tx {body = txBody})
{-# INLINEABLE bodyAlonzoTxL #-}
-- | `TxWits` setter and getter for `AlonzoTx`.
witsAlonzoTxL :: Lens' (AlonzoTx era) (TxWits era)
witsAlonzoTxL = lens wits (\tx txWits -> tx {wits = txWits})
{-# INLINEABLE witsAlonzoTxL #-}
-- | `TxAuxData` setter and getter for `AlonzoTx`.
auxDataAlonzoTxL :: Lens' (AlonzoTx era) (StrictMaybe (TxAuxData era))
auxDataAlonzoTxL = lens auxiliaryData (\tx txTxAuxData -> tx {auxiliaryData = txTxAuxData})
{-# INLINEABLE auxDataAlonzoTxL #-}
-- | txsize computes the length of the serialised bytes
sizeAlonzoTxF :: forall era. EraTx era => SimpleGetter (AlonzoTx era) Integer
sizeAlonzoTxF =
to $
fromIntegral
. LBS.length
. serialize (eraProtVerLow @era)
. toCBORForSizeComputation
{-# INLINEABLE sizeAlonzoTxF #-}
isValidAlonzoTxL :: Lens' (AlonzoTx era) IsValid
isValidAlonzoTxL = lens isValid (\tx valid -> tx {isValid = valid})
{-# INLINEABLE isValidAlonzoTxL #-}
deriving instance
(Era era, Eq (TxBody era), Eq (TxWits era), Eq (TxAuxData era)) => Eq (AlonzoTx era)
deriving instance
(Era era, Show (TxBody era), Show (TxAuxData era), Show (Script era), Show (TxWits era)) =>
Show (AlonzoTx era)
instance
( Era era
, NoThunks (TxWits era)
, NoThunks (TxAuxData era)
, NoThunks (TxBody era)
) =>
NoThunks (AlonzoTx era)
instance
( Era era
, NFData (TxWits era)
, NFData (TxAuxData era)
, NFData (TxBody era)
) =>
NFData (AlonzoTx era)
-- =========================================================
-- Figure 2: Definitions for Transactions
getCoin :: EraTxOut era => TxOut era -> Coin
getCoin txOut = txOut ^. coinTxOutL
{-# DEPRECATED getCoin "In favor of `coinTxOutL`" #-}
-- | A ScriptIntegrityHash is the hash of three things. The first two come
-- from the witnesses and the last comes from the Protocol Parameters.
data ScriptIntegrity era
= ScriptIntegrity
!(Redeemers era) -- From the witnesses
!(TxDats era)
!(Set LangDepView) -- From the Protocol parameters
deriving (Eq, Generic, Typeable)
deriving instance HashAlgorithm (HASH (EraCrypto era)) => Show (ScriptIntegrity era)
deriving instance Typeable era => NoThunks (ScriptIntegrity era)
-- ScriptIntegrity is not transmitted over the network. The bytes are independently
-- reconstructed by all nodes. There are no original bytes to preserve.
-- Instead, we must use a reproducable serialization
instance Era era => SafeToHash (ScriptIntegrity era) where
originalBytes (ScriptIntegrity m d l) =
let dBytes = if nullDats d then mempty else originalBytes d
lBytes = serialize' (eraProtVerLow @era) (encodeLangViews l)
in originalBytes m <> dBytes <> lBytes
instance
(Era era, c ~ EraCrypto era) =>
HashAnnotated (ScriptIntegrity era) EraIndependentScriptIntegrity c
hashScriptIntegrity ::
forall era.
Era era =>
Set LangDepView ->
Redeemers era ->
TxDats era ->
StrictMaybe (ScriptIntegrityHash (EraCrypto era))
hashScriptIntegrity langViews rdmrs dats =
if nullRedeemers rdmrs && Set.null langViews && nullDats dats
then SNothing
else SJust (hashAnnotated (ScriptIntegrity rdmrs dats langViews))
-- ===============================================================
-- From the specification, Figure 4 "Functions related to fees"
-- ===============================================================
isTwoPhaseScriptAddress ::
forall era.
(EraTx era, TxWits era ~ AlonzoTxWits era) =>
AlonzoTx era ->
Addr (EraCrypto era) ->
Bool
isTwoPhaseScriptAddress tx =
isTwoPhaseScriptAddressFromMap @era (wits tx ^. scriptTxWitsL)
-- | This ensures that the size of transactions from Mary is unchanged.
-- The individual components all store their bytes; the only work we do in this
-- function is concatenating
toCBORForSizeComputation ::
( EncCBOR (TxBody era)
, EncCBOR (TxWits era)
, EncCBOR (TxAuxData era)
) =>
AlonzoTx era ->
Encoding
toCBORForSizeComputation AlonzoTx {body, wits, auxiliaryData} =
encodeListLen 3
<> encCBOR body
<> encCBOR wits
<> encodeNullMaybe encCBOR (strictMaybeToMaybe auxiliaryData)
alonzoMinFeeTx ::
( EraTx era
, AlonzoEraTxWits era
, AlonzoEraPParams era
) =>
PParams era ->
Tx era ->
Coin
alonzoMinFeeTx pp tx =
(tx ^. sizeTxF <×> pp ^. ppMinFeeAL)
<+> pp ^. ppMinFeeBL
<+> txscriptfee (pp ^. ppPricesL) allExunits
where
allExunits = totExUnits tx
minfee ::
( EraTx era
, AlonzoEraTxWits era
, AlonzoEraPParams era
) =>
PParams era ->
Tx era ->
Coin
minfee = alonzoMinFeeTx
{-# DEPRECATED minfee "In favor of `getMinFeeTx`" #-}
totExUnits ::
(EraTx era, AlonzoEraTxWits era) =>
Tx era ->
ExUnits
totExUnits tx =
foldMap snd . Map.elems . unRedeemers $ tx ^. witsTxL . rdmrsTxWitsL
-- ===============================================================
-- Operations on scripts from specification
-- Figure 6:Indexing script and data objects
-- ===============================================================
data ScriptPurpose era
= Minting !(PolicyID (EraCrypto era))
| Spending !(TxIn (EraCrypto era))
| Rewarding !(RewardAcnt (EraCrypto era))
| Certifying !(TxCert era)
deriving (Generic)
deriving instance (Era era, Eq (TxCert era)) => Eq (ScriptPurpose era)
deriving instance (Era era, Show (TxCert era)) => Show (ScriptPurpose era)
deriving instance (Era era, NoThunks (TxCert era)) => NoThunks (ScriptPurpose era)
instance (Era era, NFData (TxCert era)) => NFData (ScriptPurpose era) where
rnf = \case
Certifying c -> rnf c
sp -> rwhnf sp
instance (Era era, EncCBOR (TxCert era)) => EncCBOR (ScriptPurpose era) where
encCBOR (Minting x) = encode (Sum (Minting @era) 0 !> To x)
encCBOR (Spending x) = encode (Sum (Spending @era) 1 !> To x)
encCBOR (Rewarding x) = encode (Sum (Rewarding @era) 2 !> To x)
encCBOR (Certifying x) = encode (Sum Certifying 3 !> To x)
instance (Era era, DecCBOR (TxCert era)) => DecCBOR (ScriptPurpose era) where
decCBOR = decode (Summands "ScriptPurpose" dec)
where
dec 0 = SumD Minting <! From
dec 1 = SumD Spending <! From
dec 2 = SumD Rewarding <! From
dec 3 = SumD Certifying <! From
dec n = Invalid n
{-# INLINE decCBOR #-}
-- =======================================
class Indexable elem container where
indexOf :: elem -> container -> StrictMaybe Word64
fromIndex :: Word64 -> container -> StrictMaybe elem
instance Ord k => Indexable k (Set k) where
indexOf n set = case Set.lookupIndex n set of
Just x -> SJust (fromIntegral x)
Nothing -> SNothing
fromIndex i set =
if fromIntegral i < Set.size set
then SJust $ Set.elemAt (fromIntegral i) set
else SNothing
instance Eq k => Indexable k (StrictSeq k) where
indexOf n seqx = case StrictSeq.findIndexL (== n) seqx of
Just m -> SJust (fromIntegral m)
Nothing -> SNothing
fromIndex i seqx = maybeToStrictMaybe $ StrictSeq.lookup (fromIntegral i) seqx
instance Ord k => Indexable k (Map.Map k v) where
indexOf n mp = case Map.lookupIndex n mp of
Just x -> SJust (fromIntegral x)
Nothing -> SNothing
fromIndex i mp =
if fromIntegral i < Map.size mp
then SJust . fst $ Map.elemAt (fromIntegral i) mp
else SNothing
rdptr ::
forall era.
MaryEraTxBody era =>
TxBody era ->
ScriptPurpose era ->
StrictMaybe RdmrPtr
rdptr txBody = \case
Minting (PolicyID hash) ->
RdmrPtr Mint <$> indexOf hash (txBody ^. mintedTxBodyF :: Set (ScriptHash (EraCrypto era)))
Spending txin ->
RdmrPtr Spend <$> indexOf txin (txBody ^. inputsTxBodyL)
Rewarding racnt ->
RdmrPtr Rewrd <$> indexOf racnt (unWithdrawals (txBody ^. withdrawalsTxBodyL))
Certifying d ->
RdmrPtr Cert <$> indexOf d (txBody ^. certsTxBodyL)
rdptrInv ::
forall era.
MaryEraTxBody era =>
TxBody era ->
RdmrPtr ->
StrictMaybe (ScriptPurpose era)
rdptrInv txBody = \case
RdmrPtr Mint idx ->
Minting . PolicyID <$> fromIndex idx (txBody ^. mintedTxBodyF)
RdmrPtr Spend idx ->
Spending <$> fromIndex idx (txBody ^. inputsTxBodyL)
RdmrPtr Rewrd idx ->
Rewarding <$> fromIndex idx (unWithdrawals (txBody ^. withdrawalsTxBodyL))
RdmrPtr Cert idx ->
Certifying <$> fromIndex idx (txBody ^. certsTxBodyL)
{-# DEPRECATED getMapFromValue "No longer used" #-}
getMapFromValue :: MaryValue c -> Map.Map (PolicyID c) (Map.Map AssetName Integer)
getMapFromValue (MaryValue _ (MultiAsset m)) = m
-- | Find the Data and ExUnits assigned to a script.
indexedRdmrs ::
forall era.
(MaryEraTxBody era, AlonzoEraTxWits era, EraTx era) =>
Tx era ->
ScriptPurpose era ->
Maybe (Data era, ExUnits)
indexedRdmrs tx sp = case rdptr @era (tx ^. bodyTxL) sp of
SNothing -> Nothing
SJust rPtr -> Map.lookup rPtr rdmrs
where
rdmrs = unRedeemers (tx ^. witsTxL . rdmrsTxWitsL)
--------------------------------------------------------------------------------
-- Serialisation
--------------------------------------------------------------------------------
-- | Construct an annotated Alonzo style transaction.
alonzoSegwitTx ::
AlonzoEraTx era =>
Annotator (TxBody era) ->
Annotator (TxWits era) ->
IsValid ->
Maybe (Annotator (TxAuxData era)) ->
Annotator (Tx era)
alonzoSegwitTx txBodyAnn txWitsAnn isValid auxDataAnn = Annotator $ \bytes ->
let txBody = runAnnotator txBodyAnn bytes
txWits = runAnnotator txWitsAnn bytes
txAuxData = maybeToStrictMaybe (flip runAnnotator bytes <$> auxDataAnn)
in mkBasicTx txBody
& witsTxL .~ txWits
& auxDataTxL .~ txAuxData
& isValidTxL .~ isValid
--------------------------------------------------------------------------------
-- Mempool Serialisation
--
-- We do not store the Tx bytes for the following reasons:
-- - A Tx serialised in this way never forms part of any hashed structure, hence
-- we do not worry about the serialisation changing and thus seeing a new
-- hash.
-- - The three principal components of this Tx already store their own bytes;
-- here we simply concatenate them. The final component, `IsValid`, is
-- just a flag and very cheap to serialise.
--------------------------------------------------------------------------------
-- | Encode to CBOR for the purposes of transmission from node to node, or from
-- wallet to node.
--
-- Note that this serialisation is neither the serialisation used on-chain
-- (where Txs are deconstructed using segwit), nor the serialisation used for
-- computing the transaction size (which omits the `IsValid` field for
-- compatibility with Mary - see 'toCBORForSizeComputation').
toCBORForMempoolSubmission ::
( EncCBOR (TxBody era)
, EncCBOR (TxWits era)
, EncCBOR (TxAuxData era)
) =>
AlonzoTx era ->
Encoding
toCBORForMempoolSubmission
AlonzoTx {body, wits, auxiliaryData, isValid} =
encode $
Rec AlonzoTx
!> To body
!> To wits
!> To isValid
!> E (encodeNullMaybe encCBOR . strictMaybeToMaybe) auxiliaryData
instance
( Era era
, EncCBOR (TxBody era)
, EncCBOR (TxAuxData era)
, EncCBOR (TxWits era)
) =>
EncCBOR (AlonzoTx era)
where
encCBOR = toCBORForMempoolSubmission
instance
( Era era
, EncCBOR (TxBody era)
, EncCBOR (TxAuxData era)
, EncCBOR (TxWits era)
) =>
ToCBOR (AlonzoTx era)
where
toCBOR = toEraCBOR @era
instance
( Typeable era
, DecCBOR (Annotator (TxBody era))
, DecCBOR (Annotator (TxWits era))
, DecCBOR (Annotator (TxAuxData era))
) =>
DecCBOR (Annotator (AlonzoTx era))
where
decCBOR =
decode $
Ann (RecD AlonzoTx)
<*! From
<*! From
<*! Ann From
<*! D
( sequence . maybeToStrictMaybe
<$> decodeNullMaybe decCBOR
)
{-# INLINE decCBOR #-}
-- =======================================================================
-- Some generic functions that compute over Tx. We try to be abstract over
-- things that might differ from Era to Era like
-- 1) TxOut will have additional fields
-- 2) Scripts might appear in places other than the witness set. So
-- we need such a 'witness' we pass it as a parameter and each call site
-- can use a different method to compute it in the current Era.
-- | Compute if an Addr has the hash of a TwoPhaseScript, we can tell
-- what kind of Script from the Hash, by looking it up in the Map
isTwoPhaseScriptAddressFromMap ::
forall era.
EraScript era =>
Map.Map (ScriptHash (EraCrypto era)) (Script era) ->
Addr (EraCrypto era) ->
Bool
isTwoPhaseScriptAddressFromMap hashScriptMap addr =
case Shelley.getScriptHash @(EraCrypto era) addr of
Nothing -> False
Just hash -> any ok hashScriptMap
where
ok script = hashScript @era script == hash && not (isNativeScript @era script)