-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
205 lines (173 loc) · 5.33 KB
/
main.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package main
import (
"fmt"
"os"
"runtime"
"strconv"
"strings"
"golang.org/x/exp/slices"
"github.com/charmbracelet/huh"
"github.com/charmbracelet/huh/spinner"
"github.com/spf13/pflag"
)
func main() {
// Defined flags
var accountsNumber = pflag.IntP("accounts-number", "n", 0, "Amount of accounts you need")
var matcherMode = pflag.StringP("mode", "m", "", "Matcher mode (contains, starts-with, ends-with, regex)")
var searchString = pflag.StringP("search", "s", "", "Search string")
var chainflag = pflag.StringP("chain", "c", "", "Chain selector string")
var verbose = pflag.BoolP("verbose", "v", false, "Verbose output")
// Extra flags
var letters = pflag.IntP("letters", "l", 0, "Amount of letters (a-z) that the address must contain")
var digits = pflag.IntP("digits", "d", 0, "Amount of digits (0-9) that the address must contain")
// Parse flags
pflag.Parse()
// Validate matcher mode flag if exists
if *matcherMode != "" {
if !slices.Contains(MatcherModes, *matcherMode) {
fmt.Println("ERROR: Invalid matcher mode. Must be one of: contains, starts-with, ends-with, regex")
os.Exit(1)
}
}
// Validate chain flag
var selectedChain chain = chain{}
if *chainflag != "" {
var chainnamesslice []string = make([]string, len(AvailableChains))
for i, chain := range AvailableChains {
chainnamesslice[i] = chain.Name
}
if !slices.Contains(chainnamesslice, *chainflag) {
fmt.Println("ERROR: Invalid chain. Must be one of the available chains: ", chainnamesslice)
os.Exit(1)
}
for i, chain := range AvailableChains {
if chain.Name == *chainflag {
selectedChain = AvailableChains[i]
}
}
}
if selectedChain.Encryption == Secp256k1 {
// Validate letters flag
if *letters < 0 || *letters > 38 {
fmt.Println("ERROR: Invalid letters. Must be between 0 and 38.")
os.Exit(1)
}
// Validate digits flag
if *digits < 0 || *digits > 38 {
fmt.Println("ERROR: Invalid digits. Must be between 0 and 38.")
os.Exit(1)
}
// Letters + Digits must be less than 38
if *letters+*digits > 38 {
fmt.Println("ERROR: Letters + Digits must be less than 38.")
os.Exit(1)
}
}
// Prompt user for missing settings on chain
selectChainOptions := make([]huh.Option[chain], len(AvailableChains))
for i, chain := range AvailableChains {
selectChainOptions[i] = huh.NewOption(chain.Name, chain)
}
// Create settings struct with details from flags
settings := settings{
SelectedChain: selectedChain,
MatcherMode: *matcherMode,
SearchString: *searchString,
NumAccounts: strconv.Itoa(*accountsNumber),
RequiredLetters: *letters,
RequiredDigits: *digits,
}
if settings.SelectedChain == (chain{}) {
huh.NewSelect[chain]().
Title("Select Chain").
Options(selectChainOptions...).
Value(&settings.SelectedChain).
Run()
}
// Prompt user for missing settings on matcher mode
selectMatcherModeOptions := make([]huh.Option[string], len(MatcherModes))
for i, mode := range MatcherModes {
selectMatcherModeOptions[i] = huh.NewOption(mode, mode)
}
if settings.MatcherMode == "" {
huh.NewSelect[string]().
Title("Matcher Mode").
Options(selectMatcherModeOptions...).
Value(&settings.MatcherMode).
Run()
}
// Prompt user for missing settings on search string
if settings.SearchString == "" {
huh.NewInput().
Title("Search string").
CharLimit(38).
Value(&settings.SearchString).
Run()
}
// Prompt user for missing settings on number of accounts to generate
if settings.NumAccounts == "0" {
huh.NewInput().
Title("Number of accounts to generate").
Validate(func(s string) error {
_, err := strconv.Atoi(s)
return err
}).
Value(&settings.NumAccounts).
Run()
}
// Initialize Matcher struct
m := matcher{
Mode: settings.MatcherMode,
SearchString: strings.ToLower(*&settings.SearchString),
Chain: *&settings.SelectedChain,
RequiredLetters: *&settings.RequiredLetters,
RequiredDigits: *&settings.RequiredDigits,
}
matcherValidationErrs := m.ValidateInput()
if len(matcherValidationErrs) > 0 {
for i := 0; i < len(matcherValidationErrs); i++ {
fmt.Println(matcherValidationErrs[i])
}
os.Exit(1)
}
var matchingWallet wallet
NumAccountsInt, err := strconv.Atoi(*&settings.NumAccounts)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Print out settings
if *verbose == true {
fmt.Println("Matcher Mode: " + *&settings.MatcherMode)
fmt.Println("Search String: " + *&settings.SearchString)
fmt.Println("Number of Accounts to Generate: " + *&settings.NumAccounts)
fmt.Println("Selected Chain: ")
fmt.Println(" Name: " + *&settings.SelectedChain.Name)
fmt.Println(" Prefix: " + *&settings.SelectedChain.Prefix)
fmt.Println(" Encryption: ")
switch *&settings.SelectedChain.Encryption {
case Secp256k1:
fmt.Println(" Secp256k1")
case Ethsecp256k1:
fmt.Println(" Ethsecp256k1")
case ECSDA:
fmt.Println(" ECSDA")
}
}
action := func() {
for i := 0; i < NumAccountsInt; i++ {
// TODO limit CPU cores by flag
matchingWallet = findMatchingWalletConcurrent(m, runtime.NumCPU())
fmt.Printf("\nFound a new matching wallet (%d out of %d):\n", i+1, NumAccountsInt)
fmt.Println(matchingWallet)
}
}
spinerr := spinner.New().
Type(spinner.Meter).
Action(action).
Title(" Generating accounts...").
Run()
if spinerr != nil {
fmt.Println(spinerr)
}
}