diff --git a/extkeys/mnemonic.go b/extkeys/mnemonic.go index 0d59e6414..1509d14b0 100644 --- a/extkeys/mnemonic.go +++ b/extkeys/mnemonic.go @@ -240,7 +240,7 @@ func (m *Mnemonic) ValidMnemonic(mnemonic string, language Language) bool { // WordList returns list of words for a given language func (m *Mnemonic) WordList(language Language) (*WordList, error) { - if m.wordLists[language] == nil { + if int(language) < 0 || int(language) > len(m.wordLists)-1 || m.wordLists[language] == nil { return nil, fmt.Errorf("language word list is missing (language id: %d)", language) } return m.wordLists[language], nil diff --git a/extkeys/mnemonic_test.go b/extkeys/mnemonic_test.go index 4953ed0d8..fbcf4915d 100644 --- a/extkeys/mnemonic_test.go +++ b/extkeys/mnemonic_test.go @@ -29,6 +29,22 @@ func TestNewMnemonic(t *testing.T) { } } +func TestMnemonic_WordList(t *testing.T) { + m := NewMnemonic("") + _, err := m.WordList(EnglishLanguage) + if err != nil { + t.Errorf("expected WordList to return WordList without errors, got: %s", err) + } + + indexes := []Language{-1, Language(len(m.wordLists))} + for _, index := range indexes { + _, err := m.WordList(index) + if err == nil { + t.Errorf("expected WordList to return an error with index %d", index) + } + } +} + // TestMnemonicPhrase func TestMnemonicPhrase(t *testing.T) {