Skip to content

Commit

Permalink
Introduce new SerialiseAsRawBytesError error type
Browse files Browse the repository at this point in the history
  • Loading branch information
newhoggy committed Nov 15, 2022
1 parent 973e9d6 commit 02ea740
Show file tree
Hide file tree
Showing 16 changed files with 76 additions and 69 deletions.
16 changes: 8 additions & 8 deletions cardano-api/src/Cardano/Api/Address.hs
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,8 @@ instance SerialiseAsRawBytes (Address ByronAddr) where

eitherDeserialiseFromRawBytes (AsAddress AsByronAddr) bs =
case Shelley.deserialiseAddr bs :: Maybe (Shelley.Addr StandardCrypto) of
Nothing -> Left "Unable to deserialise Address ByronAddr"
Just Shelley.Addr{} -> Left "Unable to deserialise Address ByronAddr"
Nothing -> Left (SerialiseAsRawBytesError "Unable to deserialise Address ByronAddr")
Just Shelley.Addr{} -> Left (SerialiseAsRawBytesError "Unable to deserialise Address ByronAddr")
Just (Shelley.AddrBootstrap (Shelley.BootstrapAddress addr)) ->
Right (ByronAddress addr)

Expand All @@ -236,8 +236,8 @@ instance SerialiseAsRawBytes (Address ShelleyAddr) where

eitherDeserialiseFromRawBytes (AsAddress AsShelleyAddr) bs =
case Shelley.deserialiseAddr bs of
Nothing -> Left "Unable to deserialise Address ShelleyAddr"
Just Shelley.AddrBootstrap{} -> Left "Unable to deserialise Address ShelleyAddr"
Nothing -> Left (SerialiseAsRawBytesError "Unable to deserialise Address ShelleyAddr")
Just Shelley.AddrBootstrap{} -> Left (SerialiseAsRawBytesError "Unable to deserialise Address ShelleyAddr")
Just (Shelley.Addr nw pc scr) -> Right (ShelleyAddress nw pc scr)

instance SerialiseAsBech32 (Address ShelleyAddr) where
Expand Down Expand Up @@ -331,7 +331,7 @@ instance SerialiseAsRawBytes AddressAny where

eitherDeserialiseFromRawBytes AsAddressAny bs =
case Shelley.deserialiseAddr bs of
Nothing -> Left "Unable to deserialise AddressAny"
Nothing -> Left (SerialiseAsRawBytesError "Unable to deserialise AddressAny")
Just (Shelley.AddrBootstrap (Shelley.BootstrapAddress addr)) ->
Right (AddressByron (ByronAddress addr))

