11{-# LANGUAGE DataKinds #-}
22{-# LANGUAGE DeriveAnyClass #-}
33{-# LANGUAGE DerivingVia #-}
4+ {-# LANGUAGE FlexibleContexts #-}
45{-# LANGUAGE FlexibleInstances #-}
6+ {-# LANGUAGE GADTs #-}
57{-# LANGUAGE GeneralizedNewtypeDeriving #-}
68{-# LANGUAGE InstanceSigs #-}
79{-# LANGUAGE MultiParamTypeClasses #-}
10+ {-# LANGUAGE RankNTypes #-}
811{-# LANGUAGE ScopedTypeVariables #-}
912{-# LANGUAGE TypeFamilies #-}
13+ {-# LANGUAGE UndecidableInstances #-}
1014-- The Shelley ledger uses promoted data kinds which we have to use, but we do
1115-- not export any from this API. We also use them unticked as nature intended.
1216{-# OPTIONS_GHC -Wno-unticked-promoted-constructors #-}
@@ -37,6 +41,10 @@ module Cardano.Api.Internal.Keys.Shelley
3741 , VerificationKey (.. )
3842 , SigningKey (.. )
3943 , Hash (.. )
44+ , AnyStakePoolVerificationKey (.. )
45+ , anyStakePoolVerificationKeyHash
46+ , AnyStakePoolSigningKey (.. )
47+ , anyStakePoolSigningKeyToVerificationKey
4048 )
4149where
4250
@@ -60,7 +68,11 @@ import Cardano.Crypto.Wallet qualified as Crypto.HD
6068import Cardano.Ledger.Keys (DSIGN )
6169import Cardano.Ledger.Keys qualified as Shelley
6270
63- import Data.Aeson.Types (ToJSONKey (.. ), toJSONKeyText , withText )
71+ import Data.Aeson.Types
72+ ( ToJSONKey (.. )
73+ , toJSONKeyText
74+ , withText
75+ )
6476import Data.Bifunctor (first )
6577import Data.ByteString (ByteString )
6678import Data.ByteString qualified as BS
@@ -1658,6 +1670,29 @@ instance CastSigningKeyRole GenesisUTxOKey PaymentKey where
16581670-- stake pool keys
16591671--
16601672
1673+ -- | Wrapper that handles both normal and extended StakePoolKeys VerificationKeys
1674+ data AnyStakePoolVerificationKey
1675+ = AnyStakePoolNormalVerificationKey (VerificationKey StakePoolKey )
1676+ | AnyStakePoolExtendedVerificationKey (VerificationKey StakePoolExtendedKey )
1677+ deriving (Show , Eq )
1678+
1679+ anyStakePoolVerificationKeyHash :: AnyStakePoolVerificationKey -> Hash StakePoolKey
1680+ anyStakePoolVerificationKeyHash (AnyStakePoolNormalVerificationKey vk) = verificationKeyHash vk
1681+ anyStakePoolVerificationKeyHash (AnyStakePoolExtendedVerificationKey vk) =
1682+ let StakePoolExtendedKeyHash hash = verificationKeyHash vk in StakePoolKeyHash hash
1683+
1684+ -- | Wrapper that handles both normal and extended StakePoolKeys SigningKeys
1685+ data AnyStakePoolSigningKey
1686+ = AnyStakePoolNormalSigningKey (SigningKey StakePoolKey )
1687+ | AnyStakePoolExtendedSigningKey (SigningKey StakePoolExtendedKey )
1688+ deriving Show
1689+
1690+ anyStakePoolSigningKeyToVerificationKey :: AnyStakePoolSigningKey -> AnyStakePoolVerificationKey
1691+ anyStakePoolSigningKeyToVerificationKey (AnyStakePoolNormalSigningKey sk) =
1692+ AnyStakePoolNormalVerificationKey (getVerificationKey sk)
1693+ anyStakePoolSigningKeyToVerificationKey (AnyStakePoolExtendedSigningKey vk) =
1694+ AnyStakePoolExtendedVerificationKey (getVerificationKey vk)
1695+
16611696data StakePoolKey
16621697
16631698instance HasTypeProxy StakePoolKey where
@@ -1892,6 +1927,10 @@ instance SerialiseAsRawBytes (Hash StakePoolExtendedKey) where
18921927 (SerialiseAsRawBytesError " Unable to deserialise Hash StakePoolExtendedKey" )
18931928 (StakePoolExtendedKeyHash . Shelley. KeyHash <$> Crypto. hashFromBytes bs)
18941929
1930+ instance SerialiseAsBech32 (Hash StakePoolExtendedKey ) where
1931+ bech32PrefixFor _ = " pool_xvkh"
1932+ bech32PrefixesPermitted _ = [" pool_xvkh" ]
1933+
18951934instance HasTextEnvelope (VerificationKey StakePoolExtendedKey ) where
18961935 textEnvelopeType _ = " StakePoolExtendedVerificationKey_ed25519_bip32"
18971936
@@ -1906,6 +1945,24 @@ instance SerialiseAsBech32 (SigningKey StakePoolExtendedKey) where
19061945 bech32PrefixFor _ = " pool_xsk"
19071946 bech32PrefixesPermitted _ = [" pool_xsk" ]
19081947
1948+ instance ToJSON (Hash StakePoolExtendedKey ) where
1949+ toJSON = toJSON . serialiseToBech32
1950+
1951+ instance ToJSONKey (Hash StakePoolExtendedKey ) where
1952+ toJSONKey = toJSONKeyText serialiseToBech32
1953+
1954+ instance FromJSON (Hash StakePoolExtendedKey ) where
1955+ parseJSON = withText " PoolId" $ \ str ->
1956+ case deserialiseFromBech32 (AsHash AsStakePoolExtendedKey ) str of
1957+ Left err ->
1958+ fail $
1959+ docToString $
1960+ mconcat
1961+ [ " Error deserialising Hash StakePoolKey: " <> pretty str
1962+ , " Error: " <> prettyError err
1963+ ]
1964+ Right h -> pure h
1965+
19091966instance CastVerificationKeyRole StakePoolExtendedKey StakePoolKey where
19101967 castVerificationKey (StakePoolExtendedVerificationKey vk) =
19111968 StakePoolVerificationKey
0 commit comments