content: {-# LANGUAGE DataKinds #-}
  {-# LANGUAGE FlexibleContexts #-}
  {-# LANGUAGE FlexibleInstances #-}
  {-# LANGUAGE GADTs #-}
  {-# LANGUAGE MultiParamTypeClasses #-}
  {-# LANGUAGE OverloadedStrings #-}
  {-# LANGUAGE RankNTypes #-}
  {-# LANGUAGE RecordWildCards #-}
  {-# LANGUAGE ScopedTypeVariables #-}
  {-# LANGUAGE TypeApplications #-}
  {-# LANGUAGE TypeFamilies #-}
  {-# LANGUAGE UndecidableInstances #-}
  {-# OPTIONS_GHC -Wno-orphans #-}
  
  module Test.Cardano.Ledger.Conformance.ExecSpecRule.Conway.Base (
    externalFunctions,
  ) where
  
  import Cardano.Crypto.DSIGN (SignedDSIGN (..), verifySignedDSIGN)
  import Cardano.Ledger.Conway.Core
  import Cardano.Ledger.Keys (DSIGN, VKey (..))
  import Data.ByteString (ByteString)
  import Data.Either (isRight)
  import Data.Maybe (fromMaybe)
  import qualified MAlonzo.Code.Ledger.Foreign.API as Agda
  import Test.Cardano.Ledger.Conformance (integerToHash)
  import Test.Cardano.Ledger.Conformance.SpecTranslate.Conway ()
  import Test.Cardano.Ledger.Conformance.SpecTranslate.Core (signatureFromInteger, vkeyFromInteger)
  import Test.Cardano.Ledger.Conway.Arbitrary ()
  import Test.Cardano.Ledger.Conway.ImpTest ()
  
  -- ======================================================================
  
  externalFunctions :: Agda.ExternalFunctions
  externalFunctions = Agda.MkExternalFunctions {..}
    where
      extIsSigned vk ser sig =
        isRight $
          verifySignedDSIGN
            @DSIGN
            @(Hash HASH ByteString)
            ()
            vkey
            hash
            signature
        where
          vkey =
            unVKey
              . fromMaybe (error "Failed to convert an Agda VKey to a Haskell VKey")
              $ vkeyFromInteger vk
          hash =
            fromMaybe
              (error $ "Failed to get hash from integer:\n" <> show ser)
              $ integerToHash ser
          signature =
            SignedDSIGN
              . fromMaybe
                (error "Failed to decode the signature")
              $ signatureFromInteger sig
  