Skip to content

Commit

Permalink
Check produced number of mnemonic words is as expected
Browse files Browse the repository at this point in the history
  • Loading branch information
palas committed Nov 26, 2024
1 parent 992e6b9 commit b05b69b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
13 changes: 13 additions & 0 deletions cardano-cli/src/Cardano/CLI/Run/Key.hs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import Cardano.Prelude (isSpace)

import qualified Codec.Binary.Bech32 as Bech32
import qualified Control.Exception as Exception
import Control.Monad (when)
import Data.Bifunctor (Bifunctor (..))
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
Expand Down Expand Up @@ -219,11 +220,23 @@ runGenerateMnemonicCmd
, mnemonicWords
} = do
mnemonic <- firstExceptT KeyCmdMnemonicError $ generateMnemonic mnemonicWords
let expectedNumOfMnemonicWords = mnemonicSizeToInt mnemonicWords
obtainedNumOfMnemonicWords = length mnemonic
when (obtainedNumOfMnemonicWords /= expectedNumOfMnemonicWords) $
left $
KeyCmdWrongNumOfMnemonics expectedNumOfMnemonicWords obtainedNumOfMnemonicWords
case mnemonicOutputFormat of
Just outFile ->
firstExceptT KeyCmdWriteFileError . newExceptT $
writeTextFile outFile (T.unwords mnemonic)
Nothing -> liftIO $ putStrLn $ T.unpack (T.unwords mnemonic)
where
mnemonicSizeToInt :: MnemonicSize -> Int
mnemonicSizeToInt MS12 = 12
mnemonicSizeToInt MS15 = 15
mnemonicSizeToInt MS18 = 18
mnemonicSizeToInt MS21 = 21
mnemonicSizeToInt MS24 = 24

readExtendedVerificationKeyFile
:: VerificationKeyFile In
Expand Down
7 changes: 7 additions & 0 deletions cardano-cli/src/Cardano/CLI/Types/Errors/KeyCmdError.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ data KeyCmdError
| KeyCmdReadMnemonicFileError !(FileError ())
| KeyCmdWriteFileError !(FileError ())
| KeyCmdMnemonicError MnemonicToSigningKeyError
| KeyCmdWrongNumOfMnemonics
!Int
-- ^ Expected number of mnemonics
!Int
-- ^ Actual number of mnemonics
| KeyCmdByronKeyFailure !Byron.ByronKeyFailure
| -- | Text representation of the parse error. Unfortunately, the actual
-- error type isn't exported.
Expand Down Expand Up @@ -51,6 +56,8 @@ renderKeyCmdError err =
prettyError fileErr
KeyCmdMnemonicError mnemonicErr ->
"Error converting the mnemonic into a key: " <> prettyError mnemonicErr
KeyCmdWrongNumOfMnemonics expected actual ->
"Internal error: expected " <> pretty expected <> " mnemonics, but got " <> pretty actual
KeyCmdByronKeyFailure e ->
Byron.renderByronKeyFailure e
KeyCmdByronKeyParseError errTxt ->
Expand Down

0 comments on commit b05b69b

Please sign in to comment.