Skip to content

Commit a30099c

Browse files
committed
Simplify SerialiseAsRawBytes type class.
`deserialiseFromRawBytes` method of the `SerialiseAsRawBytes` type class to return `Either` instead of `Maybe`. Deprecate `eitherDeserialiseFromRawBytes`. Use `deserialiseFromRawBytes` instead.
1 parent 9de2ba8 commit a30099c

File tree

24 files changed

+88
-82
lines changed

24 files changed

+88
-82
lines changed

cardano-api/ChangeLog.md

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
- **Breaking change** - Reduce exposed modules in cardano-api ([PR4546](https://github.com/input-output-hk/cardano-node/pull/4546))
1616

17+
- **Breaking change** - `deserialiseFromRawBytes` method of the `SerialiseAsRawBytes` type class to return `Either` instead of `Maybe`. Deprecate `eitherDeserialiseFromRawBytes`. Use `deserialiseFromRawBytes` instead.
18+
1719
### Bugs
1820

1921
- Allow reading text envelopes from pipes ([PR 4384](https://github.com/input-output-hk/cardano-node/pull/4384))

cardano-api/src/Cardano/Api.hs

+1
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,7 @@ module Cardano.Api (
472472
-- | Some types have a natural raw binary format.
473473
SerialiseAsRawBytes,
474474
serialiseToRawBytes,
475+
deserialiseFromRawBytes,
475476
eitherDeserialiseFromRawBytes,
476477
serialiseToRawBytesHex,
477478
deserialiseFromRawBytesHex,

cardano-api/src/Cardano/Api/Address.hs

+7-7
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ instance SerialiseAsRawBytes (Address ByronAddr) where
220220
. Shelley.BootstrapAddress
221221
$ addr
222222

223-
eitherDeserialiseFromRawBytes (AsAddress AsByronAddr) bs =
223+
deserialiseFromRawBytes (AsAddress AsByronAddr) bs =
224224
case Shelley.deserialiseAddr bs :: Maybe (Shelley.Addr StandardCrypto) of
225225
Nothing -> Left (SerialiseAsRawBytesError "Unable to deserialise Address ByronAddr")
226226
Just Shelley.Addr{} -> Left (SerialiseAsRawBytesError "Unable to deserialise Address ByronAddr")
@@ -231,7 +231,7 @@ instance SerialiseAsRawBytes (Address ShelleyAddr) where
231231
serialiseToRawBytes (ShelleyAddress nw pc scr) =
232232
Shelley.serialiseAddr (Shelley.Addr nw pc scr)
233233

234-
eitherDeserialiseFromRawBytes (AsAddress AsShelleyAddr) bs =
234+
deserialiseFromRawBytes (AsAddress AsShelleyAddr) bs =
235235
case Shelley.deserialiseAddr bs of
236236
Nothing -> Left (SerialiseAsRawBytesError "Unable to deserialise bootstrap Address ShelleyAddr")
237237
Just Shelley.AddrBootstrap{} -> Left (SerialiseAsRawBytesError "Unable to deserialise bootstrap Address ShelleyAddr")
@@ -253,7 +253,7 @@ instance SerialiseAddress (Address ByronAddr) where
253253

254254
deserialiseAddress (AsAddress AsByronAddr) txt = do
255255
bs <- Base58.decodeBase58 Base58.bitcoinAlphabet (Text.encodeUtf8 txt)
256-
rightToMaybe (eitherDeserialiseFromRawBytes (AsAddress AsByronAddr) bs)
256+
rightToMaybe (deserialiseFromRawBytes (AsAddress AsByronAddr) bs)
257257

258258
instance SerialiseAddress (Address ShelleyAddr) where
259259
serialiseAddress addr@ShelleyAddress{} =
@@ -326,7 +326,7 @@ instance SerialiseAsRawBytes AddressAny where
326326
serialiseToRawBytes (AddressByron addr) = serialiseToRawBytes addr
327327
serialiseToRawBytes (AddressShelley addr) = serialiseToRawBytes addr
328328

329-
eitherDeserialiseFromRawBytes AsAddressAny bs =
329+
deserialiseFromRawBytes AsAddressAny bs =
330330
case Shelley.deserialiseAddr bs of
331331
Nothing -> Left (SerialiseAsRawBytesError "Unable to deserialise AddressAny")
332332
Just (Shelley.AddrBootstrap (Shelley.BootstrapAddress addr)) ->
@@ -452,9 +452,9 @@ instance (IsCardanoEra era, Typeable era) => SerialiseAsRawBytes (AddressInEra e
452452
serialiseToRawBytes (AddressInEra ShelleyAddressInEra{} addr) =
453453
serialiseToRawBytes addr
454454

455-
eitherDeserialiseFromRawBytes _ bs =
455+
deserialiseFromRawBytes _ bs =
456456
first (const (SerialiseAsRawBytesError "Unable to deserialise AddressInEra era")) $
457-
anyAddressInEra cardanoEra =<< first unSerialiseAsRawBytesError (eitherDeserialiseFromRawBytes AsAddressAny bs)
457+
anyAddressInEra cardanoEra =<< first unSerialiseAsRawBytesError (deserialiseFromRawBytes AsAddressAny bs)
458458

459459
instance IsCardanoEra era => SerialiseAddress (AddressInEra era) where
460460
serialiseAddress (AddressInEra ByronAddressInAnyEra addr) =
@@ -571,7 +571,7 @@ instance SerialiseAsRawBytes StakeAddress where
571571
serialiseToRawBytes (StakeAddress nw sc) =
572572
Shelley.serialiseRewardAcnt (Shelley.RewardAcnt nw sc)
573573

574-
eitherDeserialiseFromRawBytes AsStakeAddress bs =
574+
deserialiseFromRawBytes AsStakeAddress bs =
575575
case Shelley.deserialiseRewardAcnt bs of
576576
Nothing -> Left (SerialiseAsRawBytesError "Unable to deserialise StakeAddress")
577577
Just (Shelley.RewardAcnt nw sc) -> Right (StakeAddress nw sc)

cardano-api/src/Cardano/Api/Block.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ newtype instance Hash BlockHeader = HeaderHash SBS.ShortByteString
291291
instance SerialiseAsRawBytes (Hash BlockHeader) where
292292
serialiseToRawBytes (HeaderHash bs) = SBS.fromShort bs
293293

294-
eitherDeserialiseFromRawBytes (AsHash AsBlockHeader) bs
294+
deserialiseFromRawBytes (AsHash AsBlockHeader) bs
295295
| BS.length bs == 32 = Right $! HeaderHash (SBS.toShort bs)
296296
| otherwise = Left (SerialiseAsRawBytesError "Unable to deserialise Hash BlockHeader")
297297

cardano-api/src/Cardano/Api/Keys/Byron.hs

+6-6
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,15 @@ instance SerialiseAsRawBytes (VerificationKey ByronKey) where
142142
serialiseToRawBytes (ByronVerificationKey (Byron.VerificationKey xvk)) =
143143
Crypto.HD.unXPub xvk
144144

145-
eitherDeserialiseFromRawBytes (AsVerificationKey AsByronKey) bs =
145+
deserialiseFromRawBytes (AsVerificationKey AsByronKey) bs =
146146
first (\msg -> SerialiseAsRawBytesError ("Unable to deserialise VerificationKey ByronKey" ++ msg)) $
147147
ByronVerificationKey . Byron.VerificationKey <$> Crypto.HD.xpub bs
148148

149149
instance SerialiseAsRawBytes (SigningKey ByronKey) where
150150
serialiseToRawBytes (ByronSigningKey (Byron.SigningKey xsk)) =
151151
toStrictByteString $ Crypto.toCBORXPrv xsk
152152

153-
eitherDeserialiseFromRawBytes (AsSigningKey AsByronKey) bs =
153+
deserialiseFromRawBytes (AsSigningKey AsByronKey) bs =
154154
first (\e -> SerialiseAsRawBytesError ("Unable to deserialise SigningKey ByronKey" ++ show e)) $
155155
ByronSigningKey . Byron.SigningKey . snd <$> CBOR.deserialiseFromBytes Byron.fromCBORXPrv (LB.fromStrict bs)
156156

@@ -164,7 +164,7 @@ instance SerialiseAsRawBytes (Hash ByronKey) where
164164
serialiseToRawBytes (ByronKeyHash (Byron.KeyHash vkh)) =
165165
Byron.abstractHashToBytes vkh
166166

167-
eitherDeserialiseFromRawBytes (AsHash AsByronKey) bs =
167+
deserialiseFromRawBytes (AsHash AsByronKey) bs =
168168
maybeToRight (SerialiseAsRawBytesError "Unable to deserialise Hash ByronKey") $
169169
ByronKeyHash . Byron.KeyHash <$> Byron.abstractHashFromBytes bs
170170

@@ -236,23 +236,23 @@ instance SerialiseAsRawBytes (Hash ByronKeyLegacy) where
236236
serialiseToRawBytes (ByronKeyHashLegacy (Byron.KeyHash vkh)) =
237237
Byron.abstractHashToBytes vkh
238238

239-
eitherDeserialiseFromRawBytes (AsHash AsByronKeyLegacy) bs =
239+
deserialiseFromRawBytes (AsHash AsByronKeyLegacy) bs =
240240
maybeToRight (SerialiseAsRawBytesError "Unable to deserialise Hash ByronKeyLegacy") $
241241
ByronKeyHashLegacy . Byron.KeyHash <$> Byron.abstractHashFromBytes bs
242242

243243
instance SerialiseAsRawBytes (VerificationKey ByronKeyLegacy) where
244244
serialiseToRawBytes (ByronVerificationKeyLegacy (Byron.VerificationKey xvk)) =
245245
Crypto.HD.unXPub xvk
246246

247-
eitherDeserialiseFromRawBytes (AsVerificationKey AsByronKeyLegacy) bs =
247+
deserialiseFromRawBytes (AsVerificationKey AsByronKeyLegacy) bs =
248248
first (\msg -> SerialiseAsRawBytesError ("Unable to deserialise VerificationKey ByronKeyLegacy" ++ msg)) $
249249
ByronVerificationKeyLegacy . Byron.VerificationKey <$> Crypto.HD.xpub bs
250250

251251
instance SerialiseAsRawBytes (SigningKey ByronKeyLegacy) where
252252
serialiseToRawBytes (ByronSigningKeyLegacy (Byron.SigningKey xsk)) =
253253
Crypto.HD.unXPrv xsk
254254

255-
eitherDeserialiseFromRawBytes (AsSigningKey AsByronKeyLegacy) bs =
255+
deserialiseFromRawBytes (AsSigningKey AsByronKeyLegacy) bs =
256256
first (\e -> SerialiseAsRawBytesError ("Unable to deserialise SigningKey ByronKeyLegacy" ++ show e)) $
257257
ByronSigningKeyLegacy . snd <$> CBOR.deserialiseFromBytes decodeLegacyDelegateKey (LB.fromStrict bs)
258258
where

cardano-api/src/Cardano/Api/Keys/Class.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ generateInsecureSigningKey
7575
-> IO (SigningKey keyrole, StdGen)
7676
generateInsecureSigningKey g keytype = do
7777
let (bs, g') = Random.genByteString (fromIntegral $ deterministicSigningKeySeedSize keytype) g
78-
case eitherDeserialiseFromRawBytes (AsSigningKey keytype) bs of
78+
case deserialiseFromRawBytes (AsSigningKey keytype) bs of
7979
Right key -> return (key, g')
8080
Left (SerialiseAsRawBytesError msg) -> error $ "generateInsecureSigningKey: Unable to generate insecure key: " <> msg
8181

cardano-api/src/Cardano/Api/Keys/Praos.hs

+6-6
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,15 @@ instance SerialiseAsRawBytes (VerificationKey KesKey) where
9797
serialiseToRawBytes (KesVerificationKey vk) =
9898
Crypto.rawSerialiseVerKeyKES vk
9999

100-
eitherDeserialiseFromRawBytes (AsVerificationKey AsKesKey) bs =
100+
deserialiseFromRawBytes (AsVerificationKey AsKesKey) bs =
101101
maybeToRight (SerialiseAsRawBytesError "Unable to deserialise VerificationKey KesKey") $
102102
KesVerificationKey <$> Crypto.rawDeserialiseVerKeyKES bs
103103

104104
instance SerialiseAsRawBytes (SigningKey KesKey) where
105105
serialiseToRawBytes (KesSigningKey sk) =
106106
Crypto.rawSerialiseSignKeyKES sk
107107

108-
eitherDeserialiseFromRawBytes (AsSigningKey AsKesKey) bs =
108+
deserialiseFromRawBytes (AsSigningKey AsKesKey) bs =
109109
maybeToRight (SerialiseAsRawBytesError "Unable to deserialise SigningKey KesKey") $
110110
KesSigningKey <$> Crypto.rawDeserialiseSignKeyKES bs
111111

@@ -130,7 +130,7 @@ instance SerialiseAsRawBytes (Hash KesKey) where
130130
serialiseToRawBytes (KesKeyHash vkh) =
131131
Crypto.hashToBytes vkh
132132

133-
eitherDeserialiseFromRawBytes (AsHash AsKesKey) bs =
133+
deserialiseFromRawBytes (AsHash AsKesKey) bs =
134134
maybeToRight (SerialiseAsRawBytesError "Unable to deserialise Hash KesKey") $
135135
KesKeyHash <$> Crypto.hashFromBytes bs
136136

@@ -204,15 +204,15 @@ instance SerialiseAsRawBytes (VerificationKey VrfKey) where
204204
serialiseToRawBytes (VrfVerificationKey vk) =
205205
Crypto.rawSerialiseVerKeyVRF vk
206206

207-
eitherDeserialiseFromRawBytes (AsVerificationKey AsVrfKey) bs =
207+
deserialiseFromRawBytes (AsVerificationKey AsVrfKey) bs =
208208
maybeToRight (SerialiseAsRawBytesError "Unable to deserialise VerificationKey VrfKey") $
209209
VrfVerificationKey <$> Crypto.rawDeserialiseVerKeyVRF bs
210210

211211
instance SerialiseAsRawBytes (SigningKey VrfKey) where
212212
serialiseToRawBytes (VrfSigningKey sk) =
213213
Crypto.rawSerialiseSignKeyVRF sk
214214

215-
eitherDeserialiseFromRawBytes (AsSigningKey AsVrfKey) bs =
215+
deserialiseFromRawBytes (AsSigningKey AsVrfKey) bs =
216216
maybeToRight (SerialiseAsRawBytesError "Unable to deserialise SigningKey VrfKey") $
217217
VrfSigningKey <$> Crypto.rawDeserialiseSignKeyVRF bs
218218

@@ -236,7 +236,7 @@ instance SerialiseAsRawBytes (Hash VrfKey) where
236236
serialiseToRawBytes (VrfKeyHash vkh) =
237237
Crypto.hashToBytes vkh
238238

239-
eitherDeserialiseFromRawBytes (AsHash AsVrfKey) bs =
239+
deserialiseFromRawBytes (AsHash AsVrfKey) bs =
240240
maybeToRight (SerialiseAsRawBytesError "Unable to deserialise Hash VrfKey") $
241241
VrfKeyHash <$> Crypto.hashFromBytes bs
242242

0 commit comments

Comments
 (0)