Spaces:
Runtime error
Runtime error
Commit ·
e415917
1
Parent(s): 9ecbe10
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,15 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
from eth_account import Account
|
| 5 |
-
import
|
| 6 |
from eth_utils import keccak, to_checksum_address
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
"lesson", "cushion", "crane", "affair", "federal",
|
| 11 |
-
"decline", "limb", "spend", "reunion", "chief", "cart", "zebra",
|
| 12 |
-
]
|
| 13 |
-
|
| 14 |
-
PHRASE_MODE = "subset"
|
| 15 |
-
USE_BIP39 = True
|
| 16 |
-
BIP39_PASSPHRASE = ""
|
| 17 |
-
HD_DERIVATION = True
|
| 18 |
-
ADDRESS_INDEX = 0
|
| 19 |
-
CHECK_BIP39_CHECKSUM = False # ✅ prefilter band
|
| 20 |
-
ADDRESSES_FILE = "12730802_addresses.csv"
|
| 21 |
-
MATCHED_FILE = f"matched_keys_{PHRASE_MODE}.txt"
|
| 22 |
-
BATCH_SIZE = 500
|
| 23 |
-
NUM_WORKERS = 96
|
| 24 |
-
# ============================================================
|
| 25 |
|
| 26 |
-
#
|
| 27 |
_SECP256K1_N = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141
|
| 28 |
_HARDENED = 0x80000000
|
| 29 |
|
|
@@ -35,8 +21,6 @@ def _bip32_derive_child_priv(parent_priv, parent_chain, index):
|
|
| 35 |
data = pub + index.to_bytes(4, 'big')
|
| 36 |
I = hmac.new(parent_chain, data, hashlib.sha512).digest()
|
| 37 |
child_int = (int.from_bytes(I[:32], 'big') + int.from_bytes(parent_priv, 'big')) % _SECP256K1_N
|
| 38 |
-
if child_int == 0:
|
| 39 |
-
raise ValueError("Invalid child key")
|
| 40 |
return child_int.to_bytes(32, 'big'), I[32:]
|
| 41 |
|
| 42 |
def seed_to_privkey_hd(seed, address_index=0):
|
|
@@ -47,157 +31,40 @@ def seed_to_privkey_hd(seed, address_index=0):
|
|
| 47 |
key, chain = _bip32_derive_child_priv(key, chain, idx)
|
| 48 |
return key
|
| 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 |
-
def normalize(addr):
|
| 89 |
-
a = addr.strip().lower()
|
| 90 |
-
if a.startswith("0x"):
|
| 91 |
-
a = a[2:]
|
| 92 |
-
return a
|
| 93 |
-
|
| 94 |
-
# ---------- Generator ----------
|
| 95 |
-
def generate_phrases():
|
| 96 |
-
n = len(words)
|
| 97 |
-
if PHRASE_MODE == "order":
|
| 98 |
-
for perm in permutations(words):
|
| 99 |
-
yield " ".join(perm)
|
| 100 |
-
elif PHRASE_MODE == "replace":
|
| 101 |
-
for i in range(n):
|
| 102 |
-
for w in words:
|
| 103 |
-
new = words.copy()
|
| 104 |
-
new[i] = w
|
| 105 |
-
yield " ".join(new)
|
| 106 |
-
elif PHRASE_MODE == "subset":
|
| 107 |
-
for r in range(1, n + 1):
|
| 108 |
-
for combo in combinations(words, r):
|
| 109 |
-
yield " ".join(combo)
|
| 110 |
-
|
| 111 |
-
def count_total():
|
| 112 |
-
n = len(words)
|
| 113 |
-
if PHRASE_MODE == "order": return math.factorial(n)
|
| 114 |
-
if PHRASE_MODE == "replace": return n * n
|
| 115 |
-
if PHRASE_MODE == "subset": return 2**n - 1
|
| 116 |
-
|
| 117 |
-
# ---------- Worker ----------
|
| 118 |
-
_KNOWN = None
|
| 119 |
-
|
| 120 |
-
def init_worker(known):
|
| 121 |
-
global _KNOWN
|
| 122 |
-
_KNOWN = known
|
| 123 |
-
_get_mnemonic()
|
| 124 |
-
|
| 125 |
-
def process_batch(batch):
|
| 126 |
-
hits = []
|
| 127 |
-
known = _KNOWN
|
| 128 |
-
for phrase in batch:
|
| 129 |
-
if CHECK_BIP39_CHECKSUM and not is_valid_bip39_fast(phrase):
|
| 130 |
-
continue
|
| 131 |
-
try:
|
| 132 |
-
seed = phrase_to_seed(phrase)
|
| 133 |
-
if HD_DERIVATION:
|
| 134 |
-
priv = seed_to_privkey_hd(seed, ADDRESS_INDEX)
|
| 135 |
-
else:
|
| 136 |
-
priv = seed[:32]
|
| 137 |
-
pub = coincurve.PrivateKey(priv).public_key.format(compressed=False)[1:]
|
| 138 |
-
addr = to_checksum_address(keccak(pub)[-20:])
|
| 139 |
-
except Exception:
|
| 140 |
-
continue
|
| 141 |
-
if normalize(addr) in known:
|
| 142 |
-
hits.append((phrase, priv.hex(), addr))
|
| 143 |
-
return hits
|
| 144 |
-
|
| 145 |
-
def batch_generator():
|
| 146 |
-
batch = []
|
| 147 |
-
for phrase in generate_phrases():
|
| 148 |
-
batch.append(phrase)
|
| 149 |
-
if len(batch) >= BATCH_SIZE:
|
| 150 |
-
yield batch
|
| 151 |
-
batch = []
|
| 152 |
-
if batch:
|
| 153 |
-
yield batch
|
| 154 |
-
|
| 155 |
-
# ---------- Main ----------
|
| 156 |
-
def main():
|
| 157 |
-
if not os.path.exists(ADDRESSES_FILE):
|
| 158 |
-
raise SystemExit(f"File nahi mili: {ADDRESSES_FILE}")
|
| 159 |
-
|
| 160 |
-
total = count_total()
|
| 161 |
-
print(f"\nMode: {PHRASE_MODE} | Total: {total:,} | Workers: {NUM_WORKERS}")
|
| 162 |
-
print(f"HD: {HD_DERIVATION} (index {ADDRESS_INDEX})")
|
| 163 |
-
print(f"Checksum prefilter: {CHECK_BIP39_CHECKSUM}\n")
|
| 164 |
-
|
| 165 |
-
known = load_addresses(ADDRESSES_FILE)
|
| 166 |
-
print(f"Loaded {len(known):,} addresses\n")
|
| 167 |
-
|
| 168 |
-
matches = []
|
| 169 |
-
done = 0
|
| 170 |
-
start = time.time()
|
| 171 |
-
last_log = start
|
| 172 |
-
|
| 173 |
-
if os.path.exists(MATCHED_FILE):
|
| 174 |
-
os.remove(MATCHED_FILE)
|
| 175 |
-
|
| 176 |
-
gc.collect()
|
| 177 |
-
gc.freeze()
|
| 178 |
-
|
| 179 |
-
with Pool(processes=NUM_WORKERS,
|
| 180 |
-
initializer=init_worker,
|
| 181 |
-
initargs=(known,),
|
| 182 |
-
maxtasksperchild=1000) as pool:
|
| 183 |
-
for hits in pool.imap_unordered(process_batch, batch_generator(), chunksize=1):
|
| 184 |
-
done += BATCH_SIZE
|
| 185 |
-
for phrase, key_hex, addr in hits:
|
| 186 |
-
print(f"\n✅ MATCH: {phrase}\n priv: {key_hex}\n addr: {addr}\n")
|
| 187 |
-
matches.append((phrase, key_hex, addr))
|
| 188 |
-
with open(MATCHED_FILE, "a", encoding="utf-8") as f:
|
| 189 |
-
f.write(f"{phrase} | {key_hex} | {addr}\n")
|
| 190 |
-
|
| 191 |
-
now = time.time()
|
| 192 |
-
if now - last_log >= 2:
|
| 193 |
-
elapsed = now - start
|
| 194 |
-
rate = done / elapsed if elapsed else 0
|
| 195 |
-
eta = (total - done) / rate if rate else 0
|
| 196 |
-
print(f" ...{done:,}/{total:,} ({100*done/total:.1f}%) ETA {eta:.0f}s")
|
| 197 |
-
last_log = now
|
| 198 |
-
|
| 199 |
-
elapsed = time.time() - start
|
| 200 |
-
print(f"\nDone {elapsed:.1f}s. {len(matches)} match(es).")
|
| 201 |
-
|
| 202 |
-
if __name__ == "__main__":
|
| 203 |
-
main()
|
|
|
|
| 1 |
+
# debug_replace.py
|
| 2 |
+
from itertools import permutations
|
| 3 |
+
import hashlib, hmac
|
| 4 |
from eth_account import Account
|
| 5 |
+
from mnemonic import Mnemonic
|
| 6 |
from eth_utils import keccak, to_checksum_address
|
| 7 |
+
import coincurve
|
| 8 |
|
| 9 |
+
words = ["lesson", "cushion", "crane", "affair", "federal", "decline", "limb", "spend", "reunion", "chief", "cart", "zebra"]
|
| 10 |
+
TARGET = "0x3f0482fc3e9132c3d3acb5f588e980d7b3a5af07"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
# BIP32 helpers (aapke code se copy)
|
| 13 |
_SECP256K1_N = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141
|
| 14 |
_HARDENED = 0x80000000
|
| 15 |
|
|
|
|
| 21 |
data = pub + index.to_bytes(4, 'big')
|
| 22 |
I = hmac.new(parent_chain, data, hashlib.sha512).digest()
|
| 23 |
child_int = (int.from_bytes(I[:32], 'big') + int.from_bytes(parent_priv, 'big')) % _SECP256K1_N
|
|
|
|
|
|
|
| 24 |
return child_int.to_bytes(32, 'big'), I[32:]
|
| 25 |
|
| 26 |
def seed_to_privkey_hd(seed, address_index=0):
|
|
|
|
| 31 |
key, chain = _bip32_derive_child_priv(key, chain, idx)
|
| 32 |
return key
|
| 33 |
|
| 34 |
+
# Generate first 5 replace phrases
|
| 35 |
+
n = len(words)
|
| 36 |
+
count = 0
|
| 37 |
+
for i in range(n):
|
| 38 |
+
for w in words:
|
| 39 |
+
new = words.copy()
|
| 40 |
+
new[i] = w
|
| 41 |
+
phrase = " ".join(new)
|
| 42 |
+
count += 1
|
| 43 |
+
|
| 44 |
+
# Derive address
|
| 45 |
+
seed = Mnemonic("english").to_seed(phrase, passphrase="")
|
| 46 |
+
priv = seed_to_privkey_hd(seed, 0)
|
| 47 |
+
pub = coincurve.PrivateKey(priv).public_key.format(compressed=False)[1:]
|
| 48 |
+
addr = to_checksum_address(keccak(pub)[-20:])
|
| 49 |
+
addr_lower = addr.lower()
|
| 50 |
+
|
| 51 |
+
match = "✅ MATCH" if addr_lower == TARGET.lower() else ""
|
| 52 |
+
print(f"{count:3d}. {phrase}")
|
| 53 |
+
print(f" -> {addr} {match}")
|
| 54 |
+
|
| 55 |
+
if count >= 5:
|
| 56 |
+
break
|
| 57 |
+
if count >= 5:
|
| 58 |
+
break
|
| 59 |
+
|
| 60 |
+
# Also check the exact target phrase
|
| 61 |
+
print("\n--- Exact target phrase ---")
|
| 62 |
+
phrase = "lesson cushion crane affair federal decline limb spend reunion chief cart zebra"
|
| 63 |
+
seed = Mnemonic("english").to_seed(phrase, passphrase="")
|
| 64 |
+
priv = seed_to_privkey_hd(seed, 0)
|
| 65 |
+
pub = coincurve.PrivateKey(priv).public_key.format(compressed=False)[1:]
|
| 66 |
+
addr = to_checksum_address(keccak(pub)[-20:])
|
| 67 |
+
print(f"Phrase: {phrase}")
|
| 68 |
+
print(f"Addr: {addr}")
|
| 69 |
+
print(f"Target: {TARGET}")
|
| 70 |
+
print(f"Match: {addr.lower() == TARGET.lower()}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|