Skip to content

Commit 8c3e378

Browse files
committed
Remove parameterised type
1 parent b541a7c commit 8c3e378

File tree

5 files changed

+63
-126
lines changed

5 files changed

+63
-126
lines changed

cardano-api/gen/Test/Gen/Cardano/Api/Typed.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -559,10 +559,10 @@ genOperationalCertificateWithCounter
559559
genOperationalCertificateWithCounter = do
560560
kesVKey <- genVerificationKey AsKesKey
561561
stkPoolOrGenDelExtSign <-
562-
Gen.either (StakePoolNormalKeyWrapper <$> genSigningKey AsStakePoolKey) (genSigningKey AsGenesisDelegateExtendedKey)
562+
Gen.either (AnyStakePoolNormalSigningKey <$> genSigningKey AsStakePoolKey) (genSigningKey AsGenesisDelegateExtendedKey)
563563
kesP <- genKESPeriod
564564
c <- Gen.integral $ Range.linear 0 1000
565-
let stakePoolVer = either (\x -> liftStakePoolKey x (const getVerificationKey)) (StakePoolNormalKeyWrapper . convert' . getVerificationKey) stkPoolOrGenDelExtSign
565+
let stakePoolVer = either anyStakePoolSigningKeyToVerificationKey (AnyStakePoolNormalVerificationKey . convert' . getVerificationKey) stkPoolOrGenDelExtSign
566566
iCounter = OperationalCertificateIssueCounter c stakePoolVer
567567

568568
case issueOperationalCertificate kesVKey stkPoolOrGenDelExtSign kesP iCounter of

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

Lines changed: 45 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
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
)
5449
where
5550

@@ -65,6 +60,7 @@ import Cardano.Api.Internal.SerialiseRaw
6560
import Cardano.Api.Internal.SerialiseTextEnvelope
6661
import Cardano.Api.Internal.SerialiseUsing
6762

63+
import Cardano.Binary (DecoderError (DecoderErrorUnknownTag), cborError)
6864
import Cardano.Crypto.DSIGN.Class qualified as Crypto
6965
import Cardano.Crypto.Hash.Class qualified as Crypto
7066
import Cardano.Crypto.Seed qualified as Crypto
@@ -73,6 +69,8 @@ import Cardano.Ledger.Crypto (StandardCrypto)
7369
import Cardano.Ledger.Crypto qualified as Shelley (DSIGN)
7470
import Cardano.Ledger.Keys qualified as Shelley
7571

72+
import Codec.CBOR.Decoding (decodeListLenOf)
73+
import Codec.CBOR.Encoding (encodeListLen)
7674
import Data.Aeson.Types
7775
( ToJSONKey (..)
7876
, toJSONKeyText
@@ -84,6 +82,7 @@ import Data.ByteString qualified as BS
8482
import Data.Either.Combinators (maybeToRight)
8583
import Data.Maybe
8684
import 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

17801719
data StakePoolKey
17811720

cardano-api/src/Cardano/Api/Internal/OperationalCertificate.hs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ import Data.Word
4444
data OperationalCertificate
4545
= OperationalCertificate
4646
!(Shelley.OCert StandardCrypto)
47-
!(AnyStakePoolKeyWrapper VerificationKey)
47+
!AnyStakePoolVerificationKey
4848
deriving (Eq, Show)
4949
deriving anyclass SerialiseAsCBOR
5050

5151
data OperationalCertificateIssueCounter
5252
= OperationalCertificateIssueCounter
5353
{ opCertIssueCount :: !Word64
54-
, opCertIssueColdKey :: !(AnyStakePoolKeyWrapper VerificationKey) -- For consistency checking
54+
, opCertIssueColdKey :: !AnyStakePoolVerificationKey -- For consistency checking
5555
}
5656
deriving (Eq, Show)
5757
deriving anyclass SerialiseAsCBOR
@@ -95,8 +95,8 @@ data OperationalCertIssueError
9595
--
9696
-- Order: pool vkey expected, pool skey supplied
9797
OperationalCertKeyMismatch
98-
(AnyStakePoolKeyWrapper VerificationKey)
99-
(AnyStakePoolKeyWrapper VerificationKey)
98+
AnyStakePoolVerificationKey
99+
AnyStakePoolVerificationKey
100100
deriving Show
101101

102102
instance Error OperationalCertIssueError where
@@ -108,7 +108,7 @@ instance Error OperationalCertIssueError where
108108
issueOperationalCertificate
109109
:: VerificationKey KesKey
110110
-> Either
111-
(AnyStakePoolKeyWrapper SigningKey)
111+
AnyStakePoolSigningKey
112112
(SigningKey GenesisDelegateExtendedKey)
113113
-- TODO: this may be better with a type that
114114
-- captured the three (four?) choices, stake pool
@@ -133,11 +133,11 @@ issueOperationalCertificate
133133
, OperationalCertificateIssueCounter (succ counter) poolVKey
134134
)
135135
where
136-
poolVKey' :: AnyStakePoolKeyWrapper VerificationKey
136+
poolVKey' :: AnyStakePoolVerificationKey
137137
poolVKey' =
138138
either
139-
(\x -> liftStakePoolKey x (const getVerificationKey))
140-
(StakePoolNormalKeyWrapper . convert . getVerificationKey)
139+
anyStakePoolSigningKeyToVerificationKey
140+
(AnyStakePoolNormalVerificationKey . convert . getVerificationKey)
141141
skey
142142
where
143143
convert
@@ -167,10 +167,10 @@ issueOperationalCertificate
167167
where
168168
skey' :: ShelleySigningKey
169169
skey' = case skey of
170-
Left (StakePoolNormalKeyWrapper (StakePoolSigningKey poolSKey)) ->
170+
Left (AnyStakePoolNormalSigningKey (StakePoolSigningKey poolSKey)) ->
171171
ShelleyNormalSigningKey poolSKey
172172
Left
173-
( StakePoolExtendedKeyWrapper
173+
( AnyStakePoolExtendedSigningKey
174174
(StakePoolExtendedSigningKey poolExtendedSKey)
175175
) ->
176176
ShelleyExtendedSigningKey poolExtendedSKey

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,12 +200,10 @@ module Cardano.Api.Shelley
200200
, DRepMetadataReference (DRepMetadataReference)
201201

202202
-- ** Stake pool operator's keys
203-
, AnyStakePoolKeyWrapper (..)
204-
, rewrapAnyStakePoolKey
205-
, foldStakePoolKey
206-
, liftStakePoolKey
207-
, liftStakePoolKeyM
208-
, castHashToNormal
203+
, AnyStakePoolVerificationKey (..)
204+
, anyStakePoolVerificationKeyHash
205+
, AnyStakePoolSigningKey (..)
206+
, anyStakePoolSigningKeyToVerificationKey
209207
, StakePoolExtendedKey
210208
, StakePoolKey
211209
, PoolId

cardano-api/test/cardano-api-golden/Test/Golden/ErrorsSpec.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,8 @@ test_OperationalCertIssueError =
215215
[
216216
( "OperationalCertKeyMismatch"
217217
, OperationalCertKeyMismatch
218-
(StakePoolNormalKeyWrapper stakePoolVerKey1)
219-
(StakePoolNormalKeyWrapper stakePoolVerKey2)
218+
(AnyStakePoolNormalVerificationKey stakePoolVerKey1)
219+
(AnyStakePoolNormalVerificationKey stakePoolVerKey2)
220220
)
221221
]
222222

0 commit comments

Comments
 (0)