@@ -41,57 +41,53 @@ import GHC.Exts (IsList (..))
41
41
-- | Cip129 is a typeclass that captures the serialisation requirements of https://cips.cardano.org/cip/CIP-0129
42
42
-- which pertain to governance credentials and governance action ids.
43
43
class (SerialiseAsRawBytes a , HasTypeProxy a ) => Cip129 a where
44
- cip129Bech32PrefixFor :: AsType a -> Text
44
+ cip129Bech32PrefixFor :: AsType a -> Bech32. HumanReadablePart
45
45
46
46
cip129HeaderHexByte :: a -> ByteString
47
47
48
48
cip129Bech32PrefixesPermitted :: AsType a -> [Text ]
49
49
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
51
56
52
57
instance Cip129 (Credential L. ColdCommitteeRole ) where
53
- cip129Bech32PrefixFor _ = " cc_cold"
58
+ cip129Bech32PrefixFor _ = unsafeHumanReadablePartFromText " cc_cold"
54
59
cip129Bech32PrefixesPermitted AsColdCommitteeCredential = [" cc_cold" ]
55
60
cip129HeaderHexByte c =
56
61
case c of
57
62
L. KeyHashObj {} -> BS. singleton 0x12 -- 0001 0010
58
63
L. ScriptHashObj {} -> BS. singleton 0x13 -- 0001 0011
59
64
60
65
instance Cip129 (Credential L. HotCommitteeRole ) where
61
- cip129Bech32PrefixFor _ = " cc_hot"
66
+ cip129Bech32PrefixFor _ = unsafeHumanReadablePartFromText " cc_hot"
62
67
cip129Bech32PrefixesPermitted AsHotCommitteeCredential = [" cc_hot" ]
63
68
cip129HeaderHexByte c =
64
69
case c of
65
70
L. KeyHashObj {} -> BS. singleton 0x02 -- 0000 0010
66
71
L. ScriptHashObj {} -> BS. singleton 0x03 -- 0000 0011
67
72
68
73
instance Cip129 (Credential L. DRepRole ) where
69
- cip129Bech32PrefixFor _ = " drep"
74
+ cip129Bech32PrefixFor _ = unsafeHumanReadablePartFromText " drep"
70
75
cip129Bech32PrefixesPermitted AsDrepCredential = [" drep" ]
71
76
cip129HeaderHexByte c =
72
77
case c of
73
78
L. KeyHashObj {} -> BS. singleton 0x22 -- 0010 0010
74
79
L. ScriptHashObj {} -> BS. singleton 0x23 -- 0010 0011
75
80
76
81
-- | 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
78
83
-- via 'serialiseGovActionIdToBech32CIP129'.
79
84
serialiseToBech32Cip129 :: forall a . Cip129 a => a -> Text
80
85
serialiseToBech32Cip129 a =
81
86
Bech32. encodeLenient
82
87
humanReadablePart
83
88
(Bech32. dataPartFromBytes (cip129HeaderHexByte a <> serialiseToRawBytes a))
84
89
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 ))
95
91
96
92
deserialiseFromBech32CIP129
97
93
:: Cip129 a
@@ -124,7 +120,7 @@ deserialiseFromBech32CIP129 asType bech32Str = do
124
120
guard (header == expectedHeader)
125
121
?! Bech32UnexpectedHeader (toBase16Text expectedHeader) (toBase16Text header)
126
122
127
- let expectedPrefix = cip129Bech32PrefixFor asType
123
+ let expectedPrefix = Bech32. humanReadablePartToText $ cip129Bech32PrefixFor asType
128
124
guard (actualPrefix == expectedPrefix)
129
125
?! Bech32WrongPrefix actualPrefix expectedPrefix
130
126
0 commit comments