-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
327 lines (289 loc) · 11.8 KB
/
Copy pathProgram.cs
File metadata and controls
327 lines (289 loc) · 11.8 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Data.SQLite;
using System.Runtime.InteropServices;
using System.Data;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Crypto.Digests;
class Program
{
private static object logMutex = new object();
private static StreamWriter logFile;
private static int counter = 0;
private static bool stopFlag = false;
static void Main(string[] args)
{
try
{
int maxThreads = Environment.ProcessorCount;
Console.WriteLine($"Detected {maxThreads} logical processors.");
int userThreads;
while (true)
{
Console.Write("Enter the desired number of processors to use (minimum 2): ");
if (int.TryParse(Console.ReadLine(), out userThreads))
{
if (userThreads < 2)
{
Console.WriteLine($"Entered {userThreads} (less than 2), using 2 processors.");
userThreads = 2;
break;
}
else if (userThreads > maxThreads)
{
Console.WriteLine($"Entered {userThreads} logical processors, but only {maxThreads} are available. Using {maxThreads}.");
userThreads = maxThreads;
break;
}
else
{
break;
}
}
else
{
Console.WriteLine("Please enter a valid number.");
}
}
string dbPath = "database.db";
while (!File.Exists(dbPath))
{
Console.Write("Database file database.db not found.\nEnter the path to the database (or press Enter to exit): ");
string input = Console.ReadLine();
if (string.IsNullOrEmpty(input))
{
Console.WriteLine("Program terminated by user.");
return;
}
dbPath = input;
if (!File.Exists(dbPath))
{
Console.WriteLine($"File not found: {dbPath}");
continue;
}
try
{
// Проверяем, можем ли мы открыть базу данных
using (var conn = new SQLiteConnection($"Data Source={dbPath};Version=3;"))
{
conn.Open();
using (var cmd = new SQLiteCommand("SELECT name FROM sqlite_master WHERE type='table' AND name='addresses'", conn))
{
if (cmd.ExecuteScalar() == null)
{
Console.WriteLine("Error: The database file does not contain the required 'addresses' table.");
dbPath = "database.db"; // Сбрасываем путь и просим ввести снова
continue;
}
}
}
}
catch (SQLiteException ex)
{
Console.WriteLine($"Error opening database: {ex.Message}");
dbPath = "database.db"; // Сбрасываем путь и просим ввести снова
continue;
}
}
string logPath = "log.txt";
try
{
if (!File.Exists(logPath))
{
Console.WriteLine("Log file log.txt not found, creating automatically.");
File.Create(logPath).Close();
}
else
{
Console.WriteLine("Log file log.txt already exists, using it.");
}
logFile = new StreamWriter(logPath, true);
}
catch (Exception ex)
{
Console.WriteLine($"Error creating/opening log file: {ex.Message}");
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
return;
}
Console.WriteLine("Press Enter in the console to gracefully exit the program.");
Console.WriteLine("Program started successfully!");
DateTime startTime = DateTime.Now;
Thread reportThread = new Thread(() =>
{
while (!stopFlag)
{
double mins = (DateTime.Now - startTime).TotalMinutes;
int total = counter;
double speed = total / Math.Max(mins, 1e-9);
Console.Write($"\rSpeed: {speed:F2} addresses/minute");
Thread.Sleep(1000);
}
});
Thread inputThread = new Thread(() =>
{
Console.ReadLine();
stopFlag = true;
});
List<Thread> workers = new List<Thread>();
for (int i = 0; i < userThreads; i++)
{
workers.Add(new Thread(() =>
{
using (var connection = new SQLiteConnection($"Data Source={dbPath};Version=3;"))
{
connection.Open();
var command = new SQLiteCommand("SELECT 1 FROM addresses WHERE address = ?", connection);
command.Parameters.Add(new SQLiteParameter("@address", DbType.Binary));
while (!stopFlag)
{
try
{
var (mnemonic, seed) = GenerateMnemonicAndSeed();
var (addrHex, binAddr, privKeyHex) = GetWalletInfo(seed);
lock (logMutex)
{
counter++;
}
command.Parameters["@address"].Value = binAddr;
var result = command.ExecuteNonQuery();
if (result > 0)
{
lock (logMutex)
{
logFile.WriteLine($"Seed: {mnemonic}");
logFile.WriteLine($"Address: {addrHex}");
logFile.WriteLine($"Private: {privKeyHex}");
logFile.Flush();
}
}
}
catch (Exception e)
{
Console.WriteLine($"Error: {e.Message}");
}
}
}
}));
}
reportThread.Start();
inputThread.Start();
foreach (var worker in workers)
{
worker.Start();
}
foreach (var worker in workers)
{
worker.Join();
}
reportThread.Join();
inputThread.Join();
Console.WriteLine($"\nProgram completed. Log file {logPath} created (if it did not exist).");
}
catch (Exception ex)
{
Console.WriteLine($"Unexpected error occurred: {ex.Message}");
Console.WriteLine(ex.StackTrace);
Console.WriteLine("\nPress any key to exit...");
Console.ReadKey();
}
finally
{
if (logFile != null)
{
logFile.Close();
}
}
}
static (string, byte[]) GenerateMnemonicAndSeed()
{
byte[] entropy = new byte[16]; // 128 bits of entropy for 12 words
using (var rng = new RNGCryptoServiceProvider())
{
rng.GetBytes(entropy);
}
// Преобразуем байты в hex строку для временной реализации
string entropyHex = BitConverter.ToString(entropy).Replace("-", "").ToLower();
string mnemonic = entropyHex; // Временная реализация
// Создаем seed из мнемоники (временная реализация)
byte[] seed = new byte[64];
using (var hmac = new HMACSHA512(Encoding.UTF8.GetBytes("mnemonic")))
{
seed = hmac.ComputeHash(Encoding.UTF8.GetBytes(mnemonic));
}
return (mnemonic, seed);
}
static (string, byte[], string) GetWalletInfo(byte[] seed)
{
// Создаем временный master key
var master = new ext_key
{
privKey = new byte[33], // 32 bytes + 1 prefix byte
pubKey = new byte[33],
chainCode = new byte[32],
depth = 0,
childNum = 0,
parentFingerprint = 0
};
// Копируем seed в privKey (временная реализация)
Array.Copy(seed, 0, master.privKey, 1, Math.Min(32, seed.Length));
using (var ecdsa = ECDsa.Create(ECCurve.CreateFromFriendlyName("secp256k1")))
{
byte[] privKeyForImport = new byte[32];
Array.Copy(master.privKey, 1, privKeyForImport, 0, 32); // Пропускаем первый байт
ecdsa.ImportParameters(new ECParameters
{
Curve = ECCurve.CreateFromFriendlyName("secp256k1"),
D = privKeyForImport
});
byte[] publicKey = ecdsa.ExportParameters(false).Q.X.Concat(ecdsa.ExportParameters(false).Q.Y).ToArray();
byte[] hash = Keccak256(publicKey);
byte[] binAddr = hash.Skip(12).Take(20).ToArray();
string addrHex = ToChecksumAddress(binAddr);
string privKeyHex = "0x" + BitConverter.ToString(privKeyForImport).Replace("-", "").ToLower();
return (addrHex, binAddr, privKeyHex);
}
}
static string ToChecksumAddress(byte[] addr)
{
string hexAddr = BitConverter.ToString(addr).Replace("-", "").ToLower();
string lowerHex = hexAddr;
byte[] hash = Keccak256(Encoding.UTF8.GetBytes(lowerHex));
string hashHex = BitConverter.ToString(hash).Replace("-", "").ToLower();
StringBuilder outStr = new StringBuilder("0x");
for (int i = 0; i < 40; i++)
{
if (hashHex[i] >= '8')
{
outStr.Append(hexAddr[i].ToString().ToUpper());
}
else
{
outStr.Append(hexAddr[i].ToString().ToLower());
}
}
return outStr.ToString();
}
static byte[] Keccak256(byte[] input)
{
var keccak = new KeccakDigest(256);
byte[] hash = new byte[32];
keccak.BlockUpdate(input, 0, input.Length);
keccak.DoFinal(hash, 0);
return hash;
}
}
public struct ext_key
{
public byte[] privKey;
public byte[] pubKey;
public byte[] chainCode;
public uint depth;
public uint childNum;
public uint parentFingerprint;
}