-
Notifications
You must be signed in to change notification settings - Fork 15
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -222,6 +222,7 @@ library | |
exceptions, | ||
filepath, | ||
formatting, | ||
haskeline, | ||
http-client, | ||
http-client-tls, | ||
http-types, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,6 +49,7 @@ import qualified Cardano.Crypto.Signing as Byron | |
import qualified Cardano.Crypto.Signing as Byron.Crypto | ||
import qualified Cardano.Crypto.Signing as Crypto | ||
import qualified Cardano.Crypto.Wallet as Crypto | ||
import Cardano.Prelude (isSpace) | ||
|
||
import qualified Codec.Binary.Bech32 as Bech32 | ||
import qualified Control.Exception as Exception | ||
|
@@ -60,6 +61,10 @@ import Data.Text (Text) | |
import qualified Data.Text as T | ||
import qualified Data.Text as Text | ||
import qualified Data.Text.Encoding as Text | ||
import System.Console.Haskeline (Completion, InputT, Settings (..), completeWord', | ||
defaultBehavior, defaultPrefs, getInputLineWithInitial, | ||
runInputTBehaviorWithPrefs, simpleCompletion) | ||
import System.Console.Haskeline.Completion (CompletionFunc) | ||
import System.Exit (exitFailure) | ||
|
||
-- Note on these constants: | ||
|
@@ -317,10 +322,58 @@ runExtendedSigningKeyFromMnemonicCmd | |
writeTextFile sKeyPath $ | ||
serialiseToBech32 skey | ||
|
||
readMnemonic :: Cmd.MnemonicSource -> ExceptT KeyCmdError IO [Text] | ||
readMnemonic (Cmd.MnemonicFromFile filePath) = do | ||
fileText <- firstExceptT KeyCmdReadMnemonicFileError $ except =<< readTextFile filePath | ||
return $ map T.pack $ words $ T.unpack fileText | ||
readMnemonic :: Cmd.MnemonicSource -> ExceptT KeyCmdError IO [Text] | ||
readMnemonic (Cmd.MnemonicFromFile filePath) = do | ||
fileText <- firstExceptT KeyCmdReadMnemonicFileError $ except =<< readTextFile filePath | ||
return $ map T.pack $ words $ T.unpack fileText | ||
readMnemonic Cmd.MnemonicFromInteractivePrompt = | ||
liftIO $ do | ||
putStrLn "" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
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 ("", "") []) | ||
Comment on lines
+331
to
+340
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
where | ||
settings :: Monad m => Settings m | ||
settings = | ||
Settings | ||
{ complete = completionFunc | ||
, historyFile = Nothing | ||
, autoAddHistory = False | ||
} | ||
|
||
completionFunc :: Monad m => CompletionFunc m | ||
completionFunc = completeWord' Nothing isSpace completeMnemonicWord | ||
|
||
completeMnemonicWord :: Monad m => String -> m [Completion] | ||
completeMnemonicWord prefix = return $ map (simpleCompletion . T.unpack . fst) $ findMnemonicWordsWithPrefix (T.pack prefix) | ||
|
||
inputT :: (String, String) -> [Text] -> InputT IO [Text] | ||
inputT prefill mnemonic = do | ||
minput <- getInputLineWithInitial (show (length mnemonic + 1) <> ". ") prefill | ||
case minput of | ||
Nothing -> return $ reverse mnemonic | ||
Just "" -> return $ reverse mnemonic | ||
Just input -> | ||
let newWords = map (T.toLower . T.pack) $ filter (not . null) $ words input | ||
in case span isValidMnemonicWord newWords of | ||
(allWords, []) -> inputT ("", "") (reverse allWords ++ mnemonic) | ||
(validWords, invalidWord : notValidatedWords) -> do | ||
liftIO $ putStrLn $ "The word \"" <> T.unpack invalidWord <> "\" is not in the memonic dictionary" | ||
let textBeforeCursor = unwords (map T.unpack validWords <> [T.unpack invalidWord]) | ||
textAfterCursor = | ||
if null notValidatedWords | ||
then "" | ||
else ' ' : unwords (map T.unpack notValidatedWords) | ||
inputT (textBeforeCursor, textAfterCursor) mnemonic | ||
|
||
isValidMnemonicWord :: Text -> Bool | ||
isValidMnemonicWord word = word `elem` map fst (findMnemonicWordsWithPrefix word) | ||
|
||
runConvertByronKeyCmd | ||
:: Cmd.KeyConvertByronKeyCmdArgs | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DerivedExtendedSigningKeyType
?There was a problem hiding this comment.
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"