forked from kristapsk/segvan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsegvan.cpp
198 lines (176 loc) · 4.87 KB
/
segvan.cpp
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
/*
* Bitcoin SegWit vanity address generator
* By Kristaps Kaupe. Licensed under public domain.
*
* https://github.com/kristapsk/segvan
*
*/
#include <cctype>
#include <cstdlib>
#include <iostream>
#include <mutex>
#include <regex>
#include <thread>
#include <unistd.h>
#include <boost/algorithm/string.hpp>
#include <sodium.h>
#include <bitcoin/bitcoin.hpp>
using namespace bc;
using namespace libbitcoin::chain;
using namespace libbitcoin::machine;
using namespace libbitcoin::wallet;
std::regex mainnet_p2sh_pattern("^3[a-km-zA-HJ-NP-Z1-9]{1,34}$");
std::regex testnet_p2sh_pattern("^2[a-km-zA-HJ-NP-Z1-9]{1,34}$");
bool debug_output = false;
bool ignore_case = false;
std::string pattern;
bool testnet;
std::mutex mtx;
void usage (void)
{
std::cout <<
"Usage: segvan [options] pattern\n"
"Options:\n"
"\t-d\tEnable debug output\n"
"\t-i\tCase insensitive matching\n"
"\t-t num\tNumber of CPU threads to use (default 1)" << std::endl;
}
ec_secret random_secret (void)
{
char randbytes[ec_secret_size];
ec_secret secret;
randombytes_buf(randbytes, ec_secret_size);
std::copy(std::begin(randbytes), std::end(randbytes), std::begin(secret));
return secret;
}
operation::list witness_program (const ec_compressed& pubkey)
{
short_hash keyhash = bitcoin_short_hash(pubkey);
return {
operation(opcode(0)),
operation(to_chunk(keyhash))
};
}
std::string p2sh_address (const ec_secret& secret, bool testnet)
{
ec_compressed pubkey;
if (secret_to_public(pubkey, secret)) {
return payment_address(
script(witness_program(pubkey)),
(testnet ? payment_address::testnet_p2sh : payment_address::mainnet_p2sh)
).encoded();
}
return "";
}
bool match_found (const std::string& address, const std::string& pattern, bool ignore_case)
{
if (ignore_case) {
auto addr_it = address.begin();
for (auto it = pattern.begin(); it != pattern.end(); ++it, ++addr_it) {
if (*it != std::tolower(*addr_it)) {
return false;
}
}
return true;
}
else {
return (address.compare(0, pattern.size(), pattern) == 0);
}
}
void vanity_thread (int tid)
{
while (true)
{
ec_secret secret = random_secret();
std::string address = p2sh_address(secret, testnet);
if (!address.empty()) {
if (debug_output) {
mtx.lock();
std::cout << "[Thread " << tid << "] Trying " << address << std::endl;
mtx.unlock();
}
if (match_found(address, pattern, ignore_case)) {
mtx.lock();
std::cout << "Found vanity address! " << address;
if (testnet) {
std::cout << " (testnet)";
}
std::cout << std::endl;
ec_private privkey(secret, 0x8000, true);
std::cout << "Private key: " << privkey.encoded() << std::endl;
exit(EXIT_SUCCESS);
}
}
else {
mtx.lock();
std::cerr << "Error creating address from secret!" << std::endl;
abort();
}
}
}
int main (int argc, char** argv)
{
int num_threads = 1;
int c;
while ((c = getopt(argc, argv, "dit:")) != -1) {
switch (c) {
case 'd':
debug_output = true;
break;
case 'i':
ignore_case = true;
break;
case 't':
num_threads = atoi(optarg);
break;
default:
abort();
}
}
if (optind == argc) {
usage();
return EXIT_FAILURE;
}
pattern = argv[optind];
if (ignore_case) {
boost::algorithm::to_lower(pattern);
}
if (std::regex_match(pattern, mainnet_p2sh_pattern)) {
testnet = false;
}
else if (std::regex_match(pattern, testnet_p2sh_pattern)) {
testnet = true;
}
else {
std::cerr << "Invalid address patern " << pattern << std::endl;
usage();
abort();
}
if (debug_output) {
std::cout << "Searching for address beginning with " << pattern;
if (ignore_case) {
std::cout << " (case insensitive)";
}
if (testnet) {
std::cout << " (testnet)";
}
std::cout << " using " << num_threads << " CPU thread";
if (num_threads > 1) {
std::cout << "s";
}
std::cout << std::endl;
}
if (num_threads <= 1) {
vanity_thread(0);
}
else {
std::thread t[num_threads];
for (int i = 0; i < num_threads; ++i) {
t[i] = std::thread(vanity_thread, i);
}
for (int i = 0; i < num_threads; ++i) {
t[i].join();
}
}
return EXIT_FAILURE;
}