{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE StandaloneDeriving #-}

module Cardano.CLI.EraBased.Script.Guard.Type
  ( Guard (..)
  , GuardScriptSource (..)
  , TxGuardArguments (..)
  , TxGuardRequirements (..)
  )
where

import Cardano.Api
import Cardano.Api.Experimental qualified as Exp

import Cardano.CLI.Type.Common

-- | The guard credential is derived from a file or supplied with a reference input.
data GuardScriptSource
  = GuardScriptOnDisk ScriptFile
  | NativeGuardScriptReference TxIn ScriptHash
  | PlutusGuardScriptReference TxIn ScriptHash
  deriving Int -> GuardScriptSource -> ShowS
[GuardScriptSource] -> ShowS
GuardScriptSource -> String
(Int -> GuardScriptSource -> ShowS)
-> (GuardScriptSource -> String)
-> ([GuardScriptSource] -> ShowS)
-> Show GuardScriptSource
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> GuardScriptSource -> ShowS
showsPrec :: Int -> GuardScriptSource -> ShowS
$cshow :: GuardScriptSource -> String
show :: GuardScriptSource -> String
$cshowList :: [GuardScriptSource] -> ShowS
showList :: [GuardScriptSource] -> ShowS
Show

-- | A Dijkstra guard supplied on the command line. Files are read before
-- constructing the API's unified 'Exp.TxGuards' collection.
data Guard
  = KeyGuard RequiredSigner
  | ScriptGuard GuardScriptSource (Maybe (ScriptDataOrFile, ExecutionUnits))
  deriving Int -> Guard -> ShowS
[Guard] -> ShowS
Guard -> String
(Int -> Guard -> ShowS)
-> (Guard -> String) -> ([Guard] -> ShowS) -> Show Guard
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Guard -> ShowS
showsPrec :: Int -> Guard -> ShowS
$cshow :: Guard -> String
show :: Guard -> String
$cshowList :: [Guard] -> ShowS
showList :: [Guard] -> ShowS
Show

-- | Conway accepts required signers; Dijkstra accepts one list of key and script guards.
data TxGuardArguments era where
  ConwayRequiredSignerArguments :: [RequiredSigner] -> TxGuardArguments Exp.ConwayEra
  DijkstraGuardArguments :: [Guard] -> TxGuardArguments Exp.DijkstraEra

deriving instance Show (TxGuardArguments era)

-- | Resolved requirements passed to the transaction builders. Dijkstra keeps
-- key and script guards together in the API's validated collection.
data TxGuardRequirements era where
  ConwayRequiredSigners :: [Hash PaymentKey] -> TxGuardRequirements Exp.ConwayEra
  DijkstraGuards
    :: Exp.TxGuards (Exp.LedgerEra Exp.DijkstraEra)
    -> TxGuardRequirements Exp.DijkstraEra