Expand Down Expand Up @@ -456,8 +456,8 @@ instance (IsCardanoEra era, Typeable era) => SerialiseAsRawBytes (AddressInEra e
serialiseToRawBytes addr

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

instance IsCardanoEra era => SerialiseAddress (AddressInEra era) where
serialiseAddress (AddressInEra ByronAddressInAnyEra addr) =
Expand Down Expand Up @@ -576,7 +576,7 @@ instance SerialiseAsRawBytes StakeAddress where

eitherDeserialiseFromRawBytes AsStakeAddress bs =
case Shelley.deserialiseRewardAcnt bs of
Nothing -> Left "Unable to deserialise StakeAddress"
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 @@ -295,7 +295,7 @@ instance SerialiseAsRawBytes (Hash BlockHeader) where

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

instance HasTypeProxy BlockHeader where
data AsType BlockHeader = AsBlockHeader
Expand Down
2 changes: 1 addition & 1 deletion cardano-api/src/Cardano/Api/Key.hs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ generateInsecureSigningKey g keytype = do
let (bs, g') = Random.genByteString (fromIntegral $ deterministicSigningKeySeedSize keytype) g
case eitherDeserialiseFromRawBytes (AsSigningKey keytype) bs of
Right key -> return (key, g')
Left msg -> error $ "generateInsecureSigningKey: Unable to generate insecure key: " <> msg
Left (SerialiseAsRawBytesError msg) -> error $ "generateInsecureSigningKey: Unable to generate insecure key: " <> msg

instance HasTypeProxy a => HasTypeProxy (VerificationKey a) where
data AsType (VerificationKey a) = AsVerificationKey (AsType a)
Expand Down
12 changes: 6 additions & 6 deletions cardano-api/src/Cardano/Api/KeysByron.hs
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,15 @@ instance SerialiseAsRawBytes (VerificationKey ByronKey) where
Crypto.HD.unXPub xvk

eitherDeserialiseFromRawBytes (AsVerificationKey AsByronKey) bs =
first ("Unable to deserialise VerificationKey ByronKey" ++) $
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 =
first (\e -> "Unable to deserialise SigningKey ByronKey" ++ show e) $
first (\e -> SerialiseAsRawBytesError ("Unable to deserialise SigningKey ByronKey" ++ show e)) $
ByronSigningKey . Byron.SigningKey . snd <$> CBOR.deserialiseFromBytes Byron.fromCBORXPrv (LB.fromStrict bs)

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

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

instance CastVerificationKeyRole ByronKey PaymentExtendedKey where
Expand Down Expand Up @@ -237,23 +237,23 @@ instance SerialiseAsRawBytes (Hash ByronKeyLegacy) where
Byron.abstractHashToBytes vkh

eitherDeserialiseFromRawBytes (AsHash AsByronKeyLegacy) bs =
maybeToRight "Unable to deserialise Hash ByronKeyLegacy" $
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 =
first ("Unable to deserialise VerificationKey ByronKeyLegacy" ++) $
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 =
first (\e -> "Unable to deserialise SigningKey ByronKeyLegacy" ++ show e) $
first (\e -> SerialiseAsRawBytesError ("Unable to deserialise SigningKey ByronKeyLegacy" ++ show e)) $
ByronSigningKeyLegacy . snd <$> CBOR.deserialiseFromBytes decodeLegacyDelegateKey (LB.fromStrict bs)
where
-- Stolen from: cardano-sl/binary/src/Pos/Binary/Class/Core.hs
Expand Down
12 changes: 6 additions & 6 deletions cardano-api/src/Cardano/Api/KeysPraos.hs
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@ instance SerialiseAsRawBytes (VerificationKey KesKey) where
Crypto.rawSerialiseVerKeyKES vk

eitherDeserialiseFromRawBytes (AsVerificationKey AsKesKey) bs =
maybeToRight "Unable to deserialise VerificationKey KesKey" $
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 =
maybeToRight "Unable to deserialise SigningKey KesKey" $
maybeToRight (SerialiseAsRawBytesError "Unable to deserialise SigningKey KesKey") $
KesSigningKey <$> Crypto.rawDeserialiseSignKeyKES bs

instance SerialiseAsBech32 (VerificationKey KesKey) where
Expand All @@ -129,7 +129,7 @@ instance SerialiseAsRawBytes (Hash KesKey) where
Crypto.hashToBytes vkh

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

instance HasTextEnvelope (VerificationKey KesKey) where
Expand Down Expand Up @@ -196,15 +196,15 @@ instance SerialiseAsRawBytes (VerificationKey VrfKey) where
Crypto.rawSerialiseVerKeyVRF vk

eitherDeserialiseFromRawBytes (AsVerificationKey AsVrfKey) bs =
maybeToRight "Unable to deserialise VerificationKey VrfKey" $
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 =
maybeToRight "Unable to deserialise SigningKey VrfKey" $
maybeToRight (SerialiseAsRawBytesError "Unable to deserialise SigningKey VrfKey") $
VrfSigningKey <$> Crypto.rawDeserialiseSignKeyVRF bs

instance SerialiseAsBech32 (VerificationKey VrfKey) where
Expand All @@ -228,7 +228,7 @@ instance SerialiseAsRawBytes (Hash VrfKey) where
Crypto.hashToBytes vkh

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

instance HasTextEnvelope (VerificationKey VrfKey) where
Expand Down
Loading

0 comments on commit 02ea740

Please sign in to comment.