-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
214 lines (187 loc) · 5.53 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
206
207
208
209
210
211
212
213
214
package main
import (
"encoding/hex"
"fmt"
"math/big"
mathrand "math/rand"
"net/http"
"os"
"os/signal"
"strings"
"sync/atomic"
"time"
tm "github.com/buger/goterm"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
_ "github.com/joho/godotenv/autoload"
"github.com/mteam88/keyswarm/multicall"
)
// config
const HQ = "http://localhost:8000/"
const initialGeneratorCount int = 4 // Best observed performance, not tested
var minimumBalanceWei *big.Int = big.NewInt(1)
const reportSpeed int32 = 1 // seconds
const MULTICALL_SIZE int = 8000
// definitions
var ETHProviders []ETHProvider
var ScannerState State
var HitKeysBox tm.Box
type ETHProvider struct {
RawURL string
isMax bool
client ethclient.Client
}
type State struct {
totalKeys int32
scannedKeys int32
generators uint32
filterers uint32
runningMulticallRequests uint32
completedMulticallRequests uint64
}
func (E ETHProvider) GetClient() ethclient.Client { return E.client }
func main() {
ETHProviders = loadETHProviders()
genkeys := make(chan []string, 1000)
keyswithbalance := make(chan []string)
HitKeysBox = *tm.NewBox(50|tm.PCT, 100|tm.PCT, 0)
fmt.Fprintln(&HitKeysBox, "Hit Keys:")
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for range c {
tm.Clear()
tm.MoveCursor(1, 1)
tm.Println("KEYSWARM - STOPPED")
tm.Println("[X] Total Keys Scanned: ", ScannerState.totalKeys+ScannerState.scannedKeys)
tm.Flush()
os.Exit(0)
}
}()
go func() {
for {
tm.Clear()
tm.MoveCursor(1, 1)
tm.Println("KEYSWARM - RUNNING")
time.Sleep(time.Second * time.Duration(reportSpeed))
tm.Println("[$] Total Scanned Keys:", ScannerState.totalKeys+ScannerState.scannedKeys)
tm.Println("[$] Scanned Keys Per Second: ", (ScannerState.scannedKeys / reportSpeed))
tm.Println("[i] generators running", ScannerState.generators)
tm.Println("[i] requests running", ScannerState.runningMulticallRequests)
tm.Println("[i] requests completed", ScannerState.completedMulticallRequests)
tm.Println("[!] Private Keys with balance", len(keyswithbalance))
// Move Box to approx center of the screen
tm.Print(tm.MoveTo(HitKeysBox.String(), 50|tm.PCT, 0|tm.PCT))
tm.Flush()
ScannerState.totalKeys += ScannerState.scannedKeys
ScannerState.scannedKeys = 0
}
}()
filterForBalance(genkeys, keyswithbalance)
for i := 0; i < initialGeneratorCount; i++ {
go generateKeys(genkeys, keyswithbalance)
}
go callhome(keyswithbalance)
select {} // do not stop main thread
}
func generateKeys(generatedkeys chan []string, keyswithbalance chan []string) {
atomic.AddUint32(&ScannerState.generators, 1)
for {
// Create an account
key, err := crypto.GenerateKey()
if err != nil {
panic(err)
}
// Get the address
address := crypto.PubkeyToAddress(key.PublicKey).Hex()
// Get the private key
privateKey := hex.EncodeToString(key.D.Bytes())
generatedkeys <- []string{privateKey, address}
}
}
func filterForBalance(generatedkeys chan []string, keyswithbalance chan []string) chan []string {
atomic.AddUint32(&ScannerState.filterers, 1)
buf := make(chan []string, MULTICALL_SIZE)
go func() {
for {
select {
case buf <- <-generatedkeys: // Buffer not full
default: // Buffer must be full, making multicall request
var keysInBatch [MULTICALL_SIZE][]string
for i := 0; i < MULTICALL_SIZE; i++ {
keysInBatch[i] = <-buf
}
go func() {
atomic.AddUint32(&ScannerState.runningMulticallRequests, 1)
for keyIndex, hasBalance := range hasbalance(keysInBatch[:]) {
if hasBalance == 0 || hasBalance == 1 {
keyswithbalance <- keysInBatch[keyIndex]
}
}
atomic.AddUint32(&ScannerState.runningMulticallRequests, ^uint32(0))
atomic.AddUint64(&ScannerState.completedMulticallRequests, 1)
}()
}
}
}()
return buf
}
func callhome(keyswithbalance <-chan []string) {
for keypair := range keyswithbalance {
beacon(keypair)
}
}
func hasbalance(keypairs [][]string) []int {
var retVal []int
bals, err := getbalance(keypairs)
for _, bal := range bals {
if err == nil {
atomic.AddInt32(&ScannerState.scannedKeys, 1)
retVal = append(retVal, bal.Cmp(minimumBalanceWei))
} else {
panic(err)
}
}
return retVal
}
func getbalance(keypairs [][]string) ([]big.Int, error) { //returns slice of wei balances for keypairs
var ethprovider ETHProvider
for {
ethprovider = ETHProviders[mathrand.Intn(len(ETHProviders))]
if !ethprovider.isMax {
break
}
}
var publicKeyPairs []string
for _, keyPair := range keypairs {
publicKeyPairs = append(publicKeyPairs, keyPair[1])
}
return multicall.GetBalances(publicKeyPairs, ethprovider) // Maybe should use client initialized earlier.
}
func beacon(keypair []string) {
_, err := http.Get(HQ + keypair[0])
if err != nil {
panic(err.Error())
}
fmt.Fprint(&HitKeysBox, "[!] BEACON CALL: " + "\n[-] Private: " + keypair[0] + "\n[-] Public: " + keypair[1]+"\n")
}
func loadETHProviders() []ETHProvider {
RawInfuraKeys := strings.Split(os.Getenv("INFURA_KEYS"), ",")
InfuraKeys := []ETHProvider{}
for _, key := range RawInfuraKeys {
if key == "" {
break
}
RawUrl := "https://mainnet.infura.io/v3/" + key
client, err := ethclient.Dial(RawUrl)
if err != nil {
panic(err.Error())
}
InfuraKeys = append(InfuraKeys, ETHProvider{RawUrl, false, *client})
}
ETHProviders = append(ETHProviders, InfuraKeys...)
if len(ETHProviders) == 0 {
panic("Please provide some api keys. You may need a .env file. See the docs.")
}
return ETHProviders
}