Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify SerialiseAsRawBytes type class #4876

Merged
merged 1 commit into from
Feb 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cardano-api/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

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

- **Breaking change** - `deserialiseFromRawBytes` method of the `SerialiseAsRawBytes` type class to return `Either` instead of `Maybe`. Deprecate `eitherDeserialiseFromRawBytes`. Use `deserialiseFromRawBytes` instead.

### Bugs

- Allow reading text envelopes from pipes ([PR 4384](https://github.com/input-output-hk/cardano-node/pull/4384))
Expand Down
1 change: 1 addition & 0 deletions cardano-api/src/Cardano/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ module Cardano.Api (
-- | Some types have a natural raw binary format.
SerialiseAsRawBytes,
serialiseToRawBytes,
deserialiseFromRawBytes,
eitherDeserialiseFromRawBytes,
serialiseToRawBytesHex,
deserialiseFromRawBytesHex,
Expand Down
14 changes: 7 additions & 7 deletions cardano-api/src/Cardano/Api/Address.hs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ instance SerialiseAsRawBytes (Address ByronAddr) where
. Shelley.BootstrapAddress
$ addr

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

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

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

instance SerialiseAddress (Address ShelleyAddr) where
serialiseAddress addr@ShelleyAddress{} =
Expand Down Expand Up @@ -326,7 +326,7 @@ instance SerialiseAsRawBytes AddressAny where
serialiseToRawBytes (AddressByron addr) = serialiseToRawBytes addr
serialiseToRawBytes (AddressShelley addr) = serialiseToRawBytes addr

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

eitherDeserialiseFromRawBytes _ bs =
deserialiseFromRawBytes _ bs =
first (const (SerialiseAsRawBytesError "Unable to deserialise AddressInEra era")) $
anyAddressInEra cardanoEra =<< first unSerialiseAsRawBytesError (eitherDeserialiseFromRawBytes AsAddressAny bs)
anyAddressInEra cardanoEra =<< first unSerialiseAsRawBytesError (deserialiseFromRawBytes AsAddressAny bs)

instance IsCardanoEra era => SerialiseAddress (AddressInEra era) where
serialiseAddress (AddressInEra ByronAddressInAnyEra addr) =
Expand Down Expand Up @@ -571,7 +571,7 @@ instance SerialiseAsRawBytes StakeAddress where
serialiseToRawBytes (StakeAddress nw sc) =
Shelley.serialiseRewardAcnt (Shelley.RewardAcnt nw sc)

eitherDeserialiseFromRawBytes AsStakeAddress bs =
deserialiseFromRawBytes AsStakeAddress bs =
case Shelley.deserialiseRewardAcnt bs of
Nothing -> Left (SerialiseAsRawBytesError "Unable to deserialise StakeAddress")
Just (Shelley.RewardAcnt nw sc) -> Right (StakeAddress nw sc)
Expand Down
2 changes: 1 addition & 1 deletion cardano-api/src/Cardano/Api/Block.hs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ newtype instance Hash BlockHeader = HeaderHash SBS.ShortByteString
instance SerialiseAsRawBytes (Hash BlockHeader) where
serialiseToRawBytes (HeaderHash bs) = SBS.fromShort bs

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

Expand Down
12 changes: 6 additions & 6 deletions cardano-api/src/Cardano/Api/Keys/Byron.hs
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,15 @@ instance SerialiseAsRawBytes (VerificationKey ByronKey) where
serialiseToRawBytes (ByronVerificationKey (Byron.VerificationKey xvk)) =
Crypto.HD.unXPub xvk

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

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

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

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

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

Expand Down Expand Up @@ -236,23 +236,23 @@ instance SerialiseAsRawBytes (Hash ByronKeyLegacy) where
serialiseToRawBytes (ByronKeyHashLegacy (Byron.KeyHash vkh)) =
Byron.abstractHashToBytes vkh

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

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

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

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

eitherDeserialiseFromRawBytes (AsSigningKey AsByronKeyLegacy) bs =
deserialiseFromRawBytes (AsSigningKey AsByronKeyLegacy) bs =
first (\e -> SerialiseAsRawBytesError ("Unable to deserialise SigningKey ByronKeyLegacy" ++ show e)) $
ByronSigningKeyLegacy . snd <$> CBOR.deserialiseFromBytes decodeLegacyDelegateKey (LB.fromStrict bs)
where
Expand Down
2 changes: 1 addition & 1 deletion cardano-api/src/Cardano/Api/Keys/Class.hs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ generateInsecureSigningKey
-> IO (SigningKey keyrole, StdGen)
generateInsecureSigningKey g keytype = do
let (bs, g') = Random.genByteString (fromIntegral $ deterministicSigningKeySeedSize keytype) g
case eitherDeserialiseFromRawBytes (AsSigningKey keytype) bs of
case deserialiseFromRawBytes (AsSigningKey keytype) bs of
Right key -> return (key, g')
Left (SerialiseAsRawBytesError msg) -> error $ "generateInsecureSigningKey: Unable to generate insecure key: " <> msg

Expand Down
12 changes: 6 additions & 6 deletions cardano-api/src/Cardano/Api/Keys/Praos.hs
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,15 @@ instance SerialiseAsRawBytes (VerificationKey KesKey) where
serialiseToRawBytes (KesVerificationKey vk) =
Crypto.rawSerialiseVerKeyKES vk

eitherDeserialiseFromRawBytes (AsVerificationKey AsKesKey) bs =
deserialiseFromRawBytes (AsVerificationKey AsKesKey) bs =
maybeToRight (SerialiseAsRawBytesError "Unable to deserialise VerificationKey KesKey") $
KesVerificationKey <$> Crypto.rawDeserialiseVerKeyKES bs

instance SerialiseAsRawBytes (SigningKey KesKey) where
serialiseToRawBytes (KesSigningKey sk) =
Crypto.rawSerialiseSignKeyKES sk

eitherDeserialiseFromRawBytes (AsSigningKey AsKesKey) bs =
deserialiseFromRawBytes (AsSigningKey AsKesKey) bs =
maybeToRight (SerialiseAsRawBytesError "Unable to deserialise SigningKey KesKey") $
KesSigningKey <$> Crypto.rawDeserialiseSignKeyKES bs

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

eitherDeserialiseFromRawBytes (AsHash AsKesKey) bs =
deserialiseFromRawBytes (AsHash AsKesKey) bs =
maybeToRight (SerialiseAsRawBytesError "Unable to deserialise Hash KesKey") $
KesKeyHash <$> Crypto.hashFromBytes bs

Expand Down Expand Up @@ -204,15 +204,15 @@ instance SerialiseAsRawBytes (VerificationKey VrfKey) where
serialiseToRawBytes (VrfVerificationKey vk) =
Crypto.rawSerialiseVerKeyVRF vk

eitherDeserialiseFromRawBytes (AsVerificationKey AsVrfKey) bs =
deserialiseFromRawBytes (AsVerificationKey AsVrfKey) bs =
maybeToRight (SerialiseAsRawBytesError "Unable to deserialise VerificationKey VrfKey") $
VrfVerificationKey <$> Crypto.rawDeserialiseVerKeyVRF bs

instance SerialiseAsRawBytes (SigningKey VrfKey) where
serialiseToRawBytes (VrfSigningKey sk) =
Crypto.rawSerialiseSignKeyVRF sk

eitherDeserialiseFromRawBytes (AsSigningKey AsVrfKey) bs =
deserialiseFromRawBytes (AsSigningKey AsVrfKey) bs =
maybeToRight (SerialiseAsRawBytesError "Unable to deserialise SigningKey VrfKey") $
VrfSigningKey <$> Crypto.rawDeserialiseSignKeyVRF bs

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

eitherDeserialiseFromRawBytes (AsHash AsVrfKey) bs =
deserialiseFromRawBytes (AsHash AsVrfKey) bs =
maybeToRight (SerialiseAsRawBytesError "Unable to deserialise Hash VrfKey") $
VrfKeyHash <$> Crypto.hashFromBytes bs

Expand Down
Loading