@@ -41,57 +41,53 @@ import GHC.Exts (IsList (..))
4141-- | Cip129 is a typeclass that captures the serialisation requirements of https://cips.cardano.org/cip/CIP-0129
4242-- which pertain to governance credentials and governance action ids.
4343class (SerialiseAsRawBytes a , HasTypeProxy a ) => Cip129 a where
44- cip129Bech32PrefixFor :: AsType a -> Text
44+ cip129Bech32PrefixFor :: AsType a -> Bech32. HumanReadablePart
4545
4646 cip129HeaderHexByte :: a -> ByteString
4747
4848 cip129Bech32PrefixesPermitted :: AsType a -> [Text ]
4949 default cip129Bech32PrefixesPermitted :: AsType a -> [Text ]
50- cip129Bech32PrefixesPermitted = return . cip129Bech32PrefixFor
50+ cip129Bech32PrefixesPermitted = return . Bech32. humanReadablePartToText . cip129Bech32PrefixFor
51+
52+ unsafeHumanReadablePartFromText :: Text -> Bech32. HumanReadablePart
53+ unsafeHumanReadablePartFromText =
54+ either (error . (" Error while parsing Bech32: " <> ) . show ) id
55+ . Bech32. humanReadablePartFromText
5156
5257instance Cip129 (Credential L. ColdCommitteeRole ) where
53- cip129Bech32PrefixFor _ = " cc_cold"
58+ cip129Bech32PrefixFor _ = unsafeHumanReadablePartFromText " cc_cold"
5459 cip129Bech32PrefixesPermitted AsColdCommitteeCredential = [" cc_cold" ]
5560 cip129HeaderHexByte c =
5661 case c of
5762 L. KeyHashObj {} -> BS. singleton 0x12 -- 0001 0010
5863 L. ScriptHashObj {} -> BS. singleton 0x13 -- 0001 0011
5964
6065instance Cip129 (Credential L. HotCommitteeRole ) where
61- cip129Bech32PrefixFor _ = " cc_hot"
66+ cip129Bech32PrefixFor _ = unsafeHumanReadablePartFromText " cc_hot"
6267 cip129Bech32PrefixesPermitted AsHotCommitteeCredential = [" cc_hot" ]
6368 cip129HeaderHexByte c =
6469 case c of
6570 L. KeyHashObj {} -> BS. singleton 0x02 -- 0000 0010
6671 L. ScriptHashObj {} -> BS. singleton 0x03 -- 0000 0011
6772
6873instance Cip129 (Credential L. DRepRole ) where
69- cip129Bech32PrefixFor _ = " drep"
74+ cip129Bech32PrefixFor _ = unsafeHumanReadablePartFromText " drep"
7075 cip129Bech32PrefixesPermitted AsDrepCredential = [" drep" ]
7176 cip129HeaderHexByte c =
7277 case c of
7378 L. KeyHashObj {} -> BS. singleton 0x22 -- 0010 0010
7479 L. ScriptHashObj {} -> BS. singleton 0x23 -- 0010 0011
7580
7681-- | Serialize a accoding to the serialisation requirements of https://cips.cardano.org/cip/CIP-0129
77- -- which currently pertain to governance credentials. Governance action ids are dealt separately with
82+ -- which currently pertain to governance credentials. Governance action ids are dealt separately with
7883-- via 'serialiseGovActionIdToBech32CIP129'.
7984serialiseToBech32Cip129 :: forall a . Cip129 a => a -> Text
8085serialiseToBech32Cip129 a =
8186 Bech32. encodeLenient
8287 humanReadablePart
8388 (Bech32. dataPartFromBytes (cip129HeaderHexByte a <> serialiseToRawBytes a))
8489 where
85- prefix = cip129Bech32PrefixFor (proxyToAsType (Proxy :: Proxy a ))
86- humanReadablePart =
87- case Bech32. humanReadablePartFromText prefix of
88- Right p -> p
89- Left err ->
90- error $
91- " serialiseToBech32Cip129: invalid prefix "
92- ++ show prefix
93- ++ " , "
94- ++ show err
90+ humanReadablePart = cip129Bech32PrefixFor (proxyToAsType (Proxy :: Proxy a ))
9591
9692deserialiseFromBech32CIP129
9793 :: Cip129 a
@@ -124,7 +120,7 @@ deserialiseFromBech32CIP129 asType bech32Str = do
124120 guard (header == expectedHeader)
125121 ?! Bech32UnexpectedHeader (toBase16Text expectedHeader) (toBase16Text header)
126122
127- let expectedPrefix = cip129Bech32PrefixFor asType
123+ let expectedPrefix = Bech32. humanReadablePartToText $ cip129Bech32PrefixFor asType
128124 guard (actualPrefix == expectedPrefix)
129125 ?! Bech32WrongPrefix actualPrefix expectedPrefix
130126
0 commit comments