{-# OPTIONS_GHC -Wno-orphans #-}

module Hydra.Cardano.Api.ScriptHash where

import Hydra.Cardano.Api.Prelude

import Cardano.Ledger.Credential qualified as Ledger
import Hydra.Cardano.Api.PlutusScript ()

-- * Extras

-- | Extract the payment part of an address, as a script hash.
getPaymentScriptHash :: AddressInEra era -> Maybe ScriptHash
getPaymentScriptHash :: forall era. AddressInEra era -> Maybe ScriptHash
getPaymentScriptHash = \case
  AddressInEra AddressTypeInEra addrtype era
_ (ShelleyAddress Network
_ (Ledger.ScriptHashObj ScriptHash StandardCrypto
h) StakeReference StandardCrypto
_) ->
    ScriptHash -> Maybe ScriptHash
forall a. a -> Maybe a
Just (ScriptHash StandardCrypto -> ScriptHash
fromShelleyScriptHash ScriptHash StandardCrypto
h)
  AddressInEra era
_ ->
    Maybe ScriptHash
forall a. Maybe a
Nothing

-- | Like 'hashScript', but for a 'ScriptInAnyLang'.
hashScriptInAnyLang :: ScriptInAnyLang -> ScriptHash
hashScriptInAnyLang :: ScriptInAnyLang -> ScriptHash
hashScriptInAnyLang (ScriptInAnyLang ScriptLanguage lang
_ Script lang
script) =
  Script lang -> ScriptHash
forall lang. Script lang -> ScriptHash
hashScript Script lang
script

-- * Orphans

instance Arbitrary ScriptHash where
  arbitrary :: Gen ScriptHash
arbitrary = do
    Script PlutusScriptV2 -> ScriptHash
forall lang. Script lang -> ScriptHash
hashScript (Script PlutusScriptV2 -> ScriptHash)
-> (PlutusScript PlutusScriptV2 -> Script PlutusScriptV2)
-> PlutusScript PlutusScriptV2
-> ScriptHash
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PlutusScriptVersion PlutusScriptV2
-> PlutusScript PlutusScriptV2 -> Script PlutusScriptV2
forall lang.
PlutusScriptVersion lang -> PlutusScript lang -> Script lang
PlutusScript PlutusScriptVersion PlutusScriptV2
PlutusScriptV2 (PlutusScript PlutusScriptV2 -> ScriptHash)
-> Gen (PlutusScript PlutusScriptV2) -> Gen ScriptHash
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen (PlutusScript PlutusScriptV2)
forall a. Arbitrary a => Gen a
arbitrary