77{-# LANGUAGE FlexibleInstances #-}
88{-# LANGUAGE FlexibleContexts #-}
99{-# LANGUAGE KindSignatures #-}
10+ {-# LANGUAGE GeneralizedNewtypeDeriving #-}
1011{-# OPTIONS_GHC -Wno-orphans #-}
1112-- | UTxO namespace export.
1213module Cardano.Ledger.Export.Namespace.UTxO
@@ -24,7 +25,10 @@ import Cardano.Ledger.Binary (decodeMemPack, encodeMemPack, EncCBOR(..), DecCBOR
2425import Cardano.SCLS.CBOR.Canonical.Decoder
2526import qualified Codec.CBOR.Encoding as E
2627import qualified Codec.CBOR.Decoding as D
27- import Cardano.Chain.UTxO (TxIn (.. ))
28+ import Cardano.Ledger.Conway (ConwayEra )
29+ import Cardano.Ledger.TxIn (TxIn (.. ))
30+ import Cardano.Ledger.Core (TxOut (.. ))
31+ -- import Cardano.Ledger.TxOut (TxOut(..))
2832import Cardano.Ledger.Compactible
2933import Cardano.Ledger.Address
3034import Cardano.Ledger.Credential
@@ -33,6 +37,7 @@ import Cardano.Ledger.Hashes
3337import Cardano.Ledger.Plutus.Data (Datum (.. ))
3438import Cardano.Ledger.Plutus.Data (BinaryData )
3539import Cardano.Ledger.Mary (MaryEra , MaryValue )
40+ import Cardano.SCLS.Internal.Entry
3641import Cardano.SCLS.Internal.Version
3742import Data.Typeable (Typeable )
3843import qualified Cardano.Ledger.Shelley.TxOut as Shelley
@@ -41,7 +46,7 @@ import Cardano.Ledger.Allegra.Scripts (Timelock(..))
4146import Data.MemPack
4247import Data.Word (Word8 , Word16 )
4348import Cardano.Ledger.Alonzo.TxOut (DataHash32 , Addr28Extra )
44- import Cardano.Ledger.Alonzo.Scripts (AlonzoPlutusPurpose , AsItem )
49+ import Cardano.Ledger.Alonzo.Scripts (AlonzoPlutusPurpose , AsItem , AlonzoScript )
4550
4651-- | Helper that allows us to deriving instances via internal CBOR representation
4752newtype LedgerCBOR (v :: Version ) a = LedgerCBOR { unLedgerCBOR :: a }
@@ -65,7 +70,34 @@ instance (MemPack a) => FromCanonicalCBOR V1 (MemPackCBOR a) where
6570-- | Input wrapper for the keys that are used in utxo namespace
6671data UtxoKey
6772 = UtxoKeyIn TxIn
68- | UtxoKeyScript ScriptUtxoIn
73+ -- | UtxoKeyScript ScriptUtxoIn
74+
75+ instance Eq UtxoKey where
76+ (UtxoKeyIn txIn1) == (UtxoKeyIn txIn2) = txIn1 == txIn2
77+ -- (UtxoKeyScript script1) == (UtxoKeyScript script2) = undefined script1 script2
78+ -- _ == _ = False
79+
80+ instance Ord UtxoKey where
81+ 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
85+
86+ instance IsKey UtxoKey where
87+ keySize = 34
88+ packKeyM (UtxoKeyIn (TxIn a b)) = do
89+ packM a
90+ packM b
91+ -- packKeyM (UtxoKeyScript (ScriptUtxoIn purpose hash)) = do
92+ -- undefined purpose hash
93+ unpackKeyM = do
94+ a <- unpackM
95+ b <- unpackM
96+ return $ UtxoKeyIn (TxIn a b)
97+ -- toKeyBytes v key = toStrictByteString $ toCanonicalCBOR v key
98+
99+ newtype Out = Out (TxOut ConwayEra )
100+ deriving newtype (ToCanonicalCBOR V1 , FromCanonicalCBOR V1 )
69101
70102data AddrUtxoIn = AddrUtxoIn { addrUtxoInAddress :: DataHash32 , addrUtxoInIndex :: Word16 }
71103data ScriptUtxoIn = ScriptUtxoIn { scriptUtxoInPurpose :: AlonzoPlutusPurpose AsItem MaryEra , scriptUtxoInHash :: ScriptHash }
@@ -81,7 +113,7 @@ data UtxoOut
81113
82114instance ToCanonicalCBOR V1 UtxoKey where
83115 toCanonicalCBOR v (UtxoKeyIn txIn) = E. encodeTag 0 <> toCanonicalCBOR v txIn
84- toCanonicalCBOR v (UtxoKeyScript script) = E. encodeTag 1 <> toCanonicalCBOR v script
116+ -- toCanonicalCBOR v (UtxoKeyScript script) = E.encodeTag 1 <> toCanonicalCBOR v script
85117
86118
87119instance ToCanonicalCBOR V1 AddrUtxoIn where
@@ -101,7 +133,7 @@ instance FromCanonicalCBOR V1 UtxoKey where
101133 tag <- fromCanonicalCBOR
102134 case unVer tag :: Word8 of
103135 0 -> fmap UtxoKeyIn <$> fromCanonicalCBOR
104- 1 -> fmap UtxoKeyScript <$> fromCanonicalCBOR
136+ -- 1 -> fmap UtxoKeyScript <$> fromCanonicalCBOR
105137 _ -> fail " Unknown UtxoKey tag"
106138
107139instance ToCanonicalCBOR V1 UtxoOut where
@@ -118,6 +150,24 @@ instance FromCanonicalCBOR V1 UtxoOut where
118150 3 -> fmap UtxoValue <$> fromCanonicalCBOR
119151 t -> fail $ " Unknown UtxoOut tag: " <> show t
120152
153+ instance 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)
160+
161+ instance FromCanonicalCBOR V1 (Babbage. BabbageTxOut ConwayEra ) where
162+ fromCanonicalCBOR = do
163+ D. decodeTag >>= \ case
164+ 0 -> fmap (\ (c, f) -> Babbage. TxOutCompact' c f) <$> fromCanonicalCBOR
165+ 1 -> fmap (\ (a,b,c) -> Babbage. TxOutCompactDH' a b c) <$> fromCanonicalCBOR
166+ 2 -> fmap (\ (a,b,c) -> Babbage. TxOutCompactDatum a b c) <$> fromCanonicalCBOR
167+ 3 -> fmap (\ (a,b,c,d) -> Babbage. TxOutCompactRefScript a b c d) <$> fromCanonicalCBOR
168+ 4 -> fmap (\ (a,b,c) -> Babbage. TxOut_AddrHash28_AdaOnly a b c) <$> fromCanonicalCBOR
169+ 5 -> fmap (\ (a,b,c,d) -> Babbage. TxOut_AddrHash28_AdaOnly_DataHash32 a b c d) <$> fromCanonicalCBOR
170+ t -> fail $ " Unknown BabbageTxOut tag: " <> show t
121171
122172instance ToCanonicalCBOR V1 (Babbage. BabbageTxOut MaryEra ) where
123173 toCanonicalCBOR v (Babbage. TxOutCompact' cAddr form) = E. encodeTag 0 <> toCanonicalCBOR v (cAddr, form)
@@ -155,6 +205,10 @@ deriving via (LedgerCBOR v (Shelley.ShelleyTxOut MaryEra)) instance ToCanonicalC
155205deriving via (LedgerCBOR v (Shelley. ShelleyTxOut MaryEra )) instance FromCanonicalCBOR v (Shelley. ShelleyTxOut MaryEra )
156206deriving via (LedgerCBOR v (AlonzoPlutusPurpose AsItem MaryEra )) instance ToCanonicalCBOR v (AlonzoPlutusPurpose AsItem MaryEra )
157207deriving via (LedgerCBOR v (AlonzoPlutusPurpose AsItem MaryEra )) instance FromCanonicalCBOR v (AlonzoPlutusPurpose AsItem MaryEra )
208+ deriving via (LedgerCBOR v (AlonzoPlutusPurpose AsItem ConwayEra )) instance ToCanonicalCBOR v (AlonzoPlutusPurpose AsItem ConwayEra )
209+ deriving via (LedgerCBOR v (AlonzoPlutusPurpose AsItem ConwayEra )) instance FromCanonicalCBOR v (AlonzoPlutusPurpose AsItem ConwayEra )
210+ deriving via (MemPackCBOR (AlonzoScript ConwayEra )) instance ToCanonicalCBOR V1 (AlonzoScript ConwayEra )
211+ deriving via (MemPackCBOR (AlonzoScript ConwayEra )) instance FromCanonicalCBOR V1 (AlonzoScript ConwayEra )
158212deriving via (MemPackCBOR (CompactForm a )) instance (MemPack (CompactForm a )) => ToCanonicalCBOR V1 (CompactForm a )
159213deriving via (MemPackCBOR (CompactForm a )) instance (MemPack (CompactForm a )) => FromCanonicalCBOR V1 (CompactForm a )
160214deriving via (MemPackCBOR CompactAddr ) instance FromCanonicalCBOR V1 CompactAddr
@@ -177,8 +231,12 @@ deriving via (LedgerCBOR v (ScriptHash)) instance FromCanonicalCBOR v ScriptHash
177231deriving via (LedgerCBOR v (ScriptHash )) instance ToCanonicalCBOR v ScriptHash
178232deriving via (LedgerCBOR v (Datum MaryEra )) instance ToCanonicalCBOR v (Datum MaryEra )
179233deriving via (LedgerCBOR v (Datum MaryEra )) instance FromCanonicalCBOR v (Datum MaryEra )
234+ deriving via (LedgerCBOR v (Datum ConwayEra )) instance ToCanonicalCBOR v (Datum ConwayEra )
235+ deriving via (LedgerCBOR v (Datum ConwayEra )) instance FromCanonicalCBOR v (Datum ConwayEra )
180236deriving via (LedgerCBOR v (BinaryData MaryEra )) instance ToCanonicalCBOR v (BinaryData MaryEra )
181237deriving via (LedgerCBOR v (BinaryData MaryEra )) instance FromCanonicalCBOR v (BinaryData MaryEra )
238+ deriving via (LedgerCBOR v (BinaryData ConwayEra )) instance ToCanonicalCBOR v (BinaryData ConwayEra )
239+ deriving via (LedgerCBOR v (BinaryData ConwayEra )) instance FromCanonicalCBOR v (BinaryData ConwayEra )
182240deriving via (LedgerCBOR v (SafeHash EraIndependentData )) instance ToCanonicalCBOR v ((SafeHash EraIndependentData ))
183241deriving via (LedgerCBOR v (SafeHash EraIndependentData )) instance FromCanonicalCBOR v ((SafeHash EraIndependentData ))
184242
0 commit comments