1
+ {-# LANGUAGE ConstraintKinds #-}
1
2
{-# LANGUAGE DeriveGeneric #-}
2
- {-# LANGUAGE ConstraintKinds #-}
3
3
{-# LANGUAGE DuplicateRecordFields #-}
4
4
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
5
5
{-# LANGUAGE OverloadedStrings #-}
6
6
{-# LANGUAGE TemplateHaskell #-}
7
7
{-# LANGUAGE ViewPatterns #-}
8
8
{-# OPTIONS_GHC -Wall #-}
9
9
module Cardano.Faucet.Types (
10
- FaucetConfig (.. ), mkFaucetConfig
10
+ FaucetConfig (.. ), mkFaucetConfig , testFC
11
11
, HasFaucetConfig (.. )
12
12
, FaucetEnv (.. ), initEnv
13
13
, HasFaucetEnv (.. )
14
14
, incWithDrawn
15
15
, decrWithDrawn
16
16
, setWalletBalance
17
- , WithDrawlRequest (.. ), wWalletId , wAmount
17
+ , WithDrawlRequest (.. ), wAddress , wAmount
18
18
, WithDrawlResult (.. )
19
19
, DepositRequest (.. ), dWalletId , dAmount
20
20
, DepositResult (.. )
@@ -26,47 +26,64 @@ import Control.Lens hiding ((.=))
26
26
import Control.Monad.Except
27
27
import Control.Monad.Reader
28
28
import 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 )
30
34
import Data.Typeable (Typeable )
31
35
import 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 )
32
42
import Servant (ServantErr )
33
43
import Servant.Client.Core (BaseUrl (.. ), Scheme (.. ))
34
44
import System.Metrics (Store , createCounter , createGauge )
35
45
import System.Metrics.Counter (Counter )
36
46
import qualified System.Metrics.Counter as Counter
37
47
import System.Metrics.Gauge (Gauge )
38
48
import 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 )
42
52
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
+ --
48
59
49
60
--------------------------------------------------------------------------------
50
61
data 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
53
64
} deriving (Show , Typeable , Generic )
54
65
55
66
makeLenses ''WithDrawlRequest
56
67
57
68
instance FromJSON WithDrawlRequest where
58
69
parseJSON = withObject " WithDrawlRequest" $ \ v -> WithDrawlRequest
59
- <$> v .: " wallet "
60
- <*> ( Coin <$> v .: " amount" )
70
+ <$> v .: " address "
71
+ <*> v .: " amount"
61
72
62
73
instance 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]
65
76
66
- data WithDrawlResult = WithDrawlResult
77
+ data WithDrawlResult =
78
+ WithdrawlError ClientError
79
+ | WithdrawlSuccess Transaction
67
80
deriving (Show , Typeable , Generic )
68
81
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]
70
87
71
88
72
89
--------------------------------------------------------------------------------
@@ -94,13 +111,28 @@ data FaucetConfig = FaucetConfig {
94
111
, _fcFaucetPaymentSource :: PaymentSource
95
112
, _fcStatsdOpts :: StatsdOptions
96
113
, _fcLoggerConfigFile :: FilePath
114
+ , _fcPubCertFile :: FilePath
115
+ , _fcPrivKeyFile :: FilePath
97
116
}
98
117
99
118
makeClassy ''FaucetConfig
100
119
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
102
129
mkFaucetConfig = FaucetConfig
103
130
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
+
104
136
--------------------------------------------------------------------------------
105
137
data FaucetEnv = FaucetEnv {
106
138
_feWithdrawn :: Counter
@@ -119,13 +151,34 @@ initEnv fc store = do
119
151
withdrawn <- createCounter " total-withdrawn" store
120
152
withdrawCount <- createCounter " num-withdrawals" store
121
153
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) " "
124
156
return $ FaucetEnv withdrawn withdrawCount balance
125
157
store
126
158
fc
127
159
(mkHttpClient url manager)
128
160
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
+
129
182
incWithDrawn :: (MonadReader e m , HasFaucetEnv e , MonadIO m ) => Coin -> m ()
130
183
incWithDrawn (Coin (fromIntegral -> c)) = do
131
184
wd <- view feWithdrawn
0 commit comments