@@ -101,7 +101,7 @@ module Cardano.Api.Tx.Body
101
101
-- * Transaction inputs
102
102
, TxIn (.. )
103
103
, TxIns
104
- , txInsToIndexed
104
+ , indexTxIns
105
105
, TxIx (.. )
106
106
, genesisUTxOPseudoTxIn
107
107
, getReferenceInputsSizeForTxIds
@@ -133,19 +133,19 @@ module Cardano.Api.Tx.Body
133
133
, TxAuxScripts (.. )
134
134
, TxExtraKeyWitnesses (.. )
135
135
, TxWithdrawals (.. )
136
- , txWithdrawalsToIndexed
136
+ , indexTxWithdrawals
137
137
, TxCertificates (.. )
138
- , txCertificatesToIndexed
138
+ , indexTxCertificates
139
139
, TxUpdateProposal (.. )
140
140
, TxMintValue (.. )
141
141
, txMintValueToValue
142
- , txMintValueToIndexed
142
+ , indexTxMintValue
143
143
, TxVotingProcedures (.. )
144
144
, mkTxVotingProcedures
145
- , txVotingProceduresToIndexed
145
+ , indexTxVotingProcedures
146
146
, TxProposalProcedures (.. )
147
147
, mkTxProposalProcedures
148
- , txProposalProceduresToIndexed
148
+ , indexTxProposalProcedures
149
149
, convProposalProcedures
150
150
151
151
-- ** Building vs viewing transactions
@@ -317,7 +317,8 @@ import Data.String
317
317
import Data.Text (Text )
318
318
import qualified Data.Text as Text
319
319
import qualified Data.Text.Encoding as Text
320
- import Data.Type.Equality (TestEquality (.. ), (:~:) (Refl ))
320
+ import Data.Type.Equality
321
+ import Data.Typeable
321
322
import Data.Word (Word16 , Word32 , Word64 )
322
323
import GHC.Exts (IsList (.. ))
323
324
import GHC.Stack
@@ -938,11 +939,12 @@ deriving instance Show a => Show (BuildTxWith build a)
938
939
type TxIns build era = [(TxIn , BuildTxWith build (Witness WitCtxTxIn era ))]
939
940
940
941
-- | Index transaction inputs ordered by TxIn
942
+ -- Please note that the result can contain also 'KeyWitness'es.
941
943
-- See section 4.1 of https://github.com/intersectmbo/cardano-ledger/releases/latest/download/alonzo-ledger.pdf
942
- txInsToIndexed
944
+ indexTxIns
943
945
:: TxIns BuildTx era
944
946
-> [(ScriptWitnessIndex , TxIn , Witness WitCtxTxIn era )]
945
- txInsToIndexed txins =
947
+ indexTxIns txins =
946
948
[ (ScriptWitnessIndexTxIn ix, txIn, witness)
947
949
| (ix, (txIn, BuildTxWith witness)) <- zip [0 .. ] $ orderTxIns txins
948
950
]
@@ -1259,11 +1261,11 @@ deriving instance Show (TxWithdrawals build era)
1259
1261
1260
1262
-- | Index the withdrawals with witnesses in the order of stake addresses.
1261
1263
-- See section 4.1 of https://github.com/intersectmbo/cardano-ledger/releases/latest/download/alonzo-ledger.pdf
1262
- txWithdrawalsToIndexed
1264
+ indexTxWithdrawals
1263
1265
:: TxWithdrawals BuildTx era
1264
1266
-> [(ScriptWitnessIndex , StakeAddress , L. Coin , Witness WitCtxStake era )]
1265
- txWithdrawalsToIndexed TxWithdrawalsNone = []
1266
- txWithdrawalsToIndexed (TxWithdrawals _ withdrawals) =
1267
+ indexTxWithdrawals TxWithdrawalsNone = []
1268
+ indexTxWithdrawals (TxWithdrawals _ withdrawals) =
1267
1269
[ (ScriptWitnessIndexWithdrawal ix, addr, coin, witness)
1268
1270
| (ix, (addr, coin, BuildTxWith witness)) <- zip [0 .. ] (orderStakeAddrs withdrawals)
1269
1271
]
@@ -1292,19 +1294,21 @@ deriving instance Eq (TxCertificates build era)
1292
1294
1293
1295
deriving instance Show (TxCertificates build era )
1294
1296
1295
- -- | Index certificates with witnesses by the order they appear in the list (in the transaction). If there
1296
- -- are multiple witnesses for the credential, the last one is returned.
1297
+ -- | Index certificates with witnesses by the order they appear in the list (in the transaction). If there are multiple witnesses for the same stake credential, they will be present multiple times with the same index.
1298
+ -- are multiple witnesses for the credential, there will be multiple entries for
1297
1299
-- See section 4.1 of https://github.com/intersectmbo/cardano-ledger/releases/latest/download/alonzo-ledger.pdf
1298
- txCertificatesToIndexed
1300
+ indexTxCertificates
1299
1301
:: TxCertificates BuildTx era
1300
1302
-> [(ScriptWitnessIndex , Certificate era , StakeCredential , Witness WitCtxStake era )]
1301
- txCertificatesToIndexed TxCertificatesNone = []
1302
- txCertificatesToIndexed (TxCertificates _ certs (BuildTxWith witnesses)) =
1303
+ indexTxCertificates TxCertificatesNone = []
1304
+ indexTxCertificates (TxCertificates _ certs (BuildTxWith witnesses)) =
1303
1305
[ (ScriptWitnessIndexCertificate ix, cert, stakeCred, wit)
1304
1306
| (ix, cert) <- zip [0 .. ] certs
1305
1307
, stakeCred <- maybeToList (selectStakeCredentialWitness cert)
1306
- , wit <- maybeToList $ List. lookup stakeCred witnesses
1308
+ , wit <- findAll stakeCred witnesses
1307
1309
]
1310
+ where
1311
+ findAll needle = map snd . filter ((==) needle . fst )
1308
1312
1309
1313
-- ----------------------------------------------------------------------------
1310
1314
-- Transaction update proposal (era-dependent)
@@ -1351,7 +1355,7 @@ txMintValueToValue (TxMintValue _ policiesWithAssets) =
1351
1355
1352
1356
-- | Index the assets with witnesses in the order of policy ids.
1353
1357
-- See section 4.1 of https://github.com/intersectmbo/cardano-ledger/releases/latest/download/alonzo-ledger.pdf
1354
- txMintValueToIndexed
1358
+ indexTxMintValue
1355
1359
:: TxMintValue build era
1356
1360
-> [ ( ScriptWitnessIndex
1357
1361
, PolicyId
@@ -1360,8 +1364,8 @@ txMintValueToIndexed
1360
1364
, BuildTxWith build (ScriptWitness WitCtxMint era )
1361
1365
)
1362
1366
]
1363
- txMintValueToIndexed TxMintNone = []
1364
- txMintValueToIndexed (TxMintValue _ policiesWithAssets) =
1367
+ indexTxMintValue TxMintNone = []
1368
+ indexTxMintValue (TxMintValue _ policiesWithAssets) =
1365
1369
[ (ScriptWitnessIndexMint ix, policyId', assetName', quantity, witness)
1366
1370
| (ix, (policyId', assets)) <- zip [0 .. ] $ toList policiesWithAssets
1367
1371
, (assetName', quantity, witness) <- assets
@@ -1419,15 +1423,15 @@ mkTxVotingProcedures votingProcedures = do
1419
1423
listToMaybe $ Map. keys m
1420
1424
1421
1425
-- | Index voting procedures by the order of the votes ('Ord').
1422
- txVotingProceduresToIndexed
1426
+ indexTxVotingProcedures
1423
1427
:: TxVotingProcedures BuildTx era
1424
1428
-> [ ( ScriptWitnessIndex
1425
1429
, L. Voter (Ledger. EraCrypto (ShelleyLedgerEra era ))
1426
1430
, ScriptWitness WitCtxStake era
1427
1431
)
1428
1432
]
1429
- txVotingProceduresToIndexed TxVotingProceduresNone = []
1430
- txVotingProceduresToIndexed (TxVotingProcedures vProcedures (BuildTxWith sWitMap)) =
1433
+ indexTxVotingProcedures TxVotingProceduresNone = []
1434
+ indexTxVotingProcedures (TxVotingProcedures vProcedures (BuildTxWith sWitMap)) =
1431
1435
[ (ScriptWitnessIndexVoting $ fromIntegral index, vote, scriptWitness)
1432
1436
| let allVoteMap = L. unVotingProcedures vProcedures
1433
1437
, (vote, scriptWitness) <- toList sWitMap
@@ -1476,11 +1480,11 @@ mkTxProposalProcedures proposalsWithWitnessesList = do
1476
1480
(DList. snoc ps p, DList. snoc pws (p, w)) -- add a proposal both to the list and to the witnessed list
1477
1481
1478
1482
-- | Index proposal procedures by their order ('Ord').
1479
- txProposalProceduresToIndexed
1483
+ indexTxProposalProcedures
1480
1484
:: TxProposalProcedures BuildTx era
1481
1485
-> [(ScriptWitnessIndex , L. ProposalProcedure (ShelleyLedgerEra era ), ScriptWitness WitCtxStake era )]
1482
- txProposalProceduresToIndexed TxProposalProceduresNone = []
1483
- txProposalProceduresToIndexed txpp@ (TxProposalProcedures _ (BuildTxWith witnesses)) = do
1486
+ indexTxProposalProcedures TxProposalProceduresNone = []
1487
+ indexTxProposalProcedures txpp@ (TxProposalProcedures _ (BuildTxWith witnesses)) = do
1484
1488
let allProposalsList = toList $ convProposalProcedures txpp
1485
1489
[ (ScriptWitnessIndexProposing $ fromIntegral ix, proposal, scriptWitness)
1486
1490
| (proposal, scriptWitness) <- toList witnesses
@@ -3389,10 +3393,26 @@ toShelleyTxOutAny _ = \case
3389
3393
-- | A 'ScriptWitness' in any 'WitCtx'. This lets us handle heterogeneous
3390
3394
-- collections of script witnesses from multiple contexts.
3391
3395
data AnyScriptWitness era where
3392
- AnyScriptWitness :: ScriptWitness witctx era -> AnyScriptWitness era
3396
+ AnyScriptWitness
3397
+ :: Typeable witctx
3398
+ => ScriptWitness witctx era
3399
+ -> AnyScriptWitness era
3393
3400
3394
3401
deriving instance Show (AnyScriptWitness era )
3395
3402
3403
+ instance Eq (AnyScriptWitness era ) where
3404
+ AnyScriptWitness sw1 == AnyScriptWitness sw2 =
3405
+ case eqsw sw1 sw2 of
3406
+ Just Refl -> sw1 == sw2
3407
+ Nothing -> False
3408
+ where
3409
+ eqsw
3410
+ :: (Typeable w1 , Typeable w2 )
3411
+ => ScriptWitness w1 era
3412
+ -> ScriptWitness w2 era
3413
+ -> Maybe (w1 :~: w2 )
3414
+ eqsw _ _ = eqT
3415
+
3396
3416
-- | Identify the location of a 'ScriptWitness' within the context of a
3397
3417
-- 'TxBody'. These are indexes of the objects within the transaction that
3398
3418
-- need or can use script witnesses: inputs, minted assets, withdrawals and
@@ -3561,54 +3581,60 @@ collectTxBodyScriptWitnesses
3561
3581
:: [(TxIn , BuildTxWith BuildTx (Witness WitCtxTxIn era ))]
3562
3582
-> [(ScriptWitnessIndex , AnyScriptWitness era )]
3563
3583
scriptWitnessesTxIns txIns' =
3564
- [ (ix, AnyScriptWitness witness)
3565
- | (ix, _, ScriptWitness _ witness) <- txInsToIndexed txIns'
3566
- ]
3584
+ List. nub
3585
+ [ (ix, AnyScriptWitness witness)
3586
+ | (ix, _, ScriptWitness _ witness) <- indexTxIns txIns'
3587
+ ]
3567
3588
3568
3589
scriptWitnessesWithdrawals
3569
3590
:: TxWithdrawals BuildTx era
3570
3591
-> [(ScriptWitnessIndex , AnyScriptWitness era )]
3571
3592
scriptWitnessesWithdrawals TxWithdrawalsNone = []
3572
3593
scriptWitnessesWithdrawals txw =
3573
- [ (ix, AnyScriptWitness witness)
3574
- | (ix, _, _, ScriptWitness _ witness) <- txWithdrawalsToIndexed txw
3575
- ]
3594
+ List. nub
3595
+ [ (ix, AnyScriptWitness witness)
3596
+ | (ix, _, _, ScriptWitness _ witness) <- indexTxWithdrawals txw
3597
+ ]
3576
3598
3577
3599
scriptWitnessesCertificates
3578
3600
:: TxCertificates BuildTx era
3579
3601
-> [(ScriptWitnessIndex , AnyScriptWitness era )]
3580
3602
scriptWitnessesCertificates TxCertificatesNone = []
3581
3603
scriptWitnessesCertificates txc =
3582
- [ (ix, AnyScriptWitness witness)
3583
- | (ix, _, _, ScriptWitness _ witness) <- txCertificatesToIndexed txc
3584
- ]
3604
+ List. nub
3605
+ [ (ix, AnyScriptWitness witness)
3606
+ | (ix, _, _, ScriptWitness _ witness) <- indexTxCertificates txc
3607
+ ]
3585
3608
3586
3609
scriptWitnessesMinting
3587
3610
:: TxMintValue BuildTx era
3588
3611
-> [(ScriptWitnessIndex , AnyScriptWitness era )]
3589
3612
scriptWitnessesMinting TxMintNone = []
3590
3613
scriptWitnessesMinting txMintValue' =
3591
- [ (ix, AnyScriptWitness witness)
3592
- | (ix, _, _, _, BuildTxWith witness) <- txMintValueToIndexed txMintValue'
3593
- ]
3614
+ List. nub
3615
+ [ (ix, AnyScriptWitness witness)
3616
+ | (ix, _, _, _, BuildTxWith witness) <- indexTxMintValue txMintValue'
3617
+ ]
3594
3618
3595
3619
scriptWitnessesVoting
3596
3620
:: TxVotingProcedures BuildTx era
3597
3621
-> [(ScriptWitnessIndex , AnyScriptWitness era )]
3598
3622
scriptWitnessesVoting TxVotingProceduresNone = []
3599
3623
scriptWitnessesVoting txv =
3600
- [ (ix, AnyScriptWitness witness)
3601
- | (ix, _, witness) <- txVotingProceduresToIndexed txv
3602
- ]
3624
+ List. nub
3625
+ [ (ix, AnyScriptWitness witness)
3626
+ | (ix, _, witness) <- indexTxVotingProcedures txv
3627
+ ]
3603
3628
3604
3629
scriptWitnessesProposing
3605
3630
:: TxProposalProcedures BuildTx era
3606
3631
-> [(ScriptWitnessIndex , AnyScriptWitness era )]
3607
3632
scriptWitnessesProposing TxProposalProceduresNone = []
3608
3633
scriptWitnessesProposing txp =
3609
- [ (ix, AnyScriptWitness witness)
3610
- | (ix, _, witness) <- txProposalProceduresToIndexed txp
3611
- ]
3634
+ List. nub
3635
+ [ (ix, AnyScriptWitness witness)
3636
+ | (ix, _, witness) <- indexTxProposalProcedures txp
3637
+ ]
3612
3638
3613
3639
-- TODO: Investigate if we need
3614
3640
toShelleyWithdrawal :: [(StakeAddress , L. Coin , a )] -> L. Withdrawals StandardCrypto
0 commit comments