{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings #-}

module Cardano.Crypto.Signing.Redeem.SigningKey
  ( RedeemSigningKey (..),
    redeemToVerification,
  )
where

import Cardano.Binary (FromCBOR, ToCBOR)
import Cardano.Crypto.Signing.Redeem.VerificationKey
  ( RedeemVerificationKey (..),
    redeemVKB64F,
  )
import Cardano.Prelude
import qualified Crypto.PubKey.Ed25519 as Ed25519
import Formatting (bprint)
import qualified Formatting.Buildable as B
import NoThunks.Class (InspectHeap (..), NoThunks (..))

-- | Wrapper around 'Ed25519.SecretKey'.
newtype RedeemSigningKey
  = RedeemSigningKey Ed25519.SecretKey
  deriving (RedeemSigningKey -> RedeemSigningKey -> Bool
(RedeemSigningKey -> RedeemSigningKey -> Bool)
-> (RedeemSigningKey -> RedeemSigningKey -> Bool)
-> Eq RedeemSigningKey
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: RedeemSigningKey -> RedeemSigningKey -> Bool
$c/= :: RedeemSigningKey -> RedeemSigningKey -> Bool
== :: RedeemSigningKey -> RedeemSigningKey -> Bool
$c== :: RedeemSigningKey -> RedeemSigningKey -> Bool
Eq, Int -> RedeemSigningKey -> ShowS
[RedeemSigningKey] -> ShowS
RedeemSigningKey -> String
(Int -> RedeemSigningKey -> ShowS)
-> (RedeemSigningKey -> String)
-> ([RedeemSigningKey] -> ShowS)
-> Show RedeemSigningKey
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [RedeemSigningKey] -> ShowS
$cshowList :: [RedeemSigningKey] -> ShowS
show :: RedeemSigningKey -> String
$cshow :: RedeemSigningKey -> String
showsPrec :: Int -> RedeemSigningKey -> ShowS
$cshowsPrec :: Int -> RedeemSigningKey -> ShowS
Show, (forall x. RedeemSigningKey -> Rep RedeemSigningKey x)
-> (forall x. Rep RedeemSigningKey x -> RedeemSigningKey)
-> Generic RedeemSigningKey
forall x. Rep RedeemSigningKey x -> RedeemSigningKey
forall x. RedeemSigningKey -> Rep RedeemSigningKey x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep RedeemSigningKey x -> RedeemSigningKey
$cfrom :: forall x. RedeemSigningKey -> Rep RedeemSigningKey x
Generic, RedeemSigningKey -> ()
(RedeemSigningKey -> ()) -> NFData RedeemSigningKey
forall a. (a -> ()) -> NFData a
rnf :: RedeemSigningKey -> ()
$crnf :: RedeemSigningKey -> ()
NFData, Typeable RedeemSigningKey
Decoder s RedeemSigningKey
Typeable RedeemSigningKey
-> (forall s. Decoder s RedeemSigningKey)
-> (Proxy RedeemSigningKey -> Text)
-> FromCBOR RedeemSigningKey
Proxy RedeemSigningKey -> Text
forall s. Decoder s RedeemSigningKey
forall a.
Typeable a
-> (forall s. Decoder s a) -> (Proxy a -> Text) -> FromCBOR a
label :: Proxy RedeemSigningKey -> Text
$clabel :: Proxy RedeemSigningKey -> Text
fromCBOR :: Decoder s RedeemSigningKey
$cfromCBOR :: forall s. Decoder s RedeemSigningKey
$cp1FromCBOR :: Typeable RedeemSigningKey
FromCBOR, Typeable RedeemSigningKey
Typeable RedeemSigningKey
-> (RedeemSigningKey -> Encoding)
-> ((forall t. ToCBOR t => Proxy t -> Size)
    -> Proxy RedeemSigningKey -> Size)
-> ((forall t. ToCBOR t => Proxy t -> Size)
    -> Proxy [RedeemSigningKey] -> Size)
-> ToCBOR RedeemSigningKey
RedeemSigningKey -> Encoding
(forall t. ToCBOR t => Proxy t -> Size)
-> Proxy [RedeemSigningKey] -> Size
(forall t. ToCBOR t => Proxy t -> Size)
-> Proxy RedeemSigningKey -> Size
forall a.
Typeable a
-> (a -> Encoding)
-> ((forall t. ToCBOR t => Proxy t -> Size) -> Proxy a -> Size)
-> ((forall t. ToCBOR t => Proxy t -> Size) -> Proxy [a] -> Size)
-> ToCBOR a
encodedListSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size)
-> Proxy [RedeemSigningKey] -> Size
$cencodedListSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size)
-> Proxy [RedeemSigningKey] -> Size
encodedSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size)
-> Proxy RedeemSigningKey -> Size
$cencodedSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size)
-> Proxy RedeemSigningKey -> Size
toCBOR :: RedeemSigningKey -> Encoding
$ctoCBOR :: RedeemSigningKey -> Encoding
$cp1ToCBOR :: Typeable RedeemSigningKey
ToCBOR)
  deriving (Context -> RedeemSigningKey -> IO (Maybe ThunkInfo)
Proxy RedeemSigningKey -> String
(Context -> RedeemSigningKey -> IO (Maybe ThunkInfo))
-> (Context -> RedeemSigningKey -> IO (Maybe ThunkInfo))
-> (Proxy RedeemSigningKey -> String)
-> NoThunks RedeemSigningKey
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> String)
-> NoThunks a
showTypeOf :: Proxy RedeemSigningKey -> String
$cshowTypeOf :: Proxy RedeemSigningKey -> String
wNoThunks :: Context -> RedeemSigningKey -> IO (Maybe ThunkInfo)
$cwNoThunks :: Context -> RedeemSigningKey -> IO (Maybe ThunkInfo)
noThunks :: Context -> RedeemSigningKey -> IO (Maybe ThunkInfo)
$cnoThunks :: Context -> RedeemSigningKey -> IO (Maybe ThunkInfo)
NoThunks) via InspectHeap RedeemSigningKey

-- Note that there is deliberately no Ord instance. The crypto libraries
-- encourage using key /hashes/ not keys for things like sets, map etc.

instance B.Buildable RedeemSigningKey where
  build :: RedeemSigningKey -> Builder
build = Format Builder (RedeemVerificationKey -> Builder)
-> RedeemVerificationKey -> Builder
forall a. Format Builder a -> a
bprint (Format
  (RedeemVerificationKey -> Builder)
  (RedeemVerificationKey -> Builder)
"redeem_sec_of_vk:" Format
  (RedeemVerificationKey -> Builder)
  (RedeemVerificationKey -> Builder)
-> Format Builder (RedeemVerificationKey -> Builder)
-> Format Builder (RedeemVerificationKey -> Builder)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Format Builder (RedeemVerificationKey -> Builder)
forall r. Format r (RedeemVerificationKey -> r)
redeemVKB64F) (RedeemVerificationKey -> Builder)
-> (RedeemSigningKey -> RedeemVerificationKey)
-> RedeemSigningKey
-> Builder
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. RedeemSigningKey -> RedeemVerificationKey
redeemToVerification

-- | Verification key derivation function.
redeemToVerification :: RedeemSigningKey -> RedeemVerificationKey
redeemToVerification :: RedeemSigningKey -> RedeemVerificationKey
redeemToVerification (RedeemSigningKey SecretKey
k) = PublicKey -> RedeemVerificationKey
RedeemVerificationKey (SecretKey -> PublicKey
Ed25519.toPublic SecretKey
k)