Skip to content

Commit

Permalink
BUGFIX: Fix issue #32; not properly validating checksums.
Browse files Browse the repository at this point in the history
  • Loading branch information
tyler-smith committed Aug 8, 2019
1 parent dbb3b84 commit b91f685
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 21 deletions.
21 changes: 2 additions & 19 deletions bip39.go
Expand Up @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions bip39_test.go
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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"},
}
}

Expand Down

0 comments on commit b91f685

Please sign in to comment.