@@ -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,6 +29,8 @@ 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 )
32
36
@@ -74,6 +78,25 @@ data WalletMode
74
78
| WalletModeSafe
75
79
deriving (Eq , Show )
76
80
81
+ instance FromJSON WalletMode where
82
+ parseJSON (String " safe" ) = pure WalletModeSafe
83
+ parseJSON (String " normal" ) = pure WalletModeNormal
84
+ parseJSON _ = pure WalletModeNormal
85
+
86
+ instance ToJSON WalletMode where
87
+ toJSON WalletModeNormal = String " normal"
88
+ toJSON WalletModeSafe = String " safe"
89
+
90
+ data SafeModeConfig = SafeModeConfig
91
+ { smcSafeMode :: WalletMode
92
+ } deriving (Generic , Show )
93
+
94
+ instance FromJSON SafeModeConfig where
95
+ parseJSON = genericParseJSON defaultOptions
96
+
97
+ instance ToJSON SafeModeConfig where
98
+ toJSON = genericToJSON defaultOptions
99
+
77
100
-- | All the important exit codes. Since we cover "other" cases as well, this is a total mapping.
78
101
-- That is good.
79
102
-- TODO(KS): We could extend this to try to encode some interesting properties about it.
@@ -125,6 +148,22 @@ instance Isomorphism DaedalusExitCode ExitCode where
125
148
-- Functions
126
149
--------------------------------------------------------------------------------
127
150
151
+ readSafeMode :: FilePath -> IO WalletMode
152
+ readSafeMode stateDir = do
153
+ let safeModeConfigFile = getSafeModeConfigPath stateDir
154
+ decoded <- liftIO $ Y. decodeFileEither safeModeConfigFile
155
+ case decoded of
156
+ Right value -> pure $ smcSafeMode value
157
+ Left _ -> pure WalletModeNormal
158
+
159
+ saveSafeMode :: FilePath -> WalletMode -> IO ()
160
+ saveSafeMode stateDir mode = do
161
+ let safeModeConfigFile = getSafeModeConfigPath stateDir
162
+ Y. encodeFile safeModeConfigFile $ SafeModeConfig mode
163
+
164
+ getSafeModeConfigPath :: FilePath -> FilePath
165
+ getSafeModeConfigPath stateDir = stateDir </> " safemode.yaml"
166
+
128
167
-- | Here we handle the exit codes.
129
168
-- It's a simple mapping from exit code to actions that the launcher takes.
130
169
--
@@ -165,25 +204,30 @@ runWalletProcess
165
204
-> WalletRunner
166
205
-> RunUpdateFunc
167
206
-> UpdaterData
207
+ -> FilePath
168
208
-> IO ExitCode
169
209
runWalletProcess
170
210
logDep
171
211
walletMode
172
212
daedalusBin
173
213
walletRunner
174
214
runUpdateFunc
175
- updaterData = do
215
+ updaterData
216
+ stateDir = do
176
217
177
218
-- Parametrized by @WalletMode@ so we can change it on restart depending
178
219
-- on the Daedalus exit code.
179
220
let restart :: WalletMode -> IO ExitCode
180
- restart = \ walletMode' -> runWalletProcess
221
+ restart = \ walletMode' -> do
222
+ saveSafeMode stateDir walletMode'
223
+ runWalletProcess
181
224
logDep
182
225
walletMode'
183
226
daedalusBin
184
227
walletRunner
185
228
runUpdateFunc
186
229
updaterData
230
+ stateDir
187
231
188
232
-- Additional arguments we need to pass if it's a SAFE mode.
189
233
let walletSafeModeArgs :: WalletArguments
@@ -269,8 +313,10 @@ runLauncher
269
313
-> DaedalusBin
270
314
-> RunUpdateFunc
271
315
-> UpdaterData
316
+ -> FilePath
272
317
-> IO ExitCode
273
- runLauncher loggingDependencies walletRunner daedalusBin runUpdateFunc updaterData = do
318
+ runLauncher loggingDependencies walletRunner daedalusBin runUpdateFunc updaterData stateDir = do
319
+ safeMode <- readSafeMode stateDir
274
320
275
321
-- In the case the user wants to avoid installing the update now, we
276
322
-- run the update (if there is one) when we have it downloaded.
@@ -283,11 +329,12 @@ runLauncher loggingDependencies walletRunner daedalusBin runUpdateFunc updaterDa
283
329
-- You still want to run the wallet even if the update fails
284
330
runWalletProcess
285
331
loggingDependencies
286
- WalletModeNormal
332
+ safeMode
287
333
daedalusBin
288
334
walletRunner
289
335
runUpdateFunc
290
336
updaterData
337
+ stateDir
291
338
292
339
-- | Generation of the TLS certificates.
293
340
-- This just covers the generation of the TLS certificates and nothing else.
0 commit comments