{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilies #-}
module Cardano.Api.Key
( Key(..)
, generateSigningKey
, CastVerificationKeyRole(..)
, CastSigningKeyRole(..)
, AsType(AsVerificationKey, AsSigningKey)
) where
import Prelude
import Data.Kind (Type)
import qualified Cardano.Crypto.DSIGN.Class as Crypto
import qualified Cardano.Crypto.Seed as Crypto
import Cardano.Api.Hash
import Cardano.Api.HasTypeProxy
import Cardano.Api.SerialiseRaw
import Cardano.Api.SerialiseTextEnvelope
class (Eq (VerificationKey keyrole),
Show (VerificationKey keyrole),
SerialiseAsRawBytes (Hash keyrole),
HasTextEnvelope (VerificationKey keyrole),
HasTextEnvelope (SigningKey keyrole))
=> Key keyrole where
data VerificationKey keyrole :: Type
data SigningKey keyrole :: Type
getVerificationKey :: SigningKey keyrole -> VerificationKey keyrole
deterministicSigningKey :: AsType keyrole -> Crypto.Seed -> SigningKey keyrole
deterministicSigningKeySeedSize :: AsType keyrole -> Word
verificationKeyHash :: VerificationKey keyrole -> Hash keyrole
generateSigningKey :: Key keyrole => AsType keyrole -> IO (SigningKey keyrole)
generateSigningKey :: AsType keyrole -> IO (SigningKey keyrole)
generateSigningKey AsType keyrole
keytype = do
Seed
seed <- Word -> IO Seed
Crypto.readSeedFromSystemEntropy Word
seedSize
SigningKey keyrole -> IO (SigningKey keyrole)
forall (m :: * -> *) a. Monad m => a -> m a
return (SigningKey keyrole -> IO (SigningKey keyrole))
-> SigningKey keyrole -> IO (SigningKey keyrole)
forall a b. (a -> b) -> a -> b
$! AsType keyrole -> Seed -> SigningKey keyrole
forall keyrole.
Key keyrole =>
AsType keyrole -> Seed -> SigningKey keyrole
deterministicSigningKey AsType keyrole
keytype Seed
seed
where
seedSize :: Word
seedSize = AsType keyrole -> Word
forall keyrole. Key keyrole => AsType keyrole -> Word
deterministicSigningKeySeedSize AsType keyrole
keytype
instance HasTypeProxy a => HasTypeProxy (VerificationKey a) where
data AsType (VerificationKey a) = AsVerificationKey (AsType a)
proxyToAsType :: Proxy (VerificationKey a) -> AsType (VerificationKey a)
proxyToAsType Proxy (VerificationKey a)
_ = AsType a -> AsType (VerificationKey a)
forall a. AsType a -> AsType (VerificationKey a)
AsVerificationKey (Proxy a -> AsType a
forall t. HasTypeProxy t => Proxy t -> AsType t
proxyToAsType (forall a. Proxy a
forall k (t :: k). Proxy t
Proxy :: Proxy a))
instance HasTypeProxy a => HasTypeProxy (SigningKey a) where
data AsType (SigningKey a) = AsSigningKey (AsType a)
proxyToAsType :: Proxy (SigningKey a) -> AsType (SigningKey a)
proxyToAsType Proxy (SigningKey a)
_ = AsType a -> AsType (SigningKey a)
forall a. AsType a -> AsType (SigningKey a)
AsSigningKey (Proxy a -> AsType a
forall t. HasTypeProxy t => Proxy t -> AsType t
proxyToAsType (forall a. Proxy a
forall k (t :: k). Proxy t
Proxy :: Proxy a))
class CastVerificationKeyRole keyroleA keyroleB where
castVerificationKey :: VerificationKey keyroleA -> VerificationKey keyroleB
class CastSigningKeyRole keyroleA keyroleB where
castSigningKey :: SigningKey keyroleA -> SigningKey keyroleB