-- SPDX-FileCopyrightText: 2021 Oxhead Alpha
-- SPDX-License-Identifier: LicenseRef-MIT-OA

-- | Abstraction layer for @tezos-client@ functionality.
-- We use it to fake @tezos-client@ in tests.

module Morley.Client.TezosClient.Class
  ( HasTezosClient (..)
  ) where

import Data.ByteArray (ScrubbedBytes)

import Morley.Tezos.Address
import Morley.Tezos.Address.Alias
import Morley.Tezos.Crypto

-- | Type class that provides interaction with @tezos-client@ binary
class (Monad m) => HasTezosClient m where
  signBytes :: ImplicitAddressOrAlias -> Maybe ScrubbedBytes -> ByteString -> m Signature
  -- ^ Sign an operation with @tezos-client@.
  genKey :: ImplicitAlias -> m ImplicitAddress
  -- ^ Generate a secret key and store it with given alias.
  -- If a key with this alias already exists, the corresponding address
  -- will be returned and no state will be changed.
  genFreshKey :: ImplicitAlias -> m ImplicitAddress
  -- ^ Generate a secret key and store it with given alias.
  -- Unlike 'genKey' this function overwrites
  -- the existing key when given alias is already stored.
  revealKey :: ImplicitAlias -> Maybe ScrubbedBytes -> m ()
  -- ^ Reveal public key associated with given implicit account.
  rememberContract :: Bool -> ContractAddress -> ContractAlias -> m ()
  -- ^ Associate the given contract with alias.
  -- The 'Bool' variable indicates whether or not we should replace already
  -- existing contract alias or not.
  resolveAddressMaybe :: AddressOrAlias kind -> m (Maybe (KindedAddress kind))
  -- ^ Retrieve an address from given address or alias. If address or alias does not exist
  -- returns `Nothing`
  getAlias :: L1AddressKind kind => AddressOrAlias kind -> m (Alias kind)
  -- ^ Retrieve an alias from given address using @tezos-client@.  The
  -- primary (and probably only) reason this function exists is that
  -- @tezos-client sign@ command only works with aliases. It was
  -- reported upstream: <https://gitlab.com/tezos/tezos/-/issues/836>.
  registerDelegate :: ImplicitAlias -> Maybe ScrubbedBytes -> m ()
  -- ^ Register a given address as delegate
  -- TODO [#869] move to HasTezosRpc
  getKeyPassword :: ImplicitAddress -> m (Maybe ScrubbedBytes)
  -- ^ Get password for secret key associated with given address
  -- in case this key is password-protected. Obtained password is used
  -- in two places:
  --   * 1) In @signBytes@ call.
  --   * 2) in @revealKey@ call.