Examples¶
Here are some of the anticipated uses of the BIP39 Validator API.
- Validate that Levenshtein distances >= 2, then find all the word pairs with Levenshtein distance less than 2:
from bip39validator import BIP39WordList, InvalidWordList, ValidationFailed
f = open('wordlist-en.txt')
try:
wordlist = BIP39Wordlist('English wordlist', handle=f)
wordlist.test_lev_distance(2)
# At this point, no word pairs have Levenshtein distance < 2.
except ValidationFailed as e:
dists = e.status_obj.getwordpairs_lt(2)
for wordpair in dists:
word1 = wordpair[0]
word2 = wordpair[1]
# Do something with word1 and word2...
except InvalidWordList as e:
print("Wordlist file is not well-formed")
- Validate that Levenshtein distances >= 2, then calculate the number and percentage of word pairs with Levenshtein distance less than 2 (assume 2048-word list):
from bip39validator import BIP39WordList, InvalidWordList, ValidationFailed
f = open('wordlist-en.txt')
try:
wordlist = BIP39Wordlist('English wordlist', handle=f)
wordlist.test_lev_distance(2)
# At this point, the percentage and number of
# words fulfilling the condition are 0.
except ValidationFailed as e:
dists = e.status_obj.getwordpairs_lt(2)
n = len(dists)
prct = n/(2048*2048)
except InvalidWordList as e:
print("Wordlist file is not well-formed")
- Validate that words are unique in at least 4 initial characters, then find all the words beginning with “str” (prefix-3 group “str”):
from bip39validator import BIP39WordList, InvalidWordList, ValidationFailed
f = open('wordlist-en.txt')
try:
wordlist = BIP39Wordlist('English wordlist', handle=f)
wordlist.test_initial_chars(4)
# At this point, all words are unique in at least 4 initial characters
except ValidationFailed as e:
words = e.status_obj.similar_wordgroup("str")
for word in words:
# Do something with word...
except InvalidWordList as e:
print("Wordlist file is not well-formed")
- Validate that words are unique in at least 4 initial characters, then calculate the number and percentage of word prefix-4 groups with at least two words in them:
from bip39validator import BIP39WordList, InvalidWordList, ValidationFailed
f = open('wordlist-en.txt')
try:
wordlist = BIP39Wordlist('English wordlist', handle=f)
wordlist.test_initial_chars(4)
# At this point, the percentage and number of
# words fulfilling the condition are 0.
except ValidationFailed as e:
groups = e.status_obj.similar_wordgroup_all(4)
n = sum([c for c in groups.values() if len(c) >= 2])
denom = len(groups.values())
perc = n/denom
except InvalidWordList as e:
print("Wordlist file is not well-formed")
- Validate that words are no longer than 8 characters, then find all of the words longer than 8 characters:
from bip39validator import BIP39WordList, InvalidWordList, ValidationFailed
f = open('wordlist-en.txt')
try:
wordlist = BIP39Wordlist('English wordlist', handle=f)
wordlist.test_max_length(8)
# At this point, all words are no longer than 8 characters
except ValidationFailed as e:
words = e.status_obj.getwords_gt(8)
lines = e.status_obj.getlines_gt(8)
for word, line in [*zip(words, lines)]:
# Do something with word and line...
except InvalidWordList as e:
print("Wordlist file is not well-formed")
- Validate that words are no longer than 8 characters, then calculate the number and percentage of words longer than 8 characters:
from bip39validator import BIP39WordList, InvalidWordList, ValidationFailed
f = open('wordlist-en.txt')
try:
wordlist = BIP39Wordlist('English wordlist', handle=f)
wordlist.test_max_length(8)
# At this point, the percentage and number of
# words fulfilling the condition are 0.
except ValidationFailed as e:
words = e.status_obj.getwords_gt(8)
n = sum([w for w in words if len(w) > 8])
perc = n/len(words)
except InvalidWordList as e:
print("Wordlist file is not well-formed")