forked from hukkin/cosmosvanity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
175 lines (153 loc) · 4.54 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
package main
import (
"encoding/hex"
"fmt"
"os"
"runtime"
"strings"
flag "github.com/spf13/pflag"
"github.com/cosmos/cosmos-sdk/types/bech32"
"github.com/tendermint/tendermint/crypto/secp256k1"
)
type matcher struct {
StartsWith string
EndsWith string
Contains string
Letters int
Digits int
}
func (m matcher) Match(candidate string) bool {
candidate = strings.TrimPrefix(candidate, "cosmos1")
if !strings.HasPrefix(candidate, m.StartsWith) {
return false
}
if !strings.HasSuffix(candidate, m.EndsWith) {
return false
}
if !strings.Contains(candidate, m.Contains) {
return false
}
if countUnionChars(candidate, bech32digits) < m.Digits {
return false
}
if countUnionChars(candidate, bech32letters) < m.Letters {
return false
}
return true
}
func (m matcher) ValidationErrors() []string {
var errs []string
if !bech32Only(m.Contains) || !bech32Only(m.StartsWith) || !bech32Only(m.EndsWith) {
errs = append(errs, "ERROR: A provided matcher contains bech32 incompatible characters")
}
if len(m.Contains) > 38 || len(m.StartsWith) > 38 || len(m.EndsWith) > 38 {
errs = append(errs, "ERROR: A provided matcher is too long. Must be max 38 characters.")
}
if m.Digits < 0 || m.Letters < 0 {
errs = append(errs, "ERROR: Can't require negative amount of characters")
}
if m.Digits+m.Letters > 38 {
errs = append(errs, "ERROR: Can't require more than 38 characters")
}
return errs
}
type wallet struct {
Address string
Pubkey []byte
Privkey []byte
}
func (w wallet) String() string {
return "Address:\t" + w.Address + "\n" +
"Public key:\t" + hex.EncodeToString(w.Pubkey) + "\n" +
"Private key:\t" + hex.EncodeToString(w.Privkey)
}
func generateWallet() wallet {
var privkey secp256k1.PrivKey = secp256k1.GenPrivKey()
var pubkey secp256k1.PubKey = privkey.PubKey().(secp256k1.PubKey)
bech32Addr, err := bech32.ConvertAndEncode("cosmos", pubkey.Address())
if err != nil {
panic(err)
}
return wallet{bech32Addr, pubkey, privkey}
}
func findMatchingWallets(ch chan wallet, quit chan struct{}, m matcher) {
for {
select {
case <-quit:
return
default:
w := generateWallet()
if m.Match(w.Address) {
// Do a non-blocking write instead of simple `ch <- w` to prevent
// blocking when it's time to quit and ch is full.
select {
case ch <- w:
default:
}
}
}
}
}
func findMatchingWalletConcurrent(m matcher, goroutines int) wallet {
ch := make(chan wallet)
quit := make(chan struct{})
defer close(quit)
for i := 0; i < goroutines; i++ {
go findMatchingWallets(ch, quit, m)
}
return <-ch
}
const bech32digits = "023456789"
const bech32letters = "acdefghjklmnpqrstuvwxyzACDEFGHJKLMNPQRSTUVWXYZ"
// This is alphanumeric chars minus chars "1", "b", "i", "o" (case insensitive)
const bech32chars = bech32digits + bech32letters
func bech32Only(s string) bool {
return countUnionChars(s, bech32chars) == len(s)
}
func countUnionChars(s string, letterSet string) int {
count := 0
for _, char := range s {
if strings.Contains(letterSet, string(char)) {
count++
}
}
return count
}
func main() {
var walletsToFind = flag.IntP("count", "n", 1, "Amount of matching wallets to find")
var cpuCount = flag.Int("cpus", runtime.NumCPU(), "Amount of CPU cores to use")
var mustContain = flag.StringP("contains", "c", "", "A string that the address must contain")
var mustStartWith = flag.StringP("startswith", "s", "", "A string that the address must start with")
var mustEndWith = flag.StringP("endswith", "e", "", "A string that the address must end with")
var letters = flag.IntP("letters", "l", 0, "Amount of letters (a-z) that the address must contain")
var digits = flag.IntP("digits", "d", 0, "Amount of digits (0-9) that the address must contain")
flag.Parse()
if *walletsToFind < 1 {
fmt.Println("ERROR: The number of wallets to generate must be 1 or more")
os.Exit(1)
}
if *cpuCount < 1 {
fmt.Println("ERROR: Must use at least 1 CPU core")
os.Exit(1)
}
m := matcher{
StartsWith: strings.ToLower(*mustStartWith),
EndsWith: strings.ToLower(*mustEndWith),
Contains: strings.ToLower(*mustContain),
Letters: *letters,
Digits: *digits,
}
matcherValidationErrs := m.ValidationErrors()
if len(matcherValidationErrs) > 0 {
for i := 0; i < len(matcherValidationErrs); i++ {
fmt.Println(matcherValidationErrs[i])
}
os.Exit(1)
}
var matchingWallet wallet
for i := 0; i < *walletsToFind; i++ {
matchingWallet = findMatchingWalletConcurrent(m, *cpuCount)
fmt.Printf(":::: Matching wallet %d/%d found ::::\n", i+1, *walletsToFind)
fmt.Println(matchingWallet)
}
}