Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mnemonic sentence support #975

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
13 changes: 13 additions & 0 deletions cardano-cli/src/Cardano/CLI/Commands/Key.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Cardano.CLI.Commands.Key
( KeyCmds (..)
, KeyVerificationKeyCmdArgs (..)
, KeyNonExtendedKeyCmdArgs (..)
, KeyGenerateMnemonicCmdArgs (..)
, KeyExtendedSigningKeyFromMnemonicArgs (..)
, ExtendedSigningType (..)
, KeyConvertByronKeyCmdArgs (..)
Expand All @@ -28,6 +29,7 @@ import Data.Text (Text)
data KeyCmds
= KeyVerificationKeyCmd !KeyVerificationKeyCmdArgs
| KeyNonExtendedKeyCmd !KeyNonExtendedKeyCmdArgs
| KeyGenerateMnemonicCmd !KeyGenerateMnemonicCmdArgs
| KeyExtendedSigningKeyFromMnemonicCmd !KeyExtendedSigningKeyFromMnemonicArgs
| KeyConvertByronKeyCmd !KeyConvertByronKeyCmdArgs
| KeyConvertByronGenesisVKeyCmd !KeyConvertByronGenesisVKeyCmdArgs
Expand Down Expand Up @@ -56,6 +58,15 @@ data KeyNonExtendedKeyCmdArgs = KeyNonExtendedKeyCmdArgs
}
deriving Show

-- | Generate a mnemonic phrase that can be used to derive signing keys.
data KeyGenerateMnemonicCmdArgs = KeyGenerateMnemonicCmdArgs
{ mnemonicOutputFormat :: !(Maybe (File () Out))
-- ^ Output format for the mnemonic phrase
, mnemonicWords :: !MnemonicSize
-- ^ Number of mnemonic words to generate it must be one of: 12, 15, 18, 21, or 24.
}
deriving Show

-- | Get a verification key from a mnemonic. This supports all extended key types.
palas marked this conversation as resolved.
Show resolved Hide resolved
data KeyExtendedSigningKeyFromMnemonicArgs = KeyExtendedSigningKeyFromMnemonicArgs
{ keyOutputFormat :: !KeyOutputFormat
Expand Down Expand Up @@ -146,6 +157,8 @@ renderKeyCmds = \case
"key verification-key"
KeyNonExtendedKeyCmd{} ->
"key non-extended-key"
KeyGenerateMnemonicCmd{} ->
"key generate-mnemonic"
KeyExtendedSigningKeyFromMnemonicCmd{} ->
"key from-mnemonic"
KeyConvertByronKeyCmd{} ->
Expand Down
36 changes: 36 additions & 0 deletions cardano-cli/src/Cardano/CLI/Options/Key.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ pKeyCmds =
, "extended verification key. This supports all "
, "extended key types."
]
, subParser "generate-mnemonic" $
Opt.info pKeyGenerateMnemonicCmd $
Opt.progDesc $
mconcat
[ "Generate a mnemonic sentence that can be used "
, "for key derivation."
]
, subParser "key-from-mnemonic" $
Opt.info pKeyExtendedSigningKeyFromMnemonicCmd $
Opt.progDesc $
Expand Down Expand Up @@ -122,6 +129,35 @@ pKeyNonExtendedKeyCmd =
<$> pExtendedVerificationKeyFileIn
<*> pVerificationKeyFileOut

pKeyGenerateMnemonicCmd :: Parser KeyCmds
pKeyGenerateMnemonicCmd =
fmap KeyGenerateMnemonicCmd $
KeyGenerateMnemonicCmdArgs
<$> optional pOutputFile
<*> pMnemonicSize

pMnemonicSize :: Parser MnemonicSize
pMnemonicSize = do
option
(maybeReader parseSize)
( long "size"
<> metavar "WORD32"
<> Opt.help
( mconcat
[ "Specify the desired number of words for the output"
, "mnemonic sentence (valid options are: 12, 15, 18, 21, and 24)"
]
)
)
where
parseSize :: String -> Maybe MnemonicSize
palas marked this conversation as resolved.
Show resolved Hide resolved
parseSize "12" = Just MS12
parseSize "15" = Just MS15
parseSize "18" = Just MS18
parseSize "21" = Just MS21
parseSize "24" = Just MS24
parseSize _ = Nothing

