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

BUGFIX: Fix issue #32; not properly validating checksums. #33

Merged
merged 1 commit into from Aug 8, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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