Skip to content

Commit 810ccbc

Browse files
committed
feat: validate mnemonic
1 parent 405be08 commit 810ccbc

File tree

6 files changed

+2290
-10
lines changed

6 files changed

+2290
-10
lines changed

bip39/bip39.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package bip39
2+
3+
import (
4+
"strings"
5+
)
6+
7+
// IsMnemonicValid attempts to verify that the provided mnemonic is valid.
8+
// Validity is determined by both the number of words being appropriate,
9+
// and that all the words in the mnemonic are present in the word list.
10+
func IsMnemonicValid(mnemonic string) bool {
11+
// Create a list of all the words in the mnemonic sentence
12+
words := strings.Fields(mnemonic)
13+
14+
//Get num of words
15+
numOfWords := len(words)
16+
17+
// The number of words should be 12, 15, 18, 21 or 24
18+
if numOfWords < 12 || numOfWords > 24 || numOfWords%3 != 0 {
19+
return false
20+
}
21+
22+
// Check if all words belong in the wordlist
23+
for i := 0; i < numOfWords; i++ {
24+
if _, ok := ReverseWordMap[words[i]]; !ok {
25+
return false
26+
}
27+
}
28+
29+
return true
30+
}

0 commit comments

Comments
 (0)