pKeyExtendedSigningKeyFromMnemonicCmd :: Parser KeyCmds
pKeyExtendedSigningKeyFromMnemonicCmd =
fmap KeyExtendedSigningKeyFromMnemonicCmd $
Expand Down
15 changes: 15 additions & 0 deletions cardano-cli/src/Cardano/CLI/Run/Key.hs
palas marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ runKeyCmds = \case
runVerificationKeyCmd cmd
Cmd.KeyNonExtendedKeyCmd cmd ->
runNonExtendedKeyCmd cmd
Cmd.KeyGenerateMnemonicCmd cmd ->
runGenerateMnemonicCmd cmd
Cmd.KeyExtendedSigningKeyFromMnemonicCmd cmd ->
runExtendedSigningKeyFromMnemonicCmd cmd
Cmd.KeyConvertByronKeyCmd cmd ->
Expand Down Expand Up @@ -205,6 +207,19 @@ runNonExtendedKeyCmd
writeLazyByteStringFile vkf' $
textEnvelopeToJSON descr vk

runGenerateMnemonicCmd :: Cmd.KeyGenerateMnemonicCmdArgs -> ExceptT KeyCmdError IO ()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think when writing to file we should at least set restrictive permissions. Can you get in touch with the wallet team and ask them what precautions they took when generating mnemonics?

runGenerateMnemonicCmd
Cmd.KeyGenerateMnemonicCmdArgs
{ mnemonicOutputFormat
, mnemonicWords
} = do
mnemonic <- firstExceptT KeyCmdMnemonicError $ generateMnemonic mnemonicWords
palas marked this conversation as resolved.
Show resolved Hide resolved
case mnemonicOutputFormat of
Just outFile ->
firstExceptT KeyCmdWriteFileError . newExceptT $
writeTextFile outFile (T.unwords mnemonic)
Nothing -> liftIO $ putStrLn $ T.unpack (T.unwords mnemonic)

