diff --git a/bip39.go b/bip39.go index 62503b0..180adda 100644 --- a/bip39.go +++ b/bip39.go @@ -273,25 +273,8 @@ func NewSeed(mnemonic string, password string) []byte { // Validity is determined by both the number of words being appropriate, // and that all the words in the mnemonic are present in the word list. func IsMnemonicValid(mnemonic string) bool { - // Create a list of all the words in the mnemonic sentence - words := strings.Fields(mnemonic) - - // Get word count - wordCount := len(words) - - // The number of words should be 12, 15, 18, 21 or 24 - if wordCount%3 != 0 || wordCount < 12 || wordCount > 24 { - return false - } - - // Check if all words belong in the wordlist - for _, word := range words { - if _, ok := wordMap[word]; !ok { - return false - } - } - - return true + _, err := EntropyFromMnemonic(mnemonic) + return err == nil } // Appends to data the first (len(data) / 32)bits of the result of sha256(data) diff --git a/bip39_test.go b/bip39_test.go index 230c831..f8acb1d 100644 --- a/bip39_test.go +++ b/bip39_test.go @@ -79,7 +79,7 @@ func TestMnemonicToByteArrayInvalidMnemonic(t *testing.T) { _, err := MnemonicToByteArray("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon yellow") assertNotNil(t, err) - assertEqual(t, err, ErrChecksumIncorrect) + assertEqual(t, err, ErrInvalidMnemonic) } func TestNewEntropy(t *testing.T) { @@ -257,7 +257,7 @@ func TestEntropyFromMnemonicInvalidChecksum(t *testing.T) { func TestEntropyFromMnemonicInvalidMnemonicSize(t *testing.T) { for _, mnemonic := range []string{ "a a a a a a a a a a a a a a a a a a a a a a a a a", // Too many words - "a", // Too few + "a", // Too few "a a a a a a a a a a a a a a", // Not multiple of 3 } { _, err := EntropyFromMnemonic(mnemonic) @@ -423,6 +423,9 @@ func badMnemonicSentences() []vector { {mnemonic: "jello better achieve collect unaware mountain thought cargo oxygen act hood bridge"}, {mnemonic: "renew, stay, biology, evidence, goat, welcome, casual, join, adapt, armor, shuffle, fault, little, machine, walk, stumble, urge, swap"}, {mnemonic: "dignity pass list indicate nasty"}, + + // From issue 32 + {mnemonic: "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon letter"}, } }