Skip to content

Commit 105a6c0

Browse files
committed
Reorganize Orphans module to avoid cyclical dependencies
1 parent 09eeece commit 105a6c0

File tree

16 files changed

+513
-413
lines changed

16 files changed

+513
-413
lines changed

cardano-api/cardano-api.cabal

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ library
8484
Cardano.Api.Internal.IO
8585
Cardano.Api.Internal.LedgerState
8686
Cardano.Api.Internal.Modes
87-
Cardano.Api.Internal.Orphans
87+
Cardano.Api.Internal.Orphans.All
88+
Cardano.Api.Internal.Orphans.Serialization
89+
Cardano.Api.Internal.Orphans.Misc
8890
Cardano.Api.Internal.Plutus
8991
Cardano.Api.Internal.Pretty
9092
Cardano.Api.Internal.ProtocolParameters

cardano-api/src/Cardano/Api.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1156,7 +1156,7 @@ import Cardano.Api.Internal.Modes
11561156
import Cardano.Api.Internal.Monad.Error
11571157
import Cardano.Api.Internal.NetworkId
11581158
import Cardano.Api.Internal.OperationalCertificate
1159-
import Cardano.Api.Internal.Orphans ()
1159+
import Cardano.Api.Internal.Orphans.All ()
11601160
import Cardano.Api.Internal.Plutus
11611161
import Cardano.Api.Internal.Pretty
11621162
import Cardano.Api.Internal.Protocol

cardano-api/src/Cardano/Api/Internal/CIP/CIP129.hs

Lines changed: 6 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,29 @@ where
1717

1818
import Cardano.Api.Internal.Governance.Actions.ProposalProcedure
1919
import Cardano.Api.Internal.HasTypeProxy
20-
import Cardano.Api.Internal.Orphans ()
20+
import Cardano.Api.Internal.Orphans.All (AsType (..))
2121
import Cardano.Api.Internal.SerialiseBech32
2222
import Cardano.Api.Internal.SerialiseRaw
2323
import Cardano.Api.Internal.TxIn
2424
import Cardano.Api.Internal.Utils
2525

26-
import Cardano.Binary qualified as CBOR
2726
import Cardano.Ledger.Conway.Governance qualified as Gov
2827
import Cardano.Ledger.Credential (Credential (..))
2928
import Cardano.Ledger.Credential qualified as L
3029
import Cardano.Ledger.Keys qualified as L
3130

3231
import Codec.Binary.Bech32 qualified as Bech32
3332
import Control.Monad (guard)
34-
import Data.Bifunctor
3533
import Data.ByteString (ByteString)
3634
import Data.ByteString qualified as BS
3735
import Data.ByteString.Base16 qualified as Base16
3836
import Data.ByteString.Char8 qualified as C8
3937
import Data.Text (Text)
4038
import Data.Text.Encoding qualified as Text
4139
import GHC.Exts (IsList (..))
42-
import Text.Read
4340

4441
-- | CIP129 is a typeclass that captures the serialisation requirements of https://cips.cardano.org/cip/CIP-0129
42+
-- which pertain to governance credentials and governance action ids.
4543
class (SerialiseAsRawBytes a, HasTypeProxy a) => CIP129 a where
4644
cip129Bech32PrefixFor :: AsType a -> Text
4745

@@ -59,20 +57,6 @@ instance CIP129 (Credential L.ColdCommitteeRole) where
5957
L.KeyHashObj{} -> BS.singleton 0x12 -- 0001 0010
6058
L.ScriptHashObj{} -> BS.singleton 0x13 -- 0001 0011
6159