readExtendedVerificationKeyFile
:: VerificationKeyFile In
-> ExceptT KeyCmdError IO SomeAddressVerificationKey
Expand Down
47 changes: 47 additions & 0 deletions cardano-cli/test/cardano-cli-golden/files/golden/help.cli
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ Usage: cardano-cli address info --address ADDRESS [--out-file FILEPATH]
Usage: cardano-cli key
( verification-key
| non-extended-key
| generate-mnemonic
| key-from-mnemonic
palas marked this conversation as resolved.
Show resolved Hide resolved
| convert-byron-key
| convert-byron-genesis-vkey
Expand All @@ -86,6 +87,10 @@ Usage: cardano-cli key non-extended-key --extended-verification-key-file FILEPAT
Get a non-extended verification key from an extended verification key. This
supports all extended key types.

Usage: cardano-cli key generate-mnemonic [--out-file FILEPATH] --size WORD32

Generate a mnemonic sentence that can be used for key derivation.

Usage: cardano-cli key key-from-mnemonic [--key-output-format STRING]
( --payment-key
--payment-address-number WORD32
palas marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -1020,6 +1025,7 @@ Usage: cardano-cli shelley address info --address ADDRESS [--out-file FILEPATH]
Usage: cardano-cli shelley key
( verification-key
| non-extended-key
| generate-mnemonic
| key-from-mnemonic
| convert-byron-key
| convert-byron-genesis-vkey
Expand All @@ -1042,6 +1048,11 @@ Usage: cardano-cli shelley key non-extended-key --extended-verification-key-file
Get a non-extended verification key from an extended verification key. This
supports all extended key types.

Usage: cardano-cli shelley key generate-mnemonic [--out-file FILEPATH]
--size WORD32

Generate a mnemonic sentence that can be used for key derivation.

Usage: cardano-cli shelley key key-from-mnemonic [--key-output-format STRING]
( --payment-key
--payment-address-number WORD32
Expand Down Expand Up @@ -2108,6 +2119,7 @@ Usage: cardano-cli allegra address info --address ADDRESS [--out-file FILEPATH]
Usage: cardano-cli allegra key
( verification-key
| non-extended-key
| generate-mnemonic
| key-from-mnemonic
| convert-byron-key
| convert-byron-genesis-vkey
Expand All @@ -2130,6 +2142,11 @@ Usage: cardano-cli allegra key non-extended-key --extended-verification-key-file
Get a non-extended verification key from an extended verification key. This
supports all extended key types.

Usage: cardano-cli allegra key generate-mnemonic [--out-file FILEPATH]
--size WORD32

Generate a mnemonic sentence that can be used for key derivation.

Usage: cardano-cli allegra key key-from-mnemonic [--key-output-format STRING]
( --payment-key
--payment-address-number WORD32
Expand Down Expand Up @@ -3196,6 +3213,7 @@ Usage: cardano-cli mary address info --address ADDRESS [--out-file FILEPATH]
Usage: cardano-cli mary key
( verification-key
| non-extended-key
| generate-mnemonic
| key-from-mnemonic
| convert-byron-key
| convert-byron-genesis-vkey
Expand All @@ -3218,6 +3236,11 @@ Usage: cardano-cli mary key non-extended-key --extended-verification-key-file FI
Get a non-extended verification key from an extended verification key. This
supports all extended key types.

Usage: cardano-cli mary key generate-mnemonic [--out-file FILEPATH]
--size WORD32

Generate a mnemonic sentence that can be used for key derivation.

Usage: cardano-cli mary key key-from-mnemonic [--key-output-format STRING]
( --payment-key
--payment-address-number WORD32
Expand Down Expand Up @@ -4274,6 +4297,7 @@ Usage: cardano-cli alonzo address info --address ADDRESS [--out-file FILEPATH]
Usage: cardano-cli alonzo key
( verification-key
| non-extended-key
| generate-mnemonic
| key-from-mnemonic
| convert-byron-key
| convert-byron-genesis-vkey
Expand All @@ -4296,6 +4320,11 @@ Usage: cardano-cli alonzo key non-extended-key --extended-verification-key-file
Get a non-extended verification key from an extended verification key. This
supports all extended key types.

Usage: cardano-cli alonzo key generate-mnemonic [--out-file FILEPATH]
--size WORD32

Generate a mnemonic sentence that can be used for key derivation.

Usage: cardano-cli alonzo key key-from-mnemonic [--key-output-format STRING]
( --payment-key
--payment-address-number WORD32
Expand Down Expand Up @@ -5367,6 +5396,7 @@ Usage: cardano-cli babbage address info --address ADDRESS [--out-file FILEPATH]
Usage: cardano-cli babbage key
( verification-key
| non-extended-key
| generate-mnemonic
| key-from-mnemonic
| convert-byron-key
| convert-byron-genesis-vkey
Expand All @@ -5389,6 +5419,11 @@ Usage: cardano-cli babbage key non-extended-key --extended-verification-key-file
Get a non-extended verification key from an extended verification key. This
supports all extended key types.

Usage: cardano-cli babbage key generate-mnemonic [--out-file FILEPATH]
--size WORD32

Generate a mnemonic sentence that can be used for key derivation.

Usage: cardano-cli babbage key key-from-mnemonic [--key-output-format STRING]
( --payment-key
--payment-address-number WORD32
Expand Down Expand Up @@ -6740,6 +6775,7 @@ Usage: cardano-cli conway address info --address ADDRESS [--out-file FILEPATH]
Usage: cardano-cli conway key
( verification-key
| non-extended-key
| generate-mnemonic
| key-from-mnemonic
| convert-byron-key
| convert-byron-genesis-vkey
Expand All @@ -6762,6 +6798,11 @@ Usage: cardano-cli conway key non-extended-key --extended-verification-key-file
Get a non-extended verification key from an extended verification key. This
supports all extended key types.

Usage: cardano-cli conway key generate-mnemonic [--out-file FILEPATH]
--size WORD32

Generate a mnemonic sentence that can be used for key derivation.

Usage: cardano-cli conway key key-from-mnemonic [--key-output-format STRING]
( --payment-key
--payment-address-number WORD32
Expand Down Expand Up @@ -8755,6 +8796,7 @@ Usage: cardano-cli latest address info --address ADDRESS [--out-file FILEPATH]
Usage: cardano-cli latest key
( verification-key
| non-extended-key
| generate-mnemonic
| key-from-mnemonic
| convert-byron-key
| convert-byron-genesis-vkey
Expand All @@ -8777,6 +8819,11 @@ Usage: cardano-cli latest key non-extended-key --extended-verification-key-file
Get a non-extended verification key from an extended verification key. This
supports all extended key types.

Usage: cardano-cli latest key generate-mnemonic [--out-file FILEPATH]
--size WORD32

Generate a mnemonic sentence that can be used for key derivation.

Usage: cardano-cli latest key key-from-mnemonic [--key-output-format STRING]
( --payment-key
--payment-address-number WORD32
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Usage: cardano-cli allegra key
( verification-key
| non-extended-key
| generate-mnemonic
| key-from-mnemonic
palas marked this conversation as resolved.
Show resolved Hide resolved
| convert-byron-key
| convert-byron-genesis-vkey
Expand All @@ -21,6 +22,8 @@ Available commands:
non-extended-key Get a non-extended verification key from an extended
verification key. This supports all extended key
types.
generate-mnemonic Generate a mnemonic sentence that can be used for key
derivation.
key-from-mnemonic Derive an extended signing key from a mnemonic.
convert-byron-key Convert a Byron payment, genesis or genesis delegate
key (signing or verification) to a corresponding
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Usage: cardano-cli allegra key generate-mnemonic [--out-file FILEPATH]
--size WORD32

Generate a mnemonic sentence that can be used for key derivation.

Available options:
--out-file FILEPATH The output file.
--size WORD32 Specify the desired number of words for the
outputmnemonic sentence (valid options are: 12, 15,
18, 21, and 24)
-h,--help Show this help text
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Usage: cardano-cli alonzo key
( verification-key
| non-extended-key
| generate-mnemonic
| key-from-mnemonic
| convert-byron-key
| convert-byron-genesis-vkey
Expand All @@ -21,6 +22,8 @@ Available commands:
non-extended-key Get a non-extended verification key from an extended
verification key. This supports all extended key
types.
generate-mnemonic Generate a mnemonic sentence that can be used for key
derivation.
key-from-mnemonic Derive an extended signing key from a mnemonic.
convert-byron-key Convert a Byron payment, genesis or genesis delegate
key (signing or verification) to a corresponding
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Usage: cardano-cli alonzo key generate-mnemonic [--out-file FILEPATH]
--size WORD32

Generate a mnemonic sentence that can be used for key derivation.

Available options:
--out-file FILEPATH The output file.
--size WORD32 Specify the desired number of words for the
outputmnemonic sentence (valid options are: 12, 15,
18, 21, and 24)
-h,--help Show this help text
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Usage: cardano-cli babbage key
( verification-key
| non-extended-key
| generate-mnemonic
| key-from-mnemonic
| convert-byron-key
| convert-byron-genesis-vkey
Expand All @@ -21,6 +22,8 @@ Available commands:
non-extended-key Get a non-extended verification key from an extended
verification key. This supports all extended key
types.
generate-mnemonic Generate a mnemonic sentence that can be used for key
derivation.
key-from-mnemonic Derive an extended signing key from a mnemonic.
convert-byron-key Convert a Byron payment, genesis or genesis delegate
key (signing or verification) to a corresponding
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Usage: cardano-cli babbage key generate-mnemonic [--out-file FILEPATH]
--size WORD32

Generate a mnemonic sentence that can be used for key derivation.

Available options:
--out-file FILEPATH The output file.
--size WORD32 Specify the desired number of words for the
outputmnemonic sentence (valid options are: 12, 15,
18, 21, and 24)
-h,--help Show this help text
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Usage: cardano-cli conway key
( verification-key
| non-extended-key
| generate-mnemonic
| key-from-mnemonic
| convert-byron-key
| convert-byron-genesis-vkey
Expand All @@ -21,6 +22,8 @@ Available commands:
non-extended-key Get a non-extended verification key from an extended
verification key. This supports all extended key
types.
generate-mnemonic Generate a mnemonic sentence that can be used for key
derivation.
key-from-mnemonic Derive an extended signing key from a mnemonic.
convert-byron-key Convert a Byron payment, genesis or genesis delegate
key (signing or verification) to a corresponding
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Usage: cardano-cli conway key generate-mnemonic [--out-file FILEPATH]
--size WORD32

Generate a mnemonic sentence that can be used for key derivation.

Available options:
--out-file FILEPATH The output file.
--size WORD32 Specify the desired number of words for the
outputmnemonic sentence (valid options are: 12, 15,
18, 21, and 24)
-h,--help Show this help text
Loading