-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
194 lines (174 loc) · 4.63 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
package main
import (
"errors"
"fmt"
"os"
"runtime"
"strconv"
"strings"
"sync"
"time"
"github.com/nbd-wtf/go-nostr"
"github.com/nbd-wtf/go-nostr/nip19"
)
const (
version string = "v0.0.3"
npubPrefix string = "npub1"
bech32charset string = "023456789acdefghjklmnpqrstuvwxyz"
ERR_SEARCH_FAILED string = "did not find prefix"
)
var (
misses uint64
target string
limit uint64
startTime time.Time
unbounded bool
)
type keypair struct {
npub string
nsec string
pub string
sec string
attempts uint64
dur time.Duration
}
func isBech32(s string) bool {
for _, c := range s {
if !strings.Contains(bech32charset, string(c)) {
return false
}
}
return true
}
func main() {
// var tries string
mult := time.Second
// whats up
fmt.Printf("Glasnostr (%s)\nMine a vanity prefix for your Nostr npub\nhttps://github.com/eyelight/glasnostr\n\n", version)
// handle no target
if len(os.Args) < 2 {
fmt.Printf("Please supply Glasnostr with a target and optionally an upper limit for the number of guesses (default: 21 million guesses, or use '0' for infinity)\n example: $ glasnostr foo 50000\n\nAlso, private keys will be sent to the screen, so you may want to redirect the output\n example: $ glasnostr foo 50000 > glasnostr.txt\n")
os.Exit(1)
}
// validate target
target = os.Args[1]
if !isBech32(target) {
fmt.Printf("Target '%s' is invalid\nThe valid character set for an encoded npub is bech32. Try again with only the following characters: \n\n %s\n\n", target, bech32charset)
os.Exit(1)
}
// set limit
if len(os.Args) < 3 {
limit = 21000000
} else {
tries := os.Args[2]
l, e := strconv.ParseUint(tries, 10, 64)
if e != nil {
fmt.Printf("Cannot parse limit (stay positive): \n%s\n\n", e.Error())
os.Exit(1)
}
limit = l
if limit == 0 {
unbounded = true
}
}
numWorkers := runtime.GOMAXPROCS(runtime.NumCPU())
success := make(chan bool, 1)
cancelAll := make(chan struct{}, numWorkers)
var wg sync.WaitGroup
startTime = time.Now()
if !unbounded {
fmt.Printf("(%d attempts): ", limit)
}
fmt.Printf("Starting %d threads looking for prefix '%s' at %s\n\n", numWorkers, target, startTime.Local().Format(time.RFC822))
// set up workers
for i := 0; i < numWorkers; i++ {
wg.Add(1)
go work(i, &wg, success, cancelAll)
}
Work:
for {
select {
case <-success:
// stop
for i := 0; i < numWorkers; i++ {
cancelAll <- struct{}{}
}
break Work
default:
// report progress
if !unbounded {
if misses >= limit {
fmt.Printf("¯\\_(ツ)_/¯ Tried %d attempts.\n", misses)
for i := 0; i < numWorkers; i++ {
cancelAll <- struct{}{}
}
break Work
}
}
if time.Since(startTime) > mult && misses%uint64(100000) == 0 {
fmt.Printf("%d tries after %s...\n", misses, time.Since(startTime).Round(time.Second))
mult *= 2
}
}
}
wg.Wait()
fmt.Printf("Done hogging your CPU. Thanks for using Glasnostr.\n")
}
// evaluate returns true if `target` immediately follows 'npub1' in the key
func evaluate(key, target string) bool {
target = npubPrefix + target
return strings.HasPrefix(key, target)
}
// mine returns a keypair if a generated npub matches the target, or an error
func mine() (keypair, error) {
// generate keys
sk := nostr.GeneratePrivateKey()
pk, pkerr := nostr.GetPublicKey(sk)
if pkerr != nil {
fmt.Printf("error getting public key: %s\n", pkerr.Error())
return keypair{}, pkerr
}
// make the npub from the pubkey
npub, nerr := nip19.EncodePublicKey(pk)
if nerr != nil {
fmt.Printf("error encoding NIP-19 npub from public key: %s\n", nerr.Error())
return keypair{}, nerr
}
// evaluate the target against the npub; finalize & return keypair if found
result := evaluate(npub, target)
if !result {
return keypair{}, errors.New(ERR_SEARCH_FAILED)
} else {
nsec, err := nip19.EncodePrivateKey(sk)
if err != nil {
fmt.Printf("error encoding NIP-19 nsec from secret key: %s\n", err.Error())
return keypair{}, err
}
kp := keypair{
npub: npub,
nsec: nsec,
sec: sk,
pub: pk,
attempts: misses,
dur: time.Since(startTime),
}
return kp, nil
}
}
// work mines for keys and reports upon success, until receiving a cancelAll channel
func work(id int, wg *sync.WaitGroup, success chan<- bool, cancelAll <-chan struct{}) {
defer wg.Done()
for {
select {
case <-cancelAll:
return
default:
kp, err := mine()
if err == nil {
fmt.Printf("Glasnostr found '%s' after %d tries (%s):\n (pub) %s\n (sec) %s\n (npub) %s\n (nsec) %s\n\n", target, kp.attempts, kp.dur, kp.pub, kp.sec, kp.npub, kp.nsec)
success <- true
}
misses++
}
}
}