1+ {-# LANGUAGE ConstraintKinds #-}
12{-# LANGUAGE DeriveGeneric #-}
2- {-# LANGUAGE ConstraintKinds #-}
33{-# LANGUAGE DuplicateRecordFields #-}
44{-# LANGUAGE GeneralizedNewtypeDeriving #-}
55{-# LANGUAGE OverloadedStrings #-}
66{-# LANGUAGE TemplateHaskell #-}
77{-# LANGUAGE ViewPatterns #-}
88{-# OPTIONS_GHC -Wall #-}
99module Cardano.Faucet.Types (
10- FaucetConfig (.. ), mkFaucetConfig
10+ FaucetConfig (.. ), mkFaucetConfig , testFC
1111 , HasFaucetConfig (.. )
1212 , FaucetEnv (.. ), initEnv
1313 , HasFaucetEnv (.. )
1414 , incWithDrawn
1515 , decrWithDrawn
1616 , setWalletBalance
17- , WithDrawlRequest (.. ), wWalletId , wAmount
17+ , WithDrawlRequest (.. ), wAddress , wAmount
1818 , WithDrawlResult (.. )
1919 , DepositRequest (.. ), dWalletId , dAmount
2020 , DepositResult (.. )
@@ -26,47 +26,64 @@ import Control.Lens hiding ((.=))
2626import Control.Monad.Except
2727import Control.Monad.Reader
2828import Data.Aeson (FromJSON (.. ), ToJSON (.. ), object , withObject , (.:) , (.=) )
29- import Data.Text (Text )
29+ import qualified Data.ByteString as BS
30+ import Data.Default (def )
31+ import Data.Monoid ((<>) )
32+ import Data.Text (Text )
33+ import Data.Text.Lens (packed )
3034import Data.Typeable (Typeable )
3135import GHC.Generics (Generic )
36+ import Network.Connection (TLSSettings (.. ))
37+ import Network.HTTP.Client (Manager , newManager )
38+ import Network.HTTP.Client.TLS (mkManagerSettings )
39+ import Network.TLS (ClientParams (.. ), credentialLoadX509FromMemory , defaultParamsClient ,
40+ onCertificateRequest , onServerCertificate , supportedCiphers )
41+ import Network.TLS.Extra.Cipher (ciphersuite_all )
3242import Servant (ServantErr )
3343import Servant.Client.Core (BaseUrl (.. ), Scheme (.. ))
3444import System.Metrics (Store , createCounter , createGauge )
3545import System.Metrics.Counter (Counter )
3646import qualified System.Metrics.Counter as Counter
3747import System.Metrics.Gauge (Gauge )
3848import qualified System.Metrics.Gauge as Gauge
39- import System.Remote.Monitoring.Statsd (StatsdOptions )
40- import System.Wlog (CanLog , WithLogger , HasLoggerName , LoggerName (.. ), LoggerNameBox (.. ),
41- launchFromFile )
49+ import System.Remote.Monitoring.Statsd (StatsdOptions , defaultStatsdOptions )
50+ import System.Wlog (CanLog , HasLoggerName , LoggerName (.. ), LoggerNameBox (.. ),
51+ WithLogger , launchFromFile )
4252
43- import Cardano.Wallet.API.V1.Types (PaymentSource (.. ))
44- import Cardano.Wallet.Client (WalletClient )
45- import Cardano.Wallet.Client.Http (defaultManagerSettings , mkHttpClient , newManager )
46- import Pos.Core (Coin (.. ))
47- import Pos.Wallet.Web.ClientTypes.Types (Addr (.. ), CAccountId (.. ), CId (.. ))
53+ import Cardano.Wallet.API.V1.Types (PaymentSource (.. ), Transaction ,
54+ V1 , WalletId (.. ))
55+ import Cardano.Wallet.Client (ClientError (.. ), WalletClient )
56+ import Cardano.Wallet.Client.Http (mkHttpClient )
57+ import Pos.Core (Address (.. ), Coin (.. ))
58+ --
4859
4960--------------------------------------------------------------------------------
5061data WithDrawlRequest = WithDrawlRequest {
51- _wWalletId :: Text -- Pos.Wallet.Web.ClientTypes.Types.CAccountId
52- , _wAmount :: Coin -- Pos.Core.Common.Types.Coin
62+ _wAddress :: V1 Address -- Pos.Wallet.Web.ClientTypes.Types.CAccountId
63+ , _wAmount :: V1 Coin -- Pos.Core.Common.Types.Coin
5364 } deriving (Show , Typeable , Generic )
5465
5566makeLenses ''WithDrawlRequest
5667
5768instance FromJSON WithDrawlRequest where
5869 parseJSON = withObject " WithDrawlRequest" $ \ v -> WithDrawlRequest
59- <$> v .: " wallet "
60- <*> ( Coin <$> v .: " amount" )
70+ <$> v .: " address "
71+ <*> v .: " amount"
6172
6273instance ToJSON WithDrawlRequest where
63- toJSON (WithDrawlRequest w ( Coin a) ) =
64- object [" wallet " .= w, " amount" .= a]
74+ toJSON (WithDrawlRequest w a ) =
75+ object [" address " .= w, " amount" .= a]
6576
66- data WithDrawlResult = WithDrawlResult
77+ data WithDrawlResult =
78+ WithdrawlError ClientError
79+ | WithdrawlSuccess Transaction
6780 deriving (Show , Typeable , Generic )
6881
69- instance ToJSON WithDrawlResult
82+ instance ToJSON WithDrawlResult where
83+ toJSON (WithdrawlSuccess txn) =
84+ object [" success" .= txn]
85+ toJSON (WithdrawlError err) =
86+ object [" error" .= show err]
7087
7188
7289--------------------------------------------------------------------------------
@@ -94,13 +111,28 @@ data FaucetConfig = FaucetConfig {
94111 , _fcFaucetPaymentSource :: PaymentSource
95112 , _fcStatsdOpts :: StatsdOptions
96113 , _fcLoggerConfigFile :: FilePath
114+ , _fcPubCertFile :: FilePath
115+ , _fcPrivKeyFile :: FilePath
97116 }
98117
99118makeClassy ''FaucetConfig
100119
101- mkFaucetConfig :: String -> Int -> PaymentSource -> StatsdOptions -> String -> FaucetConfig
120+ mkFaucetConfig
121+ :: String
122+ -> Int
123+ -> PaymentSource
124+ -> StatsdOptions
125+ -> FilePath
126+ -> FilePath
127+ -> FilePath
128+ -> FaucetConfig
102129mkFaucetConfig = FaucetConfig
103130
131+ testFC :: FaucetConfig
132+ testFC = FaucetConfig " 127.0.0.1" 8090 ps defaultStatsdOptions " ./logging.cfg" " ./tls/ca.crt" " ./tls/server.key"
133+ where
134+ ps = PaymentSource (WalletId " Ae2tdPwUPEZLBG2sEmiv8Y6DqD4LoZKQ5wosXucbLnYoacg2YZSPhMn4ETi" ) 2147483648
135+
104136--------------------------------------------------------------------------------
105137data FaucetEnv = FaucetEnv {
106138 _feWithdrawn :: Counter
@@ -119,13 +151,34 @@ initEnv fc store = do
119151 withdrawn <- createCounter " total-withdrawn" store
120152 withdrawCount <- createCounter " num-withdrawals" store
121153 balance <- createGauge " wallet-balance" store
122- manager <- newManager defaultManagerSettings
123- let url = BaseUrl Http (fc ^. fcWalletApiHost) (fc ^. fcWalletApiPort) " "
154+ manager <- createManager fc
155+ let url = BaseUrl Https (fc ^. fcWalletApiHost) (fc ^. fcWalletApiPort) " "
124156 return $ FaucetEnv withdrawn withdrawCount balance
125157 store
126158 fc
127159 (mkHttpClient url manager)
128160
161+ createManager :: FaucetConfig -> IO Manager
162+ createManager fc = do
163+ pubCert <- BS. readFile (fc ^. fcPubCertFile)
164+ privKey <- BS. readFile (fc ^. fcPrivKeyFile)
165+ case credentialLoadX509FromMemory pubCert privKey of
166+ Left problem -> error $ " Unable to load credentials: " <> (problem ^. packed)
167+ Right credential ->
168+ let hooks = def {
169+ onCertificateRequest = \ _ -> return $ Just credential,
170+ onServerCertificate = \ _ _ _ _ -> return []
171+ }
172+ clientParams = (defaultParamsClient " localhost" " " ) {
173+ clientHooks = hooks,
174+ clientSupported = def {
175+ supportedCiphers = ciphersuite_all
176+ }
177+ }
178+ tlsSettings = TLSSettings clientParams
179+ in
180+ newManager $ mkManagerSettings tlsSettings Nothing
181+
129182incWithDrawn :: (MonadReader e m , HasFaucetEnv e , MonadIO m ) => Coin -> m ()
130183incWithDrawn (Coin (fromIntegral -> c)) = do
131184 wd <- view feWithdrawn
0 commit comments