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

Few more unit tests for mnemonics + adjusting wallet API spec #58

Merged
merged 1 commit into from
Mar 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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


Copy link
Member

Choose a reason for hiding this comment

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

neat-picking but, these two extra lines are bothering me 😅

where
testVectors :: [TestVector]
testVectors =
Expand Down