Skip to content

Commit c15902d

Browse files
committed
[cli] Improve decryption error
1 parent f91934f commit c15902d

File tree

6 files changed

+50
-36
lines changed

6 files changed

+50
-36
lines changed

cli/anti.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ library
171171
, directory
172172
, exceptions
173173
, github
174+
, haskeline
174175
, http-client
175176
, http-client-tls
176177
, http-types

cli/src/Core/Encryption.hs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ decrypt
148148
-- ^ passphrase
149149
-> ByteString
150150
-- ^ encrypted message
151-
-> IO ByteString
151+
-> Either String ByteString
152152
decrypt passphrase encrypted = do
153153
case parseEncrypted encrypted of
154154
Nothing -> error "Failed to parse encrypted message iv"
@@ -178,12 +178,14 @@ decryptText
178178
-- ^ passphrase
179179
-> Text
180180
-- ^ encrypted message
181-
-> IO Text
181+
-> Either String Text
182182
-- ^ decrypted message
183183
decryptText passphrase encrypted = do
184184
let encryptedBS = convertFromBase Base16 (encodeUtf8 encrypted)
185185
case encryptedBS of
186-
Left err -> error $ "Failed to decode encrypted message: " ++ err
186+
Left _err -> Left "Decryption failed: invalid base16 message"
187187
Right encBS -> do
188188
decrypted <- decrypt (encodeUtf8 passphrase) encBS
189-
pure $ decodeUtf8 decrypted
189+
case decodeUtf8' decrypted of
190+
Left _err -> Left "Decryption failed: invalid UTF8 decrypted"
191+
Right txt -> pure txt

cli/src/Core/Types/Mnemonics/Options.hs

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,27 @@ import Data.Aeson
1212
import Data.Aeson qualified as Aeson
1313
import Data.ByteString.Lazy qualified as BL
1414
import Data.Text (Text)
15+
import Data.Text qualified as T
1516
import OptEnvConf
1617
( Alternative ((<|>))
17-
, Builder
1818
, Parser
19+
, checkEither
1920
, checkMapIO
2021
, conf
2122
, env
2223
, help
24+
, long
2325
, mapIO
2426
, metavar
27+
, option
2528
, reader
2629
, setting
30+
, short
2731
, str
32+
, switch
2833
, withConfig
2934
)
35+
import System.Console.Haskeline
3036

3137
mnemonicsClearTextOption :: Parser Text
3238
mnemonicsClearTextOption =
@@ -43,33 +49,48 @@ mnemonicsEncryptedOption =
4349
, conf "encryptedMnemonics"
4450
, metavar "ENCRYPTED_MNEMONICS"
4551
]
46-
walletPassphraseCommon :: [Builder a]
52+
53+
walletPassphraseCommon :: Parser Text
4754
walletPassphraseCommon =
48-
[ env "ANTI_WALLET_PASSPHRASE"
49-
, metavar "PASSPHRASE"
50-
, help "The passphrase for the encrypted mnemonics"
51-
]
52-
walletPassphraseOption
53-
:: Parser (Text -> IO Text)
54-
walletPassphraseOption =
55-
fmap decryptText
55+
mapIO id
5656
$ setting
57-
$ reader str : walletPassphraseCommon
57+
[ help "Prompt for the passphrase for the encrypted mnemonics"
58+
, long "interactive-wallet-passphrase"
59+
, switch $ queryConsole "Enter passphrase for encrypted mnemonics"
60+
]
61+
<|> setting
62+
[ env "ANTI_WALLET_PASSPHRASE"
63+
, metavar "PASSPHRASE"
64+
, help "The passphrase for the encrypted mnemonics"
65+
, reader $ fmap (pure . T.pack) str
66+
]
67+
68+
queryConsole :: String -> IO Text
69+
queryConsole prompt = runInputT defaultSettings $ do
70+
pw <- getPassword (Just '*') (prompt <> ": ")
71+
case pw of
72+
Nothing -> pure ""
73+
Just pw' -> pure $ T.pack pw'
5874

