11{-# LANGUAGE DataKinds #-}
22{-# LANGUAGE FlexibleContexts #-}
33{-# LANGUAGE GADTs #-}
4+ {-# LANGUAGE LambdaCase #-}
45{-# LANGUAGE ScopedTypeVariables #-}
56
67module Cardano.CLI.EraIndependent.Key.Option
@@ -17,8 +18,10 @@ import Cardano.CLI.Type.Common
1718
1819import Data.Foldable
1920import Data.Text (Text )
21+ import GHC.Word (Word32 )
2022import Options.Applicative hiding (help , str )
2123import Options.Applicative qualified as Opt
24+ import Options.Applicative.Types (readerAsk )
2225
2326{- HLINT ignore "Use <$>" -}
2427{- HLINT ignore "Move brackets to avoid $" -}
@@ -42,6 +45,23 @@ pKeyCmds =
4245 , " extended verification key. This supports all "
4346 , " extended key types."
4447 ]
48+ , subParser " generate-mnemonic" $
49+ Opt. info pKeyGenerateMnemonicCmd $
50+ Opt. progDesc $
51+ mconcat
52+ [ " Generate a mnemonic sentence that can be used "
53+ , " for key derivation."
54+ ]
55+ , subParser " derive-from-mnemonic" $
56+ Opt. info pKeyExtendedSigningKeyFromMnemonicCmd $
57+ Opt. progDesc $
58+ mconcat
59+ [ " Derive an extended signing key from a mnemonic "
60+ , " sentence. "
61+ , " To ensure the safety of the mnemonic phrase, "
62+ , " we recommend that key derivation is performed "
63+ , " in an air-gapped environment."
64+ ]
4565 , subParser " convert-byron-key" $
4666 Opt. info pKeyConvertByronKeyCmd $
4767 Opt. progDesc $
@@ -114,6 +134,107 @@ pKeyNonExtendedKeyCmd =
114134 <$> pExtendedVerificationKeyFileIn
115135 <*> pVerificationKeyFileOut
116136
137+ pKeyGenerateMnemonicCmd :: Parser KeyCmds
138+ pKeyGenerateMnemonicCmd =
139+ fmap KeyGenerateMnemonicCmd $
140+ KeyGenerateMnemonicCmdArgs
141+ <$> optional pOutputFile
142+ <*> pMnemonicSize
143+
144+ pMnemonicSize :: Parser MnemonicSize
145+ pMnemonicSize = do
146+ option
147+ parseSize
148+ ( long " size"
149+ <> metavar " WORD32"
150+ <> Opt. help
151+ ( mconcat
152+ [ " Specify the desired number of words for the output"
153+ , " mnemonic sentence (valid options are: 12, 15, 18, 21, and 24)"
154+ ]
155+ )
156+ )
157+ where
158+ parseSize :: ReadM MnemonicSize
159+ parseSize =
160+ readerAsk
161+ >>= \ case
162+ " 12" -> return MS12
163+ " 15" -> return MS15
164+ " 18" -> return MS18
165+ " 21" -> return MS21
166+ " 24" -> return MS24
167+ invalidSize ->
168+ readerError $
169+ " Invalid mnemonic size " <> show invalidSize <> " ! It must be one of: 12, 15, 18, 21, or 24."
170+
171+ pKeyExtendedSigningKeyFromMnemonicCmd :: Parser KeyCmds
172+ pKeyExtendedSigningKeyFromMnemonicCmd =
173+ fmap KeyExtendedSigningKeyFromMnemonicCmd $
174+ KeyExtendedSigningKeyFromMnemonicArgs
175+ <$> pKeyOutputFormat
176+ <*> pDerivedExtendedSigningKeyType
177+ <*> pAccountNumber
178+ <*> pMnemonicSource
179+ <*> pSigningKeyFileOut
180+
181+ pDerivedExtendedSigningKeyType :: Parser ExtendedSigningType
182+ pDerivedExtendedSigningKeyType =
183+ asum
184+ [ Opt. option (ExtendedSigningPaymentKey <$> integralReader) $
185+ mconcat
186+ [ Opt. long " payment-key-with-number"
187+ , Opt. metavar " WORD32"
188+ , Opt. help
189+ " Derive an extended payment key with the given payment address number from the derivation path."
190+ ]
191+ , Opt. option (ExtendedSigningStakeKey <$> integralReader) $
192+ mconcat
193+ [ Opt. long " stake-key-with-number"
194+ , Opt. metavar " WORD32"
195+ , Opt. help
196+ " Derive an extended stake key with the given stake address number from the derivation path."
197+ ]
198+ , Opt. flag' ExtendedSigningDRepKey $
199+ mconcat
200+ [ Opt. long " drep-key"
201+ , Opt. help " Derive an extended DRep key."
202+ ]
203+ , Opt. flag' ExtendedSigningCCColdKey $
204+ mconcat
205+ [ Opt. long " cc-cold-key"
206+ , Opt. help " Derive an extended committee cold key."
207+ ]
208+ , Opt. flag' ExtendedSigningCCHotKey $
209+ mconcat
210+ [ Opt. long " cc-hot-key"
211+ , Opt. help " Derive an extended committee hot key."
212+ ]
213+ ]
214+
215+ pMnemonicSource :: Parser MnemonicSource
216+ pMnemonicSource =
217+ asum
218+ [ MnemonicFromFile . File <$> parseFilePath " mnemonic-from-file" " Input text file with the mnemonic."
219+ , Opt. flag' MnemonicFromInteractivePrompt $
220+ mconcat
221+ [ Opt. long " mnemonic-from-interactive-prompt"
222+ , Opt. help $
223+ " Input the mnemonic through an interactive prompt. "
224+ <> " This mode also accepts receiving the mnemonic through "
225+ <> " standard input directly, for example, by using a pipe."
226+ ]
227+ ]
228+
229+ pAccountNumber :: Parser Word32
230+ pAccountNumber =
231+ Opt. option integralReader $
232+ mconcat
233+ [ Opt. long " account-number"
234+ , Opt. metavar " WORD32"
235+ , Opt. help " Account number in the derivation path."
236+ ]
237+
117238pKeyConvertByronKeyCmd :: Parser KeyCmds
118239pKeyConvertByronKeyCmd =
119240 fmap KeyConvertByronKeyCmd $
0 commit comments