62-
instance HasTypeProxy (Credential L.ColdCommitteeRole) where
63-
data AsType (Credential L.ColdCommitteeRole) = AsColdCommitteeCredential
64-
proxyToAsType _ = AsColdCommitteeCredential
65-
66-
instance SerialiseAsRawBytes (Credential L.ColdCommitteeRole) where
67-
serialiseToRawBytes = CBOR.serialize'
68-
deserialiseFromRawBytes AsColdCommitteeCredential =
69-
first
70-
( \e ->
71-
SerialiseAsRawBytesError
72-
("Unable to deserialise Credential ColdCommitteeRole: " ++ show e)
73-
)
74-
. CBOR.decodeFull'
75-
7660
instance CIP129 (Credential L.HotCommitteeRole) where
7761
cip129Bech32PrefixFor _ = "cc_hot"
7862
cip129Bech32PrefixesPermitted AsHotCommitteeCredential = ["cc_hot"]
@@ -81,20 +65,6 @@ instance CIP129 (Credential L.HotCommitteeRole) where
8165
L.KeyHashObj{} -> BS.singleton 0x02 -- 0000 0010
8266
L.ScriptHashObj{} -> BS.singleton 0x03 -- 0000 0011
8367

84-
instance HasTypeProxy (Credential L.HotCommitteeRole) where
85-
data AsType (Credential L.HotCommitteeRole) = AsHotCommitteeCredential
86-
proxyToAsType _ = AsHotCommitteeCredential
87-
88-
instance SerialiseAsRawBytes (Credential L.HotCommitteeRole) where
89-
serialiseToRawBytes = CBOR.serialize'
90-
deserialiseFromRawBytes AsHotCommitteeCredential =
91-
first
92-
( \e ->
93-
SerialiseAsRawBytesError
94-
("Unable to deserialise Credential HotCommitteeRole: " ++ show e)
95-
)
96-
. CBOR.decodeFull'
97-
9868
instance CIP129 (Credential L.DRepRole) where
9969
cip129Bech32PrefixFor _ = "drep"
10070
cip129Bech32PrefixesPermitted AsDrepCredential = ["drep"]
@@ -103,19 +73,6 @@ instance CIP129 (Credential L.DRepRole) where
10373
L.KeyHashObj{} -> BS.singleton 0x22 -- 0010 0010
10474
L.ScriptHashObj{} -> BS.singleton 0x23 -- 0010 0011
10575

106-
instance HasTypeProxy (Credential L.DRepRole) where
107-
data AsType (Credential L.DRepRole) = AsDrepCredential
108-
proxyToAsType _ = AsDrepCredential
109-
110-
instance SerialiseAsRawBytes (Credential L.DRepRole) where
111-
serialiseToRawBytes = CBOR.serialize'
112-
deserialiseFromRawBytes AsDrepCredential =
113-
first
114-
( \e ->
115-
SerialiseAsRawBytesError ("Unable to deserialise Credential DRepRole: " ++ show e)
116-
)
117-
. CBOR.decodeFull'
118-
11976
serialiseToBech32CIP129 :: forall a. CIP129 a => a -> Text
12077
serialiseToBech32CIP129 a =
12178
Bech32.encodeLenient
@@ -150,7 +107,10 @@ deserialiseFromBech32CIP129 asType bech32Str = do
150107
Bech32.dataPartToBytes dataPart
151108
?! Bech32DataPartToBytesError (Bech32.dataPartToText dataPart)
152109

153-
let (header, credential) = BS.uncons payload
110+
(header, credential) <-
111+
case C8.uncons payload of
112+
Just (header, credential) -> return (C8.singleton header, credential)
113+
Nothing -> Left $ Bech32DeserialiseFromBytesError payload
154114

