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

module Cardano.Chain.Update.InstallerHash (
  InstallerHash (..),
)
where

import Cardano.Crypto (Hash, hashRaw)
import Cardano.Crypto.Raw (Raw)
import Cardano.Ledger.Binary (
  DecCBOR (..),
  EncCBOR (..),
  FromCBOR (..),
  ToCBOR (..),
  dropBytes,
  encodeListLen,
  enforceSize,
  fromByronCBOR,
  toByronCBOR,
 )
import Cardano.Prelude
import Data.Aeson (ToJSON)
import Formatting (bprint, build)
import qualified Formatting.Buildable as B
import NoThunks.Class (NoThunks (..))

-- | The hash of the installer of the new application
newtype InstallerHash = InstallerHash
  { InstallerHash -> Hash Raw
unInstallerHash :: Hash Raw
  }
  deriving (InstallerHash -> InstallerHash -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: InstallerHash -> InstallerHash -> Bool
$c/= :: InstallerHash -> InstallerHash -> Bool
== :: InstallerHash -> InstallerHash -> Bool
$c== :: InstallerHash -> InstallerHash -> Bool
Eq, Int -> InstallerHash -> ShowS
[InstallerHash] -> ShowS
InstallerHash -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [InstallerHash] -> ShowS
$cshowList :: [InstallerHash] -> ShowS
show :: InstallerHash -> String
$cshow :: InstallerHash -> String
showsPrec :: Int -> InstallerHash -> ShowS
$cshowsPrec :: Int -> InstallerHash -> ShowS
Show, forall x. Rep InstallerHash x -> InstallerHash
forall x. InstallerHash -> Rep InstallerHash x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep InstallerHash x -> InstallerHash
$cfrom :: forall x. InstallerHash -> Rep InstallerHash x
Generic)
  deriving anyclass (InstallerHash -> ()
forall a. (a -> ()) -> NFData a
rnf :: InstallerHash -> ()
$crnf :: InstallerHash -> ()
NFData, Context -> InstallerHash -> IO (Maybe ThunkInfo)
Proxy InstallerHash -> String
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> String)
-> NoThunks a
showTypeOf :: Proxy InstallerHash -> String
$cshowTypeOf :: Proxy InstallerHash -> String
wNoThunks :: Context -> InstallerHash -> IO (Maybe ThunkInfo)
$cwNoThunks :: Context -> InstallerHash -> IO (Maybe ThunkInfo)
noThunks :: Context -> InstallerHash -> IO (Maybe ThunkInfo)
$cnoThunks :: Context -> InstallerHash -> IO (Maybe ThunkInfo)
NoThunks)

instance B.Buildable InstallerHash where
  build :: InstallerHash -> Builder
build (InstallerHash Hash Raw
h) = forall a. Format Builder a -> a
bprint (Format (Hash Raw -> Builder) (Hash Raw -> Builder)
"{ installer hash: " forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall a r. Buildable a => Format r (a -> r)
build forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Format Builder Builder
" }") Hash Raw
h

instance ToCBOR InstallerHash where
  toCBOR :: InstallerHash -> Encoding
toCBOR = forall a. EncCBOR a => a -> Encoding
toByronCBOR

instance FromCBOR InstallerHash where
  fromCBOR :: forall s. Decoder s InstallerHash
fromCBOR = forall a s. DecCBOR a => Decoder s a
fromByronCBOR

-- Used for debugging purposes only
instance ToJSON InstallerHash

instance EncCBOR InstallerHash where
  encCBOR :: InstallerHash -> Encoding
encCBOR (InstallerHash Hash Raw
h) =
    Word -> Encoding
encodeListLen Word
4
      forall a. Semigroup a => a -> a -> a
<> forall a. EncCBOR a => a -> Encoding
encCBOR Hash Raw
emptyHash
      forall a. Semigroup a => a -> a -> a
<> forall a. EncCBOR a => a -> Encoding
encCBOR Hash Raw
h
      forall a. Semigroup a => a -> a -> a
<> forall a. EncCBOR a => a -> Encoding
encCBOR Hash Raw
emptyHash
      forall a. Semigroup a => a -> a -> a
<> forall a. EncCBOR a => a -> Encoding
encCBOR Hash Raw
emptyHash
    where
      emptyHash :: Hash Raw
emptyHash = ByteString -> Hash Raw
hashRaw ByteString
"\NUL"

instance DecCBOR InstallerHash where
  decCBOR :: forall s. Decoder s InstallerHash
decCBOR = do
    forall s. Text -> Int -> Decoder s ()
enforceSize Text
"InstallerHash" Int
4
    forall s. Dropper s
dropBytes
    Hash Raw
h <- forall a s. DecCBOR a => Decoder s a
decCBOR
    forall s. Dropper s
dropBytes
    forall s. Dropper s
dropBytes
    forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ Hash Raw -> InstallerHash
InstallerHash Hash Raw
h