{-# LANGUAGE TemplateHaskell #-}

module BtcLsp.Data.Env
  ( Env (..),
    RawConfig (..),
    BitcoindEnv (..),
    readRawConfig,
    readGCEnv,
    withEnv,
    parseFromJSON,
  )
where

import BtcLsp.Data.Kind
import BtcLsp.Data.Type
import BtcLsp.Grpc.Client.LowLevel
import BtcLsp.Grpc.Server.LowLevel
import qualified BtcLsp.Grpc.Sig as Sig
import BtcLsp.Import.External
import qualified BtcLsp.Import.Psql as Psql
import qualified BtcLsp.Math.Swap as Math
import Control.Monad.Logger (runNoLoggingT)
import Data.Aeson (withObject, (.:))
import qualified Data.Aeson as A (decode)
import qualified Data.ByteString as BS
import Data.ByteString.Lazy.Char8 as C8L (pack)
import qualified Env as E
  ( Error (..),
    Mod,
    Var,
    auto,
    def,
    header,
    help,
    keep,
    nonempty,
    parse,
    str,
    var,
  )
import qualified LndClient as Lnd
import qualified LndClient.Data.SignMessage as Lnd
import qualified LndClient.RPC.Katip as Lnd
import qualified Network.Bitcoin as Btc

data Env = Env
  { -- | General
    Env -> Pool SqlBackend
envSQLPool :: Pool Psql.SqlBackend,
    Env -> Liquidity 'Outgoing
envMinTotalExtOutgoingLiquidity :: Liquidity 'Outgoing,
    Env -> Liquidity 'Incoming
envMinTotalExtIncomingLiquidity :: Liquidity 'Incoming,
    Env -> MSat
envMinTotalOnChainLiquidity :: MSat,
    -- | Logging
    Env -> Namespace
envKatipNS :: Namespace,
    Env -> LogContexts
envKatipCTX :: LogContexts,
    Env -> LogEnv
envKatipLE :: LogEnv,
    Env -> YesodLog
envYesodLog :: YesodLog,
    -- | Lnd
    Env -> LndEnv
envLnd :: Lnd.LndEnv,
    Env -> String
envLndP2PHost :: HostName,
    Env -> PortNumber
envLndP2PPort :: PortNumber,
    Env -> Money 'Usr 'OnChain 'Fund
envSwapIntoLnMinAmt :: Money 'Usr 'OnChain 'Fund,
    Env -> Maybe MSat
envMsatPerByte :: Maybe MSat,
    Env -> MVar NodePubKey
envLndPubKey :: MVar Lnd.NodePubKey,
    -- | Grpc
    Env -> GSEnv
envGrpcServer :: GSEnv,
    -- | Bitcoind
    Env -> Client
envBtc :: Btc.Client
  }

data RawConfig = RawConfig
  { -- | General
    RawConfig -> ConnectionString
rawConfigLibpqConnStr :: Psql.ConnectionString,
    RawConfig -> Liquidity 'Outgoing
rawConfigMinTotalExtOutgoingLiquidity :: Liquidity 'Outgoing,
    RawConfig -> Liquidity 'Incoming
rawConfigMinTotalExtIncomingLiquidity :: Liquidity 'Incoming,
    RawConfig -> MSat
rawConfigMinTotalOnChainLiquidity :: MSat,
    -- | Logging
    RawConfig -> Text
rawConfigLogEnv :: Text,
    RawConfig -> LogFormat
rawConfigLogFormat :: LogFormat,
    RawConfig -> Verbosity
rawConfigLogVerbosity :: Verbosity,
    RawConfig -> Severity
rawConfigLogSeverity :: Severity,
    RawConfig -> SecretVision
rawConfigLogSecrets :: SecretVision,
    RawConfig -> YesodLog
rawConfigLogYesod :: YesodLog,
    -- | Lnd
    RawConfig -> LndEnv
rawConfigLndEnv :: Lnd.LndEnv,
    RawConfig -> String
rawConfigLndP2PHost :: HostName,
    RawConfig -> PortNumber
rawConfigLndP2PPort :: PortNumber,
    RawConfig -> Money 'Chan 'Ln 'Fund
rawConfigMinChanCap :: Money 'Chan 'Ln 'Fund,
    RawConfig -> Maybe MSat
rawConfigMsatPerByte :: Maybe MSat,
    -- | Grpc
    RawConfig -> GSEnv
rawConfigGrpcServerEnv :: GSEnv,
    -- | Bitcoind
    RawConfig -> BitcoindEnv
rawConfigBtcEnv :: BitcoindEnv
  }

data BitcoindEnv = BitcoindEnv
  { BitcoindEnv -> Text
bitcoindEnvHost :: Text,
    BitcoindEnv -> Text
bitcoindEnvUsername :: Text,
    BitcoindEnv -> Text
bitcoindEnvPassword :: Text
  }
  deriving stock ((forall x. BitcoindEnv -> Rep BitcoindEnv x)
-> (forall x. Rep BitcoindEnv x -> BitcoindEnv)
-> Generic BitcoindEnv
forall x. Rep BitcoindEnv x -> BitcoindEnv
forall x. BitcoindEnv -> Rep BitcoindEnv x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep BitcoindEnv x -> BitcoindEnv
$cfrom :: forall x. BitcoindEnv -> Rep BitcoindEnv x
Generic)

instance FromJSON BitcoindEnv where
  parseJSON :: Value -> Parser BitcoindEnv
parseJSON =
    String
-> (Object -> Parser BitcoindEnv) -> Value -> Parser BitcoindEnv
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject
      String
"BitcoindEnv"
      ( \Object
x ->
          Text -> Text -> Text -> BitcoindEnv
BitcoindEnv
            (Text -> Text -> Text -> BitcoindEnv)
-> Parser Text -> Parser (Text -> Text -> BitcoindEnv)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
x Object -> Text -> Parser Text
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"host"
            Parser (Text -> Text -> BitcoindEnv)
-> Parser Text -> Parser (Text -> BitcoindEnv)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
x Object -> Text -> Parser Text
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"username"
            Parser (Text -> BitcoindEnv) -> Parser Text -> Parser BitcoindEnv
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
x Object -> Text -> Parser Text
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"password"
      )

parseFromJSON :: (FromJSON a) => String -> Either E.Error a
parseFromJSON :: forall a. FromJSON a => String -> Either Error a
parseFromJSON =
  Either Error a
-> (a -> Either Error a) -> Maybe a -> Either Error a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe
    (Error -> Either Error a
forall a b. a -> Either a b
Left (Error -> Either Error a) -> Error -> Either Error a
forall a b. (a -> b) -> a -> b
$ String -> Error
E.UnreadError String
"parseFromJSON failed")
    a -> Either Error a
forall a b. b -> Either a b
Right
    (Maybe a -> Either Error a)
-> (String -> Maybe a) -> String -> Either Error a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Maybe a
forall a. FromJSON a => ByteString -> Maybe a
A.decode
    (ByteString -> Maybe a)
-> (String -> ByteString) -> String -> Maybe a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ByteString
C8L.pack

readRawConfig :: IO RawConfig
readRawConfig :: IO RawConfig
readRawConfig =
  (Info Error -> Info Error)
-> Parser Error RawConfig -> IO RawConfig
forall e a. (Info Error -> Info e) -> Parser e a -> IO a
E.parse (String -> Info Error -> Info Error
forall e. String -> Info e -> Info e
E.header String
"BtcLsp") (Parser Error RawConfig -> IO RawConfig)
-> Parser Error RawConfig -> IO RawConfig
forall a b. (a -> b) -> a -> b
$
    ConnectionString
-> Liquidity 'Outgoing
-> Liquidity 'Incoming
-> MSat
-> Text
-> LogFormat
-> Verbosity
-> Severity
-> SecretVision
-> YesodLog
-> LndEnv
-> String
-> PortNumber
-> Money 'Chan 'Ln 'Fund
-> Maybe MSat
-> GSEnv
-> BitcoindEnv
-> RawConfig
RawConfig
      -- General
      (ConnectionString
 -> Liquidity 'Outgoing
 -> Liquidity 'Incoming
 -> MSat
 -> Text
 -> LogFormat
 -> Verbosity
 -> Severity
 -> SecretVision
 -> YesodLog
 -> LndEnv
 -> String
 -> PortNumber
 -> Money 'Chan 'Ln 'Fund
 -> Maybe MSat
 -> GSEnv
 -> BitcoindEnv
 -> RawConfig)
-> Parser Error ConnectionString
-> Parser
     Error
     (Liquidity 'Outgoing
      -> Liquidity 'Incoming
      -> MSat
      -> Text
      -> LogFormat
      -> Verbosity
      -> Severity
      -> SecretVision
      -> YesodLog
      -> LndEnv
      -> String
      -> PortNumber
      -> Money 'Chan 'Ln 'Fund
      -> Maybe MSat
      -> GSEnv
      -> BitcoindEnv
      -> RawConfig)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Reader Error ConnectionString
-> String
-> Mod Var ConnectionString
-> Parser Error ConnectionString
forall e a.
AsUnset e =>
Reader e a -> String -> Mod Var a -> Parser e a
E.var (Reader Error ConnectionString
forall s e. IsString s => Reader e s
E.str Reader Error ConnectionString
-> (String -> Either Error String) -> Reader Error ConnectionString
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< String -> Either Error String
forall e s. (AsEmpty e, IsString s) => Reader e s
E.nonempty) String
"LSP_LIBPQ_CONN_STR" Mod Var ConnectionString
forall a. Mod Var a
opts
      Parser
  Error
  (Liquidity 'Outgoing
   -> Liquidity 'Incoming
   -> MSat
   -> Text
   -> LogFormat
   -> Verbosity
   -> Severity
   -> SecretVision
   -> YesodLog
   -> LndEnv
   -> String
   -> PortNumber
   -> Money 'Chan 'Ln 'Fund
   -> Maybe MSat
   -> GSEnv
   -> BitcoindEnv
   -> RawConfig)
-> Parser Error (Liquidity 'Outgoing)
-> Parser
     Error
     (Liquidity 'Incoming
      -> MSat
      -> Text
      -> LogFormat
      -> Verbosity
      -> Severity
      -> SecretVision
      -> YesodLog
      -> LndEnv
      -> String
      -> PortNumber
      -> Money 'Chan 'Ln 'Fund
      -> Maybe MSat
      -> GSEnv
      -> BitcoindEnv
      -> RawConfig)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Reader Error (Liquidity 'Outgoing)
-> String
-> Mod Var (Liquidity 'Outgoing)
-> Parser Error (Liquidity 'Outgoing)
forall e a.
AsUnset e =>
Reader e a -> String -> Mod Var a -> Parser e a
E.var (Reader Error (Liquidity 'Outgoing)
forall e a. (AsUnread e, Read a) => Reader e a
E.auto Reader Error (Liquidity 'Outgoing)
-> (String -> Either Error String)
-> Reader Error (Liquidity 'Outgoing)
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< String -> Either Error String
forall e s. (AsEmpty e, IsString s) => Reader e s
E.nonempty) String
"LSP_MIN_TOTAL_EXT_OUTGOING_LIQUIDITY_MSAT" (Mod Var (Liquidity 'Outgoing)
forall a. Mod Var a
opts Mod Var (Liquidity 'Outgoing)
-> Mod Var (Liquidity 'Outgoing) -> Mod Var (Liquidity 'Outgoing)
forall a. Semigroup a => a -> a -> a
<> Liquidity 'Outgoing -> Mod Var (Liquidity 'Outgoing)
forall a. a -> Mod Var a
E.def Liquidity 'Outgoing
0)
      Parser
  Error
  (Liquidity 'Incoming
   -> MSat
   -> Text
   -> LogFormat
   -> Verbosity
   -> Severity
   -> SecretVision
   -> YesodLog
   -> LndEnv
   -> String
   -> PortNumber
   -> Money 'Chan 'Ln 'Fund
   -> Maybe MSat
   -> GSEnv
   -> BitcoindEnv
   -> RawConfig)
-> Parser Error (Liquidity 'Incoming)
-> Parser
     Error
     (MSat
      -> Text
      -> LogFormat
      -> Verbosity
      -> Severity
      -> SecretVision
      -> YesodLog
      -> LndEnv
      -> String
      -> PortNumber
      -> Money 'Chan 'Ln 'Fund
      -> Maybe MSat
      -> GSEnv
      -> BitcoindEnv
      -> RawConfig)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Reader Error (Liquidity 'Incoming)
-> String
-> Mod Var (Liquidity 'Incoming)
-> Parser Error (Liquidity 'Incoming)
forall e a.
AsUnset e =>
Reader e a -> String -> Mod Var a -> Parser e a
E.var (Reader Error (Liquidity 'Incoming)
forall e a. (AsUnread e, Read a) => Reader e a
E.auto Reader Error (Liquidity 'Incoming)
-> (String -> Either Error String)
-> Reader Error (Liquidity 'Incoming)
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< String -> Either Error String
forall e s. (AsEmpty e, IsString s) => Reader e s
E.nonempty) String
"LSP_MIN_TOTAL_EXT_INCOMING_LIQUIDITY_MSAT" (Mod Var (Liquidity 'Incoming)
forall a. Mod Var a
opts Mod Var (Liquidity 'Incoming)
-> Mod Var (Liquidity 'Incoming) -> Mod Var (Liquidity 'Incoming)
forall a. Semigroup a => a -> a -> a
<> Liquidity 'Incoming -> Mod Var (Liquidity 'Incoming)
forall a. a -> Mod Var a
E.def Liquidity 'Incoming
0)
      Parser
  Error
  (MSat
   -> Text
   -> LogFormat
   -> Verbosity
   -> Severity
   -> SecretVision
   -> YesodLog
   -> LndEnv
   -> String
   -> PortNumber
   -> Money 'Chan 'Ln 'Fund
   -> Maybe MSat
   -> GSEnv
   -> BitcoindEnv
   -> RawConfig)
-> Parser Error MSat
-> Parser
     Error
     (Text
      -> LogFormat
      -> Verbosity
      -> Severity
      -> SecretVision
      -> YesodLog
      -> LndEnv
      -> String
      -> PortNumber
      -> Money 'Chan 'Ln 'Fund
      -> Maybe MSat
      -> GSEnv
      -> BitcoindEnv
      -> RawConfig)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Reader Error MSat -> String -> Mod Var MSat -> Parser Error MSat
forall e a.
AsUnset e =>
Reader e a -> String -> Mod Var a -> Parser e a
E.var (Reader Error MSat
forall e a. (AsUnread e, Read a) => Reader e a
E.auto Reader Error MSat
-> (String -> Either Error String) -> Reader Error MSat
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< String -> Either Error String
forall e s. (AsEmpty e, IsString s) => Reader e s
E.nonempty) String
"LSP_MIN_TOTAL_ON_CHAIN_LIQUIDITY_MSAT" (Mod Var MSat
forall a. Mod Var a
opts Mod Var MSat -> Mod Var MSat -> Mod Var MSat
forall a. Semigroup a => a -> a -> a
<> MSat -> Mod Var MSat
forall a. a -> Mod Var a
E.def MSat
0)
      -- Logging
      Parser
  Error
  (Text
   -> LogFormat
   -> Verbosity
   -> Severity
   -> SecretVision
   -> YesodLog
   -> LndEnv
   -> String
   -> PortNumber
   -> Money 'Chan 'Ln 'Fund
   -> Maybe MSat
   -> GSEnv
   -> BitcoindEnv
   -> RawConfig)
-> Parser Error Text
-> Parser
     Error
     (LogFormat
      -> Verbosity
      -> Severity
      -> SecretVision
      -> YesodLog
      -> LndEnv
      -> String
      -> PortNumber
      -> Money 'Chan 'Ln 'Fund
      -> Maybe MSat
      -> GSEnv
      -> BitcoindEnv
      -> RawConfig)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Reader Error Text -> String -> Mod Var Text -> Parser Error Text
forall e a.
AsUnset e =>
Reader e a -> String -> Mod Var a -> Parser e a
E.var (Reader Error Text
forall s e. IsString s => Reader e s
E.str Reader Error Text
-> (String -> Either Error String) -> Reader Error Text
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< String -> Either Error String
forall e s. (AsEmpty e, IsString s) => Reader e s
E.nonempty) String
"LSP_LOG_ENV" Mod Var Text
forall a. Mod Var a
opts
      Parser
  Error
  (LogFormat
   -> Verbosity
   -> Severity
   -> SecretVision
   -> YesodLog
   -> LndEnv
   -> String
   -> PortNumber
   -> Money 'Chan 'Ln 'Fund
   -> Maybe MSat
   -> GSEnv
   -> BitcoindEnv
   -> RawConfig)
-> Parser Error LogFormat
-> Parser
     Error
     (Verbosity
      -> Severity
      -> SecretVision
      -> YesodLog
      -> LndEnv
      -> String
      -> PortNumber
      -> Money 'Chan 'Ln 'Fund
      -> Maybe MSat
      -> GSEnv
      -> BitcoindEnv
      -> RawConfig)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Reader Error LogFormat
-> String -> Mod Var LogFormat -> Parser Error LogFormat
forall e a.
AsUnset e =>
Reader e a -> String -> Mod Var a -> Parser e a
E.var (Reader Error LogFormat
forall e a. (AsUnread e, Read a) => Reader e a
E.auto Reader Error LogFormat
-> (String -> Either Error String) -> Reader Error LogFormat
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< String -> Either Error String
forall e s. (AsEmpty e, IsString s) => Reader e s
E.nonempty) String
"LSP_LOG_FORMAT" Mod Var LogFormat
forall a. Mod Var a
opts
      Parser
  Error
  (Verbosity
   -> Severity
   -> SecretVision
   -> YesodLog
   -> LndEnv
   -> String
   -> PortNumber
   -> Money 'Chan 'Ln 'Fund
   -> Maybe MSat
   -> GSEnv
   -> BitcoindEnv
   -> RawConfig)
-> Parser Error Verbosity
-> Parser
     Error
     (Severity
      -> SecretVision
      -> YesodLog
      -> LndEnv
      -> String
      -> PortNumber
      -> Money 'Chan 'Ln 'Fund
      -> Maybe MSat
      -> GSEnv
      -> BitcoindEnv
      -> RawConfig)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Reader Error Verbosity
-> String -> Mod Var Verbosity -> Parser Error Verbosity
forall e a.
AsUnset e =>
Reader e a -> String -> Mod Var a -> Parser e a
E.var (Reader Error Verbosity
forall e a. (AsUnread e, Read a) => Reader e a
E.auto Reader Error Verbosity
-> (String -> Either Error String) -> Reader Error Verbosity
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< String -> Either Error String
forall e s. (AsEmpty e, IsString s) => Reader e s
E.nonempty) String
"LSP_LOG_VERBOSITY" Mod Var Verbosity
forall a. Mod Var a
opts
      Parser
  Error
  (Severity
   -> SecretVision
   -> YesodLog
   -> LndEnv
   -> String
   -> PortNumber
   -> Money 'Chan 'Ln 'Fund
   -> Maybe MSat
   -> GSEnv
   -> BitcoindEnv
   -> RawConfig)
-> Parser Error Severity
-> Parser
     Error
     (SecretVision
      -> YesodLog
      -> LndEnv
      -> String
      -> PortNumber
      -> Money 'Chan 'Ln 'Fund
      -> Maybe MSat
      -> GSEnv
      -> BitcoindEnv
      -> RawConfig)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Reader Error Severity
-> String -> Mod Var Severity -> Parser Error Severity
forall e a.
AsUnset e =>
Reader e a -> String -> Mod Var a -> Parser e a
E.var (Reader Error Severity
forall e a. (AsUnread e, Read a) => Reader e a
E.auto Reader Error Severity
-> (String -> Either Error String) -> Reader Error Severity
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< String -> Either Error String
forall e s. (AsEmpty e, IsString s) => Reader e s
E.nonempty) String
"LSP_LOG_SEVERITY" Mod Var Severity
forall a. Mod Var a
opts
      Parser
  Error
  (SecretVision
   -> YesodLog
   -> LndEnv
   -> String
   -> PortNumber
   -> Money 'Chan 'Ln 'Fund
   -> Maybe MSat
   -> GSEnv
   -> BitcoindEnv
   -> RawConfig)
-> Parser Error SecretVision
-> Parser
     Error
     (YesodLog
      -> LndEnv
      -> String
      -> PortNumber
      -> Money 'Chan 'Ln 'Fund
      -> Maybe MSat
      -> GSEnv
      -> BitcoindEnv
      -> RawConfig)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Reader Error SecretVision
-> String -> Mod Var SecretVision -> Parser Error SecretVision
forall e a.
AsUnset e =>
Reader e a -> String -> Mod Var a -> Parser e a
E.var (Reader Error SecretVision
forall e a. (AsUnread e, Read a) => Reader e a
E.auto Reader Error SecretVision
-> (String -> Either Error String) -> Reader Error SecretVision
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< String -> Either Error String
forall e s. (AsEmpty e, IsString s) => Reader e s
E.nonempty) String
"LSP_LOG_SECRET" (Mod Var SecretVision
forall a. Mod Var a
opts Mod Var SecretVision
-> Mod Var SecretVision -> Mod Var SecretVision
forall a. Semigroup a => a -> a -> a
<> SecretVision -> Mod Var SecretVision
forall a. a -> Mod Var a
E.def SecretVision
SecretHidden)
      Parser
  Error
  (YesodLog
   -> LndEnv
   -> String
   -> PortNumber
   -> Money 'Chan 'Ln 'Fund
   -> Maybe MSat
   -> GSEnv
   -> BitcoindEnv
   -> RawConfig)
-> Parser Error YesodLog
-> Parser
     Error
     (LndEnv
      -> String
      -> PortNumber
      -> Money 'Chan 'Ln 'Fund
      -> Maybe MSat
      -> GSEnv
      -> BitcoindEnv
      -> RawConfig)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Reader Error YesodLog
-> String -> Mod Var YesodLog -> Parser Error YesodLog
forall e a.
AsUnset e =>
Reader e a -> String -> Mod Var a -> Parser e a
E.var (Reader Error YesodLog
forall e a. (AsUnread e, Read a) => Reader e a
E.auto Reader Error YesodLog
-> (String -> Either Error String) -> Reader Error YesodLog
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< String -> Either Error String
forall e s. (AsEmpty e, IsString s) => Reader e s
E.nonempty) String
"LSP_LOG_YESOD" (Mod Var YesodLog
forall a. Mod Var a
opts Mod Var YesodLog -> Mod Var YesodLog -> Mod Var YesodLog
forall a. Semigroup a => a -> a -> a
<> YesodLog -> Mod Var YesodLog
forall a. a -> Mod Var a
E.def YesodLog
YesodLogNoMain)
      -- Lnd
      Parser
  Error
  (LndEnv
   -> String
   -> PortNumber
   -> Money 'Chan 'Ln 'Fund
   -> Maybe MSat
   -> GSEnv
   -> BitcoindEnv
   -> RawConfig)
-> Parser Error LndEnv
-> Parser
     Error
     (String
      -> PortNumber
      -> Money 'Chan 'Ln 'Fund
      -> Maybe MSat
      -> GSEnv
      -> BitcoindEnv
      -> RawConfig)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Reader Error LndEnv
-> String -> Mod Var LndEnv -> Parser Error LndEnv
forall e a.
AsUnset e =>
Reader e a -> String -> Mod Var a -> Parser e a
E.var (Reader Error LndEnv
forall a. FromJSON a => String -> Either Error a
parseFromJSON Reader Error LndEnv
-> (String -> Either Error String) -> Reader Error LndEnv
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< String -> Either Error String
forall e s. (AsEmpty e, IsString s) => Reader e s
E.nonempty) String
"LSP_LND_ENV" Mod Var LndEnv
forall a. Mod Var a
opts
      Parser
  Error
  (String
   -> PortNumber
   -> Money 'Chan 'Ln 'Fund
   -> Maybe MSat
   -> GSEnv
   -> BitcoindEnv
   -> RawConfig)
-> Parser Error String
-> Parser
     Error
     (PortNumber
      -> Money 'Chan 'Ln 'Fund
      -> Maybe MSat
      -> GSEnv
      -> BitcoindEnv
      -> RawConfig)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (String -> Either Error String)
-> String -> Mod Var String -> Parser Error String
forall e a.
AsUnset e =>
Reader e a -> String -> Mod Var a -> Parser e a
E.var (String -> Either Error String
forall s e. IsString s => Reader e s
E.str (String -> Either Error String)
-> (String -> Either Error String) -> String -> Either Error String
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< String -> Either Error String
forall e s. (AsEmpty e, IsString s) => Reader e s
E.nonempty) String
"LSP_LND_P2P_HOST" Mod Var String
forall a. Mod Var a
opts
      Parser
  Error
  (PortNumber
   -> Money 'Chan 'Ln 'Fund
   -> Maybe MSat
   -> GSEnv
   -> BitcoindEnv
   -> RawConfig)
-> Parser Error PortNumber
-> Parser
     Error
     (Money 'Chan 'Ln 'Fund
      -> Maybe MSat -> GSEnv -> BitcoindEnv -> RawConfig)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Reader Error PortNumber
-> String -> Mod Var PortNumber -> Parser Error PortNumber
forall e a.
AsUnset e =>
Reader e a -> String -> Mod Var a -> Parser e a
E.var (Reader Error PortNumber
forall e a. (AsUnread e, Read a) => Reader e a
E.auto Reader Error PortNumber
-> (String -> Either Error String) -> Reader Error PortNumber
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< String -> Either Error String
forall e s. (AsEmpty e, IsString s) => Reader e s
E.nonempty) String
"LSP_LND_P2P_PORT" Mod Var PortNumber
forall a. Mod Var a
opts
      Parser
  Error
  (Money 'Chan 'Ln 'Fund
   -> Maybe MSat -> GSEnv -> BitcoindEnv -> RawConfig)
-> Parser Error (Money 'Chan 'Ln 'Fund)
-> Parser Error (Maybe MSat -> GSEnv -> BitcoindEnv -> RawConfig)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Reader Error (Money 'Chan 'Ln 'Fund)
-> String
-> Mod Var (Money 'Chan 'Ln 'Fund)
-> Parser Error (Money 'Chan 'Ln 'Fund)
forall e a.
AsUnset e =>
Reader e a -> String -> Mod Var a -> Parser e a
E.var (Reader Error (Money 'Chan 'Ln 'Fund)
forall e a. (AsUnread e, Read a) => Reader e a
E.auto Reader Error (Money 'Chan 'Ln 'Fund)
-> (String -> Either Error String)
-> Reader Error (Money 'Chan 'Ln 'Fund)
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< String -> Either Error String
forall e s. (AsEmpty e, IsString s) => Reader e s
E.nonempty) String
"LSP_MIN_CHAN_CAP_MSAT" Mod Var (Money 'Chan 'Ln 'Fund)
forall a. Mod Var a
opts
      Parser Error (Maybe MSat -> GSEnv -> BitcoindEnv -> RawConfig)
-> Parser Error (Maybe MSat)
-> Parser Error (GSEnv -> BitcoindEnv -> RawConfig)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser Error MSat -> Parser Error (Maybe MSat)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Reader Error MSat -> String -> Mod Var MSat -> Parser Error MSat
forall e a.
AsUnset e =>
Reader e a -> String -> Mod Var a -> Parser e a
E.var (Reader Error MSat
forall e a. (AsUnread e, Read a) => Reader e a
E.auto Reader Error MSat
-> (String -> Either Error String) -> Reader Error MSat
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< String -> Either Error String
forall e s. (AsEmpty e, IsString s) => Reader e s
E.nonempty) String
"LSP_MSAT_PER_BYTE" Mod Var MSat
forall a. Mod Var a
opts)
      -- Grpc
      Parser Error (GSEnv -> BitcoindEnv -> RawConfig)
-> Parser Error GSEnv -> Parser Error (BitcoindEnv -> RawConfig)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Reader Error GSEnv -> String -> Mod Var GSEnv -> Parser Error GSEnv
forall e a.
AsUnset e =>
Reader e a -> String -> Mod Var a -> Parser e a
E.var (Reader Error GSEnv
forall a. FromJSON a => String -> Either Error a
parseFromJSON Reader Error GSEnv
-> (String -> Either Error String) -> Reader Error GSEnv
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< String -> Either Error String
forall e s. (AsEmpty e, IsString s) => Reader e s
E.nonempty) String
"LSP_GRPC_SERVER_ENV" Mod Var GSEnv
forall a. Mod Var a
opts
      -- Bitcoind
      Parser Error (BitcoindEnv -> RawConfig)
-> Parser Error BitcoindEnv -> Parser Error RawConfig
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Reader Error BitcoindEnv
-> String -> Mod Var BitcoindEnv -> Parser Error BitcoindEnv
forall e a.
AsUnset e =>
Reader e a -> String -> Mod Var a -> Parser e a
E.var (Reader Error BitcoindEnv
forall a. FromJSON a => String -> Either Error a
parseFromJSON Reader Error BitcoindEnv
-> (String -> Either Error String) -> Reader Error BitcoindEnv
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< String -> Either Error String
forall e s. (AsEmpty e, IsString s) => Reader e s
E.nonempty) String
"LSP_BITCOIND_ENV" Mod Var BitcoindEnv
forall a. Mod Var a
opts

readGCEnv :: IO GCEnv
readGCEnv :: IO GCEnv
readGCEnv =
  (Info Error -> Info Error) -> Parser Error GCEnv -> IO GCEnv
forall e a. (Info Error -> Info e) -> Parser e a -> IO a
E.parse (String -> Info Error -> Info Error
forall e. String -> Info e -> Info e
E.header String
"GCEnv") (Parser Error GCEnv -> IO GCEnv) -> Parser Error GCEnv -> IO GCEnv
forall a b. (a -> b) -> a -> b
$
    Reader Error GCEnv -> String -> Mod Var GCEnv -> Parser Error GCEnv
forall e a.
AsUnset e =>
Reader e a -> String -> Mod Var a -> Parser e a
E.var (Reader Error GCEnv
forall a. FromJSON a => String -> Either Error a
parseFromJSON Reader Error GCEnv
-> (String -> Either Error String) -> Reader Error GCEnv
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< String -> Either Error String
forall e s. (AsEmpty e, IsString s) => Reader e s
E.nonempty) String
"LSP_GRPC_CLIENT_ENV" Mod Var GCEnv
forall a. Mod Var a
opts

opts :: E.Mod E.Var a
opts :: forall a. Mod Var a
opts =
  Mod Var a
forall (t :: * -> *) a. HasKeep t => Mod t a
E.keep Mod Var a -> Mod Var a -> Mod Var a
forall a. Semigroup a => a -> a -> a
<> String -> Mod Var a
forall (t :: * -> *) a. HasHelp t => String -> Mod t a
E.help String
""

withEnv ::
  forall m a.
  ( MonadUnliftIO m
  ) =>
  RawConfig ->
  (Env -> KatipContextT m a) ->
  m a
withEnv :: forall (m :: * -> *) a.
MonadUnliftIO m =>
RawConfig -> (Env -> KatipContextT m a) -> m a
withEnv RawConfig
rc Env -> KatipContextT m a
this = do
  MVar NodePubKey
pubKeyVar <- m (MVar NodePubKey)
forall (m :: * -> *) a. MonadIO m => m (MVar a)
newEmptyMVar
  Scribe
handleScribe <-
    IO Scribe -> m Scribe
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Scribe -> m Scribe) -> IO Scribe -> m Scribe
forall a b. (a -> b) -> a -> b
$
      (forall a. LogItem a => ItemFormatter a)
-> ColorStrategy -> Handle -> PermitFunc -> Verbosity -> IO Scribe
mkHandleScribeWithFormatter
        ( case RawConfig -> LogFormat
rawConfigLogFormat RawConfig
rc of
            LogFormat
Bracket -> ItemFormatter a
forall a. LogItem a => ItemFormatter a
bracketFormat
            LogFormat
JSON -> ItemFormatter a
forall a. LogItem a => ItemFormatter a
jsonFormat
        )
        ColorStrategy
ColorIfTerminal
        Handle
stdout
        (Severity -> Item a -> IO Bool
forall (m :: * -> *) a. Monad m => Severity -> Item a -> m Bool
permitItem (Severity -> Item a -> IO Bool) -> Severity -> Item a -> IO Bool
forall a b. (a -> b) -> a -> b
$ RawConfig -> Severity
rawConfigLogSeverity RawConfig
rc)
        (RawConfig -> Verbosity
rawConfigLogVerbosity RawConfig
rc)
  let newLogEnv :: m LogEnv
newLogEnv =
        IO LogEnv -> m LogEnv
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO LogEnv -> m LogEnv) -> IO LogEnv -> m LogEnv
forall a b. (a -> b) -> a -> b
$
          Text -> Scribe -> ScribeSettings -> LogEnv -> IO LogEnv
registerScribe
            Text
"stdout"
            Scribe
handleScribe
            ScribeSettings
defaultScribeSettings
            (LogEnv -> IO LogEnv) -> IO LogEnv -> IO LogEnv
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Namespace -> Environment -> IO LogEnv
initLogEnv
              Namespace
"BtcLsp"
              ( Text -> Environment
Environment (Text -> Environment) -> Text -> Environment
forall a b. (a -> b) -> a -> b
$ RawConfig -> Text
rawConfigLogEnv RawConfig
rc
              )
  let m (Pool SqlBackend)
newSqlPool :: m (Pool Psql.SqlBackend) =
        IO (Pool SqlBackend) -> m (Pool SqlBackend)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Pool SqlBackend) -> m (Pool SqlBackend))
-> (NoLoggingT IO (Pool SqlBackend) -> IO (Pool SqlBackend))
-> NoLoggingT IO (Pool SqlBackend)
-> m (Pool SqlBackend)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NoLoggingT IO (Pool SqlBackend) -> IO (Pool SqlBackend)
forall (m :: * -> *) a. NoLoggingT m a -> m a
runNoLoggingT (NoLoggingT IO (Pool SqlBackend) -> m (Pool SqlBackend))
-> NoLoggingT IO (Pool SqlBackend) -> m (Pool SqlBackend)
forall a b. (a -> b) -> a -> b
$
          ConnectionString -> Int -> NoLoggingT IO (Pool SqlBackend)
forall (m :: * -> *).
(MonadUnliftIO m, MonadLoggerIO m) =>
ConnectionString -> Int -> m (Pool SqlBackend)
Psql.createPostgresqlPool (RawConfig -> ConnectionString
rawConfigLibpqConnStr RawConfig
rc) Int
10
  let katipCtx :: LogContexts
katipCtx = LogContexts
forall a. Monoid a => a
mempty :: LogContexts
  let katipNs :: Namespace
katipNs = Namespace
forall a. Monoid a => a
mempty :: Namespace
  let lnd :: LndEnv
lnd = RawConfig -> LndEnv
rawConfigLndEnv RawConfig
rc
  m LogEnv -> (LogEnv -> m ()) -> (LogEnv -> m a) -> m a
forall (m :: * -> *) a b c.
MonadUnliftIO m =>
m a -> (a -> m b) -> (a -> m c) -> m c
bracket m LogEnv
newLogEnv LogEnv -> m ()
rmLogEnv ((LogEnv -> m a) -> m a) -> (LogEnv -> m a) -> m a
forall a b. (a -> b) -> a -> b
$ \LogEnv
le ->
    m (Pool SqlBackend)
-> (Pool SqlBackend -> m ()) -> (Pool SqlBackend -> m a) -> m a
forall (m :: * -> *) a b c.
MonadUnliftIO m =>
m a -> (a -> m b) -> (a -> m c) -> m c
bracket m (Pool SqlBackend)
newSqlPool Pool SqlBackend -> m ()
rmSqlPool ((Pool SqlBackend -> m a) -> m a)
-> (Pool SqlBackend -> m a) -> m a
forall a b. (a -> b) -> a -> b
$ \Pool SqlBackend
pool -> do
      let rBtc :: BitcoindEnv
rBtc = RawConfig -> BitcoindEnv
rawConfigBtcEnv RawConfig
rc
      Client
btc <-
        IO Client -> m Client
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Client -> m Client) -> IO Client -> m Client
forall a b. (a -> b) -> a -> b
$
          String -> ConnectionString -> ConnectionString -> IO Client
Btc.getClient
            (Text -> String
forall source target.
(From source target, 'False ~ (source == target)) =>
source -> target
from (Text -> String) -> Text -> String
forall a b. (a -> b) -> a -> b
$ BitcoindEnv -> Text
bitcoindEnvHost BitcoindEnv
rBtc)
            (Text -> ConnectionString
forall source target.
(From source target, 'False ~ (source == target)) =>
source -> target
from (Text -> ConnectionString) -> Text -> ConnectionString
forall a b. (a -> b) -> a -> b
$ BitcoindEnv -> Text
bitcoindEnvUsername BitcoindEnv
rBtc)
            (Text -> ConnectionString
forall source target.
(From source target, 'False ~ (source == target)) =>
source -> target
from (Text -> ConnectionString) -> Text -> ConnectionString
forall a b. (a -> b) -> a -> b
$ BitcoindEnv -> Text
bitcoindEnvPassword BitcoindEnv
rBtc)
      LogEnv -> LogContexts -> Namespace -> KatipContextT m a -> m a
forall c (m :: * -> *) a.
LogItem c =>
LogEnv -> c -> Namespace -> KatipContextT m a -> m a
runKatipContextT LogEnv
le LogContexts
katipCtx Namespace
katipNs
        (KatipContextT m a -> m a)
-> ((UnliftIO (KatipContextT m) -> IO a) -> KatipContextT m a)
-> (UnliftIO (KatipContextT m) -> IO a)
-> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (UnliftIO (KatipContextT m) -> IO a) -> KatipContextT m a
forall (m :: * -> *) a.
MonadUnliftIO m =>
(UnliftIO m -> IO a) -> m a
withUnliftIO
        ((UnliftIO (KatipContextT m) -> IO a) -> m a)
-> (UnliftIO (KatipContextT m) -> IO a) -> m a
forall a b. (a -> b) -> a -> b
$ \(UnliftIO forall a. KatipContextT m a -> IO a
run) ->
          KatipContextT m a -> IO a
forall a. KatipContextT m a -> IO a
run (KatipContextT m a -> IO a)
-> (Env -> KatipContextT m a) -> Env -> IO a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Env -> KatipContextT m a
this (Env -> IO a) -> Env -> IO a
forall a b. (a -> b) -> a -> b
$
            Env :: Pool SqlBackend
-> Liquidity 'Outgoing
-> Liquidity 'Incoming
-> MSat
-> Namespace
-> LogContexts
-> LogEnv
-> YesodLog
-> LndEnv
-> String
-> PortNumber
-> Money 'Usr 'OnChain 'Fund
-> Maybe MSat
-> MVar NodePubKey
-> GSEnv
-> Client
-> Env
Env
              { -- General
                envSQLPool :: Pool SqlBackend
envSQLPool = Pool SqlBackend
pool,
                envMinTotalExtOutgoingLiquidity :: Liquidity 'Outgoing
envMinTotalExtOutgoingLiquidity =
                  RawConfig -> Liquidity 'Outgoing
rawConfigMinTotalExtOutgoingLiquidity RawConfig
rc,
                envMinTotalExtIncomingLiquidity :: Liquidity 'Incoming
envMinTotalExtIncomingLiquidity =
                  RawConfig -> Liquidity 'Incoming
rawConfigMinTotalExtIncomingLiquidity RawConfig
rc,
                envMinTotalOnChainLiquidity :: MSat
envMinTotalOnChainLiquidity =
                  RawConfig -> MSat
rawConfigMinTotalOnChainLiquidity RawConfig
rc,
                -- Logging
                envKatipLE :: LogEnv
envKatipLE = LogEnv
le,
                envKatipCTX :: LogContexts
envKatipCTX = LogContexts
katipCtx,
                envKatipNS :: Namespace
envKatipNS = Namespace
katipNs,
                envYesodLog :: YesodLog
envYesodLog = RawConfig -> YesodLog
rawConfigLogYesod RawConfig
rc,
                -- Lnd
                envLnd :: LndEnv
envLnd = LndEnv
lnd,
                envLndP2PHost :: String
envLndP2PHost = RawConfig -> String
rawConfigLndP2PHost RawConfig
rc,
                envLndP2PPort :: PortNumber
envLndP2PPort = RawConfig -> PortNumber
rawConfigLndP2PPort RawConfig
rc,
                envSwapIntoLnMinAmt :: Money 'Usr 'OnChain 'Fund
envSwapIntoLnMinAmt =
                  Money 'Chan 'Ln 'Fund -> Money 'Usr 'OnChain 'Fund
Math.newSwapIntoLnMinAmt (Money 'Chan 'Ln 'Fund -> Money 'Usr 'OnChain 'Fund)
-> Money 'Chan 'Ln 'Fund -> Money 'Usr 'OnChain 'Fund
forall a b. (a -> b) -> a -> b
$
                    RawConfig -> Money 'Chan 'Ln 'Fund
rawConfigMinChanCap RawConfig
rc,
                envMsatPerByte :: Maybe MSat
envMsatPerByte = RawConfig -> Maybe MSat
rawConfigMsatPerByte RawConfig
rc,
                envLndPubKey :: MVar NodePubKey
envLndPubKey = MVar NodePubKey
pubKeyVar,
                -- Grpc
                envGrpcServer :: GSEnv
envGrpcServer =
                  (RawConfig -> GSEnv
rawConfigGrpcServerEnv RawConfig
rc)
                    { gsEnvSigner :: MsgToSign -> IO (Maybe LndSig)
gsEnvSigner = KatipContextT m (Maybe LndSig) -> IO (Maybe LndSig)
forall a. KatipContextT m a -> IO a
run (KatipContextT m (Maybe LndSig) -> IO (Maybe LndSig))
-> (MsgToSign -> KatipContextT m (Maybe LndSig))
-> MsgToSign
-> IO (Maybe LndSig)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LndEnv -> MsgToSign -> KatipContextT m (Maybe LndSig)
signT LndEnv
lnd,
                      gsEnvLogger :: Text -> IO ()
gsEnvLogger = KatipContextT m () -> IO ()
forall a. KatipContextT m a -> IO a
run (KatipContextT m () -> IO ())
-> (Text -> KatipContextT m ()) -> Text -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. $(logTM) Severity
DebugS (LogStr -> KatipContextT m ())
-> (Text -> LogStr) -> Text -> KatipContextT m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> LogStr
forall a. StringConv a Text => a -> LogStr
logStr
                    },
                envBtc :: Client
envBtc = Client
btc
              }
  where
    rmLogEnv :: LogEnv -> m ()
    rmLogEnv :: LogEnv -> m ()
rmLogEnv = m LogEnv -> m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (m LogEnv -> m ()) -> (LogEnv -> m LogEnv) -> LogEnv -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO LogEnv -> m LogEnv
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO LogEnv -> m LogEnv)
-> (LogEnv -> IO LogEnv) -> LogEnv -> m LogEnv
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LogEnv -> IO LogEnv
closeScribes
    rmSqlPool :: Pool Psql.SqlBackend -> m ()
    rmSqlPool :: Pool SqlBackend -> m ()
rmSqlPool = IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ())
-> (Pool SqlBackend -> IO ()) -> Pool SqlBackend -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Pool SqlBackend -> IO ()
forall a. Pool a -> IO ()
destroyAllResources
    signT ::
      Lnd.LndEnv ->
      Sig.MsgToSign ->
      KatipContextT m (Maybe Sig.LndSig)
    signT :: LndEnv -> MsgToSign -> KatipContextT m (Maybe LndSig)
signT LndEnv
lnd MsgToSign
msg = do
      Either LndError SignMessageResponse
eSig <-
        LndEnv
-> SignMessageRequest
-> KatipContextT m (Either LndError SignMessageResponse)
forall (m :: * -> *).
(KatipContext m, MonadUnliftIO m) =>
LndEnv
-> SignMessageRequest -> m (Either LndError SignMessageResponse)
Lnd.signMessage LndEnv
lnd (SignMessageRequest
 -> KatipContextT m (Either LndError SignMessageResponse))
-> SignMessageRequest
-> KatipContextT m (Either LndError SignMessageResponse)
forall a b. (a -> b) -> a -> b
$
          SignMessageRequest :: ConnectionString
-> KeyLocator -> Bool -> Bool -> SignMessageRequest
Lnd.SignMessageRequest
            { message :: ConnectionString
Lnd.message = MsgToSign -> ConnectionString
Sig.unMsgToSign MsgToSign
msg,
              keyLoc :: KeyLocator
Lnd.keyLoc =
                KeyLocator :: Int32 -> Int32 -> KeyLocator
Lnd.KeyLocator
                  { keyFamily :: Int32
Lnd.keyFamily = Int32
6,
                    keyIndex :: Int32
Lnd.keyIndex = Int32
0
                  },
              doubleHash :: Bool
Lnd.doubleHash = Bool
False,
              compactSig :: Bool
Lnd.compactSig = Bool
False
            }
      case Either LndError SignMessageResponse
eSig of
        Left LndError
e -> do
          $(logTM) Severity
ErrorS (LogStr -> KatipContextT m ())
-> (Text -> LogStr) -> Text -> KatipContextT m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> LogStr
forall a. StringConv a Text => a -> LogStr
logStr (Text -> KatipContextT m ()) -> Text -> KatipContextT m ()
forall a b. (a -> b) -> a -> b
$
            Text
"Server ==> signing procedure failed "
              Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> LndError -> Text
forall a. Out a => a -> Text
inspect LndError
e
          Maybe LndSig -> KatipContextT m (Maybe LndSig)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe LndSig
forall a. Maybe a
Nothing
        Right SignMessageResponse
sig0 -> do
          let sig :: ConnectionString
sig = SignMessageResponse -> ConnectionString
coerce SignMessageResponse
sig0
          $(logTM) Severity
DebugS (LogStr -> KatipContextT m ())
-> (Text -> LogStr) -> Text -> KatipContextT m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> LogStr
forall a. StringConv a Text => a -> LogStr
logStr (Text -> KatipContextT m ()) -> Text -> KatipContextT m ()
forall a b. (a -> b) -> a -> b
$
            Text
"Server ==> signing procedure succeeded for msg of "
              Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Int -> Text
forall a. Out a => a -> Text
inspect (ConnectionString -> Int
BS.length (ConnectionString -> Int) -> ConnectionString -> Int
forall a b. (a -> b) -> a -> b
$ MsgToSign -> ConnectionString
Sig.unMsgToSign MsgToSign
msg)
              Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" bytes "
              Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> MsgToSign -> Text
forall a. Out a => a -> Text
inspect MsgToSign
msg
              Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" got signature of "
              Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Int -> Text
forall a. Out a => a -> Text
inspect (ConnectionString -> Int
BS.length ConnectionString
sig)
              Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" bytes "
              Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> ConnectionString -> Text
forall a. Out a => a -> Text
inspect ConnectionString
sig
          Maybe LndSig -> KatipContextT m (Maybe LndSig)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe LndSig -> KatipContextT m (Maybe LndSig))
-> (LndSig -> Maybe LndSig)
-> LndSig
-> KatipContextT m (Maybe LndSig)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LndSig -> Maybe LndSig
forall a. a -> Maybe a
Just (LndSig -> KatipContextT m (Maybe LndSig))
-> LndSig -> KatipContextT m (Maybe LndSig)
forall a b. (a -> b) -> a -> b
$ ConnectionString -> LndSig
Sig.LndSig ConnectionString
sig