-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathlitecoin_offline_scanner.py
More file actions
370 lines (288 loc) · 13.2 KB
/
litecoin_offline_scanner.py
File metadata and controls
370 lines (288 loc) · 13.2 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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import os
import sys
import time
import json
import gzip
import shutil
import urllib.request
from urllib.error import URLError
from pathlib import Path
from typing import Dict, Set, Optional
from rich.console import Console
from rich.progress import Progress, BarColumn, TextColumn, TimeRemainingColumn, FileSizeColumn, TotalFileSizeColumn, TransferSpeedColumn
from libcrypto import Wallet
class Messages:
_data: Dict = None
@classmethod
def _load(cls) -> None:
if cls._data is None:
try:
with open('messages.json', 'r', encoding='utf-8') as f:
cls._data = json.load(f)
except:
cls._data = {}
@classmethod
def get(cls, path: str, **kwargs) -> str:
cls._load()
keys = path.split('.')
current = cls._data.get('offline_scanner', {})
try:
for key in keys:
current = current[key]
return current.format(**kwargs) if kwargs else current
except:
return f"Message not found: {path}"
class FileDownloader:
def __init__(self, console: Console):
self.console = console
self.download_url = "https://github.com/Pymmdrza/Rich-Address-Wallet/releases/download/Litecoin/Latest_Rich_Litecoin_Addresses.txt.gz"
self.temp_file = "temp_download.gz"
def download_and_extract(self, target_filename: str) -> bool:
try:
self._show_file_not_found_messages(target_filename)
if not self._ask_permission(target_filename):
return False
if not self._download_file():
return False
if not self._extract_and_rename(target_filename):
return False
self._cleanup()
self.console.print(Messages.get("download.ready"))
return True
except Exception as e:
self.console.print(Messages.get("errors.download.failed", error=str(e)))
self._cleanup()
return False
def _show_file_not_found_messages(self, filename: str) -> None:
error_keys = ['line1', 'line2', 'release', 'download', 'rename']
for key in error_keys:
self.console.print(Messages.get(f"errors.notfound.{key}", filename=filename))
print()
def _ask_permission(self, filename: str) -> bool:
self.console.print(Messages.get("download.prompt_message", filename=filename))
choice_text = Messages.get("download.prompt_choice")
prompt_text = Messages.get("prompts.download_choice")
self.console.print(choice_text, end="")
response = input(prompt_text).lower().strip()
if response not in ['y', 'yes', '1']:
self.console.print(Messages.get("errors.download.cancelled"))
return False
return True
def _download_file(self) -> bool:
try:
self.console.print(Messages.get("download.starting", url=self.download_url))
with Progress(
TextColumn("[bold blue]Downloading", justify="right"),
BarColumn(bar_width=None),
"[progress.percentage]{task.percentage:>3.1f}%",
"•",
FileSizeColumn(),
"•",
TotalFileSizeColumn(),
"•",
TransferSpeedColumn(),
"•",
TimeRemainingColumn(),
console=self.console
) as progress:
req = urllib.request.Request(self.download_url)
req.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36')
with urllib.request.urlopen(req) as response:
total_size = int(response.headers.get('Content-Length', 0))
task = progress.add_task("Download", total=total_size)
with open(self.temp_file, 'wb') as f:
downloaded = 0
chunk_size = 8192
while True:
chunk = response.read(chunk_size)
if not chunk:
break
f.write(chunk)
downloaded += len(chunk)
progress.update(task, completed=downloaded)
self.console.print(Messages.get("download.completed"))
return True
except urllib.error.URLError as e:
self.console.print(Messages.get("errors.download.network_error", error=str(e)))
return False
except Exception as e:
self.console.print(Messages.get("errors.download.failed", error=str(e)))
return False
def _extract_and_rename(self, target_filename: str) -> bool:
try:
self.console.print(Messages.get("download.extracting"))
with gzip.open(self.temp_file, 'rb') as f_in:
extracted_content = f_in.read()
temp_txt = "temp_extracted.txt"
with open(temp_txt, 'wb') as f_out:
f_out.write(extracted_content)
self.console.print(Messages.get("download.extract_completed"))
if not self._validate_txt_file(temp_txt):
os.remove(temp_txt)
return False
if os.path.exists(target_filename):
os.remove(target_filename)
shutil.move(temp_txt, target_filename)
self.console.print(Messages.get("download.txt_found", filename=temp_txt))
self.console.print(Messages.get("download.renamed", new_name=target_filename))
return True
except Exception as e:
self.console.print(Messages.get("errors.download.extract_failed", error=str(e)))
return False
def _validate_txt_file(self, filename: str) -> bool:
try:
with open(filename, 'r', encoding='utf-8') as f:
lines = [f.readline().strip() for _ in range(5)]
valid_lines = [line for line in lines if line and len(line) > 20]
if len(valid_lines) < 3:
self.console.print(Messages.get("errors.download.no_txt_found"))
return False
return True
except Exception:
return False
def _cleanup(self) -> None:
for temp_file in [self.temp_file, "temp_extracted.txt"]:
if os.path.exists(temp_file):
try:
os.remove(temp_file)
except:
pass
class AddressLoader:
@staticmethod
def load_from_file(filename: str, console: Console) -> Optional[Set[str]]:
if not os.path.exists(filename):
downloader = FileDownloader(console)
if not downloader.download_and_extract(filename):
return None
try:
with open(filename, 'r', encoding='utf-8') as f:
addresses = {addr.strip() for addr in f if addr.strip()}
console.print(Messages.get("messages.loaded_addresses",
count=len(addresses), filename=filename))
return addresses
except Exception as e:
console.print(Messages.get("messages.file_error",
filename=filename, error=str(e)))
return None
class UserInterface:
@staticmethod
def get_scan_parameters() -> tuple:
try:
start = int(input(Messages.get("prompts.start_input")))
end = int(input(Messages.get("prompts.end_input")))
speed = int(input(Messages.get("prompts.speed_input")))
output = input(Messages.get("prompts.output_input")).strip()
if not output.endswith('.txt'):
output += '.txt'
return start, end, speed, output
except ValueError as e:
console = Console()
console.print(Messages.get("messages.invalid_input", error=str(e)))
sys.exit(1)
@staticmethod
def show_header(start: int, end: int, speed: int, filename: str, output: str) -> None:
console = Console()
header = Messages.get("header",
start=start,
end=end,
start_hex=f"{start:064x}",
end_hex=f"{end:064x}",
total=f"{end - start:,}",
speed=f"{speed:,}",
filename=filename,
output=output,
current_time=time.ctime()
)
console.print(header)
class Scanner:
def __init__(self, addresses: Set[str], console: Console):
self.addresses = addresses
self.console = console
self.scan_count = 0
self.found_count = 0
def scan_range(self, start: int, end: int, speed: int, output: str) -> None:
self.console.print(Messages.get("messages.scanning_start",
start=start, end=end))
for i in range(start, end):
self.scan_count += 1
if self.scan_count % 100 == 0:
self._update_title()
private_key = f"{i:064x}"
wallet = Wallet(private_key)
addresses_to_check = [
wallet.get_address(coin="litecoin"),
wallet.get_address(coin="litecoin", address_type="p2wpkh"),
wallet.get_address(coin="litecoin", address_type="p2sh-p2wpkh")
]
for addr in addresses_to_check:
if addr in self.addresses:
self._save_found_key(addr, private_key, output)
self.console.print(Messages.get("messages.found_key", address=addr))
self._update_title()
break
if self.scan_count % speed == 0:
self._show_progress(end - start, speed)
def _update_title(self) -> None:
title = f"SCAN: {self.scan_count:,} | FOUND: {self.found_count}"
sys.stdout.write(f"\033]0;{title}\a")
sys.stdout.flush()
def _save_found_key(self, address: str, private_key: str, output_file: str) -> None:
try:
with open(output_file, 'a', encoding='utf-8') as f:
f.write(f"[ADDR]: {address}\n")
f.write(f"[KEY]: {private_key}\n")
f.write("--------------------PROGRAMMER MMDRZA.COM--------------------\n\n")
self.found_count += 1
except Exception as e:
self.console.print(f"Error saving to file: {e}")
def _show_progress(self, total_range: int, speed: int) -> None:
progress = (self.scan_count / total_range) * 100
sys.stdout.write(
f"\r[SCAN: {self.scan_count:,}] [FOUND: {self.found_count}] "
f"[PROGRESS: {progress:.2f}%] [SPEED: {speed} k/s]"
)
sys.stdout.flush()
def show_statistics(self, elapsed_time: float) -> None:
self.console.print(Messages.get("messages.scan_completed"))
self.console.print(Messages.get("messages.stats_header"))
self.console.print(Messages.get("messages.stats_scanned", count=self.scan_count))
self.console.print(Messages.get("messages.stats_found", count=self.found_count))
self.console.print(Messages.get("messages.stats_time", time=elapsed_time))
if self.scan_count > 0:
rate = self.scan_count / elapsed_time
self.console.print(Messages.get("messages.stats_speed", speed=rate))
class LitecoinScanner:
def __init__(self):
self.console = Console()
self.filename = 'ltc500.txt'
def run(self) -> None:
self.console.clear()
self.console.print(Messages.get("messages.loading"))
addresses = AddressLoader.load_from_file(self.filename, self.console)
if addresses is None:
sys.exit(1)
start, end, speed, output = UserInterface.get_scan_parameters()
if start >= end:
self.console.print(Messages.get("messages.invalid_range"))
return
self.console.clear()
UserInterface.show_header(start, end, speed, self.filename, output)
scanner = Scanner(addresses, self.console)
start_time = time.time()
try:
scanner.scan_range(start, end, speed, output)
except KeyboardInterrupt:
self.console.print(Messages.get("messages.scan_interrupted"))
finally:
elapsed = time.time() - start_time
scanner.show_statistics(elapsed)
def main():
try:
app = LitecoinScanner()
app.run()
except Exception as e:
console = Console()
console.print(Messages.get("messages.fatal_error", error=str(e)))
sys.exit(1)
if __name__ == "__main__":
main()