-
Notifications
You must be signed in to change notification settings - Fork 211
/
Mary.hs
140 lines (133 loc) · 4.45 KB
/
Mary.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
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TupleSections #-}
-- |
-- Copyright: © 2020-2022 IOHK
-- License: Apache-2.0
--
module Cardano.Wallet.Read.Primitive.Tx.Mary
( fromMaryTx
)
where
import Prelude
import Cardano.Api
( MaryEra )
import Cardano.Ledger.Era
( Era (..) )
import Cardano.Wallet.Primitive.Types.TokenPolicy
( TokenPolicyId )
import Cardano.Wallet.Read.Eras
( inject, mary )
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
( maryMint )
import Cardano.Wallet.Read.Primitive.Tx.Features.Outputs
( fromMaryTxOut )
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 (Tx) )
import Cardano.Wallet.Read.Tx.CBOR
( renderTxToCBOR )
import Cardano.Wallet.Read.Tx.Hash
( shelleyTxHash )
import Cardano.Wallet.Shelley.Compatibility.Ledger
( toWalletScript, toWalletTokenPolicyId )
import Cardano.Wallet.Transaction
( AnyScript (..)
, TokenMapWithScripts (..)
, ValidityIntervalExplicit (..)
, WitnessCount (..)
, WitnessCountCtx
, toKeyRole
)
import Data.Foldable
( toList )
import Data.Map.Strict
( Map )
import qualified Cardano.Api.Shelley as Cardano
import qualified Cardano.Ledger.BaseTypes as SL
import qualified Cardano.Ledger.Core as SL.Core
import qualified Cardano.Ledger.Crypto as Crypto
import qualified Cardano.Ledger.Mary.Value as SL
import qualified Cardano.Ledger.Shelley.API as SL
import qualified Cardano.Ledger.Shelley.Tx as Shelley
import qualified Cardano.Ledger.ShelleyMA as MA
import qualified Cardano.Ledger.ShelleyMA.AuxiliaryData as MA
import qualified Cardano.Ledger.ShelleyMA.TxBody as MA
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
fromMaryTx
:: SL.Tx (Cardano.ShelleyLedgerEra MaryEra)
-> WitnessCountCtx
-> ( W.Tx
, [W.Certificate]
, TokenMapWithScripts
, TokenMapWithScripts
, Maybe ValidityIntervalExplicit
, WitnessCount
)
fromMaryTx tx witCtx =
( W.Tx
{ txId =
W.Hash $ shelleyTxHash tx
, txCBOR =
Just $ renderTxToCBOR $ inject mary $ Tx tx
, fee =
Just $ fromShelleyCoin fee
, resolvedInputs =
map ((,Nothing) . fromShelleyTxIn) (toList ins)
, resolvedCollateralInputs =
[]
, outputs =
map fromMaryTxOut (toList outs)
, collateralOutput =
-- Collateral outputs are not supported in Mary.
Nothing
, withdrawals =
fromShelleyWdrl wdrls
, metadata =
fromShelleyMD . toSLMetadata <$> SL.strictMaybeToMaybe mad
, scriptValidity =
Nothing
}
, anyEraCerts certs
, assetsToMint
, assetsToBurn
, Just $ afterShelleyValidityInterval ttl
, countWits
)
where
SL.Tx bod wits mad = tx
MA.TxBody ins outs certs wdrls fee ttl _upd _adh mint = bod
(assetsToMint, assetsToBurn) = maryMint mint wits
scriptMap = fromMaryScriptMap $ Shelley.scriptWits wits
countWits = WitnessCount
(fromIntegral $ Set.size $ Shelley.addrWits wits)
(Map.elems scriptMap)
(fromIntegral $ Set.size $ Shelley.bootWits wits)
-- fixme: [ADP-525] It is fine for now since we do not look at script
-- pre-images. But this is precisely what we want as part of the
-- multisig/script balance reporting.
toSLMetadata (MA.AuxiliaryData blob _scripts) = SL.Metadata blob
fromMaryScriptMap
:: Map
(SL.ScriptHash (Crypto (MA.ShelleyMAEra 'MA.Mary Crypto.StandardCrypto)))
(SL.Core.Script (MA.ShelleyMAEra 'MA.Mary Crypto.StandardCrypto))
-> Map TokenPolicyId AnyScript
fromMaryScriptMap =
Map.map (NativeScript . toWalletScript (toKeyRole witCtx)) .
Map.mapKeys (toWalletTokenPolicyId . SL.PolicyID)