DILSHAD737 commited on
Commit
2feef57
·
1 Parent(s): a52cbb4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +150 -2
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  from itertools import permutations, combinations
2
  import hashlib, hmac, os, time, math, gc, random
3
  from multiprocessing import Pool
@@ -61,7 +62,7 @@ def seed_to_privkey_hd(seed, address_index=0):
61
  key, chain = _bip32_derive_child_priv(key, chain, idx)
62
  return key
63
 
64
- # ---------- BIP39 (flexible) ----------
65
  try:
66
  import bip39 as _bip39_rust
67
  _USE_RUST_BIP39 = True
@@ -98,4 +99,151 @@ def phrase_to_seed(phrase):
98
  return _seed_fn(phrase, BIP39_PASSPHRASE)
99
  return _get_mnemonic().to_seed(phrase, passphrase=BIP39_PASSPHRASE)
100
 
101
- # ---------- Address file ---------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
  from itertools import permutations, combinations
3
  import hashlib, hmac, os, time, math, gc, random
4
  from multiprocessing import Pool
 
62
  key, chain = _bip32_derive_child_priv(key, chain, idx)
63
  return key
64
 
65
+ # ---------- BIP39 ----------
66
  try:
67
  import bip39 as _bip39_rust
68
  _USE_RUST_BIP39 = True
 
99
  return _seed_fn(phrase, BIP39_PASSPHRASE)
100
  return _get_mnemonic().to_seed(phrase, passphrase=BIP39_PASSPHRASE)
101
 
