Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
229 lines (185 loc) · 4.12 KB
/
Copy pathmain.go
File metadata and controls
229 lines (185 loc) · 4.12 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package main
import (
"context"
"crypto/ecdsa"
"encoding/hex"
"fmt"
"log"
"os"
"runtime"
"strconv"
"strings"
"sync"
"github.com/ethereum/go-ethereum/crypto"
"github.com/joho/godotenv"
)
type config struct {
Pattern compiledPattern
Workers int
}
type result struct {
Address string
PublicKey string
PrivateKey string
}
type compiledPattern struct {
Indexes []int
Expected []byte
}
func main() {
_ = godotenv.Load()
cfg, err := loadConfig()
if err != nil {
log.Fatal(err)
}
match, err := findMatch(cfg)
if err != nil {
log.Fatal(err)
}
fmt.Printf("address=%s\n", match.Address)
fmt.Printf("public_key=%s\n", match.PublicKey)
fmt.Printf("private_key=%s\n", match.PrivateKey)
}
func loadConfig() (config, error) {
rawPattern := normalizePattern(os.Getenv("ADDRESS_PATTERN"))
if rawPattern == "" {
return config{}, fmt.Errorf("ADDRESS_PATTERN is required")
}
if len(rawPattern) != 40 {
return config{}, fmt.Errorf("ADDRESS_PATTERN must be exactly 40 hex characters after 0x")
}
for _, ch := range rawPattern {
if ch == '?' {
continue
}
if !strings.ContainsRune("0123456789abcdef", ch) {
return config{}, fmt.Errorf("ADDRESS_PATTERN only supports lowercase hex characters and '?' wildcards")
}
}
workers := runtime.NumCPU()
if rawWorkers := strings.TrimSpace(os.Getenv("WORKERS")); rawWorkers != "" {
parsedWorkers, err := parseWorkerCount(rawWorkers)
if err != nil {
return config{}, fmt.Errorf("WORKERS must be zero or a positive integer: %w", err)
}
if parsedWorkers > 0 {
workers = parsedWorkers
}
}
pattern := compilePattern(rawPattern)
return config{
Pattern: pattern,
Workers: workers,
}, nil
}
func normalizePattern(value string) string {
trimmed := strings.TrimSpace(strings.ToLower(value))
return strings.TrimPrefix(trimmed, "0x")
}
func parseWorkerCount(value string) (int, error) {
parsed, err := strconv.Atoi(value)
if err != nil {
return 0, err
}
if parsed < 0 {
return 0, fmt.Errorf("value must be at least zero")
}
return parsed, nil
}
func findMatch(cfg config) (result, error) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
results := make(chan result, 1)
errs := make(chan error, cfg.Workers)
var wg sync.WaitGroup
for i := 0; i < cfg.Workers; i++ {
wg.Add(1)
go func() {
defer wg.Done()
if err := worker(ctx, cfg.Pattern, results); err != nil {
select {
case errs <- err:
default:
}
}
}()
}
go func() {
wg.Wait()
close(errs)
}()
select {
case match := <-results:
cancel()
return match, nil
case err := <-errs:
if err == nil {
return result{}, fmt.Errorf("generator stopped unexpectedly")
}
cancel()
return result{}, err
}
}
func worker(ctx context.Context, pattern compiledPattern, results chan<- result) error {
for {
select {
case <-ctx.Done():
return nil
default:
}
privateKey, err := crypto.GenerateKey()
if err != nil {
return err
}
address := crypto.PubkeyToAddress(privateKey.PublicKey).Hex()
if !pattern.Matches(address) {
continue
}
match := result{
Address: address,
PublicKey: encodePublicKey(&privateKey.PublicKey),
PrivateKey: encodePrivateKey(privateKey),
}
select {
case results <- match:
return nil
case <-ctx.Done():
return nil
}
}
}
func compilePattern(raw string) compiledPattern {
indexes := make([]int, 0, len(raw))
expected := make([]byte, 0, len(raw))
for i := 0; i < len(raw); i++ {
if raw[i] == '?' {
continue
}
indexes = append(indexes, i)
expected = append(expected, raw[i])
}
return compiledPattern{
Indexes: indexes,
Expected: expected,
}
}
func (p compiledPattern) Matches(address string) bool {
for i, idx := range p.Indexes {
if lowerHex(address[idx+2]) != p.Expected[i] {
return false
}
}
return true
}
func lowerHex(ch byte) byte {
if ch >= 'A' && ch <= 'F' {
return ch + ('a' - 'A')
}
return ch
}
func encodePublicKey(publicKey *ecdsa.PublicKey) string {
return hex.EncodeToString(crypto.FromECDSAPub(publicKey))
}
func encodePrivateKey(privateKey *ecdsa.PrivateKey) string {
return hex.EncodeToString(crypto.FromECDSA(privateKey))
}