99{-# LANGUAGE MultiParamTypeClasses #-}
1010{-# LANGUAGE RankNTypes #-}
1111{-# LANGUAGE ScopedTypeVariables #-}
12- {-# LANGUAGE StandaloneDeriving #-}
1312{-# LANGUAGE TypeFamilies #-}
14- {-# LANGUAGE TypeOperators #-}
1513{-# LANGUAGE UndecidableInstances #-}
1614-- The Shelley ledger uses promoted data kinds which we have to use, but we do
1715-- not export any from this API. We also use them unticked as nature intended.
@@ -43,13 +41,10 @@ module Cardano.Api.Internal.Keys.Shelley
4341 , VerificationKey (.. )
4442 , SigningKey (.. )
4543 , Hash (.. )
46- , AnyStakePoolKeyWrapper (.. )
47- , rewrapAnyStakePoolKey
48- , foldStakePoolKey
49- , liftStakePoolKey
50- , liftStakePoolKeyM
51- , unStakePoolAnyKeyHash
52- , castHashToNormal
44+ , AnyStakePoolVerificationKey (.. )
45+ , anyStakePoolVerificationKeyHash
46+ , AnyStakePoolSigningKey (.. )
47+ , anyStakePoolSigningKeyToVerificationKey
5348 )
5449where
5550
@@ -65,6 +60,7 @@ import Cardano.Api.Internal.SerialiseRaw
6560import Cardano.Api.Internal.SerialiseTextEnvelope
6661import Cardano.Api.Internal.SerialiseUsing
6762
63+ import Cardano.Binary (DecoderError (DecoderErrorUnknownTag ), cborError )
6864import Cardano.Crypto.DSIGN.Class qualified as Crypto
6965import Cardano.Crypto.Hash.Class qualified as Crypto
7066import Cardano.Crypto.Seed qualified as Crypto
@@ -73,6 +69,8 @@ import Cardano.Ledger.Crypto (StandardCrypto)
7369import Cardano.Ledger.Crypto qualified as Shelley (DSIGN )
7470import Cardano.Ledger.Keys qualified as Shelley
7571
72+ import Codec.CBOR.Decoding (decodeListLenOf )
73+ import Codec.CBOR.Encoding (encodeListLen )
7674import Data.Aeson.Types
7775 ( ToJSONKey (.. )
7876 , toJSONKeyText
@@ -84,6 +82,7 @@ import Data.ByteString qualified as BS
8482import Data.Either.Combinators (maybeToRight )
8583import Data.Maybe
8684import Data.String (IsString (.. ))
85+ import Data.Word (Word8 )
8786
8887--
8988-- Shelley payment keys
@@ -1679,103 +1678,43 @@ instance CastSigningKeyRole GenesisUTxOKey PaymentKey where
16791678-- stake pool keys
16801679--
16811680
1682- -- | Wrapper that handles both normal and extended StakePoolKeys and hides the type with an existential
1683- data AnyStakePoolKeyWrapper t
1684- = forall x . StakePoolKey ~ x => StakePoolNormalKeyWrapper (t x )
1685- | forall x . StakePoolExtendedKey ~ x => StakePoolExtendedKeyWrapper (t x )
1686-
1687- instance
1688- (Eq (t StakePoolKey ), Eq (t StakePoolExtendedKey ))
1689- => Eq (AnyStakePoolKeyWrapper t )
1690- where
1691- (==) :: AnyStakePoolKeyWrapper t -> AnyStakePoolKeyWrapper t -> Bool
1692- (==) (StakePoolNormalKeyWrapper x) (StakePoolNormalKeyWrapper y) = x == y
1693- (==) (StakePoolExtendedKeyWrapper x) (StakePoolExtendedKeyWrapper y) = x == y
1694- (==) _ _ = False
1695-
1696- instance
1697- (Ord (t StakePoolKey ), Ord (t StakePoolExtendedKey ))
1698- => Ord (AnyStakePoolKeyWrapper t )
1699- where
1700- compare :: AnyStakePoolKeyWrapper t -> AnyStakePoolKeyWrapper t -> Ordering
1701- compare (StakePoolNormalKeyWrapper x) (StakePoolNormalKeyWrapper y) = compare x y
1702- compare (StakePoolExtendedKeyWrapper x) (StakePoolExtendedKeyWrapper y) = compare x y
1703- compare (StakePoolNormalKeyWrapper _) (StakePoolExtendedKeyWrapper _) = LT
1704- compare (StakePoolExtendedKeyWrapper _) (StakePoolNormalKeyWrapper _) = GT
1705-
1706- instance
1707- (Show (t StakePoolKey ), (Show (t StakePoolExtendedKey )))
1708- => Show (AnyStakePoolKeyWrapper t )
1709- where
1710- show (StakePoolNormalKeyWrapper x) = show x
1711- show (StakePoolExtendedKeyWrapper x) = show x
1712-
1713- instance ToCBOR (AnyStakePoolKeyWrapper VerificationKey ) where
1714- toCBOR (StakePoolNormalKeyWrapper x) = toCBOR x
1715- toCBOR (StakePoolExtendedKeyWrapper x) = toCBOR x
1716-
1717- instance FromCBOR (AnyStakePoolKeyWrapper VerificationKey ) where
1718- fromCBOR = undefined -- FixMe: implement this
1719-
1720- rewrapAnyStakePoolKey
1721- :: (forall x . t x -> f x )
1722- -> AnyStakePoolKeyWrapper t
1723- -> AnyStakePoolKeyWrapper f
1724- rewrapAnyStakePoolKey f x = liftStakePoolKey x (const f)
1725-
1726- foldStakePoolKey
1727- :: AnyStakePoolKeyWrapper t
1728- -> ( forall a
1729- . ( Key a
1730- , SerialiseAsBech32
1731- (VerificationKey a )
1732- , ToJSON (Hash a )
1733- )
1734- => AsType a -> t a -> f
1735- )
1736- -> f
1737- foldStakePoolKey (StakePoolNormalKeyWrapper x) f = f AsStakePoolKey x
1738- foldStakePoolKey (StakePoolExtendedKeyWrapper x) f = f AsStakePoolExtendedKey x
1739-
1740- liftStakePoolKey
1741- :: AnyStakePoolKeyWrapper t
1742- -> ( forall a
1743- . ( Key a
1744- , SerialiseAsBech32
1745- (VerificationKey a )
1746- , HasTypeProxy a
1747- )
1748- => AsType a -> t a -> f a
1749- )
1750- -> AnyStakePoolKeyWrapper f
1751- liftStakePoolKey (StakePoolNormalKeyWrapper x) f = StakePoolNormalKeyWrapper $ f AsStakePoolKey x
1752- liftStakePoolKey (StakePoolExtendedKeyWrapper x) f = StakePoolExtendedKeyWrapper $ f AsStakePoolExtendedKey x
1753-
1754- liftStakePoolKeyM
1755- :: Applicative g
1756- => AnyStakePoolKeyWrapper t
1757- -> ( forall a
1758- . ( Key a
1759- , SerialiseAsBech32
1760- (VerificationKey a )
1761- , HasTypeProxy a
1762- )
1763- => AsType a -> t a -> g (f a )
1764- )
1765- -> g (AnyStakePoolKeyWrapper f )
1766- liftStakePoolKeyM (StakePoolNormalKeyWrapper x) f = do
1767- StakePoolNormalKeyWrapper <$> f AsStakePoolKey x
1768- liftStakePoolKeyM (StakePoolExtendedKeyWrapper x) f = do
1769- StakePoolExtendedKeyWrapper <$> f AsStakePoolExtendedKey x
1770-
1771- castHashToNormal :: AnyStakePoolKeyWrapper Hash -> Hash StakePoolKey
1772- castHashToNormal (StakePoolNormalKeyWrapper x) = x
1773- castHashToNormal (StakePoolExtendedKeyWrapper (StakePoolExtendedKeyHash x)) = StakePoolKeyHash x
1774-
1775- unStakePoolAnyKeyHash
1776- :: AnyStakePoolKeyWrapper Hash -> Shelley. KeyHash Shelley. StakePool StandardCrypto
1777- unStakePoolAnyKeyHash (StakePoolNormalKeyWrapper (StakePoolKeyHash spkh)) = spkh
1778- unStakePoolAnyKeyHash (StakePoolExtendedKeyWrapper (StakePoolExtendedKeyHash spkh)) = spkh
1681+ -- | Wrapper that handles both normal and extended StakePoolKeys VerificationKeys
1682+ data AnyStakePoolVerificationKey
1683+ = AnyStakePoolNormalVerificationKey (VerificationKey StakePoolKey )
1684+ | AnyStakePoolExtendedVerificationKey (VerificationKey StakePoolExtendedKey )
1685+ deriving (Show , Eq )
1686+
1687+ instance ToCBOR AnyStakePoolVerificationKey where
1688+ toCBOR (AnyStakePoolNormalVerificationKey vk) =
1689+ encodeListLen 2 <> toCBOR (0 :: Word8 ) <> toCBOR vk
1690+ toCBOR (AnyStakePoolExtendedVerificationKey vk) =
1691+ encodeListLen 2 <> toCBOR (1 :: Word8 ) <> toCBOR vk
1692+
1693+ instance FromCBOR AnyStakePoolVerificationKey where
1694+ 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
1701+
1702+ anyStakePoolVerificationKeyHash :: AnyStakePoolVerificationKey -> Hash StakePoolKey
1703+ anyStakePoolVerificationKeyHash (AnyStakePoolNormalVerificationKey vk) = verificationKeyHash vk
1704+ anyStakePoolVerificationKeyHash (AnyStakePoolExtendedVerificationKey vk) =
1705+ let StakePoolExtendedKeyHash hash = verificationKeyHash vk in StakePoolKeyHash hash
1706+
1707+ -- | Wrapper that handles both normal and extended StakePoolKeys SigningKeys
1708+ data AnyStakePoolSigningKey
1709+ = AnyStakePoolNormalSigningKey (SigningKey StakePoolKey )
1710+ | AnyStakePoolExtendedSigningKey (SigningKey StakePoolExtendedKey )
1711+ deriving Show
1712+
1713+ anyStakePoolSigningKeyToVerificationKey :: AnyStakePoolSigningKey -> AnyStakePoolVerificationKey
1714+ anyStakePoolSigningKeyToVerificationKey (AnyStakePoolNormalSigningKey sk) =
1715+ AnyStakePoolNormalVerificationKey (getVerificationKey sk)
1716+ anyStakePoolSigningKeyToVerificationKey (AnyStakePoolExtendedSigningKey vk) =
1717+ AnyStakePoolExtendedVerificationKey (getVerificationKey vk)
17791718
17801719data StakePoolKey
17811720
0 commit comments