155115
value <- case deserialiseFromRawBytes asType credential of
156116
Right a -> Right a
@@ -210,21 +170,3 @@ deserialiseGovActionIdFromBech32CIP129 bech32Str = do
210170
case deserialiseFromRawBytes AsGovActionId payload of
211171
Right a -> Right a
212172
Left _ -> Left $ Bech32DeserialiseFromBytesError payload
213-
214-
instance HasTypeProxy Gov.GovActionId where
215-
data AsType Gov.GovActionId = AsGovActionId
216-
proxyToAsType _ = AsGovActionId
217-
218-
instance SerialiseAsRawBytes Gov.GovActionId where
219-
serialiseToRawBytes (Gov.GovActionId txid (Gov.GovActionIx ix)) =
220-
let hex = Base16.encode $ C8.pack $ show ix
221-
in mconcat [serialiseToRawBytes $ fromShelleyTxId txid, hex]
222-
deserialiseFromRawBytes AsGovActionId bytes = do
223-
let (txidBs, index) = BS.splitAt 32 bytes
224-
225-
txid <- deserialiseFromRawBytes AsTxId txidBs
226-
let asciiIndex = C8.unpack $ Base16.decodeLenient index
227-
case readMaybe asciiIndex of
228-
Just ix -> return $ Gov.GovActionId (toShelleyTxId txid) (Gov.GovActionIx ix)
229-
Nothing ->
230-
Left $ SerialiseAsRawBytesError $ "Unable to deserialise GovActionId: invalid index: " <> asciiIndex

cardano-api/src/Cardano/Api/Internal/DeserialiseAnyOf.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import Data.Bifunctor (first)
3939
import Data.ByteString (ByteString)
4040
import Data.ByteString.Char8 qualified as BSC
4141
import Data.Char (toLower)
42-
import Data.Data (Data)
42+
import Data.Data
4343
import Data.List.NonEmpty (NonEmpty)
4444
import Data.Text (Text)
4545
import Data.Text.Encoding qualified as Text

cardano-api/src/Cardano/Api/Internal/Eon/ShelleyBasedEra.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ where
3939
import Cardano.Api.Internal.Eon.Convert
4040
import Cardano.Api.Internal.Eras.Core
4141
import Cardano.Api.Internal.Modes
42-
import Cardano.Api.Internal.Orphans ()
42+
import Cardano.Api.Internal.Orphans.All ()
4343
import Cardano.Api.Internal.Pretty (Pretty)
4444

4545
import Cardano.Crypto.Hash.Blake2b qualified as Blake2b

cardano-api/src/Cardano/Api/Internal/InMode.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ where
3030
import Cardano.Api.Internal.Eon.ShelleyBasedEra
3131
import Cardano.Api.Internal.Eras
3232
import Cardano.Api.Internal.Modes
33-
import Cardano.Api.Internal.Orphans ()
33+
import Cardano.Api.Internal.Orphans.All ()
3434
import Cardano.Api.Internal.Tx.Body
3535
import Cardano.Api.Internal.Tx.Sign
3636
import Cardano.Api.Internal.Utils (textShow)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{-# LANGUAGE DataKinds #-}
2+
{-# LANGUAGE DeriveAnyClass #-}
3+
{-# LANGUAGE DeriveDataTypeable #-}
4+
{-# LANGUAGE DeriveGeneric #-}
5+
{-# LANGUAGE DerivingVia #-}
6+
{-# LANGUAGE FlexibleContexts #-}
7+
{-# LANGUAGE FlexibleInstances #-}
8+
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
9+
{-# LANGUAGE NamedFieldPuns #-}
10+
{-# LANGUAGE OverloadedStrings #-}
11+
{-# LANGUAGE StandaloneDeriving #-}
12+
{-# LANGUAGE TypeFamilies #-}
13+
{-# LANGUAGE UndecidableInstances #-}
14+
{-# OPTIONS_GHC -Wno-orphans -Wno-unused-imports #-}
15+
16+
module Cardano.Api.Internal.Orphans.All
17+
( AsType
18+
( AsColdCommitteeCredential
19+
, AsHotCommitteeCredential
20+
, AsDrepCredential
21+
, AsGovActionId
22+
)
23+
)
24+
where
25+
26+
import Cardano.Api.Internal.Orphans.Misc ()
27+
import Cardano.Api.Internal.Orphans.Serialization (AsType
28+
( AsColdCommitteeCredential
29+
, AsHotCommitteeCredential
30+
, AsDrepCredential
31+
, AsGovActionId
32+
))
33+
import Cardano.Api.Internal.Orphans.Serialization ()

0 commit comments

Comments
 (0)