content: {-# LANGUAGE DataKinds         #-}
  {-# LANGUAGE NoImplicitPrelude #-}
  {-# LANGUAGE TemplateHaskell   #-}
  {-# LANGUAGE TypeApplications  #-}
  {-# LANGUAGE TypeFamilies      #-}
  
  module PlutusExample.PlutusVersion1.CustomDatumRedeemerGuess
    ( MyCustomDatum(..)
    , MyCustomRedeemer(..)
    , customGuessScript
    , customDatumRedeemerGuessScriptAsShortBs
    ) where
  
  import Prelude hiding (($), (&&), (==))
  
  import Cardano.Api.Shelley (PlutusScript (..), PlutusScriptV1)
  
  import Codec.Serialise
  import Data.ByteString.Lazy qualified as LBS
  import Data.ByteString.Short qualified as SBS
  
  import Plutus.Script.Utils.Typed qualified as Scripts
  import Plutus.V1.Ledger.Api qualified as Plutus
  import Plutus.V1.Ledger.Contexts (ScriptContext)
  import PlutusTx qualified
  import PlutusTx.Prelude hiding (Semigroup ((<>)), unless, (.))
  
  newtype MyCustomDatum = MyCustomDatum Integer
  newtype MyCustomRedeemer = MyCustomRedeemer Integer
  
  PlutusTx.unstableMakeIsData ''MyCustomDatum
  PlutusTx.unstableMakeIsData ''MyCustomRedeemer
  
  {-# INLINABLE mkValidator #-}
  mkValidator :: MyCustomDatum -> MyCustomRedeemer -> ScriptContext -> Bool
  mkValidator (MyCustomDatum d) (MyCustomRedeemer r) _ =
    d == 42 && r == 42
  
  validator :: Plutus.Validator
  validator = Plutus.mkValidatorScript
      $$(PlutusTx.compile [|| wrap ||])
   where
       wrap = Scripts.mkUntypedValidator mkValidator
  
  script :: Plutus.Script
  script = Plutus.unValidatorScript validator
  
  customDatumRedeemerGuessScriptAsShortBs :: SBS.ShortByteString
  customDatumRedeemerGuessScriptAsShortBs = SBS.toShort . LBS.toStrict $ serialise script
  
  customGuessScript :: PlutusScript PlutusScriptV1
  customGuessScript = PlutusScriptSerialised customDatumRedeemerGuessScriptAsShortBs
  