module Tendermint.Utils.User where

import           Crypto.Secp256k1                (SecKey, derivePubKey, secKey)
import qualified Data.ByteArray.HexString        as Hex
import           Data.Maybe                      (fromJust)
import           Data.Proxy
import           Data.String                     (fromString)
import           Tendermint.SDK.Crypto           (Secp256k1, addressFromPubKey)
import           Tendermint.SDK.Types.Address    (Address (..))
import           Tendermint.Utils.TxClient.Types (Signer, makeSignerFromKey)

data User = User
  { User -> SecKey
userPrivKey :: SecKey
  , User -> Address
userAddress :: Address
  }

makeUser :: String -> User
makeUser :: String -> User
makeUser privKeyStr :: String
privKeyStr =
  let privateKey :: SecKey
privateKey = Maybe SecKey -> SecKey
forall a. HasCallStack => Maybe a -> a
fromJust (Maybe SecKey -> SecKey)
-> (String -> Maybe SecKey) -> String -> SecKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Maybe SecKey
secKey (ByteString -> Maybe SecKey)
-> (String -> ByteString) -> String -> Maybe SecKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HexString -> ByteString
forall ba. ByteArray ba => HexString -> ba
Hex.toBytes (HexString -> ByteString)
-> (String -> HexString) -> String -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> HexString
forall a. IsString a => String -> a
fromString (String -> SecKey) -> String -> SecKey
forall a b. (a -> b) -> a -> b
$ String
privKeyStr
      pubKey :: PubKey
pubKey = SecKey -> PubKey
derivePubKey SecKey
privateKey
      address :: Address
address = Proxy Secp256k1 -> PubKey Secp256k1 -> Address
forall k (alg :: k).
SignatureSchema alg =>
Proxy alg -> PubKey alg -> Address
addressFromPubKey (Proxy Secp256k1
forall k (t :: k). Proxy t
Proxy @Secp256k1) PubKey Secp256k1
PubKey
pubKey
  in SecKey -> Address -> User
User SecKey
privateKey Address
address

makeSignerFromUser
  :: User
  -> Signer
makeSignerFromUser :: User -> Signer
makeSignerFromUser User{SecKey
userPrivKey :: SecKey
userPrivKey :: User -> SecKey
userPrivKey} =
  Proxy Secp256k1 -> PrivateKey Secp256k1 -> Signer
forall alg.
(RecoverableSignatureSchema alg, Message alg ~ Digest SHA256) =>
Proxy alg -> PrivateKey alg -> Signer
makeSignerFromKey (Proxy Secp256k1
forall k (t :: k). Proxy t
Proxy @Secp256k1) PrivateKey Secp256k1
SecKey
userPrivKey