Skip to content

Commit

Permalink
Merge pull request #58 from input-output-hk/additional_mnemonics_tests
Browse files Browse the repository at this point in the history
Few more unit tests for mnemonics + adjusting wallet API spec
  • Loading branch information
KtorZ authored Mar 13, 2019
2 parents 98a2666 + a8570d4 commit 33026db
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
6 changes: 3 additions & 3 deletions specifications/api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ walletMnemonicSentence: &walletMnemonicSentence
description: A list of mnemonic words
type: array
minItems: 15
maxItems: 30
maxItems: 24
items:
type: string
format: bip-0039-mnemonic-word{english}
Expand All @@ -110,12 +110,12 @@ walletMnemonicSentence: &walletMnemonicSentence
wallet2ndFactor: &wallet2ndFactor
description: An optional passphrase used to encrypt the mnemonic sentence.
type: array
minItems: 6
minItems: 9
maxItems: 12
items:
type: string
format: bip-0039-mnemonic-word{english}
example: ["squirrel", "material", "silly", "twice", "direct", "slush"]
example: ["squirrel", "material", "silly", "twice", "direct", "slush", "pistol", "razor", "become"]

walletPassphrase: &walletPassphrase
description: A master passphrase to lock and protect the wallet for sensitive operation (e.g. sending funds)
Expand Down
39 changes: 39 additions & 0 deletions test/unit/Cardano/Wallet/MnemonicSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ data TestVector = TestVector

spec :: Spec
spec = do

prop "(9) entropyToMnemonic . mnemonicToEntropy == identity" $
\e -> (mnemonicToEntropy @9 . entropyToMnemonic @9 @(EntropySize 9)) e == e

Expand All @@ -83,6 +84,15 @@ spec = do
prop "(15) entropyToMnemonic . mnemonicToEntropy == identity" $
\e -> (mnemonicToEntropy @15 . entropyToMnemonic @15 @(EntropySize 15)) e == e

prop "(18) entropyToMnemonic . mnemonicToEntropy == identity" $
\e -> (mnemonicToEntropy @18 . entropyToMnemonic @18 @(EntropySize 18)) e == e

prop "(21) entropyToMnemonic . mnemonicToEntropy == identity" $
\e -> (mnemonicToEntropy @21 . entropyToMnemonic @21 @(EntropySize 21)) e == e

prop "(24) entropyToMnemonic . mnemonicToEntropy == identity" $
\e -> (mnemonicToEntropy @24 . entropyToMnemonic @24 @(EntropySize 24)) e == e

prop "(9) mkMnemonic . mnemonicToText == pure" $
\(mw :: Mnemonic 9) -> (mkMnemonic @9 . mnemonicToText) mw === pure mw

Expand All @@ -92,6 +102,16 @@ spec = do
prop "(15) mkMnemonic . mnemonicToText == pure" $
\(mw :: Mnemonic 15) -> (mkMnemonic @15 . mnemonicToText) mw === pure mw

prop "(18) mkMnemonic . mnemonicToText == pure" $
\(mw :: Mnemonic 18) -> (mkMnemonic @18 . mnemonicToText) mw === pure mw

prop "(21) mkMnemonic . mnemonicToText == pure" $
\(mw :: Mnemonic 21) -> (mkMnemonic @21 . mnemonicToText) mw === pure mw

prop "(24) mkMnemonic . mnemonicToText == pure" $
\(mw :: Mnemonic 24) -> (mkMnemonic @24 . mnemonicToText) mw === pure mw


describe "golden tests" $ do
it "No empty mnemonic" $
mkMnemonic @12 [] `shouldSatisfy` isLeft
Expand All @@ -105,6 +125,18 @@ spec = do
it "Can generate 128 bits entropy" $
(BS.length . entropyToByteString <$> genEntropy @128) `shouldReturn` 16

it "Can generate 160 bits entropy" $
(BS.length . entropyToByteString <$> genEntropy @160) `shouldReturn` 20

it "Can generate 192 bits entropy" $
(BS.length . entropyToByteString <$> genEntropy @192) `shouldReturn` 24

it "Can generate 224 bits entropy" $
(BS.length . entropyToByteString <$> genEntropy @224) `shouldReturn` 28

it "Can generate 256 bits entropy" $
(BS.length . entropyToByteString <$> genEntropy @256) `shouldReturn` 32

it "Mnemonic to Text" $ forM_ testVectors $ \TestVector{..} ->
mnemonicToText mnemonic `shouldBe` extractWords string

Expand All @@ -116,8 +148,15 @@ spec = do
"[squirrel,material,silly,twice,direct,slush,pistol,razor,become,junk,kingdom,flee,squirrel,silly,twice]"
(mkMnemonic @15 . extractWords) mnemonicFromApi `shouldSatisfy` isLeft

it "Mnemonic 2nd factor from Api is invalid" $ do
let mnemonicFromApi =
"[squirrel,material,silly,twice,direct,slush,pistol,razor,become]"
(mkMnemonic @9 . extractWords) mnemonicFromApi `shouldSatisfy` isLeft

it "Mnemonic to Entropy" $ forM_ testVectors $ \TestVector{..} ->
mnemonicToEntropy mnemonic `shouldBe` entropy


where
testVectors :: [TestVector]
testVectors =
Expand Down

0 comments on commit 33026db

Please sign in to comment.