-
Notifications
You must be signed in to change notification settings - Fork 16
/
btcvanity.go
70 lines (58 loc) · 1.17 KB
/
btcvanity.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
package btcvanity
import (
"sync"
"github.com/btcsuite/btcd/chaincfg"
)
// BTCVanity library holder
type BTCVanity struct {
config *Config
stop chan bool
}
// New creates a new BTCVanity service
func New(config *Config) *BTCVanity {
return &BTCVanity{
config: config,
stop: make(chan bool),
}
}
// Find runs a service to find matching pattern
func (b *BTCVanity) Find(pattern string) (IWallet, error) {
var resWallet IWallet
var resError error
var chainParams *chaincfg.Params
if b.config.TestNet {
chainParams = &chaincfg.TestNet3Params
} else {
chainParams = &chaincfg.MainNetParams
}
btcWorker := &worker{gen: &Generator{params: chainParams}}
mutex := sync.Mutex{}
for i := 0; i < b.config.Buffer; i++ {
go func() {
for {
wallet, err := btcWorker.Work()
if err != nil {
mutex.Lock()
resError = err
break
}
if isMatch(pattern, wallet.PublicKey()) {
mutex.Lock()
if resWallet != nil {
break
}
resWallet = wallet
break
}
}
b.stop <- true
mutex.Unlock()
}()
}
<-b.stop
return resWallet, resError
}
// Stop stops the process
func (b *BTCVanity) Stop() {
b.stop <- true
}