@@ -17,31 +17,29 @@ where
1717
1818import Cardano.Api.Internal.Governance.Actions.ProposalProcedure
1919import Cardano.Api.Internal.HasTypeProxy
20- import Cardano.Api.Internal.Orphans ( )
20+ import Cardano.Api.Internal.Orphans.All ( AsType ( .. ) )
2121import Cardano.Api.Internal.SerialiseBech32
2222import Cardano.Api.Internal.SerialiseRaw
2323import Cardano.Api.Internal.TxIn
2424import Cardano.Api.Internal.Utils
2525
26- import Cardano.Binary qualified as CBOR
2726import Cardano.Ledger.Conway.Governance qualified as Gov
2827import Cardano.Ledger.Credential (Credential (.. ))
2928import Cardano.Ledger.Credential qualified as L
3029import Cardano.Ledger.Keys qualified as L
3130
3231import Codec.Binary.Bech32 qualified as Bech32
3332import Control.Monad (guard )
34- import Data.Bifunctor
3533import Data.ByteString (ByteString )
3634import Data.ByteString qualified as BS
3735import Data.ByteString.Base16 qualified as Base16
3836import Data.ByteString.Char8 qualified as C8
3937import Data.Text (Text )
4038import Data.Text.Encoding qualified as Text
4139import 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.
4543class (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-
7660instance 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-
9868instance 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-
11976serialiseToBech32CIP129 :: forall a . CIP129 a => a -> Text
12077serialiseToBech32CIP129 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
0 commit comments