-
Notifications
You must be signed in to change notification settings - Fork 211
/
Alonzo.hs
172 lines (159 loc) · 5.07 KB
/
Alonzo.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
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeFamilies #-}
-- |
-- Copyright: © 2020-2022 IOHK
-- License: Apache-2.0
--
module Cardano.Wallet.Read.Primitive.Tx.Alonzo
( fromAlonzoTx
)
where
import Prelude
import Cardano.Api
( AlonzoEra )
import Cardano.Ledger.Era
( Era (..) )
import Cardano.Wallet.Primitive.Types.TokenPolicy
( TokenPolicyId )
import Cardano.Wallet.Read.Eras
( alonzo, inject )
import Cardano.Wallet.Read.Primitive.Tx.Features.Certificates
( anyEraCerts )
import Cardano.Wallet.Read.Primitive.Tx.Features.Fee
( fromShelleyCoin )
import Cardano.Wallet.Read.Primitive.Tx.Features.Inputs
( fromShelleyTxIn )
import Cardano.Wallet.Read.Primitive.Tx.Features.Mint
( alonzoMint )
import Cardano.Wallet.Read.Primitive.Tx.Features.Outputs
( fromAlonzoTxOut )
import Cardano.Wallet.Read.Primitive.Tx.Features.Validity
( afterShelleyValidityInterval )
import Cardano.Wallet.Read.Primitive.Tx.Features.Withdrawals
( fromShelleyWdrl )
import Cardano.Wallet.Read.Primitive.Tx.Shelley
( fromShelleyMD )
import Cardano.Wallet.Read.Tx
( Tx (..) )
import Cardano.Wallet.Read.Tx.CBOR
( renderTxToCBOR )
import Cardano.Wallet.Read.Tx.Hash
( alonzoTxHash )
import Cardano.Wallet.Shelley.Compatibility.Ledger
( toWalletScript, toWalletTokenPolicyId )
import Cardano.Wallet.Transaction
( AnyScript (..)
, PlutusScriptInfo (..)
, PlutusVersion (..)
, TokenMapWithScripts (..)
, ValidityIntervalExplicit (..)
, WitnessCount (..)
, WitnessCountCtx
, toKeyRole
)
import Data.Foldable
( toList )
import Data.Map.Strict
( Map )
import Ouroboros.Consensus.Cardano.Block
( StandardAlonzo )
import qualified Cardano.Api.Shelley as Cardano
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.TxWitness as Alonzo
import qualified Cardano.Ledger.BaseTypes as SL
import qualified Cardano.Ledger.Core as SL.Core
import qualified Cardano.Ledger.Mary.Value as SL
import qualified Cardano.Ledger.Shelley.API as SL
import qualified Cardano.Wallet.Primitive.Types as W
import qualified Cardano.Wallet.Primitive.Types.Hash as W
import qualified Cardano.Wallet.Primitive.Types.Tx as W
import qualified Data.Map.Strict as Map
import qualified Data.Set as Set
fromAlonzoTx
:: Alonzo.ValidatedTx (Cardano.ShelleyLedgerEra AlonzoEra)
-> WitnessCountCtx
-> ( W.Tx
, [W.Certificate]
, TokenMapWithScripts
, TokenMapWithScripts
, Maybe ValidityIntervalExplicit
, WitnessCount
)
fromAlonzoTx tx@(Alonzo.ValidatedTx bod wits (Alonzo.IsValid isValid) aux) witCtx =
( W.Tx
{ txId =
W.Hash $ alonzoTxHash tx
, txCBOR =
Just $ renderTxToCBOR $ inject alonzo $ Tx tx
, fee =
Just $ fromShelleyCoin fee
, resolvedInputs =
map ((,Nothing) . fromShelleyTxIn) (toList ins)
, resolvedCollateralInputs =
map ((,Nothing) . fromShelleyTxIn) (toList collateral)
, outputs =
map fromAlonzoTxOut (toList outs)
, collateralOutput =
-- Collateral outputs are not supported in Alonzo.
Nothing
, withdrawals =
fromShelleyWdrl wdrls
, metadata =
fromShelleyMD . toSLMetadata <$> SL.strictMaybeToMaybe aux
, scriptValidity =
validity
}
, anyEraCerts certs
, assetsToMint
, assetsToBurn
, Just $ afterShelleyValidityInterval ttl
, countWits
)
where
Alonzo.TxBody
ins
collateral
outs
certs
wdrls
fee
ttl
_upd
_reqSignerHashes
mint
_wwpHash
_adHash
_network
= bod
(assetsToMint, assetsToBurn) = alonzoMint mint wits
scriptMap = fromAlonzoScriptMap $ Alonzo.txscripts' wits
countWits = WitnessCount
(fromIntegral $ Set.size $ Alonzo.txwitsVKey' wits)
(Map.elems scriptMap)
(fromIntegral $ Set.size $ Alonzo.txwitsBoot' wits)
fromAlonzoScriptMap
:: Map
(SL.ScriptHash (Crypto StandardAlonzo))
(SL.Core.Script StandardAlonzo)
-> Map TokenPolicyId AnyScript
fromAlonzoScriptMap =
Map.map toAnyScript .
Map.mapKeys (toWalletTokenPolicyId . SL.PolicyID)
where
toAnyScript (Alonzo.TimelockScript script) =
NativeScript $ toWalletScript (toKeyRole witCtx) script
toAnyScript (Alonzo.PlutusScript ver _) =
PlutusScript (PlutusScriptInfo (toPlutusVer ver))
toPlutusVer Alonzo.PlutusV1 = PlutusVersionV1
toPlutusVer Alonzo.PlutusV2 = PlutusVersionV2
toSLMetadata (Alonzo.AuxiliaryData blob _scripts) = SL.Metadata blob
validity =
if isValid
then Just W.TxScriptValid
else Just W.TxScriptInvalid