1+ {-# LANGUAGE TypeApplications #-}
12{-# LANGUAGE LambdaCase #-}
23{-# LANGUAGE DataKinds #-}
34{-# LANGUAGE StandaloneDeriving #-}
@@ -26,7 +27,7 @@ import Cardano.SCLS.CBOR.Canonical.Decoder
2627import qualified Codec.CBOR.Encoding as E
2728import qualified Codec.CBOR.Decoding as D
2829import Cardano.Ledger.Conway (ConwayEra )
29- import Cardano.Ledger.TxIn (TxIn (.. ))
30+ import Cardano.Ledger.TxIn (TxIn (.. ), TxId ( .. ) )
3031import Cardano.Ledger.Core (TxOut (.. ))
3132-- import Cardano.Ledger.TxOut (TxOut(..))
3233import Cardano.Ledger.Compactible
@@ -37,6 +38,8 @@ import Cardano.Ledger.Hashes
3738import Cardano.Ledger.Plutus.Data (Datum (.. ))
3839import Cardano.Ledger.Plutus.Data (BinaryData )
3940import Cardano.Ledger.Mary (MaryEra , MaryValue )
41+ import Cardano.Ledger.Coin (Coin )
42+ import qualified Cardano.Ledger.Coin as Coin
4043import Cardano.SCLS.Internal.Entry
4144import Cardano.SCLS.Internal.Version
4245import Data.Typeable (Typeable )
@@ -45,8 +48,8 @@ import qualified Cardano.Ledger.Babbage.TxOut as Babbage
4548import Cardano.Ledger.Allegra.Scripts (Timelock (.. ))
4649import Data.MemPack
4750import Data.Word (Word8 , Word16 )
48- import Cardano.Ledger.Alonzo.TxOut (DataHash32 , Addr28Extra )
49- import Cardano.Ledger.Alonzo.Scripts (AlonzoPlutusPurpose , AsItem , AlonzoScript )
51+ import Cardano.Ledger.Alonzo.TxOut (DataHash32 , Addr28Extra , decodeAddress28 )
52+ import Cardano.Ledger.Alonzo.Scripts (AlonzoPlutusPurpose , AsItem , AlonzoScript ( .. ) )
5053
5154-- | Helper that allows us to deriving instances via internal CBOR representation
5255newtype LedgerCBOR (v :: Version ) a = LedgerCBOR { unLedgerCBOR :: a }
@@ -70,31 +73,23 @@ instance (MemPack a) => FromCanonicalCBOR V1 (MemPackCBOR a) where
7073-- | Input wrapper for the keys that are used in utxo namespace
7174data UtxoKey
7275 = UtxoKeyIn TxIn
73- -- | UtxoKeyScript ScriptUtxoIn
76+ deriving ( Show )
7477
7578instance Eq UtxoKey where
7679 (UtxoKeyIn txIn1) == (UtxoKeyIn txIn2) = txIn1 == txIn2
77- -- (UtxoKeyScript script1) == (UtxoKeyScript script2) = undefined script1 script2
78- -- _ == _ = False
7980
8081instance Ord UtxoKey where
8182 compare (UtxoKeyIn txIn1) (UtxoKeyIn txIn2) = compare txIn1 txIn2
82- -- compare (UtxoKeyScript script1) (UtxoKeyScript script2) = undefined script1 script2
83- -- compare (UtxoKeyIn _) (UtxoKeyScript _) = LT
84- -- compare (UtxoKeyScript _) (UtxoKeyIn _) = GT
8583
8684instance IsKey UtxoKey where
8785 keySize = 34
88- packKeyM (UtxoKeyIn (TxIn a b)) = do
89- packM a
86+ packKeyM (UtxoKeyIn (TxIn ( TxId a) b)) = do
87+ packByteStringM (originalBytes a)
9088 packM b
91- -- packKeyM (UtxoKeyScript (ScriptUtxoIn purpose hash)) = do
92- -- undefined purpose hash
9389 unpackKeyM = do
94- a <- unpackM
90+ a <- unpackM -- FIXME read bytestirng and create unsafe hash
9591 b <- unpackM
9692 return $ UtxoKeyIn (TxIn a b)
97- -- toKeyBytes v key = toStrictByteString $ toCanonicalCBOR v key
9893
9994newtype Out = Out (TxOut ConwayEra )
10095 deriving newtype (ToCanonicalCBOR V1 , FromCanonicalCBOR V1 )
@@ -108,11 +103,11 @@ data ScriptUtxoIn = ScriptUtxoIn { scriptUtxoInPurpose :: AlonzoPlutusPurpose As
108103-- is not efficient and we can replace it with the implementation based on the compact values
109104data UtxoOut
110105 = UtxoOutShelley (Shelley. ShelleyTxOut MaryEra )
111- | UtxoOutBabbage (Babbage. BabbageTxOut MaryEra )
106+ | UtxoOutBabbage (Babbage. BabbageTxOut ConwayEra )
112107 | UtxoValue MaryValue
113108
114109instance ToCanonicalCBOR V1 UtxoKey where
115- toCanonicalCBOR v (UtxoKeyIn txIn) = E. encodeTag 0 <> toCanonicalCBOR v txIn
110+ toCanonicalCBOR v (UtxoKeyIn txIn) = E. encodeInt 0 <> toCanonicalCBOR v txIn
116111 -- toCanonicalCBOR v (UtxoKeyScript script) = E.encodeTag 1 <> toCanonicalCBOR v script
117112
118113
@@ -137,9 +132,9 @@ instance FromCanonicalCBOR V1 UtxoKey where
137132 _ -> fail " Unknown UtxoKey tag"
138133
139134instance ToCanonicalCBOR V1 UtxoOut where
140- toCanonicalCBOR v (UtxoOutShelley shelleyOut) = E. encodeTag 1 <> toCanonicalCBOR v shelleyOut
141- toCanonicalCBOR v (UtxoOutBabbage babbageOut) = E. encodeTag 2 <> toCanonicalCBOR v babbageOut
142- toCanonicalCBOR v (UtxoValue value) = E. encodeTag 3 <> toCanonicalCBOR v value
135+ toCanonicalCBOR v (UtxoOutShelley shelleyOut) = toCanonicalCBOR v ( E. encodeInt 0 , toCanonicalCBOR v shelleyOut)
136+ toCanonicalCBOR v (UtxoOutBabbage babbageOut) = toCanonicalCBOR v ( E. encodeInt 1 , toCanonicalCBOR v babbageOut)
137+ toCanonicalCBOR v (UtxoValue value) = toCanonicalCBOR v ( E. encodeInt 2 , toCanonicalCBOR v value)
143138
144139instance FromCanonicalCBOR V1 UtxoOut where
145140 fromCanonicalCBOR = do
@@ -151,12 +146,47 @@ instance FromCanonicalCBOR V1 UtxoOut where
151146 t -> fail $ " Unknown UtxoOut tag: " <> show t
152147
153148instance ToCanonicalCBOR V1 (Babbage. BabbageTxOut ConwayEra ) where
154- toCanonicalCBOR v (Babbage. TxOutCompact' cAddr form) = E. encodeTag 0 <> toCanonicalCBOR v (cAddr, form)
155- toCanonicalCBOR v (Babbage. TxOutCompactDH' cAddr form dataHash) = E. encodeTag 1 <> toCanonicalCBOR v (cAddr, form, dataHash)
156- toCanonicalCBOR v (Babbage. TxOutCompactDatum cAddr form inlineDatum) = E. encodeTag 2 <> toCanonicalCBOR v (cAddr, form, inlineDatum)
157- toCanonicalCBOR v (Babbage. TxOutCompactRefScript cAddr form datum script) = E. encodeTag 3 <> toCanonicalCBOR v (cAddr, form, datum, script)
158- toCanonicalCBOR v (Babbage. TxOut_AddrHash28_AdaOnly staking hash28 compact) = E. encodeTag 4 <> toCanonicalCBOR v (staking, hash28, compact)
159- toCanonicalCBOR v (Babbage. TxOut_AddrHash28_AdaOnly_DataHash32 staking hash28 compact dataHash) = E. encodeTag 5 <> toCanonicalCBOR v (staking, hash28, compact, dataHash)
149+ toCanonicalCBOR v (Babbage. TxOutCompact' cAddr form) =
150+ E. encodeMapLen 2
151+ <> E. encodeInt 0 <> toCanonicalCBOR v cAddr
152+ <> E. encodeInt 1 <> toCanonicalCBOR v form
153+ toCanonicalCBOR v (Babbage. TxOutCompactDH' cAddr form datum) =
154+ E. encodeMapLen 3
155+ <> E. encodeInt 0 <> toCanonicalCBOR v cAddr
156+ <> E. encodeInt 1 <> toCanonicalCBOR v form
157+ <> E. encodeInt 2
158+ <> case datum of
159+ hash_ -> toCanonicalCBOR v (0 :: Int , originalBytes hash_ )
160+ toCanonicalCBOR v (Babbage. TxOutCompactDatum cAddr form inlineDatum) =
161+ E. encodeMapLen 3
162+ <> E. encodeInt 0 <> toCanonicalCBOR v cAddr
163+ <> E. encodeInt 1 <> toCanonicalCBOR v form
164+ <> E. encodeInt 2
165+ <> case inlineDatum of
166+ binaryData -> toCanonicalCBOR v (1 :: Int , toCanonicalCBOR v (LedgerCBOR @ V1 binaryData ))
167+ toCanonicalCBOR v (Babbage. TxOutCompactRefScript cAddr form datum script) =
168+ let datumEncoding = case datum of
169+ NoDatum -> (Nothing )
170+ DatumHash dh -> Just (toCanonicalCBOR v (0 :: Int , originalBytes dh ))
171+ Datum binaryData -> Just (toCanonicalCBOR v (1 :: Int , toCanonicalCBOR v (LedgerCBOR @ V1 binaryData )))
172+ in E. encodeMapLen (3 + (case datumEncoding of Just {} -> 1 ; Nothing -> 0 ))
173+ <> E. encodeInt 0 <> toCanonicalCBOR v cAddr
174+ <> E. encodeInt 1 <> toCanonicalCBOR v form
175+ <> case datumEncoding of
176+ Nothing -> mempty
177+ Just enc -> E. encodeInt 2 <> enc
178+ <> E. encodeInt 3 <> toCanonicalCBOR v (LedgerCBOR @ V1 script)
179+ toCanonicalCBOR v (Babbage. TxOut_AddrHash28_AdaOnly staking hash28 compactForm) =
180+ let cAddr = unCompactAddr (compactAddr (decodeAddress28 staking hash28))
181+ in E. encodeMapLen 2
182+ <> E. encodeInt 0 <> toCanonicalCBOR v cAddr
183+ <> E. encodeInt 1 <> toCanonicalCBOR v compactForm
184+ toCanonicalCBOR v (Babbage. TxOut_AddrHash28_AdaOnly_DataHash32 staking hash28 compact dataHash) =
185+ let cAddr = unCompactAddr (compactAddr (decodeAddress28 staking hash28))
186+ in E. encodeMapLen 3
187+ <> E. encodeInt 0 <> toCanonicalCBOR v cAddr
188+ <> E. encodeInt 1 <> toCanonicalCBOR v compact
189+ <> E. encodeInt 2 <> toCanonicalCBOR v (0 :: Int , dataHash )
160190
161191instance FromCanonicalCBOR V1 (Babbage. BabbageTxOut ConwayEra ) where
162192 fromCanonicalCBOR = do
@@ -170,12 +200,12 @@ instance FromCanonicalCBOR V1 (Babbage.BabbageTxOut ConwayEra) where
170200 t -> fail $ " Unknown BabbageTxOut tag: " <> show t
171201
172202instance ToCanonicalCBOR V1 (Babbage. BabbageTxOut MaryEra ) where
173- toCanonicalCBOR v (Babbage. TxOutCompact' cAddr form) = E. encodeTag 0 <> toCanonicalCBOR v (cAddr, form)
174- toCanonicalCBOR v (Babbage. TxOutCompactDH' cAddr form dataHash) = E. encodeTag 1 <> toCanonicalCBOR v (cAddr, form, dataHash)
175- toCanonicalCBOR v (Babbage. TxOutCompactDatum cAddr form inlineDatum) = E. encodeTag 2 <> toCanonicalCBOR v (cAddr, form, inlineDatum)
176- toCanonicalCBOR v (Babbage. TxOutCompactRefScript cAddr form datum script) = E. encodeTag 3 <> toCanonicalCBOR v (cAddr, form, datum, script)
177- toCanonicalCBOR v (Babbage. TxOut_AddrHash28_AdaOnly staking hash28 compact) = E. encodeTag 4 <> toCanonicalCBOR v (staking, hash28, compact)
178- toCanonicalCBOR v (Babbage. TxOut_AddrHash28_AdaOnly_DataHash32 staking hash28 compact dataHash) = E. encodeTag 5 <> toCanonicalCBOR v (staking, hash28, compact, dataHash)
203+ toCanonicalCBOR v (Babbage. TxOutCompact' cAddr form) = E. encodeInt 0 <> toCanonicalCBOR v (cAddr, form)
204+ toCanonicalCBOR v (Babbage. TxOutCompactDH' cAddr form dataHash) = E. encodeInt 1 <> toCanonicalCBOR v (cAddr, form, dataHash)
205+ toCanonicalCBOR v (Babbage. TxOutCompactDatum cAddr form inlineDatum) = E. encodeInt 2 <> toCanonicalCBOR v (cAddr, form, inlineDatum)
206+ toCanonicalCBOR v (Babbage. TxOutCompactRefScript cAddr form datum script) = E. encodeInt 3 <> toCanonicalCBOR v (cAddr, form, datum, script)
207+ toCanonicalCBOR v (Babbage. TxOut_AddrHash28_AdaOnly staking hash28 compact) = E. encodeInt 4 <> toCanonicalCBOR v (staking, hash28, compact)
208+ toCanonicalCBOR v (Babbage. TxOut_AddrHash28_AdaOnly_DataHash32 staking hash28 compact dataHash) = E. encodeInt 5 <> toCanonicalCBOR v (staking, hash28, compact, dataHash)
179209
180210instance FromCanonicalCBOR V1 (Babbage. BabbageTxOut MaryEra ) where
181211 fromCanonicalCBOR = do
@@ -209,7 +239,14 @@ deriving via (LedgerCBOR v (AlonzoPlutusPurpose AsItem ConwayEra)) instance ToCa
209239deriving via (LedgerCBOR v (AlonzoPlutusPurpose AsItem ConwayEra )) instance FromCanonicalCBOR v (AlonzoPlutusPurpose AsItem ConwayEra )
210240deriving via (MemPackCBOR (AlonzoScript ConwayEra )) instance ToCanonicalCBOR V1 (AlonzoScript ConwayEra )
211241deriving via (MemPackCBOR (AlonzoScript ConwayEra )) instance FromCanonicalCBOR V1 (AlonzoScript ConwayEra )
212- deriving via (MemPackCBOR (CompactForm a )) instance (MemPack (CompactForm a )) => ToCanonicalCBOR V1 (CompactForm a )
242+ -- deriving via (MemPackCBOR (CompactForm a)) instance {-# OVERLAPPABLE #-} (MemPack (CompactForm a)) => ToCanonicalCBOR V1 (CompactForm a)
243+
244+ instance {-# OVERLAPPING #-} ToCanonicalCBOR version (CompactForm MaryValue ) where
245+ toCanonicalCBOR version v = toCanonicalCBOR version (fromCompact v)
246+
247+ instance {-# OVERLAPPING #-} ToCanonicalCBOR v (CompactForm Coin ) where
248+ toCanonicalCBOR v (Coin. CompactCoin ci) = toCanonicalCBOR v ci
249+
213250deriving via (MemPackCBOR (CompactForm a )) instance (MemPack (CompactForm a )) => FromCanonicalCBOR V1 (CompactForm a )
214251deriving via (MemPackCBOR CompactAddr ) instance FromCanonicalCBOR V1 CompactAddr
215252deriving via (MemPackCBOR CompactAddr ) instance ToCanonicalCBOR V1 CompactAddr
0 commit comments