@@ -11,6 +11,8 @@ module Cardano.Shell.Launcher
11
11
-- * Functions
12
12
, runLauncher
13
13
, runWalletProcess
14
+ , saveSafeMode
15
+ , readSafeMode
14
16
-- * Critical exports (testing)
15
17
, DaedalusExitCode (.. )
16
18
, handleDaedalusExitCode
@@ -27,8 +29,11 @@ module Cardano.Shell.Launcher
27
29
import Cardano.Prelude hiding (onException )
28
30
29
31
import Prelude (Show (.. ))
32
+ import Data.Aeson (FromJSON , ToJSON (toJSON ), genericParseJSON , genericToJSON , defaultOptions )
33
+ import Data.Yaml as Y
30
34
import qualified System.Process as Process
31
35
import Turtle (system )
36
+ import Test.QuickCheck (Arbitrary (arbitrary ), elements )
32
37
33
38
import Cardano.Shell.Configuration (WalletArguments (.. ),
34
39
DaedalusBin (.. ))
@@ -74,6 +79,29 @@ data WalletMode
74
79
| WalletModeSafe
75
80
deriving (Eq , Show )
76
81
82
+ instance Arbitrary WalletMode where
83
+ arbitrary = elements [ WalletModeSafe , WalletModeNormal ]
84
+
85
+ instance FromJSON WalletMode where
86
+ parseJSON (Bool True ) = pure WalletModeSafe
87
+ parseJSON (String " safe" ) = pure WalletModeSafe
88
+ parseJSON (String " normal" ) = pure WalletModeNormal
89
+ parseJSON _ = pure WalletModeNormal
90
+
91
+ instance ToJSON WalletMode where
92
+ toJSON WalletModeNormal = String " normal"
93
+ toJSON WalletModeSafe = String " safe"
94
+
95
+ data SafeModeConfig = SafeModeConfig
96
+ { smcSafeMode :: WalletMode
97
+ } deriving (Generic , Show )
98
+
99
+ instance FromJSON SafeModeConfig where
100
+ parseJSON = genericParseJSON defaultOptions
101
+
102
+ instance ToJSON SafeModeConfig where
103
+ toJSON = genericToJSON defaultOptions
104
+
77
105
-- | All the important exit codes. Since we cover "other" cases as well, this is a total mapping.
78
106
-- That is good.
79
107
-- TODO(KS): We could extend this to try to encode some interesting properties about it.
@@ -125,6 +153,22 @@ instance Isomorphism DaedalusExitCode ExitCode where
125
153
-- Functions
126
154
--------------------------------------------------------------------------------
127
155
156
+ readSafeMode :: FilePath -> IO WalletMode
157
+ readSafeMode stateDir = do
158
+ let safeModeConfigFile = getSafeModeConfigPath stateDir
159
+ decoded <- liftIO $ Y. decodeFileEither safeModeConfigFile
160
+ case decoded of
161
+ Right value -> pure $ smcSafeMode value
162
+ Left _ -> pure WalletModeNormal
163
+
164
+ saveSafeMode :: FilePath -> WalletMode -> IO ()
165
+ saveSafeMode stateDir mode = do
166
+ let safeModeConfigFile = getSafeModeConfigPath stateDir
167
+ Y. encodeFile safeModeConfigFile $ SafeModeConfig mode
168
+
169
+ getSafeModeConfigPath :: FilePath -> FilePath
170
+ getSafeModeConfigPath stateDir = stateDir </> " safemode.yaml"
171
+
128
172
-- | Here we handle the exit codes.
129
173
-- It's a simple mapping from exit code to actions that the launcher takes.
130
174
--
@@ -165,25 +209,30 @@ runWalletProcess
165
209
-> WalletRunner
166
210
-> RunUpdateFunc
167
211
-> UpdaterData
212
+ -> FilePath
168
213
-> IO ExitCode
169
214
runWalletProcess
170
215
logDep
171
216
walletMode
172
217
daedalusBin
173
218
walletRunner
174
219
runUpdateFunc
175
- updaterData = do
220
+ updaterData
221
+ stateDir = do
176
222
177
223
-- Parametrized by @WalletMode@ so we can change it on restart depending
178
224
-- on the Daedalus exit code.
179
225
let restart :: WalletMode -> IO ExitCode
180
- restart = \ walletMode' -> runWalletProcess
226
+ restart = \ walletMode' -> do
227
+ saveSafeMode stateDir walletMode'
228
+ runWalletProcess
181
229
logDep
182
230
walletMode'
183
231
daedalusBin
184
232
walletRunner
185
233
runUpdateFunc
186
234
updaterData
235
+ stateDir
187
236
188
237
-- Additional arguments we need to pass if it's a SAFE mode.
189
238
let walletSafeModeArgs :: WalletArguments
@@ -269,8 +318,10 @@ runLauncher
269
318
-> DaedalusBin
270
319
-> RunUpdateFunc
271
320
-> UpdaterData
321
+ -> FilePath
272
322
-> IO ExitCode
273
- runLauncher loggingDependencies walletRunner daedalusBin runUpdateFunc updaterData = do
323
+ runLauncher loggingDependencies walletRunner daedalusBin runUpdateFunc updaterData stateDir = do
324
+ safeMode <- readSafeMode stateDir
274
325
275
326
-- In the case the user wants to avoid installing the update now, we
276
327
-- run the update (if there is one) when we have it downloaded.
@@ -283,11 +334,12 @@ runLauncher loggingDependencies walletRunner daedalusBin runUpdateFunc updaterDa
283
334
-- You still want to run the wallet even if the update fails
284
335
runWalletProcess
285
336
loggingDependencies
286
- WalletModeNormal
337
+ safeMode
287
338
daedalusBin
288
339
walletRunner
289
340
runUpdateFunc
290
341
updaterData
342
+ stateDir
291
343
292
344
-- | Generation of the TLS certificates.
293
345
-- This just covers the generation of the TLS certificates and nothing else.
0 commit comments