{-# OPTIONS_GHC -Wno-orphans #-}

-- Mirrors the equivalent V1 module in plutus-ledger-api
module Plutarch.LedgerApi.V1.Crypto (
  PPubKeyHash (..),
) where

import GHC.Generics (Generic)
import Plutarch.LedgerApi.Utils (Mret)
import Plutarch.Prelude
import Plutarch.Unsafe (punsafeCoerce)
import PlutusLedgerApi.V1 qualified as Plutus

-- | @since 2.0.0
newtype PPubKeyHash (s :: S) = PPubKeyHash (Term s (PDataNewtype PByteString))
  deriving stock
    ( -- | @since 2.0.0
      (forall x. PPubKeyHash s -> Rep (PPubKeyHash s) x)
-> (forall x. Rep (PPubKeyHash s) x -> PPubKeyHash s)
-> Generic (PPubKeyHash s)
forall x. Rep (PPubKeyHash s) x -> PPubKeyHash s
forall x. PPubKeyHash s -> Rep (PPubKeyHash s) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (s :: S) x. Rep (PPubKeyHash s) x -> PPubKeyHash s
forall (s :: S) x. PPubKeyHash s -> Rep (PPubKeyHash s) x
$cfrom :: forall (s :: S) x. PPubKeyHash s -> Rep (PPubKeyHash s) x
from :: forall x. PPubKeyHash s -> Rep (PPubKeyHash s) x
$cto :: forall (s :: S) x. Rep (PPubKeyHash s) x -> PPubKeyHash s
to :: forall x. Rep (PPubKeyHash s) x -> PPubKeyHash s
Generic
    )
  deriving anyclass
    ( -- | @since 2.0.0
      (forall (s :: S). PPubKeyHash s -> Term s (PInner PPubKeyHash))
-> (forall (s :: S) (b :: PType).
    Term s (PInner PPubKeyHash)
    -> (PPubKeyHash s -> Term s b) -> Term s b)
-> PlutusType PPubKeyHash
forall (s :: S). PPubKeyHash s -> Term s (PInner PPubKeyHash)
forall (s :: S) (b :: PType).
Term s (PInner PPubKeyHash)
-> (PPubKeyHash s -> Term s b) -> Term s b
forall (a :: PType).
(forall (s :: S). a s -> Term s (PInner a))
-> (forall (s :: S) (b :: PType).
    Term s (PInner a) -> (a s -> Term s b) -> Term s b)
-> PlutusType a
$cpcon' :: forall (s :: S). PPubKeyHash s -> Term s (PInner PPubKeyHash)
pcon' :: forall (s :: S). PPubKeyHash s -> Term s (PInner PPubKeyHash)
$cpmatch' :: forall (s :: S) (b :: PType).
Term s (PInner PPubKeyHash)
-> (PPubKeyHash s -> Term s b) -> Term s b
pmatch' :: forall (s :: S) (b :: PType).
Term s (PInner PPubKeyHash)
-> (PPubKeyHash s -> Term s b) -> Term s b
PlutusType
    , -- | @since 2.0.0
      (forall (s :: S).
 Term s (PAsData PPubKeyHash) -> Term s PPubKeyHash)
-> (forall (s :: S). Term s PPubKeyHash -> Term s PData)
-> PIsData PPubKeyHash
forall (s :: S). Term s (PAsData PPubKeyHash) -> Term s PPubKeyHash
forall (s :: S). Term s PPubKeyHash -> Term s PData
forall (a :: PType).
(forall (s :: S). Term s (PAsData a) -> Term s a)
-> (forall (s :: S). Term s a -> Term s PData) -> PIsData a
$cpfromDataImpl :: forall (s :: S). Term s (PAsData PPubKeyHash) -> Term s PPubKeyHash
pfromDataImpl :: forall (s :: S). Term s (PAsData PPubKeyHash) -> Term s PPubKeyHash
$cpdataImpl :: forall (s :: S). Term s PPubKeyHash -> Term s PData
pdataImpl :: forall (s :: S). Term s PPubKeyHash -> Term s PData
PIsData
    , -- | @since 2.0.0
      (forall (s :: S).
 Term s PPubKeyHash -> Term s PPubKeyHash -> Term s PBool)
-> PEq PPubKeyHash
forall (s :: S).
Term s PPubKeyHash -> Term s PPubKeyHash -> Term s PBool
forall (t :: PType).
(forall (s :: S). Term s t -> Term s t -> Term s PBool) -> PEq t
$c#== :: forall (s :: S).
Term s PPubKeyHash -> Term s PPubKeyHash -> Term s PBool
#== :: forall (s :: S).
Term s PPubKeyHash -> Term s PPubKeyHash -> Term s PBool
PEq
    , -- | @since 2.0.0
      PEq PPubKeyHash
PEq PPubKeyHash =>
(forall (s :: S).
 Term s PPubKeyHash -> Term s PPubKeyHash -> Term s PBool)
-> (forall (s :: S).
    Term s PPubKeyHash -> Term s PPubKeyHash -> Term s PBool)
-> (forall (s :: S).
    Term s PPubKeyHash -> Term s PPubKeyHash -> Term s PPubKeyHash)
-> (forall (s :: S).
    Term s PPubKeyHash -> Term s PPubKeyHash -> Term s PPubKeyHash)
-> POrd PPubKeyHash
forall (s :: S).
Term s PPubKeyHash -> Term s PPubKeyHash -> Term s PBool
forall (s :: S).
Term s PPubKeyHash -> Term s PPubKeyHash -> Term s PPubKeyHash
forall (t :: PType).
PEq t =>
(forall (s :: S). Term s t -> Term s t -> Term s PBool)
-> (forall (s :: S). Term s t -> Term s t -> Term s PBool)
-> (forall (s :: S). Term s t -> Term s t -> Term s t)
-> (forall (s :: S). Term s t -> Term s t -> Term s t)
-> POrd t
$c#<= :: forall (s :: S).
Term s PPubKeyHash -> Term s PPubKeyHash -> Term s PBool
#<= :: forall (s :: S).
Term s PPubKeyHash -> Term s PPubKeyHash -> Term s PBool
$c#< :: forall (s :: S).
Term s PPubKeyHash -> Term s PPubKeyHash -> Term s PBool
#< :: forall (s :: S).
Term s PPubKeyHash -> Term s PPubKeyHash -> Term s PBool
$cpmax :: forall (s :: S).
Term s PPubKeyHash -> Term s PPubKeyHash -> Term s PPubKeyHash
pmax :: forall (s :: S).
Term s PPubKeyHash -> Term s PPubKeyHash -> Term s PPubKeyHash
$cpmin :: forall (s :: S).
Term s PPubKeyHash -> Term s PPubKeyHash -> Term s PPubKeyHash
pmin :: forall (s :: S).
Term s PPubKeyHash -> Term s PPubKeyHash -> Term s PPubKeyHash
POrd
    , -- | @since 2.0.0
      (forall (s :: S). Bool -> Term s PPubKeyHash -> Term s PString)
-> PShow PPubKeyHash
forall (s :: S). Bool -> Term s PPubKeyHash -> Term s PString
forall (t :: PType).
(forall (s :: S). Bool -> Term s t -> Term s PString) -> PShow t
$cpshow' :: forall (s :: S). Bool -> Term s PPubKeyHash -> Term s PString
pshow' :: forall (s :: S). Bool -> Term s PPubKeyHash -> Term s PString
PShow
    )

-- | @since 2.0.0
instance DerivePlutusType PPubKeyHash where
  type DPTStrat _ = PlutusTypeNewtype

-- | @since WIP
deriving via
  DeriveDataPLiftable PPubKeyHash Plutus.PubKeyHash
  instance
    PLiftable PPubKeyHash

-- | @since 3.1.0
instance PTryFrom PData PPubKeyHash where
  type PTryFromExcess PData PPubKeyHash = Mret PPubKeyHash
  ptryFrom' :: forall (s :: S) (r :: PType).
Term s PData
-> ((Term s PPubKeyHash,
     Reduce (PTryFromExcess PData PPubKeyHash s))
    -> Term s r)
-> Term s r
ptryFrom' Term s PData
opq = TermCont
  s (Term s PPubKeyHash, Reduce (PTryFromExcess PData PPubKeyHash s))
-> ((Term s PPubKeyHash,
     Reduce (PTryFromExcess PData PPubKeyHash s))
    -> Term s r)
-> Term s r
forall (r :: PType) (s :: S) a.
TermCont s a -> (a -> Term s r) -> Term s r
runTermCont (TermCont
   s (Term s PPubKeyHash, Reduce (PTryFromExcess PData PPubKeyHash s))
 -> ((Term s PPubKeyHash,
      Reduce (PTryFromExcess PData PPubKeyHash s))
     -> Term s r)
 -> Term s r)
-> TermCont
     s (Term s PPubKeyHash, Reduce (PTryFromExcess PData PPubKeyHash s))
-> ((Term s PPubKeyHash,
     Reduce (PTryFromExcess PData PPubKeyHash s))
    -> Term s r)
-> Term s r
forall a b. (a -> b) -> a -> b
$ do
    Term s PByteString
unwrapped <- ((Term s PByteString -> Term s r) -> Term s r)
-> TermCont s (Term s PByteString)
forall a (s :: S) (r :: PType).
((a -> Term s r) -> Term s r) -> TermCont s a
tcont (((Term s PByteString -> Term s r) -> Term s r)
 -> TermCont s (Term s PByteString))
-> (Term s PByteString
    -> (Term s PByteString -> Term s r) -> Term s r)
-> Term s PByteString
-> TermCont s (Term s PByteString)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Term s PByteString -> (Term s PByteString -> Term s r) -> Term s r
forall (s :: S) (a :: PType) (b :: PType).
Term s a -> (Term s a -> Term s b) -> Term s b
plet (Term s PByteString -> TermCont s (Term s PByteString))
-> Term s PByteString -> TermCont s (Term s PByteString)
forall a b. (a -> b) -> a -> b
$ forall (b :: PType) (a :: PType) (s :: S) (r :: PType).
PTryFrom a b =>
Term s a
-> ((Term s b, Reduce (PTryFromExcess a b s)) -> Term s r)
-> Term s r
ptryFrom @(PAsData PByteString) Term s PData
opq (Term s (PAsData PByteString), Term s PByteString)
-> Term s PByteString
(Term s (PAsData PByteString),
 Reduce (PTryFromExcess PData (PAsData PByteString) s))
-> Term s PByteString
forall a b. (a, b) -> b
snd
    ((() -> Term s r) -> Term s r) -> TermCont s ()
forall a (s :: S) (r :: PType).
((a -> Term s r) -> Term s r) -> TermCont s a
tcont (((() -> Term s r) -> Term s r) -> TermCont s ())
-> ((() -> Term s r) -> Term s r) -> TermCont s ()
forall a b. (a -> b) -> a -> b
$ \() -> Term s r
f ->
      Term s PBool -> Term s r -> Term s r -> Term s r
forall (a :: PType) (s :: S).
Term s PBool -> Term s a -> Term s a -> Term s a
pif
        (Term s (PByteString :--> PInteger)
forall (s :: S). Term s (PByteString :--> PInteger)
plengthBS Term s (PByteString :--> PInteger)
-> Term s PByteString -> Term s PInteger
forall (s :: S) (a :: PType) (b :: PType).
Term s (a :--> b) -> Term s a -> Term s b
# Term s PByteString
unwrapped Term s PInteger -> Term s PInteger -> Term s PBool
forall (s :: S). Term s PInteger -> Term s PInteger -> Term s PBool
forall (t :: PType) (s :: S).
PEq t =>
Term s t -> Term s t -> Term s PBool
#== Term s PInteger
28)
        (() -> Term s r
f ())
        (Term s PString -> Term s r
forall (a :: PType) (s :: S). Term s PString -> Term s a
ptraceInfoError Term s PString
"ptryFrom(PPubKeyHash): must be 28 bytes long")
    (Term s PPubKeyHash, Term s PPubKeyHash)
-> TermCont s (Term s PPubKeyHash, Term s PPubKeyHash)
forall a. a -> TermCont s a
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure (Term s PData -> Term s PPubKeyHash
forall (b :: PType) (a :: PType) (s :: S). Term s a -> Term s b
punsafeCoerce Term s PData
opq, PPubKeyHash s -> Term s PPubKeyHash
forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon (PPubKeyHash s -> Term s PPubKeyHash)
-> (Term s PByteString -> PPubKeyHash s)
-> Term s PByteString
-> Term s PPubKeyHash
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Term s (PDataNewtype PByteString) -> PPubKeyHash s
forall (s :: S). Term s (PDataNewtype PByteString) -> PPubKeyHash s
PPubKeyHash (Term s (PDataNewtype PByteString) -> PPubKeyHash s)
-> (Term s PByteString -> Term s (PDataNewtype PByteString))
-> Term s PByteString
-> PPubKeyHash s
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PDataNewtype PByteString s -> Term s (PDataNewtype PByteString)
forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon (PDataNewtype PByteString s -> Term s (PDataNewtype PByteString))
-> (Term s PByteString -> PDataNewtype PByteString s)
-> Term s PByteString
-> Term s (PDataNewtype PByteString)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Term s (PAsData PByteString) -> PDataNewtype PByteString s
forall (a :: PType) (s :: S).
Term s (PAsData a) -> PDataNewtype a s
PDataNewtype (Term s (PAsData PByteString) -> PDataNewtype PByteString s)
-> (Term s PByteString -> Term s (PAsData PByteString))
-> Term s PByteString
-> PDataNewtype PByteString s
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Term s PByteString -> Term s (PAsData PByteString)
forall (a :: PType) (s :: S).
PIsData a =>
Term s a -> Term s (PAsData a)
pdata (Term s PByteString -> Term s PPubKeyHash)
-> Term s PByteString -> Term s PPubKeyHash
forall a b. (a -> b) -> a -> b
$ Term s PByteString
unwrapped)

-- | @since 2.0.0
instance PTryFrom PData (PAsData PPubKeyHash) where
  type PTryFromExcess PData (PAsData PPubKeyHash) = Mret PPubKeyHash
  ptryFrom' :: forall (s :: S) (r :: PType).
Term s PData
-> ((Term s (PAsData PPubKeyHash),
     Reduce (PTryFromExcess PData (PAsData PPubKeyHash) s))
    -> Term s r)
-> Term s r
ptryFrom' Term s PData
opq = TermCont
  s
  (Term s (PAsData PPubKeyHash),
   Reduce (PTryFromExcess PData (PAsData PPubKeyHash) s))
-> ((Term s (PAsData PPubKeyHash),
     Reduce (PTryFromExcess PData (PAsData PPubKeyHash) s))
    -> Term s r)
-> Term s r
forall (r :: PType) (s :: S) a.
TermCont s a -> (a -> Term s r) -> Term s r
runTermCont (TermCont
   s
   (Term s (PAsData PPubKeyHash),
    Reduce (PTryFromExcess PData (PAsData PPubKeyHash) s))
 -> ((Term s (PAsData PPubKeyHash),
      Reduce (PTryFromExcess PData (PAsData PPubKeyHash) s))
     -> Term s r)
 -> Term s r)
-> TermCont
     s
     (Term s (PAsData PPubKeyHash),
      Reduce (PTryFromExcess PData (PAsData PPubKeyHash) s))
-> ((Term s (PAsData PPubKeyHash),
     Reduce (PTryFromExcess PData (PAsData PPubKeyHash) s))
    -> Term s r)
-> Term s r
forall a b. (a -> b) -> a -> b
$ do
    Term s PByteString
unwrapped <- ((Term s PByteString -> Term s r) -> Term s r)
-> TermCont s (Term s PByteString)
forall a (s :: S) (r :: PType).
((a -> Term s r) -> Term s r) -> TermCont s a
tcont (((Term s PByteString -> Term s r) -> Term s r)
 -> TermCont s (Term s PByteString))
-> (Term s PByteString
    -> (Term s PByteString -> Term s r) -> Term s r)
-> Term s PByteString
-> TermCont s (Term s PByteString)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Term s PByteString -> (Term s PByteString -> Term s r) -> Term s r
forall (s :: S) (a :: PType) (b :: PType).
Term s a -> (Term s a -> Term s b) -> Term s b
plet (Term s PByteString -> TermCont s (Term s PByteString))
-> Term s PByteString -> TermCont s (Term s PByteString)
forall a b. (a -> b) -> a -> b
$ forall (b :: PType) (a :: PType) (s :: S) (r :: PType).
PTryFrom a b =>
Term s a
-> ((Term s b, Reduce (PTryFromExcess a b s)) -> Term s r)
-> Term s r
ptryFrom @(PAsData PByteString) Term s PData
opq (Term s (PAsData PByteString), Term s PByteString)
-> Term s PByteString
(Term s (PAsData PByteString),
 Reduce (PTryFromExcess PData (PAsData PByteString) s))
-> Term s PByteString
forall a b. (a, b) -> b
snd
    ((() -> Term s r) -> Term s r) -> TermCont s ()
forall a (s :: S) (r :: PType).
((a -> Term s r) -> Term s r) -> TermCont s a
tcont (((() -> Term s r) -> Term s r) -> TermCont s ())
-> ((() -> Term s r) -> Term s r) -> TermCont s ()
forall a b. (a -> b) -> a -> b
$ \() -> Term s r
f ->
      Term s PBool -> Term s r -> Term s r -> Term s r
forall (a :: PType) (s :: S).
Term s PBool -> Term s a -> Term s a -> Term s a
pif
        (Term s (PByteString :--> PInteger)
forall (s :: S). Term s (PByteString :--> PInteger)
plengthBS Term s (PByteString :--> PInteger)
-> Term s PByteString -> Term s PInteger
forall (s :: S) (a :: PType) (b :: PType).
Term s (a :--> b) -> Term s a -> Term s b
# Term s PByteString
unwrapped Term s PInteger -> Term s PInteger -> Term s PBool
forall (s :: S). Term s PInteger -> Term s PInteger -> Term s PBool
forall (t :: PType) (s :: S).
PEq t =>
Term s t -> Term s t -> Term s PBool
#== Term s PInteger
28)
        (() -> Term s r
f ())
        (Term s PString -> Term s r
forall (a :: PType) (s :: S). Term s PString -> Term s a
ptraceInfoError Term s PString
"ptryFrom(PPubKeyHash): must be 28 bytes long")
    (Term s (PAsData PPubKeyHash), Term s PPubKeyHash)
-> TermCont s (Term s (PAsData PPubKeyHash), Term s PPubKeyHash)
forall a. a -> TermCont s a
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure (Term s PData -> Term s (PAsData PPubKeyHash)
forall (b :: PType) (a :: PType) (s :: S). Term s a -> Term s b
punsafeCoerce Term s PData
opq, PPubKeyHash s -> Term s PPubKeyHash
forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon (PPubKeyHash s -> Term s PPubKeyHash)
-> (Term s PByteString -> PPubKeyHash s)
-> Term s PByteString
-> Term s PPubKeyHash
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Term s (PDataNewtype PByteString) -> PPubKeyHash s
forall (s :: S). Term s (PDataNewtype PByteString) -> PPubKeyHash s
PPubKeyHash (Term s (PDataNewtype PByteString) -> PPubKeyHash s)
-> (Term s PByteString -> Term s (PDataNewtype PByteString))
-> Term s PByteString
-> PPubKeyHash s
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PDataNewtype PByteString s -> Term s (PDataNewtype PByteString)
forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon (PDataNewtype PByteString s -> Term s (PDataNewtype PByteString))
-> (Term s PByteString -> PDataNewtype PByteString s)
-> Term s PByteString
-> Term s (PDataNewtype PByteString)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Term s (PAsData PByteString) -> PDataNewtype PByteString s
forall (a :: PType) (s :: S).
Term s (PAsData a) -> PDataNewtype a s
PDataNewtype (Term s (PAsData PByteString) -> PDataNewtype PByteString s)
-> (Term s PByteString -> Term s (PAsData PByteString))
-> Term s PByteString
-> PDataNewtype PByteString s
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Term s PByteString -> Term s (PAsData PByteString)
forall (a :: PType) (s :: S).
PIsData a =>
Term s a -> Term s (PAsData a)
pdata (Term s PByteString -> Term s PPubKeyHash)
-> Term s PByteString -> Term s PPubKeyHash
forall a b. (a -> b) -> a -> b
$ Term s PByteString
unwrapped)