Skip to content

Commit d68d3b5

Browse files
committed
Add backwards compatibility to ToCBOR AnyStakePoolVerificationKey instance
1 parent f0be7ac commit d68d3b5

File tree

1 file changed

+15
-8
lines changed
  • cardano-api/src/Cardano/Api/Internal/Keys

1 file changed

+15
-8
lines changed

cardano-api/src/Cardano/Api/Internal/Keys/Shelley.hs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
{-# LANGUAGE GADTs #-}
77
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
88
{-# LANGUAGE InstanceSigs #-}
9+
{-# LANGUAGE LambdaCase #-}
910
{-# LANGUAGE MultiParamTypeClasses #-}
1011
{-# LANGUAGE RankNTypes #-}
1112
{-# LANGUAGE ScopedTypeVariables #-}
@@ -69,8 +70,8 @@ import Cardano.Ledger.Crypto (StandardCrypto)
6970
import Cardano.Ledger.Crypto qualified as Shelley (DSIGN)
7071
import Cardano.Ledger.Keys qualified as Shelley
7172

72-
import Codec.CBOR.Decoding (decodeListLenOf)
73-
import Codec.CBOR.Encoding (encodeListLen)
73+
import Codec.CBOR.Decoding (Decoder, TokenType (TypeListLen), decodeListLenOf, peekTokenType)
74+
import Codec.CBOR.Encoding (Encoding, encodeListLen)
7475
import Data.Aeson.Types
7576
( ToJSONKey (..)
7677
, toJSONKeyText
@@ -1685,19 +1686,25 @@ data AnyStakePoolVerificationKey
16851686
deriving (Show, Eq)
16861687

16871688
instance ToCBOR AnyStakePoolVerificationKey where
1689+
toCBOR :: AnyStakePoolVerificationKey -> Encoding
16881690
toCBOR (AnyStakePoolNormalVerificationKey vk) =
16891691
encodeListLen 2 <> toCBOR (0 :: Word8) <> toCBOR vk
16901692
toCBOR (AnyStakePoolExtendedVerificationKey vk) =
16911693
encodeListLen 2 <> toCBOR (1 :: Word8) <> toCBOR vk
16921694

16931695
instance FromCBOR AnyStakePoolVerificationKey where
1696+
fromCBOR :: Decoder s AnyStakePoolVerificationKey
16941697
fromCBOR =
1695-
decodeListLenOf 2 >> do
1696-
tag <- fromCBOR
1697-
case tag of
1698-
0 -> AnyStakePoolNormalVerificationKey <$> fromCBOR
1699-
1 -> AnyStakePoolExtendedVerificationKey <$> fromCBOR
1700-
_ -> cborError $ DecoderErrorUnknownTag "AnyStakePoolVerificationKey" tag
1698+
peekTokenType >>= \case
1699+
TypeListLen ->
1700+
decodeListLenOf 2 >> do
1701+
tag <- fromCBOR
1702+
case tag of
1703+
0 -> AnyStakePoolNormalVerificationKey <$> fromCBOR
1704+
1 -> AnyStakePoolExtendedVerificationKey <$> fromCBOR
1705+
_ -> cborError $ DecoderErrorUnknownTag "AnyStakePoolVerificationKey" tag
1706+
-- This case is for backwards compatibility (with CBOR encoding that doesn't support extended keys)
1707+
_ -> AnyStakePoolNormalVerificationKey <$> fromCBOR
17011708

17021709
anyStakePoolVerificationKeyHash :: AnyStakePoolVerificationKey -> Hash StakePoolKey
17031710
anyStakePoolVerificationKeyHash (AnyStakePoolNormalVerificationKey vk) = verificationKeyHash vk

0 commit comments

Comments
 (0)