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 4 commits into
base: master
Choose a base branch
from
Open

Add mnemonic sentence support #975

wants to merge 4 commits into from

Conversation

palas
Copy link
Contributor

@palas palas commented Nov 20, 2024

Changelog

- description: |
    Added support for mnemonic sentence generation and extended key derivation from mnemonic sentences.
  type:
  - feature

Context

This PR is associated to: IntersectMBO/cardano-api#678.
It is part of an effort to integrate cardano-addresses functionalities into the cardano-cli.

How to trust this PR

I would look mainly at the logic, the changes in the help golden files, and probably testing it is the best way.

Checklist

  • Commit sequence broadly makes sense and commits have useful messages
  • New tests are added if needed and existing tests are updated. See Running tests for more details
  • Self-reviewed the diff

Copy link
Contributor

@Jimbo4350 Jimbo4350 left a comment

Choose a reason for hiding this comment

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

LGTM but I want feedback from the rest of the team before we approve and merge.

@@ -52,6 +56,24 @@ data KeyNonExtendedKeyCmdArgs = KeyNonExtendedKeyCmdArgs
}
deriving Show

-- | Get a verification key from a mnemonic. This supports all extended key types.
Copy link
Contributor

Choose a reason for hiding this comment

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

verification key?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice catch

}
deriving Show

data ExtendedSigningType
Copy link
Contributor

Choose a reason for hiding this comment

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

DerivedExtendedSigningKeyType?

Copy link
Contributor

Choose a reason for hiding this comment

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

A comment saying something like "This is the key derived from the mnemonic"

-- | Get a verification key from a mnemonic. This supports all extended key types.
data KeyExtendedSigningKeyFromMnemonicArgs = KeyExtendedSigningKeyFromMnemonicArgs
{ keyOutputFormat :: !KeyOutputFormat
, extendedSigningKeyType :: !ExtendedSigningType
Copy link
Contributor

Choose a reason for hiding this comment

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

derivedExtendedSigningKeyType?

@@ -64,6 +64,7 @@ Usage: cardano-cli address info --address ADDRESS [--out-file FILEPATH]
Usage: cardano-cli key
( verification-key
| non-extended-key
| key-from-mnemonic
Copy link
Contributor

Choose a reason for hiding this comment

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

"signing-key-from-mnemonic"?

@@ -85,6 +86,21 @@ 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 key-from-mnemonic [--key-output-format STRING]
( --payment-key
--payment-address-number WORD32
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 we can collapse to a single parser in the case of those with indexes. I.e: --payment-key-address-number instead of --payment-key and --payment-address-number. We can add information in the help text explaining what the address number is.

)
)
where
parseSize :: String -> Maybe MnemonicSize
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not use ReadM so you can fail with an appropriate error message?

@@ -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?

{ mnemonicOutputFormat
, mnemonicWords
} = do
mnemonic <- firstExceptT KeyCmdMnemonicError $ generateMnemonic mnemonicWords
Copy link
Contributor

Choose a reason for hiding this comment

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

An extra validation step where we confirm the correct number of words have been generated also can't hurt here.

return $ map T.pack $ words $ T.unpack fileText
readMnemonic Cmd.MnemonicFromInteractivePrompt =
liftIO $ do
putStrLn ""
Copy link
Contributor

Choose a reason for hiding this comment

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

unlines would be a good refactor here

@@ -1,6 +1,8 @@
Usage: cardano-cli key
Copy link
Contributor

Choose a reason for hiding this comment

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

I am wondering if we should have a new command root-key that has these commands underneath. We need to make a clear distinction between the root key and the derived keys. Putting the commands under key makes this distinction a little less clear IMO.

Comment on lines +331 to +340
putStrLn ""
putStrLn "Please enter your mnemonic sentence."
putStrLn ""
putStrLn " - It should consist of either: 12, 15, 18, 21, or 24 words."
putStrLn " - To terminate, press enter on an empty line."
putStrLn " - To abort you can press CTRL+C."
putStrLn ""
putStrLn "(If your terminal supports it, you can use the TAB key for word completion.)"
putStrLn ""
runInputTBehaviorWithPrefs defaultBehavior defaultPrefs settings (inputT ("", "") [])
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks cool. Can we support non-interactive senario with reading of mnemonic from stdin here? I imagine the following example, when someone stores their mnemonic in bitwarden, and tries to generate key using the following snippet:

bw list items --search 'cardano super secret mnemonic' \
  | jq -r '.[0].login.password' \
  | cardano-cli latest key key-from-mnemonic ... --signing-key-file key.json

@CarlosLopezDeLara thoughts? do you think this could be useful?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This would actually work with the interactive one as is, I made it with that in mind, and I tested it feeding the output file of the generate-mnemonic command. But it may be a good idea to make one that is explicitly for that, or adding a comment, because the user may not know.

Copy link
Contributor

Choose a reason for hiding this comment

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

Those new additions are quite large so I'd suggest adding a new module for them. For example:
cardano-cli/src/Cardano/CLI/Run/Key/Mnemonic.hs

Comment on lines +39 to +40
--signing-key-file FILEPATH
Output filepath of the signing key.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
--signing-key-file FILEPATH
Output filepath of the signing key.
--out-signing-key-file FILEPATH
Output filepath of the signing key.

I would suggest marking output file so it stands out. It took me a moment to find out the right flag which saves a file.

@@ -1,6 +1,8 @@
Usage: cardano-cli allegra key
( verification-key
| non-extended-key
| generate-mnemonic
| key-from-mnemonic
Copy link
Contributor

Choose a reason for hiding this comment

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

This command feels a little odd: cardano-cli latest key key-from-mnemonic

I think this key prefix seems redundant.

Suggested change
| key-from-mnemonic
| from-mnemonic

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Or we can call it derive-from-mnemonic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants