never executed always true always false
1 {-# LANGUAGE DataKinds #-}
2 {-# LANGUAGE FlexibleContexts #-}
3 {-# LANGUAGE NamedFieldPuns #-}
4 {-# LANGUAGE TypeApplications #-}
5
6 {-# OPTIONS_HADDOCK hide #-}
7
8 module Command.RecoveryPhrase.Generate
9 ( Cmd (..)
10 , mod
11 , run
12 ) where
13
14 import Prelude hiding
15 ( mod )
16
17 import Cardano.Mnemonic
18 ( entropyToMnemonic, genEntropy, mnemonicToText )
19 import Options.Applicative
20 ( CommandFields, Mod, command, helper, info, progDesc )
21 import Options.Applicative.MnemonicSize
22 ( MnemonicSize (..), mnemonicSizeOpt )
23
24 import qualified Data.ByteString.Char8 as B8
25 import qualified Data.Text as T
26 import qualified Data.Text.Encoding as T
27
28
29 newtype Cmd = Generate
30 { size :: MnemonicSize
31 } deriving (Show)
32
33 mod :: (Cmd -> parent) -> Mod CommandFields parent
34 mod liftCmd = command "generate" $
35 info (helper <*> fmap liftCmd parser) $ mempty
36 <> progDesc "Generate an English recovery phrase"
37 where
38 parser = Generate
39 <$> mnemonicSizeOpt
40
41 run :: Cmd -> IO ()
42 run Generate{size} = do
43 m <- case size of
44 MS_9 -> mnemonicToText @9 . entropyToMnemonic <$> genEntropy
45 MS_12 -> mnemonicToText @12 . entropyToMnemonic <$> genEntropy
46 MS_15 -> mnemonicToText @15 . entropyToMnemonic <$> genEntropy
47 MS_18 -> mnemonicToText @18 . entropyToMnemonic <$> genEntropy
48 MS_21 -> mnemonicToText @21 . entropyToMnemonic <$> genEntropy
49 MS_24 -> mnemonicToText @24 . entropyToMnemonic <$> genEntropy
50 B8.putStrLn $ T.encodeUtf8 $ T.unwords m