{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeFamilies #-}

-- DUPLICATE -- adapted from: cardano-api/src/Cardano/Api/OperationalCertificate.hs

-- | Operational certificates
--
module Cardano.Api.OperationalCertificate (
    --  OperationalCertIssueError (..)
    OperationalCertificate (..)
  , OperationalCertificateIssueCounter (..)
  , Shelley.KESPeriod (..)
  , getHotKey
  , getKesPeriod
  , getOpCertCount
    -- , issueOperationalCertificate
    -- * Data family instances
  , AsType (..)
  ) where

import           Cardano.Api.Any
import           Cardano.Api.Key
import           Cardano.Api.KeysByron
import           Cardano.Api.KeysPraos
import           Cardano.Api.KeysShelley
import           Cardano.Api.SerialiseTextEnvelope
import qualified Cardano.Ledger.Binary as CBOR (CBORGroup (..), shelleyProtVer,
                     toPlainDecoder, toPlainEncoding)
import           Cardano.Ledger.Crypto (StandardCrypto)
import qualified Cardano.Protocol.TPraos.OCert as Shelley
import           Data.Word


-- ----------------------------------------------------------------------------
-- Operational certificates
--

data OperationalCertificate =
     OperationalCertificate
       !(Shelley.OCert StandardCrypto)
       !(VerificationKey StakePoolKey)
  deriving (OperationalCertificate -> OperationalCertificate -> Bool
(OperationalCertificate -> OperationalCertificate -> Bool)
-> (OperationalCertificate -> OperationalCertificate -> Bool)
-> Eq OperationalCertificate
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: OperationalCertificate -> OperationalCertificate -> Bool
== :: OperationalCertificate -> OperationalCertificate -> Bool
$c/= :: OperationalCertificate -> OperationalCertificate -> Bool
/= :: OperationalCertificate -> OperationalCertificate -> Bool
Eq, Int -> OperationalCertificate -> ShowS
[OperationalCertificate] -> ShowS
OperationalCertificate -> String
(Int -> OperationalCertificate -> ShowS)
-> (OperationalCertificate -> String)
-> ([OperationalCertificate] -> ShowS)
-> Show OperationalCertificate
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> OperationalCertificate -> ShowS
showsPrec :: Int -> OperationalCertificate -> ShowS
$cshow :: OperationalCertificate -> String
show :: OperationalCertificate -> String
$cshowList :: [OperationalCertificate] -> ShowS
showList :: [OperationalCertificate] -> ShowS
Show)
  deriving anyclass HasTypeProxy OperationalCertificate
HasTypeProxy OperationalCertificate =>
(OperationalCertificate -> ByteString)
-> (AsType OperationalCertificate
    -> ByteString -> Either DecoderError OperationalCertificate)
-> SerialiseAsCBOR OperationalCertificate
AsType OperationalCertificate
-> ByteString -> Either DecoderError OperationalCertificate
OperationalCertificate -> ByteString
forall a.
HasTypeProxy a =>
(a -> ByteString)
-> (AsType a -> ByteString -> Either DecoderError a)
-> SerialiseAsCBOR a
$cserialiseToCBOR :: OperationalCertificate -> ByteString
serialiseToCBOR :: OperationalCertificate -> ByteString
$cdeserialiseFromCBOR :: AsType OperationalCertificate
-> ByteString -> Either DecoderError OperationalCertificate
deserialiseFromCBOR :: AsType OperationalCertificate
-> ByteString -> Either DecoderError OperationalCertificate
SerialiseAsCBOR

data OperationalCertificateIssueCounter =
     OperationalCertificateIssueCounter
       { OperationalCertificateIssueCounter -> Word64
opCertIssueCount   :: !Word64
       , OperationalCertificateIssueCounter -> VerificationKey StakePoolKey
opCertIssueColdKey :: !(VerificationKey StakePoolKey) -- For consistency checking
       }
  deriving (OperationalCertificateIssueCounter
-> OperationalCertificateIssueCounter -> Bool
(OperationalCertificateIssueCounter
 -> OperationalCertificateIssueCounter -> Bool)
-> (OperationalCertificateIssueCounter
    -> OperationalCertificateIssueCounter -> Bool)
-> Eq OperationalCertificateIssueCounter
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: OperationalCertificateIssueCounter
-> OperationalCertificateIssueCounter -> Bool
== :: OperationalCertificateIssueCounter
-> OperationalCertificateIssueCounter -> Bool
$c/= :: OperationalCertificateIssueCounter
-> OperationalCertificateIssueCounter -> Bool
/= :: OperationalCertificateIssueCounter
-> OperationalCertificateIssueCounter -> Bool
Eq, Int -> OperationalCertificateIssueCounter -> ShowS
[OperationalCertificateIssueCounter] -> ShowS
OperationalCertificateIssueCounter -> String
(Int -> OperationalCertificateIssueCounter -> ShowS)
-> (OperationalCertificateIssueCounter -> String)
-> ([OperationalCertificateIssueCounter] -> ShowS)
-> Show OperationalCertificateIssueCounter
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> OperationalCertificateIssueCounter -> ShowS
showsPrec :: Int -> OperationalCertificateIssueCounter -> ShowS
$cshow :: OperationalCertificateIssueCounter -> String
show :: OperationalCertificateIssueCounter -> String
$cshowList :: [OperationalCertificateIssueCounter] -> ShowS
showList :: [OperationalCertificateIssueCounter] -> ShowS
Show)
  deriving anyclass HasTypeProxy OperationalCertificateIssueCounter
HasTypeProxy OperationalCertificateIssueCounter =>
(OperationalCertificateIssueCounter -> ByteString)
-> (AsType OperationalCertificateIssueCounter
    -> ByteString
    -> Either DecoderError OperationalCertificateIssueCounter)
-> SerialiseAsCBOR OperationalCertificateIssueCounter
AsType OperationalCertificateIssueCounter
-> ByteString
-> Either DecoderError OperationalCertificateIssueCounter
OperationalCertificateIssueCounter -> ByteString
forall a.
HasTypeProxy a =>
(a -> ByteString)
-> (AsType a -> ByteString -> Either DecoderError a)
-> SerialiseAsCBOR a
$cserialiseToCBOR :: OperationalCertificateIssueCounter -> ByteString
serialiseToCBOR :: OperationalCertificateIssueCounter -> ByteString
$cdeserialiseFromCBOR :: AsType OperationalCertificateIssueCounter
-> ByteString
-> Either DecoderError OperationalCertificateIssueCounter
deserialiseFromCBOR :: AsType OperationalCertificateIssueCounter
-> ByteString
-> Either DecoderError OperationalCertificateIssueCounter
SerialiseAsCBOR


instance ToCBOR OperationalCertificate where
    toCBOR :: OperationalCertificate -> Encoding
toCBOR = Version -> Encoding -> Encoding
CBOR.toPlainEncoding Version
CBOR.shelleyProtVer (Encoding -> Encoding)
-> (OperationalCertificate -> Encoding)
-> OperationalCertificate
-> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. OperationalCertificate -> Encoding
forall a. EncCBOR a => a -> Encoding
encCBOR

instance FromCBOR OperationalCertificate where
    fromCBOR :: forall s. Decoder s OperationalCertificate
fromCBOR = Version
-> Decoder s OperationalCertificate
-> Decoder s OperationalCertificate
forall s a. Version -> Decoder s a -> Decoder s a
CBOR.toPlainDecoder Version
CBOR.shelleyProtVer Decoder s OperationalCertificate
forall s. Decoder s OperationalCertificate
forall a s. DecCBOR a => Decoder s a
decCBOR

instance ToCBOR OperationalCertificateIssueCounter where
    toCBOR :: OperationalCertificateIssueCounter -> Encoding
toCBOR = Version -> Encoding -> Encoding
CBOR.toPlainEncoding Version
CBOR.shelleyProtVer (Encoding -> Encoding)
-> (OperationalCertificateIssueCounter -> Encoding)
-> OperationalCertificateIssueCounter
-> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. OperationalCertificateIssueCounter -> Encoding
forall a. EncCBOR a => a -> Encoding
encCBOR

instance FromCBOR OperationalCertificateIssueCounter where
    fromCBOR :: forall s. Decoder s OperationalCertificateIssueCounter
fromCBOR = Version
-> Decoder s OperationalCertificateIssueCounter
-> Decoder s OperationalCertificateIssueCounter
forall s a. Version -> Decoder s a -> Decoder s a
CBOR.toPlainDecoder Version
CBOR.shelleyProtVer Decoder s OperationalCertificateIssueCounter
forall s. Decoder s OperationalCertificateIssueCounter
forall a s. DecCBOR a => Decoder s a
decCBOR

instance EncCBOR OperationalCertificate where
    encCBOR :: OperationalCertificate -> Encoding
encCBOR (OperationalCertificate OCert StandardCrypto
ocert VerificationKey StakePoolKey
vkey) =
      (CBORGroup (OCert StandardCrypto), VerificationKey StakePoolKey)
-> Encoding
forall a. EncCBOR a => a -> Encoding
encCBOR (OCert StandardCrypto -> CBORGroup (OCert StandardCrypto)
forall a. a -> CBORGroup a
CBOR.CBORGroup OCert StandardCrypto
ocert, VerificationKey StakePoolKey
vkey)

instance DecCBOR OperationalCertificate where
    decCBOR :: forall s. Decoder s OperationalCertificate
decCBOR = do
      (CBOR.CBORGroup OCert StandardCrypto
ocert, VerificationKey StakePoolKey
vkey) <- Decoder
  s (CBORGroup (OCert StandardCrypto), VerificationKey StakePoolKey)
forall s.
Decoder
  s (CBORGroup (OCert StandardCrypto), VerificationKey StakePoolKey)
forall a s. DecCBOR a => Decoder s a
decCBOR
      OperationalCertificate -> Decoder s OperationalCertificate
forall a. a -> Decoder s a
forall (m :: * -> *) a. Monad m => a -> m a
return (OCert StandardCrypto
-> VerificationKey StakePoolKey -> OperationalCertificate
OperationalCertificate OCert StandardCrypto
ocert VerificationKey StakePoolKey
vkey)

instance EncCBOR OperationalCertificateIssueCounter where
    encCBOR :: OperationalCertificateIssueCounter -> Encoding
encCBOR (OperationalCertificateIssueCounter Word64
counter VerificationKey StakePoolKey
vkey) =
      (Word64, VerificationKey StakePoolKey) -> Encoding
forall a. EncCBOR a => a -> Encoding
encCBOR (Word64
counter, VerificationKey StakePoolKey
vkey)

instance DecCBOR OperationalCertificateIssueCounter where
    decCBOR :: forall s. Decoder s OperationalCertificateIssueCounter
decCBOR = do
      (Word64
counter, VerificationKey StakePoolKey
vkey) <- Decoder s (Word64, VerificationKey StakePoolKey)
forall s. Decoder s (Word64, VerificationKey StakePoolKey)
forall a s. DecCBOR a => Decoder s a
decCBOR
      OperationalCertificateIssueCounter
-> Decoder s OperationalCertificateIssueCounter
forall a. a -> Decoder s a
forall (m :: * -> *) a. Monad m => a -> m a
return (Word64
-> VerificationKey StakePoolKey
-> OperationalCertificateIssueCounter
OperationalCertificateIssueCounter Word64
counter VerificationKey StakePoolKey
vkey)

instance HasTypeProxy OperationalCertificate where
    data AsType OperationalCertificate = AsOperationalCertificate
    proxyToAsType :: Proxy OperationalCertificate -> AsType OperationalCertificate
proxyToAsType Proxy OperationalCertificate
_ = AsType OperationalCertificate
AsOperationalCertificate

instance HasTypeProxy OperationalCertificateIssueCounter where
    data AsType OperationalCertificateIssueCounter = AsOperationalCertificateIssueCounter
    proxyToAsType :: Proxy OperationalCertificateIssueCounter
-> AsType OperationalCertificateIssueCounter
proxyToAsType Proxy OperationalCertificateIssueCounter
_ = AsType OperationalCertificateIssueCounter
AsOperationalCertificateIssueCounter

instance HasTextEnvelope OperationalCertificate where
    textEnvelopeType :: AsType OperationalCertificate -> TextEnvelopeType
textEnvelopeType AsType OperationalCertificate
_ = TextEnvelopeType
"NodeOperationalCertificate"

getHotKey :: OperationalCertificate -> VerificationKey KesKey
getHotKey :: OperationalCertificate -> VerificationKey KesKey
getHotKey (OperationalCertificate OCert StandardCrypto
cert VerificationKey StakePoolKey
_) = VerKeyKES StandardCrypto -> VerificationKey KesKey
KesVerificationKey (VerKeyKES StandardCrypto -> VerificationKey KesKey)
-> VerKeyKES StandardCrypto -> VerificationKey KesKey
forall a b. (a -> b) -> a -> b
$ OCert StandardCrypto -> VerKeyKES StandardCrypto
forall c. OCert c -> VerKeyKES c
Shelley.ocertVkHot OCert StandardCrypto
cert

getKesPeriod :: OperationalCertificate -> Word
getKesPeriod :: OperationalCertificate -> Word
getKesPeriod (OperationalCertificate OCert StandardCrypto
cert VerificationKey StakePoolKey
_) = KESPeriod -> Word
Shelley.unKESPeriod (KESPeriod -> Word) -> KESPeriod -> Word
forall a b. (a -> b) -> a -> b
$ OCert StandardCrypto -> KESPeriod
forall c. OCert c -> KESPeriod
Shelley.ocertKESPeriod OCert StandardCrypto
cert

getOpCertCount :: OperationalCertificate -> Word64
getOpCertCount :: OperationalCertificate -> Word64
getOpCertCount (OperationalCertificate OCert StandardCrypto
cert VerificationKey StakePoolKey
_) = OCert StandardCrypto -> Word64
forall c. OCert c -> Word64
Shelley.ocertN OCert StandardCrypto
cert