-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbip39checksum.go
68 lines (55 loc) · 1.22 KB
/
bip39checksum.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"bufio"
"fmt"
"github.com/fatih/color"
"github.com/tyler-smith/go-bip39"
"os"
"strings"
)
const (
prompt = "Please enter 11 or 23 BIP39 words: "
)
func main() {
reader := bufio.NewReader(os.Stdin)
fmt.Print(color.BlueString(prompt))
input, err := reader.ReadString('\n')
if err != nil {
color.Red("Error reading input:", err)
return
}
mnemonic := strings.TrimSpace(input)
nWords := countWords(mnemonic)
switch nWords {
case 11, 23:
possibleWords := brute(mnemonic)
color.Blue("\nThere are %v last words valid for this mnemonic: \n", len(possibleWords))
printWords(possibleWords)
case 12, 24:
color.Green("You already have the last word!")
default:
color.Red("The mnemonic phrase you entered is not valid.")
}
}
func brute(mnemonic string) []string {
english := bip39.GetWordList()
var validWords []string
for _, word := range english {
current := mnemonic + " " + word
isValid := bip39.IsMnemonicValid(current)
if isValid {
validWords = append(validWords, word)
}
}
return validWords
}
func printWords(arr []string) {
for _, w := range arr {
fmt.Printf("- %s\n", w)
}
fmt.Println()
}
func countWords(s string) int {
words := strings.Fields(s)
return len(words)
}