102
+ # ---------- Address file ----------
103
+ def load_addresses(path):
104
+ s = set()
105
+ with open(path, "r", encoding="utf-8") as f:
106
+ for line in f:
107
+ a = line.strip().strip('"').strip("'").lstrip("\ufeff").lower()
108
+ if a.startswith("0x"):
109
+ a = a[2:]
110
+ if a and len(a) == 40:
111
+ s.add(a)
112
+ return frozenset(s)
113
+
114
+ def normalize(addr):
115
+ a = addr.strip().lower()
116
+ if a.startswith("0x"):
117
+ a = a[2:]
118
+ return a
119
+
120
+ # ---------- Generator ----------
121
+ def generate_phrases_for_words(wordlist):
122
+ n = len(wordlist)
123
+ if PHRASE_MODE == "order":
124
+ for perm in permutations(wordlist):
125
+ yield " ".join(perm)
126
+ elif PHRASE_MODE == "replace":
127
+ for i in range(n):
128
+ for w in wordlist:
129
+ new = wordlist.copy()
130
+ new[i] = w
131
+ yield " ".join(new)
132
+ elif PHRASE_MODE == "subset":
133
+ for r in range(1, n + 1):
134
+ for combo in combinations(wordlist, r):
135
+ yield " ".join(combo)
136
+
137
+ # ---------- Worker ----------
138
+ _KNOWN = None
139
+ def init_worker(known):
140
+ global _KNOWN
141
+ _KNOWN = known
142
+ _get_mnemonic()
143
+
144
+ def process_batch(batch):
145
+ hits = []
146
+ known = _KNOWN
147
+ for phrase in batch:
148
+ if CHECK_BIP39_CHECKSUM and not is_valid_bip39(phrase):
149
+ continue
150
+ try:
151
+ seed = phrase_to_seed(phrase)
152
+ if HD_DERIVATION:
153
+ priv = seed_to_privkey_hd(seed, ADDRESS_INDEX)
154
+ else:
155
+ priv = seed[:32]
156
+ pub = coincurve.PrivateKey(priv).public_key.format(compressed=False)[1:]
157
+ addr = to_checksum_address(keccak(pub)[-20:])
158
+ except Exception:
159
+ continue
160
+ if normalize(addr) in known:
161
+ hits.append((phrase, priv.hex(), addr))
162
+ return hits
163
+
164
+ def batch_generator_for_words(wordlist):
165
+ batch = []
166
+ for phrase in generate_phrases_for_words(wordlist):
167
+ batch.append(phrase)
168
+ if len(batch) >= BATCH_SIZE:
169
+ yield batch
170
+ batch = []
171
+ if batch:
172
+ yield batch
173
+
174
+ # ---------- Progress bar ----------
175
+ def make_bar(idx, total, tag=""):
176
+ bar_len = 30
177
+ filled = int(bar_len * idx / total)
178
+ bar = "█" * filled + "░" * (bar_len - filled)
179
+ pct = int(100 * idx / total)
180
+ return f" Progress: [{bar}] {idx}/{total} ({pct}%){tag}"
181
+
182
+ # ---------- Main ----------
183
+ def main():
184
+ if not os.path.exists(ADDRESSES_FILE):
185
+ raise SystemExit(f"File nahi mili: {ADDRESSES_FILE}")
186
+
187
+ known = load_addresses(ADDRESSES_FILE)
188
+
189
+ print("\n" + "=" * 55)
190
+ print(" SCAN SETTINGS")
191
+ print("=" * 55)
192
+ print(f" {'Phrase Mode':<22}: {PHRASE_MODE}")
193
+ print(f" {'HD Derivation':<22}: {HD_DERIVATION}")
194
+ print(f" {'Address Index':<22}: {ADDRESS_INDEX}")
195
+ print(f" {'Passphrase':<22}: {'(set)' if BIP39_PASSPHRASE else '(empty)'}")
196
+ print(f" {'Checksum Prefilter':<22}: {CHECK_BIP39_CHECKSUM}")
197
+ print(f" {'Rust BIP39':<22}: {_USE_RUST_BIP39}")
198
+ print(f" {'Seed Function':<22}: {_seed_fn.__name__ if _USE_RUST_BIP39 and _seed_fn else 'fallback'}")
199
+ print(f" {'Addresses File':<22}: {ADDRESSES_FILE}")
200
+ print(f" {'Loaded Addresses':<22}: {len(known):,}")
201
+ print(f" {'Workers':<22}: {NUM_WORKERS}")
202
+ print(f" {'Batch Size':<22}: {BATCH_SIZE:,}")
203
+ print(f" {'Chunk Size':<22}: {CHUNK_SIZE}")
204
+ print(f" {'Total Phrases':<22}: {len(RANDOM_PHRASES)}")
205
+ print(f" {'Output File':<22}: {MATCHED_FILE}")
206
+ print("=" * 55 + "\n")
207
+
208
+ with open(MATCHED_FILE, "w", encoding="utf-8") as f:
209
+ f.write("")
210
+
211
+ total_matches = []
212
+ all_start = time.time()
213
+ total_phrases = len(RANDOM_PHRASES)
214
+
215
+ gc.collect()
216
+ gc.freeze()
217
+
218
+ # ✅ Ek hi Pool poore scan ke liye
219
+ with Pool(processes=NUM_WORKERS,
220
+ initializer=init_worker,
221
+ initargs=(known,)) as pool:
222
+
223
+ for idx, wordlist in enumerate(RANDOM_PHRASES, 1):
224
+ tag = " (AAPKI)" if (idx == 1 and len(my_words) == 12) else ""
225
+ print(make_bar(idx, total_phrases, tag))
226
+
227
+ batch_gen = batch_generator_for_words(wordlist)
228
+ for hits in pool.imap_unordered(process_batch, batch_gen, chunksize=CHUNK_SIZE):
229
+ for phrase, key_hex, addr in hits:
230
+ print(f"✅ MATCH: {phrase}")
231
+ print(f" priv: {key_hex}")
232
+ print(f" addr: {addr}\n")
233
+ matches_entry = (phrase, key_hex, addr)
234
+ total_matches.append(matches_entry)
235
+ with open(MATCHED_FILE, "a", encoding="utf-8") as f:
236
+ f.write(f"{phrase} | {key_hex} | {addr}\n")
237
+ f.flush()
238
+ os.fsync(f.fileno())
239
+
240
+ all_elapsed = time.time() - all_start
241
+ print(f"\n=== DONE ===")
242
+ print(f"Total time: {all_elapsed/60:.2f} min")
243
+ print(f"Total matches: {len(total_matches)}")
244
+ if total_matches:
245
+ print(f"Results: {MATCHED_FILE}")
246
+
247
+
248
+ if __name__ == "__main__":
249
+ main()