1+ {-# LANGUAGE DataKinds #-}
2+ {-# LANGUAGE RecordWildCards #-}
3+ {-# LANGUAGE MultiParamTypeClasses #-}
4+ {-# LANGUAGE FlexibleInstances #-}
5+ {-# LANGUAGE FlexibleContexts #-}
6+ {-# OPTIONS_GHC -Wno-orphans #-}
7+ -- | UTxO namespace export.
8+ module Cardano.Ledger.Export.Namespace.UTxO
9+ ( UtxoIn (.. )
10+ , UtxoOut (.. )
11+ ) where
12+
13+ import Cardano.SCLS.CBOR.Canonical.Encoder
14+ import Cardano.Ledger.Binary
15+ import Cardano.SCLS.CBOR.Canonical.Decoder
16+ -- import Cardano.Ledger.Binary.Version
17+ import Cardano.Ledger.Compactible
18+ import Cardano.Ledger.Address
19+ import Cardano.Ledger.Credential
20+ import Cardano.Ledger.Keys
21+ import Cardano.Ledger.Hashes
22+ -- import Cardano.Ledger.Coin
23+ import Cardano.Ledger.Plutus.Data (Datum (.. ))
24+ import Cardano.Ledger.Plutus.Data (BinaryData )
25+ import Cardano.Ledger.Mary (MaryEra )
26+ -- import Cardano.Ledger
27+ import Cardano.SCLS.Internal.Version
28+ -- import Cardano.Ledger.Mary.Value (MaryValue)
29+ import Data.Typeable (Typeable )
30+ import qualified Cardano.Ledger.Shelley.TxOut as Shelley
31+ import qualified Cardano.Ledger.Babbage.TxOut as Babbage
32+ import Cardano.Ledger.Allegra.Scripts (Timelock (.. ))
33+ import Data.MemPack
34+ import qualified Codec.CBOR.Encoding as E
35+ import Data.Word (Word8 , Word16 )
36+ import Cardano.Ledger.Alonzo.TxOut (DataHash32 , Addr28Extra )
37+
38+ -- | Input wrapper for the keys that are used in utxo namespace
39+ data UtxoIn = UtxoIn { utxoInAddress :: DataHash32 , utxoInIndex :: Word16 }
40+
41+ -- | Output key that is used in utxo namespace
42+ --
43+ -- Here we follow the current spec, but after benchmarks we can decide that this representation
44+ -- is not efficient and we can replace it with the implementation based on the compact values
45+ data UtxoOut
46+ = UtxoOutShelley (Shelley. ShelleyTxOut MaryEra )
47+ | UtxoOutBabbage (Babbage. BabbageTxOut MaryEra )
48+
49+ instance ToCanonicalCBOR V1 UtxoIn where
50+ toCanonicalCBOR v UtxoIn {.. } = toCanonicalCBOR v (utxoInAddress, utxoInIndex)
51+
52+ instance FromCanonicalCBOR V1 UtxoIn where
53+ fromCanonicalCBOR = do
54+ fmap (uncurry UtxoIn ) <$> fromCanonicalCBOR
55+
56+ instance ToCanonicalCBOR V1 UtxoOut where
57+ toCanonicalCBOR v (UtxoOutShelley shelleyOut) = toCanonicalCBOR v (1 :: Word8 , shelleyOut )
58+ toCanonicalCBOR v (UtxoOutBabbage babbageOut) = toCanonicalCBOR v (2 :: Word8 , babbageOut )
59+
60+ instance FromCanonicalCBOR V1 UtxoOut where
61+ fromCanonicalCBOR = do
62+ tag <- fromCanonicalCBOR
63+ case unVer tag :: Word8 of
64+ 1 -> fmap UtxoOutShelley <$> fromCanonicalCBOR
65+ 2 -> fmap UtxoOutBabbage <$> fromCanonicalCBOR
66+ t -> fail $ " Unknown UtxoOut tag: " <> show t
67+
68+ instance ToCanonicalCBOR V1 (Shelley. ShelleyTxOut MaryEra ) where
69+ toCanonicalCBOR v (Shelley. TxOutCompact cAddr cValue) =
70+ toCanonicalCBOR v (cAddr, cValue) -- TODO: differ from spec as it uses compact representation
71+ instance FromCanonicalCBOR V1 (Shelley. ShelleyTxOut MaryEra ) where
72+ fromCanonicalCBOR = undefined -- fmap (uncurry Shelley.TxOutCompact) <$> fromCanonicalCBOR
73+
74+ instance ToCanonicalCBOR V1 (Babbage. BabbageTxOut MaryEra ) where
75+ toCanonicalCBOR v (Babbage. TxOutCompact' cAddr form) = toCanonicalCBOR v (cAddr, form)
76+ toCanonicalCBOR v (Babbage. TxOutCompactDH' cAddr form dataHash) = toCanonicalCBOR v (cAddr, form, dataHash)
77+ toCanonicalCBOR v (Babbage. TxOutCompactDatum cAddr form inlineDatum) = toCanonicalCBOR v (cAddr, form, inlineDatum)
78+ toCanonicalCBOR v (Babbage. TxOutCompactRefScript cAddr form datum script) = toCanonicalCBOR v (cAddr, form, datum, script)
79+ toCanonicalCBOR v (Babbage. TxOut_AddrHash28_AdaOnly staking hash28 compact) = toCanonicalCBOR v (staking, hash28, compact)
80+ toCanonicalCBOR v (Babbage. TxOut_AddrHash28_AdaOnly_DataHash32 staking hash28 compact dataHash) = toCanonicalCBOR v (staking, hash28, compact, dataHash)
81+
82+ instance FromCanonicalCBOR V1 (Babbage. BabbageTxOut MaryEra ) where
83+ fromCanonicalCBOR = undefined
84+ -- Versioned (addr, value, mDataHash) <- fromCanonicalCBOR
85+ -- pure $! Versioned (Babbage.TxOutCompact addr value mDataHash)
86+
87+ instance EncCBOR (CompactForm a ) => ToCanonicalCBOR V1 (CompactForm a ) where
88+ toCanonicalCBOR _ a = toPlainEncoding shelleyProtVer (encCBOR a)
89+
90+ instance ToCanonicalCBOR V1 (CompactAddr ) where
91+ toCanonicalCBOR v s = toCanonicalCBOR v (unCompactAddr s)
92+
93+ instance FromCanonicalCBOR V1 (CompactAddr ) where
94+ fromCanonicalCBOR = undefined
95+
96+ instance ToCanonicalCBOR V1 DataHash32 where
97+ toCanonicalCBOR _ hash32 = E. encodeBytes $ packByteString hash32
98+
99+ instance FromCanonicalCBOR V1 DataHash32 where
100+ fromCanonicalCBOR = undefined
101+ {-
102+ Versioned bytes <- fromCanonicalCBOR
103+ case decodeDataHash32 (bytes) of
104+ dh -> pure $! Versioned (encodeDataHash32 dh)
105+ -}
106+
107+ instance Typeable kr => ToCanonicalCBOR V1 (Credential kr ) where
108+ toCanonicalCBOR v (ScriptHashObj sh) = toCanonicalCBOR v (0 :: Word8 , sh )
109+ toCanonicalCBOR v (KeyHashObj kh) = toCanonicalCBOR v (1 :: Word8 , kh )
110+
111+ instance Typeable kr => ToCanonicalCBOR V1 (KeyHash kr ) where
112+ toCanonicalCBOR _ = toPlainEncoding shelleyProtVer . encCBOR
113+
114+ instance ToCanonicalCBOR V1 ScriptHash where
115+ toCanonicalCBOR _ = toPlainEncoding shelleyProtVer . encCBOR
116+
117+ instance ToCanonicalCBOR V1 Addr28Extra where
118+ toCanonicalCBOR _ = E. encodeBytes . packByteString
119+
120+ instance ToCanonicalCBOR V1 (Timelock MaryEra ) where
121+ toCanonicalCBOR _v = toPlainEncoding shelleyProtVer . encCBOR
122+
123+ instance ToCanonicalCBOR V1 (Datum MaryEra ) where
124+ toCanonicalCBOR _v = toPlainEncoding shelleyProtVer . encCBOR
125+
126+ instance ToCanonicalCBOR V1 (BinaryData MaryEra ) where
127+ toCanonicalCBOR _v = toPlainEncoding shelleyProtVer . encCBOR
128+
129+ instance ToCanonicalCBOR V1 (SafeHash EraIndependentData ) where
130+ toCanonicalCBOR _v = toPlainEncoding shelleyProtVer . encCBOR
0 commit comments