{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeData #-}
{-# LANGUAGE UndecidableInstances #-}

module Cardano.Ledger.Keys.Internal (
  -- * DSIGN
  DSIGN,
  DSignable,
  VKey (..),
  ADDRHASH,
  KeyHash (..),
  hashKey,
  signedDSIGN,
  verifySignedDSIGN,

  -- * Key roles
  KeyRole (..),
  HasKeyRole (..),
  asWitness,
) where

import Cardano.Crypto.DSIGN hiding (
  decodeSignedDSIGN,
  encodeSignedDSIGN,
  signedDSIGN,
  verifySignedDSIGN,
 )
import qualified Cardano.Crypto.DSIGN as DSIGN
import qualified Cardano.Crypto.Hash as Hash
import Cardano.Ledger.Binary (
  DecCBOR (..),
  EncCBOR (..),
  FromCBOR,
  ToCBOR,
 )
import Cardano.Ledger.Orphans ()
import Control.DeepSeq (NFData)
import Data.Aeson (FromJSON, FromJSONKey, ToJSON, ToJSONKey)
import Data.Coerce (Coercible, coerce)
import Data.Default (Default)
import Data.Kind (Type)
import Data.MemPack (FailT (..), MemPack (..), StateT (..), Unpack (..))
import Data.Ord (comparing)
import Foreign.Storable (Storable)
import GHC.Generics (Generic)
import NoThunks.Class (NoThunks (..))
import Quiet

-- | Cryptographic signing algorithm used on Cardano blockchain.
type DSIGN = DSIGN.Ed25519DSIGN

-- | The role of a key.
--
-- All key roles are __fixed__ and unique, except for the `Witness` role. In particular,
-- keys can be cast to a `Witness` role with the help of `asWitness`, because same witness
-- can be valid for many roles.
--
-- In fact, it is perfectly allowable for a key to be used in many roles by the end user;
-- there is nothing prohibiting somebody using the same underlying key or a script as
-- their payment and staking credential, as well as the key for their stake pool. However,
-- in the ledger code mixing up keys with different roles could be catastrophic, that is
-- why we have this separation.
type data KeyRole
  = GenesisRole
  | GenesisDelegate
  | Payment
  | Staking
  | StakePool
  | BlockIssuer
  | Witness
  | DRepRole
  | HotCommitteeRole
  | ColdCommitteeRole
  | Guard

class HasKeyRole (a :: KeyRole -> Type) where
  -- | General coercion of key roles.
  --
  --   The presence of this function is mostly to help the user realise where they
  --   are converting key roles.
  coerceKeyRole ::
    a r ->
    a r'
  default coerceKeyRole ::
    Coercible (a r) (a r') =>
    a r ->
    a r'
  coerceKeyRole = a r -> a r'
forall a b. Coercible a b => a -> b
coerce

-- | Use a key as a witness.
--
--   This is the most common coercion between key roles, because most keys can
--   be used as witnesses to some types of transaction. As such, we provide an
--   explicit coercion for it.
asWitness ::
  HasKeyRole a =>
  a r ->
  a Witness
asWitness :: forall (a :: KeyRole -> *) (r :: KeyRole).
HasKeyRole a =>
a r -> a Witness
asWitness = a r -> a Witness
forall (r :: KeyRole) (r' :: KeyRole). a r -> a r'
forall (a :: KeyRole -> *) (r :: KeyRole) (r' :: KeyRole).
HasKeyRole a =>
a r -> a r'
coerceKeyRole

--------------------------------------------------------------------------------
-- Verification keys
--------------------------------------------------------------------------------

type DSignable = DSIGN.Signable DSIGN

-- | Discriminated verification key
--
--   We wrap the basic `VerKeyDSIGN` in order to add the key role.
newtype VKey (kd :: KeyRole) = VKey {forall (kd :: KeyRole). VKey kd -> VerKeyDSIGN DSIGN
unVKey :: DSIGN.VerKeyDSIGN DSIGN}
  deriving ((forall x. VKey kd -> Rep (VKey kd) x)
-> (forall x. Rep (VKey kd) x -> VKey kd) -> Generic (VKey kd)
forall x. Rep (VKey kd) x -> VKey kd
forall x. VKey kd -> Rep (VKey kd) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (kd :: KeyRole) x. Rep (VKey kd) x -> VKey kd
forall (kd :: KeyRole) x. VKey kd -> Rep (VKey kd) x
$cfrom :: forall (kd :: KeyRole) x. VKey kd -> Rep (VKey kd) x
from :: forall x. VKey kd -> Rep (VKey kd) x
$cto :: forall (kd :: KeyRole) x. Rep (VKey kd) x -> VKey kd
to :: forall x. Rep (VKey kd) x -> VKey kd
Generic, VKey kd -> VKey kd -> Bool
(VKey kd -> VKey kd -> Bool)
-> (VKey kd -> VKey kd -> Bool) -> Eq (VKey kd)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall (kd :: KeyRole). VKey kd -> VKey kd -> Bool
$c== :: forall (kd :: KeyRole). VKey kd -> VKey kd -> Bool
== :: VKey kd -> VKey kd -> Bool
$c/= :: forall (kd :: KeyRole). VKey kd -> VKey kd -> Bool
/= :: VKey kd -> VKey kd -> Bool
Eq, VKey kd -> ()
(VKey kd -> ()) -> NFData (VKey kd)
forall a. (a -> ()) -> NFData a
forall (kd :: KeyRole). VKey kd -> ()
$crnf :: forall (kd :: KeyRole). VKey kd -> ()
rnf :: VKey kd -> ()
NFData, Context -> VKey kd -> IO (Maybe ThunkInfo)
Proxy (VKey kd) -> String
(Context -> VKey kd -> IO (Maybe ThunkInfo))
-> (Context -> VKey kd -> IO (Maybe ThunkInfo))
-> (Proxy (VKey kd) -> String)
-> NoThunks (VKey kd)
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> String)
-> NoThunks a
forall (kd :: KeyRole). Context -> VKey kd -> IO (Maybe ThunkInfo)
forall (kd :: KeyRole). Proxy (VKey kd) -> String
$cnoThunks :: forall (kd :: KeyRole). Context -> VKey kd -> IO (Maybe ThunkInfo)
noThunks :: Context -> VKey kd -> IO (Maybe ThunkInfo)
$cwNoThunks :: forall (kd :: KeyRole). Context -> VKey kd -> IO (Maybe ThunkInfo)
wNoThunks :: Context -> VKey kd -> IO (Maybe ThunkInfo)
$cshowTypeOf :: forall (kd :: KeyRole). Proxy (VKey kd) -> String
showTypeOf :: Proxy (VKey kd) -> String
NoThunks, Typeable (VKey kd)
Typeable (VKey kd) =>
(forall s. Decoder s (VKey kd))
-> (forall s. Proxy (VKey kd) -> Decoder s ())
-> (Proxy (VKey kd) -> Text)
-> DecCBOR (VKey kd)
Proxy (VKey kd) -> Text
forall s. Decoder s (VKey kd)
forall a.
Typeable a =>
(forall s. Decoder s a)
-> (forall s. Proxy a -> Decoder s ())
-> (Proxy a -> Text)
-> DecCBOR a
forall s. Proxy (VKey kd) -> Decoder s ()
forall (kd :: KeyRole). Typeable kd => Typeable (VKey kd)
forall (kd :: KeyRole). Typeable kd => Proxy (VKey kd) -> Text
forall (kd :: KeyRole) s. Typeable kd => Decoder s (VKey kd)
forall (kd :: KeyRole) s.
Typeable kd =>
Proxy (VKey kd) -> Decoder s ()
$cdecCBOR :: forall (kd :: KeyRole) s. Typeable kd => Decoder s (VKey kd)
decCBOR :: forall s. Decoder s (VKey kd)
$cdropCBOR :: forall (kd :: KeyRole) s.
Typeable kd =>
Proxy (VKey kd) -> Decoder s ()
dropCBOR :: forall s. Proxy (VKey kd) -> Decoder s ()
$clabel :: forall (kd :: KeyRole). Typeable kd => Proxy (VKey kd) -> Text
label :: Proxy (VKey kd) -> Text
DecCBOR, VKey kd -> Encoding
(VKey kd -> Encoding) -> EncCBOR (VKey kd)
forall a. (a -> Encoding) -> EncCBOR a
forall (kd :: KeyRole). VKey kd -> Encoding
$cencCBOR :: forall (kd :: KeyRole). VKey kd -> Encoding
encCBOR :: VKey kd -> Encoding
EncCBOR)

instance Ord (VKey kd) where
  -- `VerKeyDSIGN` disallows an `Ord` instance and specifies that hashes should be compared instead.
  -- We need an `Ord` instance for `VKey`, so it can be stored inside other types that need an `Ord`
  -- instance, so we define it using the `VerKeyDSIGN` hashes as requested.
  compare :: VKey kd -> VKey kd -> Ordering
compare = (VKey kd -> KeyHash kd) -> VKey kd -> VKey kd -> Ordering
forall a b. Ord a => (b -> a) -> b -> b -> Ordering
comparing VKey kd -> KeyHash kd
forall (kd :: KeyRole). VKey kd -> KeyHash kd
hashKey

deriving via Quiet (VKey kd) instance Show (VKey kd)

instance HasKeyRole VKey

-- | Hashing algorithm used for hashing cryptographic keys and scripts (aka addresses).
type ADDRHASH = Hash.Blake2b_224

-- | Discriminated hash of public Key
newtype KeyHash (r :: KeyRole) = KeyHash
  {forall (r :: KeyRole).
KeyHash r -> Hash ADDRHASH (VerKeyDSIGN DSIGN)
unKeyHash :: Hash.Hash ADDRHASH (VerKeyDSIGN DSIGN)}
  deriving (Int -> KeyHash r -> ShowS
[KeyHash r] -> ShowS
KeyHash r -> String
(Int -> KeyHash r -> ShowS)
-> (KeyHash r -> String)
-> ([KeyHash r] -> ShowS)
-> Show (KeyHash r)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall (r :: KeyRole). Int -> KeyHash r -> ShowS
forall (r :: KeyRole). [KeyHash r] -> ShowS
forall (r :: KeyRole). KeyHash r -> String
$cshowsPrec :: forall (r :: KeyRole). Int -> KeyHash r -> ShowS
showsPrec :: Int -> KeyHash r -> ShowS
$cshow :: forall (r :: KeyRole). KeyHash r -> String
show :: KeyHash r -> String
$cshowList :: forall (r :: KeyRole). [KeyHash r] -> ShowS
showList :: [KeyHash r] -> ShowS
Show, KeyHash r -> KeyHash r -> Bool
(KeyHash r -> KeyHash r -> Bool)
-> (KeyHash r -> KeyHash r -> Bool) -> Eq (KeyHash r)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall (r :: KeyRole). KeyHash r -> KeyHash r -> Bool
$c== :: forall (r :: KeyRole). KeyHash r -> KeyHash r -> Bool
== :: KeyHash r -> KeyHash r -> Bool
$c/= :: forall (r :: KeyRole). KeyHash r -> KeyHash r -> Bool
/= :: KeyHash r -> KeyHash r -> Bool
Eq, Eq (KeyHash r)
Eq (KeyHash r) =>
(KeyHash r -> KeyHash r -> Ordering)
-> (KeyHash r -> KeyHash r -> Bool)
-> (KeyHash r -> KeyHash r -> Bool)
-> (KeyHash r -> KeyHash r -> Bool)
-> (KeyHash r -> KeyHash r -> Bool)
-> (KeyHash r -> KeyHash r -> KeyHash r)
-> (KeyHash r -> KeyHash r -> KeyHash r)
-> Ord (KeyHash r)
KeyHash r -> KeyHash r -> Bool
KeyHash r -> KeyHash r -> Ordering
KeyHash r -> KeyHash r -> KeyHash r
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall (r :: KeyRole). Eq (KeyHash r)
forall (r :: KeyRole). KeyHash r -> KeyHash r -> Bool
forall (r :: KeyRole). KeyHash r -> KeyHash r -> Ordering
forall (r :: KeyRole). KeyHash r -> KeyHash r -> KeyHash r
$ccompare :: forall (r :: KeyRole). KeyHash r -> KeyHash r -> Ordering
compare :: KeyHash r -> KeyHash r -> Ordering
$c< :: forall (r :: KeyRole). KeyHash r -> KeyHash r -> Bool
< :: KeyHash r -> KeyHash r -> Bool
$c<= :: forall (r :: KeyRole). KeyHash r -> KeyHash r -> Bool
<= :: KeyHash r -> KeyHash r -> Bool
$c> :: forall (r :: KeyRole). KeyHash r -> KeyHash r -> Bool
> :: KeyHash r -> KeyHash r -> Bool
$c>= :: forall (r :: KeyRole). KeyHash r -> KeyHash r -> Bool
>= :: KeyHash r -> KeyHash r -> Bool
$cmax :: forall (r :: KeyRole). KeyHash r -> KeyHash r -> KeyHash r
max :: KeyHash r -> KeyHash r -> KeyHash r
$cmin :: forall (r :: KeyRole). KeyHash r -> KeyHash r -> KeyHash r
min :: KeyHash r -> KeyHash r -> KeyHash r
Ord)
  deriving newtype
    ( KeyHash r -> ()
(KeyHash r -> ()) -> NFData (KeyHash r)
forall a. (a -> ()) -> NFData a
forall (r :: KeyRole). KeyHash r -> ()
$crnf :: forall (r :: KeyRole). KeyHash r -> ()
rnf :: KeyHash r -> ()
NFData
    , Context -> KeyHash r -> IO (Maybe ThunkInfo)
Proxy (KeyHash r) -> String
(Context -> KeyHash r -> IO (Maybe ThunkInfo))
-> (Context -> KeyHash r -> IO (Maybe ThunkInfo))
-> (Proxy (KeyHash r) -> String)
-> NoThunks (KeyHash r)
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> String)
-> NoThunks a
forall (r :: KeyRole). Context -> KeyHash r -> IO (Maybe ThunkInfo)
forall (r :: KeyRole). Proxy (KeyHash r) -> String
$cnoThunks :: forall (r :: KeyRole). Context -> KeyHash r -> IO (Maybe ThunkInfo)
noThunks :: Context -> KeyHash r -> IO (Maybe ThunkInfo)
$cwNoThunks :: forall (r :: KeyRole). Context -> KeyHash r -> IO (Maybe ThunkInfo)
wNoThunks :: Context -> KeyHash r -> IO (Maybe ThunkInfo)
$cshowTypeOf :: forall (r :: KeyRole). Proxy (KeyHash r) -> String
showTypeOf :: Proxy (KeyHash r) -> String
NoThunks
    , (forall x. KeyHash r -> Rep (KeyHash r) x)
-> (forall x. Rep (KeyHash r) x -> KeyHash r)
-> Generic (KeyHash r)
forall x. Rep (KeyHash r) x -> KeyHash r
forall x. KeyHash r -> Rep (KeyHash r) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (r :: KeyRole) x. Rep (KeyHash r) x -> KeyHash r
forall (r :: KeyRole) x. KeyHash r -> Rep (KeyHash r) x
$cfrom :: forall (r :: KeyRole) x. KeyHash r -> Rep (KeyHash r) x
from :: forall x. KeyHash r -> Rep (KeyHash r) x
$cto :: forall (r :: KeyRole) x. Rep (KeyHash r) x -> KeyHash r
to :: forall x. Rep (KeyHash r) x -> KeyHash r
Generic
    , Typeable (KeyHash r)
Typeable (KeyHash r) =>
(KeyHash r -> Encoding)
-> ((forall t. ToCBOR t => Proxy t -> Size)
    -> Proxy (KeyHash r) -> Size)
-> ((forall t. ToCBOR t => Proxy t -> Size)
    -> Proxy [KeyHash r] -> Size)
-> ToCBOR (KeyHash r)
KeyHash r -> Encoding
(forall t. ToCBOR t => Proxy t -> Size)
-> Proxy [KeyHash r] -> Size
(forall t. ToCBOR t => Proxy t -> Size)
-> Proxy (KeyHash r) -> 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
forall (r :: KeyRole). Typeable r => Typeable (KeyHash r)
forall (r :: KeyRole). Typeable r => KeyHash r -> Encoding
forall (r :: KeyRole).
Typeable r =>
(forall t. ToCBOR t => Proxy t -> Size)
-> Proxy [KeyHash r] -> Size
forall (r :: KeyRole).
Typeable r =>
(forall t. ToCBOR t => Proxy t -> Size)
-> Proxy (KeyHash r) -> Size
$ctoCBOR :: forall (r :: KeyRole). Typeable r => KeyHash r -> Encoding
toCBOR :: KeyHash r -> Encoding
$cencodedSizeExpr :: forall (r :: KeyRole).
Typeable r =>
(forall t. ToCBOR t => Proxy t -> Size)
-> Proxy (KeyHash r) -> Size
encodedSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size)
-> Proxy (KeyHash r) -> Size
$cencodedListSizeExpr :: forall (r :: KeyRole).
Typeable r =>
(forall t. ToCBOR t => Proxy t -> Size)
-> Proxy [KeyHash r] -> Size
encodedListSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size)
-> Proxy [KeyHash r] -> Size
ToCBOR
    , Typeable (KeyHash r)
Typeable (KeyHash r) =>
(forall s. Decoder s (KeyHash r))
-> (Proxy (KeyHash r) -> Text) -> FromCBOR (KeyHash r)
Proxy (KeyHash r) -> Text
forall s. Decoder s (KeyHash r)
forall a.
Typeable a =>
(forall s. Decoder s a) -> (Proxy a -> Text) -> FromCBOR a
forall (r :: KeyRole). Typeable r => Typeable (KeyHash r)
forall (r :: KeyRole). Typeable r => Proxy (KeyHash r) -> Text
forall (r :: KeyRole) s. Typeable r => Decoder s (KeyHash r)
$cfromCBOR :: forall (r :: KeyRole) s. Typeable r => Decoder s (KeyHash r)
fromCBOR :: forall s. Decoder s (KeyHash r)
$clabel :: forall (r :: KeyRole). Typeable r => Proxy (KeyHash r) -> Text
label :: Proxy (KeyHash r) -> Text
FromCBOR
    , KeyHash r -> Encoding
(KeyHash r -> Encoding) -> EncCBOR (KeyHash r)
forall a. (a -> Encoding) -> EncCBOR a
forall (r :: KeyRole). KeyHash r -> Encoding
$cencCBOR :: forall (r :: KeyRole). KeyHash r -> Encoding
encCBOR :: KeyHash r -> Encoding
EncCBOR
    , Typeable (KeyHash r)
Typeable (KeyHash r) =>
(forall s. Decoder s (KeyHash r))
-> (forall s. Proxy (KeyHash r) -> Decoder s ())
-> (Proxy (KeyHash r) -> Text)
-> DecCBOR (KeyHash r)
Proxy (KeyHash r) -> Text
forall s. Decoder s (KeyHash r)
forall a.
Typeable a =>
(forall s. Decoder s a)
-> (forall s. Proxy a -> Decoder s ())
-> (Proxy a -> Text)
-> DecCBOR a
forall s. Proxy (KeyHash r) -> Decoder s ()
forall (r :: KeyRole). Typeable r => Typeable (KeyHash r)
forall (r :: KeyRole). Typeable r => Proxy (KeyHash r) -> Text
forall (r :: KeyRole) s. Typeable r => Decoder s (KeyHash r)
forall (r :: KeyRole) s.
Typeable r =>
Proxy (KeyHash r) -> Decoder s ()
$cdecCBOR :: forall (r :: KeyRole) s. Typeable r => Decoder s (KeyHash r)
decCBOR :: forall s. Decoder s (KeyHash r)
$cdropCBOR :: forall (r :: KeyRole) s.
Typeable r =>
Proxy (KeyHash r) -> Decoder s ()
dropCBOR :: forall s. Proxy (KeyHash r) -> Decoder s ()
$clabel :: forall (r :: KeyRole). Typeable r => Proxy (KeyHash r) -> Text
label :: Proxy (KeyHash r) -> Text
DecCBOR
    , ToJSONKeyFunction [KeyHash r]
ToJSONKeyFunction (KeyHash r)
ToJSONKeyFunction (KeyHash r)
-> ToJSONKeyFunction [KeyHash r] -> ToJSONKey (KeyHash r)
forall a.
ToJSONKeyFunction a -> ToJSONKeyFunction [a] -> ToJSONKey a
forall (r :: KeyRole). ToJSONKeyFunction [KeyHash r]
forall (r :: KeyRole). ToJSONKeyFunction (KeyHash r)
$ctoJSONKey :: forall (r :: KeyRole). ToJSONKeyFunction (KeyHash r)
toJSONKey :: ToJSONKeyFunction (KeyHash r)
$ctoJSONKeyList :: forall (r :: KeyRole). ToJSONKeyFunction [KeyHash r]
toJSONKeyList :: ToJSONKeyFunction [KeyHash r]
ToJSONKey
    , FromJSONKeyFunction [KeyHash r]
FromJSONKeyFunction (KeyHash r)
FromJSONKeyFunction (KeyHash r)
-> FromJSONKeyFunction [KeyHash r] -> FromJSONKey (KeyHash r)
forall a.
FromJSONKeyFunction a -> FromJSONKeyFunction [a] -> FromJSONKey a
forall (r :: KeyRole). FromJSONKeyFunction [KeyHash r]
forall (r :: KeyRole). FromJSONKeyFunction (KeyHash r)
$cfromJSONKey :: forall (r :: KeyRole). FromJSONKeyFunction (KeyHash r)
fromJSONKey :: FromJSONKeyFunction (KeyHash r)
$cfromJSONKeyList :: forall (r :: KeyRole). FromJSONKeyFunction [KeyHash r]
fromJSONKeyList :: FromJSONKeyFunction [KeyHash r]
FromJSONKey
    , [KeyHash r] -> Value
[KeyHash r] -> Encoding
KeyHash r -> Bool
KeyHash r -> Value
KeyHash r -> Encoding
(KeyHash r -> Value)
-> (KeyHash r -> Encoding)
-> ([KeyHash r] -> Value)
-> ([KeyHash r] -> Encoding)
-> (KeyHash r -> Bool)
-> ToJSON (KeyHash r)
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> (a -> Bool)
-> ToJSON a
forall (r :: KeyRole). [KeyHash r] -> Value
forall (r :: KeyRole). [KeyHash r] -> Encoding
forall (r :: KeyRole). KeyHash r -> Bool
forall (r :: KeyRole). KeyHash r -> Value
forall (r :: KeyRole). KeyHash r -> Encoding
$ctoJSON :: forall (r :: KeyRole). KeyHash r -> Value
toJSON :: KeyHash r -> Value
$ctoEncoding :: forall (r :: KeyRole). KeyHash r -> Encoding
toEncoding :: KeyHash r -> Encoding
$ctoJSONList :: forall (r :: KeyRole). [KeyHash r] -> Value
toJSONList :: [KeyHash r] -> Value
$ctoEncodingList :: forall (r :: KeyRole). [KeyHash r] -> Encoding
toEncodingList :: [KeyHash r] -> Encoding
$comitField :: forall (r :: KeyRole). KeyHash r -> Bool
omitField :: KeyHash r -> Bool
ToJSON
    , Maybe (KeyHash r)
Value -> Parser [KeyHash r]
Value -> Parser (KeyHash r)
(Value -> Parser (KeyHash r))
-> (Value -> Parser [KeyHash r])
-> Maybe (KeyHash r)
-> FromJSON (KeyHash r)
forall a.
(Value -> Parser a)
-> (Value -> Parser [a]) -> Maybe a -> FromJSON a
forall (r :: KeyRole). Maybe (KeyHash r)
forall (r :: KeyRole). Value -> Parser [KeyHash r]
forall (r :: KeyRole). Value -> Parser (KeyHash r)
$cparseJSON :: forall (r :: KeyRole). Value -> Parser (KeyHash r)
parseJSON :: Value -> Parser (KeyHash r)
$cparseJSONList :: forall (r :: KeyRole). Value -> Parser [KeyHash r]
parseJSONList :: Value -> Parser [KeyHash r]
$comittedField :: forall (r :: KeyRole). Maybe (KeyHash r)
omittedField :: Maybe (KeyHash r)
FromJSON
    , KeyHash r
KeyHash r -> Default (KeyHash r)
forall a. a -> Default a
forall (r :: KeyRole). KeyHash r
$cdef :: forall (r :: KeyRole). KeyHash r
def :: KeyHash r
Default
    , String
String
-> (KeyHash r -> Int)
-> (forall s. KeyHash r -> Pack s ())
-> (forall b s. Buffer b => Unpack s b (KeyHash r))
-> MemPack (KeyHash r)
KeyHash r -> Int
forall a.
String
-> (a -> Int)
-> (forall s. a -> Pack s ())
-> (forall b s. Buffer b => Unpack s b a)
-> MemPack a
forall s. KeyHash r -> Pack s ()
forall b s. Buffer b => Unpack s b (KeyHash r)
forall (r :: KeyRole). String
forall (r :: KeyRole). KeyHash r -> Int
forall (r :: KeyRole) s. KeyHash r -> Pack s ()
forall (r :: KeyRole) b s. Buffer b => Unpack s b (KeyHash r)
$ctypeName :: forall (r :: KeyRole). String
typeName :: String
$cpackedByteCount :: forall (r :: KeyRole). KeyHash r -> Int
packedByteCount :: KeyHash r -> Int
$cpackM :: forall (r :: KeyRole) s. KeyHash r -> Pack s ()
packM :: forall s. KeyHash r -> Pack s ()
$cunpackM :: forall (r :: KeyRole) b s. Buffer b => Unpack s b (KeyHash r)
unpackM :: forall b s. Buffer b => Unpack s b (KeyHash r)
MemPack
    , Ptr (KeyHash r) -> IO (KeyHash r)
Ptr (KeyHash r) -> Int -> IO (KeyHash r)
Ptr (KeyHash r) -> Int -> KeyHash r -> IO ()
Ptr (KeyHash r) -> KeyHash r -> IO ()
KeyHash r -> Int
(KeyHash r -> Int)
-> (KeyHash r -> Int)
-> (Ptr (KeyHash r) -> Int -> IO (KeyHash r))
-> (Ptr (KeyHash r) -> Int -> KeyHash r -> IO ())
-> (forall b. Ptr b -> Int -> IO (KeyHash r))
-> (forall b. Ptr b -> Int -> KeyHash r -> IO ())
-> (Ptr (KeyHash r) -> IO (KeyHash r))
-> (Ptr (KeyHash r) -> KeyHash r -> IO ())
-> Storable (KeyHash r)
forall b. Ptr b -> Int -> IO (KeyHash r)
forall b. Ptr b -> Int -> KeyHash r -> IO ()
forall a.
(a -> Int)
-> (a -> Int)
-> (Ptr a -> Int -> IO a)
-> (Ptr a -> Int -> a -> IO ())
-> (forall b. Ptr b -> Int -> IO a)
-> (forall b. Ptr b -> Int -> a -> IO ())
-> (Ptr a -> IO a)
-> (Ptr a -> a -> IO ())
-> Storable a
forall (r :: KeyRole). Ptr (KeyHash r) -> IO (KeyHash r)
forall (r :: KeyRole). Ptr (KeyHash r) -> Int -> IO (KeyHash r)
forall (r :: KeyRole). Ptr (KeyHash r) -> Int -> KeyHash r -> IO ()
forall (r :: KeyRole). Ptr (KeyHash r) -> KeyHash r -> IO ()
forall (r :: KeyRole). KeyHash r -> Int
forall (r :: KeyRole) b. Ptr b -> Int -> IO (KeyHash r)
forall (r :: KeyRole) b. Ptr b -> Int -> KeyHash r -> IO ()
$csizeOf :: forall (r :: KeyRole). KeyHash r -> Int
sizeOf :: KeyHash r -> Int
$calignment :: forall (r :: KeyRole). KeyHash r -> Int
alignment :: KeyHash r -> Int
$cpeekElemOff :: forall (r :: KeyRole). Ptr (KeyHash r) -> Int -> IO (KeyHash r)
peekElemOff :: Ptr (KeyHash r) -> Int -> IO (KeyHash r)
$cpokeElemOff :: forall (r :: KeyRole). Ptr (KeyHash r) -> Int -> KeyHash r -> IO ()
pokeElemOff :: Ptr (KeyHash r) -> Int -> KeyHash r -> IO ()
$cpeekByteOff :: forall (r :: KeyRole) b. Ptr b -> Int -> IO (KeyHash r)
peekByteOff :: forall b. Ptr b -> Int -> IO (KeyHash r)
$cpokeByteOff :: forall (r :: KeyRole) b. Ptr b -> Int -> KeyHash r -> IO ()
pokeByteOff :: forall b. Ptr b -> Int -> KeyHash r -> IO ()
$cpeek :: forall (r :: KeyRole). Ptr (KeyHash r) -> IO (KeyHash r)
peek :: Ptr (KeyHash r) -> IO (KeyHash r)
$cpoke :: forall (r :: KeyRole). Ptr (KeyHash r) -> KeyHash r -> IO ()
poke :: Ptr (KeyHash r) -> KeyHash r -> IO ()
Storable
    )

instance HasKeyRole KeyHash

-- | Hash a given public key
hashKey :: VKey kd -> KeyHash kd
hashKey :: forall (kd :: KeyRole). VKey kd -> KeyHash kd
hashKey (VKey VerKeyDSIGN DSIGN
vk) = Hash ADDRHASH (VerKeyDSIGN DSIGN) -> KeyHash kd
forall (r :: KeyRole).
Hash ADDRHASH (VerKeyDSIGN DSIGN) -> KeyHash r
KeyHash (Hash ADDRHASH (VerKeyDSIGN DSIGN) -> KeyHash kd)
-> Hash ADDRHASH (VerKeyDSIGN DSIGN) -> KeyHash kd
forall a b. (a -> b) -> a -> b
$ VerKeyDSIGN DSIGN -> Hash ADDRHASH (VerKeyDSIGN DSIGN)
forall h.
HashAlgorithm h =>
VerKeyDSIGN DSIGN -> Hash h (VerKeyDSIGN DSIGN)
forall v h.
(DSIGNAlgorithm v, HashAlgorithm h) =>
VerKeyDSIGN v -> Hash h (VerKeyDSIGN v)
DSIGN.hashVerKeyDSIGN VerKeyDSIGN DSIGN
vk

-- | Produce a digital signature
signedDSIGN ::
  Signable DSIGN a =>
  SignKeyDSIGN DSIGN ->
  a ->
  SignedDSIGN DSIGN a
signedDSIGN :: forall a.
Signable DSIGN a =>
SignKeyDSIGN DSIGN -> a -> SignedDSIGN DSIGN a
signedDSIGN SignKeyDSIGN DSIGN
key a
a = ContextDSIGN DSIGN
-> a -> SignKeyDSIGN DSIGN -> SignedDSIGN DSIGN a
forall v a.
(DSIGNAlgorithm v, Signable v a) =>
ContextDSIGN v -> a -> SignKeyDSIGN v -> SignedDSIGN v a
DSIGN.signedDSIGN () a
a SignKeyDSIGN DSIGN
key

-- | Verify a digital signature
verifySignedDSIGN ::
  Signable DSIGN a =>
  VKey kd ->
  a ->
  SignedDSIGN DSIGN a ->
  Bool
verifySignedDSIGN :: forall a (kd :: KeyRole).
Signable DSIGN a =>
VKey kd -> a -> SignedDSIGN DSIGN a -> Bool
verifySignedDSIGN (VKey VerKeyDSIGN DSIGN
vk) a
vd SignedDSIGN DSIGN a
sigDSIGN =
  (String -> Bool) -> (() -> Bool) -> Either String () -> Bool
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Bool -> String -> Bool
forall a b. a -> b -> a
const Bool
False) (Bool -> () -> Bool
forall a b. a -> b -> a
const Bool
True) (Either String () -> Bool) -> Either String () -> Bool
forall a b. (a -> b) -> a -> b
$ ContextDSIGN DSIGN
-> VerKeyDSIGN DSIGN
-> a
-> SignedDSIGN DSIGN a
-> Either String ()
forall v a.
(DSIGNAlgorithm v, Signable v a, HasCallStack) =>
ContextDSIGN v
-> VerKeyDSIGN v -> a -> SignedDSIGN v a -> Either String ()
DSIGN.verifySignedDSIGN () VerKeyDSIGN DSIGN
vk a
vd SignedDSIGN DSIGN a
sigDSIGN
{-# INLINE verifySignedDSIGN #-}