5975
walletFileOption :: Parser FilePath
6076
walletFileOption =
6177
setting
6278
[ env "ANTI_WALLET_FILE"
6379
, metavar "FILEPATH"
64-
, help "The file path to the wallet secrets mnemonics"
80+
, help "The file path to the wallet secret mnemonics"
81+
, long "wallet"
82+
, short 'w'
6583
, reader str
84+
, option
6685
]
6786

6887
coreMnemonicsParser :: Parser (Mnemonics 'DecryptedS)
6988
coreMnemonicsParser =
7089
fmap ClearText
7190
$ mnemonicsClearTextOption
72-
<|> mapIO id (walletPassphraseOption <*> mnemonicsEncryptedOption)
91+
<|> checkEither
92+
id
93+
(decryptText <$> walletPassphraseCommon <*> mnemonicsEncryptedOption)
7394

7495
mnemonicsObject :: Parser Object
7596
mnemonicsObject =

cli/src/Wallet/Cli.hs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,18 @@ instance (ToJSON m a, Monad m) => ToJSON m (Either WalletError a) where
3030
data WalletInfo = WalletInfo
3131
{ address :: Address
3232
, owner :: Owner
33-
, filePath :: FilePath
3433
}
3534

3635
instance Monad m => ToJSON m WalletInfo where
37-
toJSON WalletInfo{address, owner, filePath} =
36+
toJSON WalletInfo{address, owner} =
3837
object
3938
[ "address" .= address
4039
, "owner" .= owner
41-
, "filePath" .= filePath
4240
]
4341

4442
data WalletCommand a where
4543
Info
46-
:: Wallet -> FilePath -> WalletCommand (Either WalletError WalletInfo)
44+
:: Wallet -> WalletCommand (Either WalletError WalletInfo)
4745
Create
4846
:: FilePath
4947
-> Maybe Text
@@ -53,13 +51,12 @@ deriving instance Show (WalletCommand a)
5351
deriving instance Eq (WalletCommand a)
5452

5553
walletCmd :: WalletCommand a -> IO a
56-
walletCmd (Info wallet filePath) =
54+
walletCmd (Info wallet) =
5755
pure
5856
$ Right
5957
$ WalletInfo
6058
{ address = wallet.address
6159
, owner = wallet.owner
62-
, filePath = filePath
6360
}
6461
walletCmd (Create walletFile passphrase) = do
6562
w12 <- replicateM 12 $ element englishWords
@@ -72,7 +69,6 @@ walletCmd (Create walletFile passphrase) = do
7269
$ WalletInfo
7370
{ address = wallet.address
7471
, owner = wallet.owner
75-
, filePath = walletFile
7672
}
7773

7874
element :: [a] -> IO a

cli/src/Wallet/Options.hs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,9 @@ walletCommandParser =
2626
, command
2727
"info"
2828
"Get the wallet information"
29-
$ fmap Box . Info
29+
$ Box . Info
3030
<$> walletOption
31-
<*> walletFileOption
3231
]
3332

3433
passphraseOption :: Parser (Maybe Text)
35-
passphraseOption =
36-
setting
37-
$ walletPassphraseCommon
38-
<> [ reader $ Just <$> str
39-
, value Nothing
40-
]
34+
passphraseOption = optional walletPassphraseCommon

cli/test/Core/EncryptionSpec.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ spec = do
3333
$ forAll gen
3434
$ \(message, passphrase, runs) -> do
3535
encrypted <- encrypt passphrase runs message
36-
decrypted <- decrypt passphrase encrypted
37-
decrypted `shouldBe` message
36+
let decrypted = decrypt passphrase encrypted
37+
decrypted `shouldBe` Right message
3838
it "should encrypt and decrypt a Text message correctly"
3939
$ forAll genText
4040
$ \(message, passphrase, runs) -> do
4141
encrypted <- encryptText passphrase runs message
42-
decrypted <- decryptText passphrase encrypted
43-
decrypted `shouldBe` message
42+
let decrypted = decryptText passphrase encrypted
43+
decrypted `shouldBe` Right message

0 commit comments

Comments
 (0)